Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STYLE: Prefer using f-strings #196

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/harden_transform_with_slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ def harden_transform(polydata, transform, inverse, outdir):

check_load, polydata_node = slicer.util.loadFiberBundle(str(polydata), 1)
if not check_load:
print('Could not load polydata file:', polydata)
print(f'Could not load polydata file: {polydata}')
return

check_load, transform_node = slicer.util.loadTransform(str(transform), 1)
if not check_load:
print('Could not load transform file:', transform)
print(f'Could not load transform file: {transform}')
return

if polydata_node.GetPolyData().GetNumberOfCells() == 0:
print('Empty cluster:', polydata)
print(f'Empty cluster: {polydata}')
shutil.copyfile(polydata, output_name)
return

Expand Down
18 changes: 9 additions & 9 deletions bin/wm_append_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def main():

inputdir = os.path.abspath(args.inputDirectory)
if not os.path.isdir(args.inputDirectory):
print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: Input directory {args.inputDirectory} does not exist.")
exit()

outdir = os.path.abspath(args.outputDirectory)
if not os.path.exists(args.outputDirectory):
print(f"<{os.path.basename(__file__)}> Error: Output directory", args.outputDirectory, "does not exist, creating it.")
print(f"<{os.path.basename(__file__)}> Error: Output directory {args.outputDirectory} does not exist, creating it.")
os.makedirs(outdir)

