MR Kombinér dynamisk sekvenser

MR Combine dynamic sequences

This script is adapted from Agfa’s MG Split and Regroup wizard. It combines dynamic MRI-sequences containing dyn* in series description.

# This function can be used to output information to image_client.log
# if the script is not working correctly.
#
proc log_error { msg } {

# Uncomment the following line to log errors
 # puts error $msg
}

proc determine_series_description { series } {
 return [ get_info "series_description" $series ]
}

proc number_of_series { study } {
 set series_list [ get_series $study ]
 return [ llength $series_list ]
}

proc get_series_at_index { study i } {
 set series_list [ get_series $study ]
 set series [ lindex $series_list $i]
 return $series
}

proc swap_series { study i j } {
 set series_list [ get_series $study ]
 set child1 [lindex $series_list $i]
 set child2 [lindex $series_list $j]

 children swap $study $child1 $child2
}

#
# If description of series_0 is "greater than"
# that of series_1, returns true. The sort
# order is as follows:
# -regular series
# -DBT series
# -key image series
# 
proc compare_series_description {series_0 series_1} {
 if { [is_key_image_series $series_0] } {
 if { [is_key_image_series $series_1] } {
 return 0
 } else {
 return 1
 } 
 } else {
 return 0
 }
}

#
# This function sorts the series by description.
# See "compare_series_description" for sorting
# criteria.
#
# It uses a "bubble sort" algorithm due to the fact that
# swapping series is a native viewer operation.
#
# param study [IN] : The study to sort.
#
# return : the sorted study
#
proc sort_series {study} {
 set len [number_of_series $study]
 set swapped true
 while {$swapped} {
 set swapped false
 for {set i 0} {$i < $len - 1} {incr i} {
 set j [expr {$i + 1}]
 if { [compare_series_description [get_series_at_index $study $i] [get_series_at_index $study $j] ] == 1 } {
 swap_series $study $i $j
 set swapped true
 }
 }
 incr len -1
 }
 return $study
}

# 
# This function will take a list of series and combine the series
# that share a common description. 
#
# param study [IN] : The study to which the list of series belongs
# param series_list [IN] : The list of series to be combined
#
# return : The number of series remaining.
#
proc regroup_series { study series_list } {

set new_series_list ""
 set SERIES_MODALITY "series_modality"
 set SERIES_DESCRIPTION "series_description"
 set INCLUDE_SERIES "dyn*"
 log_error "Starting regroup...."

# This function will grab the first (base) MR series and search for matches
 # in the other series in the study. A series is considered a match 
 # if the series description is the same. All the images from a matched
 # series are appended to the base series, leaving the matched series empty
 # and removable.
 #

 while {[llength $series_list] > 0} {

 # Use two lists to ensure we do not check for a match on one item more than once
 # 
 lappend new_series_list [lindex $series_list 0]
 set series_list [lreplace $series_list 0 0]

 set series_to_match [lindex $new_series_list [expr [llength $new_series_list] -1]]
 set series_to_match_modality [ get_info $SERIES_MODALITY $series_to_match ]
 set series_to_exclude_description [ get_info $SERIES_DESCRIPTION $series_to_match ]
 if { [string match $INCLUDE_SERIES $series_to_exclude_description] } {

 # This series to match must be MR
 # 
 while { $series_to_match_modality != "MR" && [llength $series_list] > 0 } {


 # Clear out the first appended item
 #
 set new_series_list [lreplace $new_series_list 0 0]

lappend new_series_list [lindex $series_list 0]
 set series_list [lreplace $series_list 0 0]

 set series_to_match [lindex $new_series_list [expr [llength $new_series_list] -1]]
 set series_to_match_modality [ get_info $SERIES_MODALITY $series_to_match ]
 }

if { [llength $series_list] <= 0 } {
 return 0
 }


 set series_to_match_description [ determine_series_description $series_to_match ]

set length [ string length $series_to_match_description ]

if { $length == 0 } {
 continue
 }

log_error "Series to match description ... $series_to_match_description"

# Compare the last $new_series_list against the remaining $series_list
 #
 foreach series_to_check $series_list {

 set series_modality [ get_info $SERIES_MODALITY $series_to_check ]

 # For this script, only MG series will be grouped.
 #
 if { $series_modality != "MR" } {
 continue
 }

set series_to_check_description [ determine_series_description $series_to_check ]

log_error "Series to check description ... $series_to_check_description"

# Check to see if the compared attribute string in NULL
 if { $series_to_match_description == $series_to_check_description } {
 log_error "Match on $series_to_match_description!!!!" 
 set images_to_move [get_images $series_to_check]

foreach image $images_to_move { 
 children remove $series_to_check $image
 children add_to_tail $series_to_match $image
 put_info $SERIES_DESCRIPTION $series_to_match $series_to_match_description
 }

# Remove series from study
 #
 children remove $study $series_to_check

 # Move the matched series out of the $series_list list
 set pos_series_to_check [lsearch -exact $series_list $series_to_check ]
 linsert new_series_list 0 $pos_series_to_check
 set remove_match_from_list $pos_series_to_check
 set series_list [lreplace $series_list $remove_match_from_list $remove_match_from_list]
 }
 }
 }

foreach series_to_sort $new_series_list {
 children sort_descending $series_to_sort [lindex [get_images $series_to_sort] 0] 
 }
 wizard sort_by_attribute { 5 } "object_detail-slice_location" "ascending";
} 
 return [llength $new_series_list]
}
# Use the selected study to get the list of series
# that need to be split.
#
set study_list [get_studies selected] 
set first_study [lindex $study_list 0]

# sort the series into regular, tomo, then key image series
sort_series $first_study

set series_list [get_series $first_study]

split_series $first_study $series_list

# Destroy invalid object handles and set new handles
#
destroy_token all
# With the images now split, the common images can be combined.
#
set study_list [get_studies selected]
set first_study [lindex $study_list 0]
set series_list [get_series $first_study]

regroup_series $first_study $series_list

destroy_token all