antspymm 1.3.3__py3-none-any.whl → 1.3.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
antspymm/mm.py CHANGED
@@ -1061,6 +1061,27 @@ def nrg_format_path( projectID, subjectID, date, modality, imageID, separator='-
1061
1061
  return os.path.join( thedirectory, thefilename )
1062
1062
 
1063
1063
 
1064
+ def get_first_item_as_string(df, column_name):
1065
+ """
1066
+ Check if the first item in the specified column of the DataFrame is a string.
1067
+ If it is not a string, attempt to convert it to an integer and then to a string.
1068
+
1069
+ Parameters:
1070
+ df (pd.DataFrame): The DataFrame to operate on.
1071
+ column_name (str): The name of the column to check.
1072
+
1073
+ Returns:
1074
+ str: The first item in the specified column, guaranteed to be returned as a string.
1075
+ """
1076
+ if isinstance(df[column_name].iloc[0], str):
1077
+ return df[column_name].iloc[0]
1078
+ else:
1079
+ try:
1080
+ return str(int(df[column_name].iloc[0]))
1081
+ except ValueError:
1082
+ raise ValueError("The value cannot be converted to an integer.")
1083
+
1084
+
1064
1085
  def study_dataframe_from_matched_dataframe( matched_dataframe, rootdir, outputdir, verbose=False ):
1065
1086
  """
1066
1087
  converts the output of antspymm.match_modalities dataframe (one row) to that needed for a study-driving dataframe for input to mm_csv
@@ -1078,12 +1099,12 @@ def study_dataframe_from_matched_dataframe( matched_dataframe, rootdir, outputdi
1078
1099
  musthavecols = ['projectID', 'subjectID','date','imageID','filename']
1079
1100
  for k in range(len(musthavecols)):
1080
1101
  if not musthavecols[k] in matched_dataframe.keys():
1081
- raise ValueError('matched_dataframe is missing column ' +musthavecols[k] + ' in study_dataframe_from_qc_dataframe' )
1102
+ raise ValueError('matched_dataframe is missing column ' + musthavecols[k] + ' in study_dataframe_from_qc_dataframe' )
1082
1103
  csvrow=matched_dataframe.dropna(axis=1)
1083
- pid=str(csvrow['projectID'].iloc[0] )
1084
- sid=str(csvrow['subjectID'].iloc[0] )
1085
- dt=str(csvrow['date'].iloc[0])
1086
- iid=str(csvrow['imageID'].iloc[0])
1104
+ pid=get_first_item_as_string( csvrow, 'projectID' )
1105
+ sid=get_first_item_as_string( csvrow, 'subjectID' ) # str(csvrow['subjectID'].iloc[0] )
1106
+ dt=get_first_item_as_string( csvrow, 'date' ) # str(csvrow['date'].iloc[0])
1107
+ iid=get_first_item_as_string( csvrow, 'imageID' ) # str(csvrow['imageID'].iloc[0])
1087
1108
  nrgt1fn=os.path.join( rootdir, pid, sid, dt, 'T1w', iid, str(csvrow['filename'].iloc[0]+iext) )
1088
1109
  if not exists( nrgt1fn ):
1089
1110
  raise ValueError("T1 " + nrgt1fn + " does not exist in study_dataframe_from_qc_dataframe")
@@ -1092,32 +1113,32 @@ def study_dataframe_from_matched_dataframe( matched_dataframe, rootdir, outputdi
1092
1113
  rsfList=[]
1093
1114
  nmList=[]
1094
1115
  if 'flairfn' in csvrow.keys():
1095
- flid=str(int(csvrow['flairid'].iloc[0]))
1116
+ flid=get_first_item_as_string( csvrow, 'flairid' )
1096
1117
  nrgt2fn=os.path.join( rootdir, pid, sid, dt, 'T2Flair', flid, str(csvrow['flairfn'].iloc[0]+iext) )
1097
1118
  if exists( nrgt2fn ):
1098
1119
  flList.append( nrgt2fn )
1099
1120
  if 'dtfn1' in csvrow.keys():
1100
- dtid=str(int(csvrow['dtid1'].iloc[0]))
1121
+ dtid=get_first_item_as_string( csvrow, 'dtid1' )
1101
1122
  dtfn1=glob.glob(os.path.join( rootdir, pid, sid, dt, 'DTI*', dtid, str(csvrow['dtfn1'].iloc[0]+iext) ))[0]
1102
1123
  if exists( dtfn1 ):
1103
1124
  dtList.append( dtfn1 )
1104
1125
  if 'dtfn2' in csvrow.keys():
1105
- dtid=str(int(csvrow['dtid2'].iloc[0]))
1126
+ dtid=get_first_item_as_string( csvrow, 'dtid2' )
1106
1127
  dtfn2=glob.glob(os.path.join(rootdir, pid, sid, dt, 'DTI*', dtid, str(csvrow['dtfn2'].iloc[0]+iext) ))[0]
1107
1128
  if exists( dtfn2 ):
1108
1129
  dtList.append( dtfn2 )
1109
1130
  if 'dtfn3' in csvrow.keys():
1110
- dtid=str(int(csvrow['dtid3'].iloc[0]))
1131
+ dtid=get_first_item_as_string( csvrow, 'dtid3' )
1111
1132
  dtfn3=glob.glob(os.path.join(rootdir, pid, sid, dt, 'DTI*', dtid, str(csvrow['dtfn3'].iloc[0]+iext) ))[0]
1112
1133
  if exists( dtfn3 ):
1113
1134
  dtList.append( dtfn3 )
1114
1135
  if 'rsffn1' in csvrow.keys():
1115
- rsid=str(int(csvrow['rsfid1'].iloc[0]))
1136
+ rsid=get_first_item_as_string( csvrow, 'rsfid1' )
1116
1137
  rsfn1=glob.glob(os.path.join( rootdir, pid, sid, dt, 'rsfMRI*', rsid, str(csvrow['rsffn1'].iloc[0]+iext) ))[0]
1117
1138
  if exists( rsfn1 ):
1118
1139
  rsfList.append( rsfn1 )
1119
1140
  if 'rsffn2' in csvrow.keys():
1120
- rsid=str(int(csvrow['rsfid2'].iloc[0]))
1141
+ rsid=get_first_item_as_string( csvrow, 'rsfid2' )
1121
1142
  rsfn2=glob.glob(os.path.join( rootdir, pid, sid, dt, 'rsfMRI*', rsid, str(csvrow['rsffn2'].iloc[0]+iext) ))[0]
1122
1143
  if exists( rsfn2 ):
1123
1144
  rsfList.append( rsfn2 )
@@ -1125,7 +1146,7 @@ def study_dataframe_from_matched_dataframe( matched_dataframe, rootdir, outputdi
1125
1146
  keyname="nmfn"+str(j)
1126
1147
  keynameid="nmid"+str(j)
1127
1148
  if keyname in csvrow.keys() and keynameid in csvrow.keys():
1128
- nmid=str(int(csvrow[keynameid].iloc[0]))
1149
+ nmid=get_first_item_as_string( csvrow, keynameid )
1129
1150
  nmsearchpath=os.path.join( rootdir, pid, sid, dt, 'NM2DMT', nmid, "*"+nmid+iext)
1130
1151
  nmfn=glob.glob( nmsearchpath )
1131
1152
  nmfn=nmfn[0]
@@ -10467,6 +10488,7 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10467
10488
  print(str(k) + " " + anattoshow[k] )
10468
10489
  mysub = zz[zz['anat'].str.contains(anattoshow[k])]
10469
10490
  anatsear=re.sub("dti.fa","",anattoshow[k])
10491
+ anatsear=re.sub("cbf.","",anatsear)
10470
10492
  anatsear=re.sub("t1.volasym","",anatsear)
10471
10493
  anatsear=re.sub("t1.thkasym","",anatsear)
10472
10494
  anatsear=re.sub("t1.areaasym","",anatsear)
@@ -10482,6 +10504,8 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10482
10504
  anatsear=re.sub("dti.mean.fa.","",anatsear)
10483
10505
  anatsear=re.sub("lravg","",anatsear)
10484
10506
  atlassearch = mydict['tidynames'].str.contains(anatsear)
10507
+ if verbose:
10508
+ print( " anatsear " + anatsear + " atlassearch " )
10485
10509
  if atlassearch.sum() > 0:
10486
10510
  whichatlas = mydict[atlassearch]['Atlas'].iloc[0]
10487
10511
  oglabelname = mydict[atlassearch]['Label'].iloc[0]
@@ -10490,13 +10514,13 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10490
10514
  oglabelname='unknown'
10491
10515
  whichatlas=None
10492
10516
  if verbose:
10493
- print("oglabelname " + oglabelname )
10517
+ print("oglabelname " + oglabelname + " whichatlas " + str(whichatlas) )
10494
10518
  vals2viz = mysub[col2viz].agg(['min', 'max'])
10495
10519
  vals2viz = vals2viz[abs(vals2viz).idxmax()]
10496
10520
  myext = None
10497
- if 'dktcortex' in anattoshow[k]:
10521
+ if 'dktcortex' in anattoshow[k] or whichatlas == 'desikan-killiany-tourville' or 'dtkregions' in anattoshow[k]:
10498
10522
  myext = 'dkt_cortex'
10499
- elif 'cit168' in anattoshow[k]:
10523
+ elif 'cit168' in anattoshow[k] or whichatlas == 'CIT168':
10500
10524
  myext = 'cit168lab'
10501
10525
  elif 'mtl' in anattoshow[k]:
10502
10526
  myext = 'mtl'
@@ -10601,9 +10625,17 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10601
10625
  print("oglabelname " + oglabelname )
10602
10626
 
10603
10627
  if myext == 'cerebellum':
10604
- atlasDescript['Description'] = atlasDescript['Description'].str.replace("l_", "")
10605
- atlasDescript['Description'] = atlasDescript['Description'].str.replace("r_", "")
10606
- whichindex = atlasDescript.index[atlasDescript['Description'] == oglabelname].values[0]
10628
+ if not atlasDescript.empty and 'Description' in atlasDescript.columns:
10629
+ atlasDescript['Description'] = atlasDescript['Description'].str.replace("l_", "")
10630
+ atlasDescript['Description'] = atlasDescript['Description'].str.replace("r_", "")
10631
+ oglabelname=re.sub("ravg","",oglabelname)
10632
+ oglabelname=re.sub("lavg","",oglabelname)
10633
+ whichindex = atlasDescript.index[atlasDescript['Description'] == oglabelname].values[0]
10634
+ else:
10635
+ if atlasDescript.empty:
10636
+ print("The DataFrame 'atlasDescript' is empty.")
10637
+ if 'Description' not in atlasDescript.columns:
10638
+ print("The column 'Description' does not exist in 'atlasDescript'.")
10607
10639
  else:
10608
10640
  whichindex = atlasDescript.index[atlasDescript['Description'].str.contains(oglabelname)]
10609
10641
 
@@ -10645,10 +10677,10 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10645
10677
  if fixed_overlay_range is not None:
10646
10678
  addemC[0:3,0:3,0:3]=fixed_overlay_range[0]
10647
10679
  addemC[4:7,4:7,4:7]=fixed_overlay_range[1]
10648
- addemC[ addemC < fixed_overlay_range[0] ] = fixed_overlay_range[0]
10649
- addemC[ addemC > fixed_overlay_range[1] ] = fixed_overlay_range[1]
10680
+ addemC[ addemC <= fixed_overlay_range[0] ] = 0 # fixed_overlay_range[0]
10681
+ addemC[ addemC >= fixed_overlay_range[1] ] = fixed_overlay_range[1]
10650
10682
  ants.plot(edgeimgC, addemC, axis=axx, nslices=nslices, ncol=ncol,
10651
- overlay_cmap=overlay_cmap, resample=False,
10683
+ overlay_cmap=overlay_cmap, resample=False, overlay_alpha=1.0,
10652
10684
  filename=figfn, cbar=axx==axes[0], crop=True, black_bg=black_bg )
10653
10685
  if verbose:
10654
10686
  print(f"{col2viz} done")
@@ -10950,10 +10982,10 @@ def aggregate_antspymm_results_sdf(
10950
10982
  myfn = os.path.basename( df['filename'].iloc[x] )
10951
10983
  temp = myfn.split( splitsep )
10952
10984
  # Generalized search paths
10953
- sid0 = str( temp[0] )
10985
+ sid0 = str( temp[1] )
10954
10986
  sid = str( df[subject_col].iloc[x] )
10955
10987
  if sid0 != sid:
10956
- warnings.warn("OUTER: the id derived from the filename " + sid + " does not match the id stored in the data frame " + sid )
10988
+ warnings.warn("OUTER: the id derived from the filename " + sid0 + " does not match the id stored in the data frame " + sid )
10957
10989
  warnings.warn( "filename is : " + myfn )
10958
10990
  warnings.warn( "sid is : " + sid )
10959
10991
  warnings.warn( "x is : " + str(x) )
@@ -11197,8 +11229,11 @@ def mm_match_by_qc_scoring(df_a, df_b, match_column, criteria, prefix='matched_'
11197
11229
  2. A DataFrame containing rows from df_b that were not matched to df_a.
11198
11230
  """
11199
11231
  from scipy.stats import zscore
11200
- df_a = df_a.loc[:, ~df_a.columns.str.startswith('Unnamed:')]
11201
- df_b = df_b.loc[:, ~df_b.columns.str.startswith('Unnamed:')].copy()
11232
+ df_a = df_a.loc[:, ~df_a.columns.str.startswith('Unnamed:')].copy()
11233
+ if df_b is not None:
11234
+ df_b = df_b.loc[:, ~df_b.columns.str.startswith('Unnamed:')].copy()
11235
+ else:
11236
+ return df_a, pd.DataFrame()
11202
11237
 
11203
11238
  # Normalize df_b based on criteria
11204
11239
  for col, crit in criteria.items():
@@ -11378,15 +11413,16 @@ def mm_match_by_qc_scoring_all( qc_dataframe, fix_LRRL=True, verbose=True ):
11378
11413
 
11379
11414
  prefixes = ['NM1_', 'NM2_', 'NM3_', 'NM4_', 'NM5_', 'NM6_']
11380
11415
  undfmod = nmdf # Initialize 'undfmod' with 'nmdf' for the first iteration
11381
- if verbose:
11382
- print('start NM')
11383
- print( undfmod.shape )
11384
- for prefix in prefixes:
11385
- if undfmod.shape[0] > 50:
11386
- mmdf, undfmod = mm_match_by_qc_scoring(mmdf, undfmod, 'subjectIDdate', criteria, prefix=prefix, exclude_columns=xcl)
11387
- if verbose:
11388
- print( prefix )
11389
- print( undfmod.shape )
11416
+ if undfmod is not None:
11417
+ if verbose:
11418
+ print('start NM')
11419
+ print( undfmod.shape )
11420
+ for prefix in prefixes:
11421
+ if undfmod.shape[0] > 50:
11422
+ mmdf, undfmod = mm_match_by_qc_scoring(mmdf, undfmod, 'subjectIDdate', criteria, prefix=prefix, exclude_columns=xcl)
11423
+ if verbose:
11424
+ print( prefix )
11425
+ print( undfmod.shape )
11390
11426
 
11391
11427
  criteria = {'ol_loop': 'min', 'noise': 'min', 'snr': 'max', 'EVR': 'max', 'dimt':'max'}
11392
11428
  # higher bvalues lead to more noise ...
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: antspymm
3
- Version: 1.3.3
3
+ Version: 1.3.4
4
4
  Summary: multi-channel/time-series medical image processing with antspyx
5
5
  Home-page: https://github.com/stnava/ANTsPyMM
6
6
  Author: Avants, Gosselin, Tustison, Reardon
@@ -0,0 +1,7 @@
1
+ antspymm/__init__.py,sha256=1fHqufHndrkJwz473av8qOf5-1xm5r-aKHuMAETGIiE,4462
2
+ antspymm/mm.py,sha256=FbboL-Mdg6KTzWvAm770qaKEMLB9wK_isrBmqVbqyQw,478666
3
+ antspymm-1.3.4.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
4
+ antspymm-1.3.4.dist-info/METADATA,sha256=EwUjVut7HWbQQlz-kUaf5qFrfYXXuPXPL5mmeiXdW3A,14590
5
+ antspymm-1.3.4.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ antspymm-1.3.4.dist-info/top_level.txt,sha256=iyD1sRhCKzfwKRJLq5ZUeV9xsv1cGQl8Ejp6QwXM1Zg,9
7
+ antspymm-1.3.4.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- antspymm/__init__.py,sha256=1fHqufHndrkJwz473av8qOf5-1xm5r-aKHuMAETGIiE,4462
2
- antspymm/mm.py,sha256=R7W1v2ejIjIc5FZaAVLfyX9De8R5A_W2U-AyHZenxUg,476733
3
- antspymm-1.3.3.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
4
- antspymm-1.3.3.dist-info/METADATA,sha256=PJy3nUwSUKIXi62KNJ73RlPBUwL16kX0ZP32gHq3IAg,14590
5
- antspymm-1.3.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- antspymm-1.3.3.dist-info/top_level.txt,sha256=iyD1sRhCKzfwKRJLq5ZUeV9xsv1cGQl8Ejp6QwXM1Zg,9
7
- antspymm-1.3.3.dist-info/RECORD,,