@@ -119,13 +119,30 @@ def load_contours_from_template_dir(
119119
120120 fsaverage_contour = None
121121 contours : list [CCContour ] = []
122+ # First pass: collect all indices to determine the range
123+ indices = []
122124 for thickness_file in thickness_files :
125+ try :
126+ idx = int (thickness_file .stem .split ("_" )[- 1 ])
127+ indices .append (idx )
128+ except ValueError :
129+ # skip files that do not follow the expected naming
130+ continue
131+
132+ # Calculate z_positions centered around the middle slice
133+ num_slices = len (indices )
134+ middle_idx = num_slices // 2
135+
136+ for i , thickness_file in enumerate (thickness_files ):
123137 try :
124138 idx = int (thickness_file .stem .split ("_" )[- 1 ])
125139 except ValueError :
126140 # skip files that do not follow the expected naming
127141 continue
128142
143+ # Calculate z_position: use the index offset from middle, scaled by resolution
144+ z_position = (idx - indices [middle_idx ]) * resolution
145+
129146 contour_file = template_dir / f"contour_{ idx } .txt"
130147
131148 if not contour_file .exists ():
@@ -138,15 +155,11 @@ def load_contours_from_template_dir(
138155 # create measurement points (points = 2 x levelpaths) according to number of thickness values
139156 fsaverage_contour .create_levelpaths (num_points = num_thickness_values // 2 , update_data = True )
140157 current_contour = fsaverage_contour .copy ()
158+ current_contour .z_position = z_position
141159 current_contour .load_thickness_values (thickness_file )
142160
143161 else :
144- # this is kinda ugly - maybe we need to overload the constructor to load the contour and thickness values?
145- # FIXME: The z_position in from_contour is still incorrect, currently all Contours would be "registered" for
146- # the midslice.
147- current_contour = CCContour .from_contour_file (contour_file , thickness_file , z_position = 0.0 )
148- # current_contour.load_contour(contour_file)
149- # current_contour.load_thickness_values(thickness_file)
162+ current_contour = CCContour .from_contour_file (contour_file , thickness_file , z_position = z_position )
150163
151164 current_contour .fill_thickness_values ()
152165 contours .append (current_contour )
0 commit comments