antspymm 1.3.1__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]
@@ -10440,6 +10461,8 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10440
10461
  mydict = pd.read_csv(data_dictionary_path)
10441
10462
  mydict = mydict[~mydict['Measurement'].str.contains("tractography-based connectivity", na=False)]
10442
10463
 
10464
+ statistical_df['anat'] = statistical_df['anat'].str.replace("_", ".", regex=True)
10465
+
10443
10466
  # Load image and process it
10444
10467
  edgeimg = ants.iMath(brain_image,"Normalize")
10445
10468
  if edge_image_dilation > 0:
@@ -10465,6 +10488,7 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10465
10488
  print(str(k) + " " + anattoshow[k] )
10466
10489
  mysub = zz[zz['anat'].str.contains(anattoshow[k])]
10467
10490
  anatsear=re.sub("dti.fa","",anattoshow[k])
10491
+ anatsear=re.sub("cbf.","",anatsear)
10468
10492
  anatsear=re.sub("t1.volasym","",anatsear)
10469
10493
  anatsear=re.sub("t1.thkasym","",anatsear)
10470
10494
  anatsear=re.sub("t1.areaasym","",anatsear)
@@ -10480,6 +10504,8 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10480
10504
  anatsear=re.sub("dti.mean.fa.","",anatsear)
10481
10505
  anatsear=re.sub("lravg","",anatsear)
10482
10506
  atlassearch = mydict['tidynames'].str.contains(anatsear)
10507
+ if verbose:
10508
+ print( " anatsear " + anatsear + " atlassearch " )
10483
10509
  if atlassearch.sum() > 0:
10484
10510
  whichatlas = mydict[atlassearch]['Atlas'].iloc[0]
10485
10511
  oglabelname = mydict[atlassearch]['Label'].iloc[0]
@@ -10488,13 +10514,13 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10488
10514
  oglabelname='unknown'
10489
10515
  whichatlas=None
10490
10516
  if verbose:
10491
- print("oglabelname " + oglabelname )
10517
+ print("oglabelname " + oglabelname + " whichatlas " + str(whichatlas) )
10492
10518
  vals2viz = mysub[col2viz].agg(['min', 'max'])
10493
10519
  vals2viz = vals2viz[abs(vals2viz).idxmax()]
10494
10520
  myext = None
10495
- if 'dktcortex' in anattoshow[k]:
10521
+ if 'dktcortex' in anattoshow[k] or whichatlas == 'desikan-killiany-tourville' or 'dtkregions' in anattoshow[k]:
10496
10522
  myext = 'dkt_cortex'
10497
- elif 'cit168' in anattoshow[k]:
10523
+ elif 'cit168' in anattoshow[k] or whichatlas == 'CIT168':
10498
10524
  myext = 'cit168lab'
10499
10525
  elif 'mtl' in anattoshow[k]:
10500
10526
  myext = 'mtl'
@@ -10520,6 +10546,10 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10520
10546
  elif whichatlas == 'yeo_homotopic':
10521
10547
  myext = 'yeo'
10522
10548
  if myext is None and verbose:
10549
+ if whichatlas is None:
10550
+ whichatlas='None'
10551
+ if anattoshow[k] is None:
10552
+ anattoshow[k]='None'
10523
10553
  print( "MYEXT " + anattoshow[k] + ' unfound ' + whichatlas )
10524
10554
  else:
10525
10555
  if verbose:
@@ -10595,9 +10625,17 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10595
10625
  print("oglabelname " + oglabelname )
10596
10626
 
10597
10627
  if myext == 'cerebellum':
10598
- atlasDescript['Description'] = atlasDescript['Description'].str.replace("l_", "")
10599
- atlasDescript['Description'] = atlasDescript['Description'].str.replace("r_", "")
10600
- 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'.")
10601
10639
  else:
10602
10640
  whichindex = atlasDescript.index[atlasDescript['Description'].str.contains(oglabelname)]
