Skip to content

Commit 7fd908b

Browse files
authored
Fix compatibility with newer nilearn, pandas, and numpy versions
- Handle connected_regions returning None when no clusters found (nilearn API change) - Fix pandas dtype issues when creating DataFrames with mixed types - Replace deprecated np.row_stack with np.vstack
1 parent 5713f70 commit 7fd908b

1 file changed

Lines changed: 43 additions & 37 deletions

File tree

atlasreader/atlasreader.py

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _check_coord_inputs(coords):
156156
if coords.shape[0] != 3:
157157
coords = coords.T
158158
# add constant term to coords to make 4 x N
159-
coords = np.row_stack([coords, np.ones_like(coords[0])])
159+
coords = np.vstack([coords, np.ones_like(coords[0])])
160160
return coords
161161

162162

@@ -520,13 +520,14 @@ def process_img(stat_img, cluster_extent, voxel_thresh=1.96, direction="both"):
520520
try:
521521
if min_region_size != 0.0:
522522
min_region_size -= 1e-8
523-
clusters += [
524-
connected_regions(
525-
image.new_img_like(thresh_img, data),
526-
min_region_size=min_region_size,
527-
extract_type="connected_components",
528-
)[0]
529-
]
523+
result = connected_regions(
524+
image.new_img_like(thresh_img, data),
525+
min_region_size=min_region_size,
526+
extract_type="connected_components",
527+
)[0]
528+
# connected_regions may return None when no clusters found
529+
if result is not None:
530+
clusters += [result]
530531
except TypeError: # for no clusters
531532
pass
532533

@@ -718,38 +719,43 @@ def get_statmap_info(
718719
cluster_id = np.repeat(n + 1, len(peak_data))
719720
peaks_info += [np.column_stack([cluster_id, peak_data])]
720721
clust_info += [[n + 1] + clust_data]
721-
clust_info = np.row_stack(clust_info)
722-
peaks_info = np.row_stack(peaks_info)
722+
clust_info = np.vstack(clust_info)
723+
peaks_info = np.vstack(peaks_info)
723724

724725
# construct dataframes and reset floats
725726
atlasnames = [a.atlas for a in atlas]
726-
clust_frame = pd.DataFrame(
727-
clust_info,
728-
columns=[
729-
"cluster_id",
730-
"peak_x",
731-
"peak_y",
732-
"peak_z",
733-
"cluster_mean",
734-
"volume_mm",
735-
]
736-
+ atlasnames,
737-
)
738-
peaks_frame = pd.DataFrame(
739-
peaks_info,
740-
columns=[
741-
"cluster_id",
742-
"peak_x",
743-
"peak_y",
744-
"peak_z",
745-
"peak_value",
746-
"volume_mm",
747-
]
748-
+ atlasnames,
749-
)
750-
for col in range(6):
751-
clust_frame.iloc[:, col] = clust_frame.iloc[:, col].astype(float)
752-
peaks_frame.iloc[:, col] = peaks_frame.iloc[:, col].astype(float)
727+
clust_columns = [
728+
"cluster_id",
729+
"peak_x",
730+
"peak_y",
731+
"peak_z",
732+
"cluster_mean",
733+
"volume_mm",
734+
] + atlasnames
735+
peaks_columns = [
736+
"cluster_id",
737+
"peak_x",
738+
"peak_y",
739+
"peak_z",
740+
"peak_value",
741+
"volume_mm",
742+
] + atlasnames
743+
744+
if len(clust_info) > 0:
745+
clust_frame = pd.DataFrame(clust_info, columns=clust_columns)
746+
# Convert numeric columns explicitly
747+
for col in clust_columns[:6]:
748+
clust_frame[col] = pd.to_numeric(clust_frame[col])
749+
else:
750+
clust_frame = pd.DataFrame(columns=clust_columns)
751+
752+
if len(peaks_info) > 0:
753+
peaks_frame = pd.DataFrame(peaks_info, columns=peaks_columns)
754+
# Convert numeric columns explicitly
755+
for col in peaks_columns[:6]:
756+
peaks_frame[col] = pd.to_numeric(peaks_frame[col])
757+
else:
758+
peaks_frame = pd.DataFrame(columns=peaks_columns)
753759

754760
return clust_frame, peaks_frame
755761

0 commit comments

Comments
 (0)