@@ -155,11 +155,17 @@ def to_dict(self) -> dict:
155155 }
156156
157157
158- def convert_to_nimads_point (analysis_id : str , point : CoordinatePoint ) -> Point :
158+ def convert_to_nimads_point (analysis_id : str , point : CoordinatePoint , study_space : Optional [ str ] = None ) -> Point :
159159 """Convert a coordinate point to a NiMADS point."""
160+ # Sanitize the coordinate space
161+ sanitized_space = sanitize_coordinate_space (point .space , study_space )
162+
163+ # Sanitize the coordinates to ensure they are integers
164+ sanitized_coordinates = sanitize_coordinates (point .coordinates )
165+
160166 nimads_point = Point (
161- coordinates = point . coordinates ,
162- space = point . space ,
167+ coordinates = sanitized_coordinates ,
168+ space = sanitized_space ,
163169 analysis_id = analysis_id
164170 )
165171
@@ -177,7 +183,7 @@ def convert_to_nimads_point(analysis_id: str, point: CoordinatePoint) -> Point:
177183 return nimads_point
178184
179185
180- def convert_to_nimads_analysis (analysis_id : str , analysis : Analysis , study_id : str ) -> NimadsAnalysis :
186+ def convert_to_nimads_analysis (analysis_id : str , analysis : Analysis , study_id : str , study_space : Optional [ str ] = None ) -> NimadsAnalysis :
181187 """Convert an analysis to a NiMADS analysis."""
182188 nimads_analysis = NimadsAnalysis (
183189 id = analysis_id ,
@@ -188,7 +194,7 @@ def convert_to_nimads_analysis(analysis_id: str, analysis: Analysis, study_id: s
188194
189195 # Convert points
190196 for point in analysis .points :
191- nimads_point = convert_to_nimads_point (analysis_id , point )
197+ nimads_point = convert_to_nimads_point (analysis_id , point , study_space )
192198 nimads_analysis .points .append (nimads_point )
193199
194200 return nimads_analysis
@@ -216,10 +222,13 @@ def convert_to_nimads_study(study_id: str, autonima_study: 'autonima.models.type
216222 year = year
217223 )
218224
225+ # Get the study's coordinate space
226+ study_space = getattr (autonima_study , 'coordinate_space' , None )
227+
219228 # Convert analyses
220229 for i , analysis in enumerate (autonima_study .analyses ):
221230 analysis_id = f"{ study_id } _analysis_{ i } "
222- nimads_analysis = convert_to_nimads_analysis (analysis_id , analysis , study_id )
231+ nimads_analysis = convert_to_nimads_analysis (analysis_id , analysis , study_id , study_space )
223232 nimads_study .analyses .append (nimads_analysis )
224233
225234 return nimads_study
@@ -302,4 +311,42 @@ def create_default_annotation(studyset_id: str, studyset: Studyset) -> Annotatio
302311 )
303312 annotation .notes .append (note )
304313
305- return annotation
314+ return annotation
315+
316+ def sanitize_coordinate_space (point_space : Optional [str ], study_space : Optional [str ]) -> Optional [str ]:
317+ """
318+ Sanitize coordinate space values for NiMADS outputs.
319+
320+ Args:
321+ point_space: The space value from the LLM extracted point
322+ study_space: The default space value from the study
323+
324+ Returns:
325+ The sanitized space value (MNI, TAL, or None)
326+ """
327+ # Valid space values
328+ valid_spaces = ['MNI' , 'TAL' ]
329+
330+ # If the point space is already valid, return it
331+ if point_space in valid_spaces :
332+ return point_space
333+
334+ # If the point space is invalid but we have a valid study space, use that
335+ if study_space in valid_spaces :
336+ return study_space
337+
338+ # If neither is valid, return None
339+ return None
340+
341+ def sanitize_coordinates (coordinates : List [float ]) -> List [int ]:
342+ """
343+ Sanitize coordinate values to ensure they are integers as required by NiMADS.
344+
345+ Args:
346+ coordinates: List of coordinate values (x, y, z)
347+
348+ Returns:
349+ List of integer coordinate values
350+ """
351+ # Convert float coordinates to integers
352+ return [int (round (coord )) for coord in coordinates ]
0 commit comments