10603
10641
 
@@ -10639,10 +10677,10 @@ def brainmap_figure(statistical_df, data_dictionary_path, output_prefix, brain_i
10639
10677
  if fixed_overlay_range is not None:
10640
10678
  addemC[0:3,0:3,0:3]=fixed_overlay_range[0]
10641
10679
  addemC[4:7,4:7,4:7]=fixed_overlay_range[1]
10642
- addemC[ addemC < fixed_overlay_range[0] ] = fixed_overlay_range[0]
10643
- 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]
10644
10682
  ants.plot(edgeimgC, addemC, axis=axx, nslices=nslices, ncol=ncol,
10645
- overlay_cmap=overlay_cmap, resample=False,
10683
+ overlay_cmap=overlay_cmap, resample=False, overlay_alpha=1.0,
10646
10684
  filename=figfn, cbar=axx==axes[0], crop=True, black_bg=black_bg )
10647
10685
  if verbose:
10648
10686
  print(f"{col2viz} done")
@@ -10944,10 +10982,10 @@ def aggregate_antspymm_results_sdf(
10944
10982
  myfn = os.path.basename( df['filename'].iloc[x] )
10945
10983
  temp = myfn.split( splitsep )
10946
10984
  # Generalized search paths
10947
- sid0 = str( temp[0] )
10985
+ sid0 = str( temp[1] )
10948
10986
  sid = str( df[subject_col].iloc[x] )
10949
10987
  if sid0 != sid:
10950
- 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 )
10951
10989
  warnings.warn( "filename is : " + myfn )
10952
10990
  warnings.warn( "sid is : " + sid )
10953
10991
  warnings.warn( "x is : " + str(x) )
@@ -11191,8 +11229,11 @@ def mm_match_by_qc_scoring(df_a, df_b, match_column, criteria, prefix='matched_'
11191
11229
  2. A DataFrame containing rows from df_b that were not matched to df_a.
11192
11230
  """
11193
11231
  from scipy.stats import zscore
11194
- df_a = df_a.loc[:, ~df_a.columns.str.startswith('Unnamed:')]
11195
- 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()
11196
11237
 
11197
11238
  # Normalize df_b based on criteria
11198
11239
  for col, crit in criteria.items():
@@ -11372,15 +11413,16 @@ def mm_match_by_qc_scoring_all( qc_dataframe, fix_LRRL=True, verbose=True ):
11372
11413
 
11373
11414
  prefixes = ['NM1_', 'NM2_', 'NM3_', 'NM4_', 'NM5_', 'NM6_']
11374
11415
  undfmod = nmdf # Initialize 'undfmod' with 'nmdf' for the first iteration
11375
- if verbose:
11376
- print('start NM')
11377
- print( undfmod.shape )
11378
- for prefix in prefixes:
11379
- if undfmod.shape[0] > 50:
11380
- mmdf, undfmod = mm_match_by_qc_scoring(mmdf, undfmod, 'subjectIDdate', criteria, prefix=prefix, exclude_columns=xcl)
11381
- if verbose:
11382
- print( prefix )
11383
- 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 )
11384
11426
 
11385
11427
  criteria = {'ol_loop': 'min', 'noise': 'min', 'snr': 'max', 'EVR': 'max', 'dimt':'max'}
11386
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.1
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=pJY1DIRTLf3ai6znZFoEnvuMHR7JUbusAq__3aahi2E,476486
3
- antspymm-1.3.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
4
- antspymm-1.3.1.dist-info/METADATA,sha256=rfgI7TXJi7yRVNE-h4_QFNN_J1gBI2JQMGDQn_-_qkE,14590
5
- antspymm-1.3.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- antspymm-1.3.1.dist-info/top_level.txt,sha256=iyD1sRhCKzfwKRJLq5ZUeV9xsv1cGQl8Ejp6QwXM1Zg,9
7
- antspymm-1.3.1.dist-info/RECORD,,