if (args.clusterList is not None) and (args.tractMRML is not None):
Expand All @@ -71,7 +71,7 @@ def main():
for c_idx in args.clusterList:
cluster_vtp_filename = 'cluster_' + str(c_idx).zfill(5) + '.vtp'
if not os.path.exists(os.path.join(inputdir, cluster_vtp_filename)):
print(f"<{os.path.basename(__file__)}> Error:", cluster_vtp_filename, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: {cluster_vtp_filename} does not exist.")
exit()
cluster_vtp_list.append(cluster_vtp_filename)

Expand All @@ -83,7 +83,7 @@ def main():
if idx > 0:
cluster_vtp_filename = line[idx-13:idx+4]
if not os.path.exists(os.path.join(inputdir, cluster_vtp_filename)):
print(f"<{os.path.basename(__file__)}> Error:", cluster_vtp_filename, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: {cluster_vtp_filename} does not exist.")
exit()
cluster_vtp_list.append(cluster_vtp_filename)
else:
Expand All @@ -92,9 +92,9 @@ def main():
print("")
print(f"<{os.path.basename(__file__)}> Start to append clusters.")
print("")
print("=====input directory======\n", inputdir)
print("=====output directory=====\n", outdir)
print("=====clusters to be appended====\n", cluster_vtp_list)
print(f"=====input directory======\n {inputdir}")
print(f"=====output directory=====\n {outdir}")
print(f"=====clusters to be appended====\n {cluster_vtp_list}")
print("")

appender = vtk.vtkAppendPolyData()
Expand Down Expand Up @@ -122,9 +122,9 @@ def main():
wma.io.write_polydata(pd_appended_cluster, outputfile)

print('')
print(f'<{os.path.basename(__file__)}> Appended fiber tract: number of fibers', pd_appended_cluster.GetNumberOfLines())
print(f'<{os.path.basename(__file__)}> Appended fiber tract: number of fibers {pd_appended_cluster.GetNumberOfLines()}')
print('')
print(f'<{os.path.basename(__file__)}> Save result to', outputfile, '\n')
print(f'<{os.path.basename(__file__)}> Save result to {outputfile}\n')

if __name__ == '__main__':
main()
32 changes: 16 additions & 16 deletions bin/wm_append_clusters_to_anatomical_tracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,26 @@ def main():

inputdir = os.path.abspath(args.inputDirectory)
if not os.path.isdir(args.inputDirectory):
print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: Input directory {args.inputDirectory} does not exist.")
exit()

inputdir_left = os.path.join(inputdir, 'tracts_left_hemisphere')
inputdir_right = os.path.join(inputdir, 'tracts_right_hemisphere')
inputdir_comm = os.path.join(inputdir, 'tracts_commissural')

if not os.path.isdir(inputdir_left):
print(f"<{os.path.basename(__file__)}> Error: Input directory", inputdir_left, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: Input directory {inputdir_left} does not exist.")
exit()
if not os.path.isdir(inputdir_right):
print(f"<{os.path.basename(__file__)}> Error: Input directory", inputdir_right, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: Input directory {inputdir_right}does not exist.")
exit()
if not os.path.isdir(inputdir_comm):
print(f"<{os.path.basename(__file__)}> Error: Input directory", inputdir_comm, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: Input directory {inputdir_comm} does not exist.")
exit()

atlasdir = os.path.abspath(args.atlasDirectory)
if not os.path.isdir(args.atlasDirectory):
print(f"<{os.path.basename(__file__)}> Error: Atlas directory", args.atlasDirectory, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: Atlas directory {args.atlasDirectory} does not exist.")
exit()

def list_mrml_files(input_dir):
Expand All @@ -100,11 +100,11 @@ def list_mrml_files(input_dir):
if len(mrml_files) == 0:
print(f"<{os.path.basename(__file__)}> Error: There is no mrml files in the input atlas folder.")
else:
print(f"<{os.path.basename(__file__)}>", len(mrml_files)-1, "mrml files are detected.")
print(f"<{os.path.basename(__file__)}> {len(mrml_files)-1} mrml files are detected.")

outdir = os.path.abspath(args.outputDirectory)
if not os.path.exists(args.outputDirectory):
print(f"<{os.path.basename(__file__)}> Output directory", args.outputDirectory, "does not exist, creating it.")
print(f"<{os.path.basename(__file__)}> Output directory {args.outputDirectory} does not exist, creating it.")
os.makedirs(outdir)

def output_appended_tract(cluster_vtp_list, outputfile):
Expand Down Expand Up @@ -142,12 +142,12 @@ def output_appended_tract(cluster_vtp_list, outputfile):
tract_idx = 1
for tract in hemispheric_tracts:

print(" *", tract_idx, "-", tract)
print(f" * {tract_idx} - {tract}")
tract_idx = tract_idx + 1
mrml = os.path.join(atlasdir, tract+".mrml")

if not os.path.exists(mrml):
print(f"<{os.path.basename(__file__)}> Error: Cannot locate", mrml)
print(f"<{os.path.basename(__file__)}> Error: Cannot locate {mrml}")
exit()

cluster_vtp_list_left = list()
Expand All @@ -160,13 +160,13 @@ def output_appended_tract(cluster_vtp_list, outputfile):

cluster_vtp_filename_left = os.path.join(inputdir_left, cluster_vtp_filename)
if not os.path.exists(cluster_vtp_filename_left):
print(f"<{os.path.basename(__file__)}> Error:", cluster_vtp_filename_left, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: {cluster_vtp_filename_left} does not exist.")
exit()
cluster_vtp_list_left.append(cluster_vtp_filename_left)

cluster_vtp_filename_right = os.path.join(inputdir_right, cluster_vtp_filename)
if not os.path.exists(cluster_vtp_filename_right):
print(f"<{os.path.basename(__file__)}> Error:", cluster_vtp_filename_right, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error: {cluster_vtp_filename_right} does not exist.")
exit()
cluster_vtp_list_right.append(cluster_vtp_filename_right)

Expand All @@ -179,13 +179,13 @@ def output_appended_tract(cluster_vtp_list, outputfile):
print(f"<{os.path.basename(__file__)}> commissural tracts: ")
for tract in commissural_tracts:

print(" *", tract_idx, "-", tract)
print(f" * {tract_idx} - {tract}")
tract_idx = tract_idx + 1

mrml = os.path.join(atlasdir, tract+".mrml")

if not os.path.exists(mrml):
print(f"<{os.path.basename(__file__)}> Error: Cannot locate", mrml)
print(f"<{os.path.basename(__file__)}> Error: Cannot locate {mrml}")
exit()

cluster_vtp_list_comm = list()
Expand All @@ -197,7 +197,7 @@ def output_appended_tract(cluster_vtp_list, outputfile):

cluster_vtp_filename_comm = os.path.join(inputdir_comm, cluster_vtp_filename)
if not os.path.exists(cluster_vtp_filename_comm):
print(f"<{os.path.basename(__file__)}> Error:", cluster_vtp_filename_comm, "does not exist.")
print(f"<{os.path.basename(__file__)}> Error {cluster_vtp_filename_comm} does not exist.")
exit()
cluster_vtp_list_comm.append(cluster_vtp_filename_comm)

Expand All @@ -216,8 +216,8 @@ def list_cluster_files(input_dir):
list_tracts= list_cluster_files(outdir)

print('')
print(f'<{os.path.basename(__file__)}> Appended tracts can be found at', outdir, '\n')
print(f'<{os.path.basename(__file__)}> A total of', len(list_tracts), 'tracts\n')
print(f'<{os.path.basename(__file__)}> Appended tracts can be found at {outdir}\n')
print(f'<{os.path.basename(__file__)}> A total of {len(list_tracts)} tracts\n')


if __name__ == "__main__":
Expand Down
18 changes: 9 additions & 9 deletions bin/wm_append_diffusion_measures_across_subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def main():

subject_IDs = [os.path.basename(folder) for folder in subject_folders]
print()
print('Subject IDs: (n=%d): ' % len(subject_IDs))
print(f'Subject IDs: (n={len(subject_IDs)}): ')
print(subject_IDs)

# anatomical tracts
Expand All @@ -49,22 +49,22 @@ def main():
fields = [col.strip() for col in stats.columns]
fields = fields[1:]
print()
print('Tract diffusion measures per subject: (n=%d): ' % len(fields))
print(f'Tract diffusion measures per subject: (n={len(fields)}): ')
print(fields)

tracts = stats.to_numpy()[:, 0]
tracts = [os.path.basename(filepath).replace('.vtp', '').replace('.vtk', '').strip() for filepath in tracts]
print()
print('White matter tracts per subject: (n=%d): ' % len(tracts))
print(f'White matter tracts per subject: (n={len(tracts)}): ')
print(tracts)

appended_fields = ['subjectkey']
for tract in tracts:
for field in fields:
appended_fields.append("%s.%s" % (tract, field))
appended_fields.append(f"{tract}.{field}")

print()
print('Appended tract diffusion measure fields per subject: (n=%d): ' % len(appended_fields))
print(f'Appended tract diffusion measure fields per subject: (n={len(appended_fields)}): ')
print(appended_fields[:10], ' ... ')

data = []
Expand Down Expand Up @@ -101,13 +101,13 @@ def main():
fields = [col.strip() for col in stats.columns]
fields = fields[1:]
print()
print('Fiber cluster diffusion measures per subject: (n=%d): ' % len(fields))
print(f'Fiber cluster diffusion measures per subject: (n={len(fields)}): ')
print(fields)

clusters = stats.to_numpy()[:, 0]
clusters = [os.path.basename(filepath).replace('.vtp', '').replace('.vtk', '').strip() for filepath in clusters]
print()
print('Fiber clusters per subject: (n=%d): ' % len(clusters))
print(f'Fiber clusters per subject: (n={len(clusters)}): ')

if len(clusters) == 800 and len(tracts) == 73:
comm_clusters = [3, 8, 33, 40, 46, 52, 56, 57, 62, 68, 69, 73, 86, 91, 109, 110, 114, 142, 144, 145, 146, 159, 163, 250, 251, 252, 257, 262, 263, 268, 271, 305, 311, 314, 322, 330, 334, 338, 342, 350, 363, 371, 375, 403, 410, 437, 440, 448, 456, 465, 468, 475, 484, 485, 488, 519, 522, 525, 543, 545, 549, 557, 576, 577, 582, 587, 591, 597, 601, 614, 620, 623, 627, 633, 645, 653, 658, 663, 670, 677, 683, 702, 770, 781]
Copy link
Contributor Author

@jhlegarreta jhlegarreta Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should not be hard-coded here; they should be in a separate file so that

  • they can be located easily.
  • they can be reused by other scripts, if necessary.
  • if they need to be changed, this can be done easily in a place that contains only data-/atlas-related labels, and does not pollute the code.

To be done in a separate PR.

Expand All @@ -132,10 +132,10 @@ def main():

for cluster in clusters_loc:
for field in fields:
appended_fields.append("%s.%s.%s" % (loc, cluster, field))
appended_fields.append(f"{loc}.{cluster}.{field}")

print()
print('Appended cluster diffusion measure fields per subject: (n=%d): ' % len(appended_fields))
print(f'Appended cluster diffusion measure fields per subject: (n={len(appended_fields)}): ')

data = []
for s_idx, subject_folder in enumerate(subject_folders):
Expand Down
4 changes: 2 additions & 2 deletions bin/wm_assess_cluster_location_by_hemisphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _read_location(_inpd):
args = _parse_args(parser)

if not os.path.isdir(args.inputDirectory):
print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist or is not a directory.")
print(f"<{os.path.basename(__file__)}> Error: Input directory {args.inputDirectory} does not exist or is not a directory.")
exit()

clusterLocationFile = args.clusterLocationFile
Expand Down Expand Up @@ -172,7 +172,7 @@ def _read_location(_inpd):
if outdir is not None:
print(f"<{os.path.basename(__file__)}> Separated clusters will be ouput.")
if not os.path.exists(outdir):
print(f"<{os.path.basename(__file__)}> Output directory", outdir, "does not exist, creating it.")
print(f"<{os.path.basename(__file__)}> Output directory {outdir} does not exist, creating it.")
os.makedirs(outdir)

outdir_right = os.path.join(outdir, 'tracts_right_hemisphere')
Expand Down
8 changes: 4 additions & 4 deletions bin/wm_average_tract_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def main():

print('All available tracts:')
all_tracts = [f_name.replace('.Num_Points', '') for f_name in fields if f_name.endswith('.Num_Points')]
print('N = ', str(len(all_tracts)), ':')
print(f'N = {len(all_tracts)} :')
print(all_tracts)

append_list = []
print('Tracts and related measures to be appended.')
for t_idx, tract in enumerate(args.tractList):
print('*', t_idx, '-', tract)
print(f'* {t_idx} - {tract}')
indices = [index for index in range(len(fields)) if fields[index].startswith(tract+'.Num_') or re.search(tract+".*.Mean", fields[index])]
if len(indices) == 0:
print("Error: tract not founded in the input file.")
Expand All @@ -68,7 +68,7 @@ def main():
append_list = np.array(append_list)
append_measures = [field.replace(args.tractList[-1], '') for field in fields[indices]]

print("Output measures:", append_measures)
print(f"Output measures: {append_measures}")

avg_stats = [stats.to_numpy()[:, 0]]
for m_idx, m_name in enumerate(append_measures):
Expand Down Expand Up @@ -125,7 +125,7 @@ def main():

df.to_csv(args.outoutmeasure, index=False)
print()
print('Output file at:', os.path.abspath(args.outoutmeasure))
print(f'Output file at: {os.path.abspath(args.outoutmeasure)}')

exit()

Expand Down
2 changes: 1 addition & 1 deletion bin/wm_change_nrrd_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def main():
with open(outputnrrd, 'w') as o:
o.write(outstr)

print('Done! New nrrd header: ' + outputnrrd)
print(f'Done! New nrrd header {outputnrrd}')


if __name__ == "__main__":
Expand Down
Loading