barplot.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import argparse
  2. from matplotlib import pyplot
  3. import pandas
  4. import seaborn
  5. from freesurfer_volume_reader import freesurfer
  6. def generate_freesurfer_mode_label(row):
  7. mri_sequences = ['T1' if row['T1_input'] else None,
  8. row['analysis_id'].split('_')[0] if row['analysis_id'] else None]
  9. return ' & '.join(filter(None, mri_sequences))
  10. def main():
  11. argparser = argparse.ArgumentParser()
  12. argparser.add_argument('--subject', required=True)
  13. argparser.add_argument('--subjects-dir', dest='subjects_dir_path', required=True)
  14. args = argparser.parse_args()
  15. volume_frame = pandas.concat(
  16. f.read_volumes_dataframe() for f in
  17. freesurfer.HippocampalSubfieldsVolumeFile.find(args.subjects_dir_path))
  18. volume_frame['subfield_segmentation_mode'] = volume_frame.apply(
  19. generate_freesurfer_mode_label, axis=1)
  20. volume_frame = volume_frame[volume_frame['subject'] == args.subject]
  21. volume_frame.to_csv('hippocampal_subfield_volumes_{}.csv'.format(args.subject), index=False)
  22. volume_frame.drop(columns=['subject', 'T1_input', 'analysis_id'], inplace=True)
  23. print(volume_frame)
  24. volume_frame.sort_values(['subfield_segmentation_mode', 'subfield'], inplace=True)
  25. seaborn.set(font_scale=0.3)
  26. volume_frame_subfields = volume_frame[volume_frame['subfield'] != 'Whole_hippocampus']
  27. for hemisphere in ['left', 'right']:
  28. pyplot.ylim(0, 750)
  29. plot_ax = seaborn.barplot(data=volume_frame_subfields[volume_frame_subfields['hemisphere']
  30. == hemisphere],
  31. x='subfield', y='volume_mm^3',
  32. hue='subfield_segmentation_mode')
  33. plot_ax.set_title('Hippocampal Subfield Volumes of Subject {}'.format(args.subject)
  34. + '\n{} Hemisphere'.format(str.capitalize(hemisphere)))
  35. pyplot.savefig('hippocampal_subfield_volumes_{}_{}.png'.format(args.subject, hemisphere))
  36. pyplot.clf()
  37. seaborn.set(font_scale=0.4)
  38. plot_ax = seaborn.barplot(data=volume_frame[volume_frame['subfield'] == 'Whole_hippocampus'],
  39. x='hemisphere', y='volume_mm^3',
  40. hue='subfield_segmentation_mode')
  41. plot_ax.set_title('Hippocampal Volume of Subject {}'.format(args.subject))
  42. pyplot.savefig('hippocampal_volume_{}.png'.format(args.subject))
  43. if __name__ == '__main__':
  44. main()