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.freesurfer import HippocampalSubfieldsVolumeFile
  6. from freesurfer_volume_reader import read_hippocampal_volume_file_dataframe
  7. def abbreviate_analysis_id(analysis_id):
  8. return analysis_id.split('_')[0] if analysis_id else None
  9. def main():
  10. argparser = argparse.ArgumentParser()
  11. argparser.add_argument('--subject', required=True)
  12. argparser.add_argument('--subjects-dir', dest='subjects_dir_path', required=True)
  13. args = argparser.parse_args()
  14. volume_frame = pandas.concat(read_hippocampal_volume_file_dataframe(p)
  15. for p in HippocampalSubfieldsVolumeFile.find(args.subjects_dir_path))
  16. volume_frame = volume_frame[volume_frame['subject'] == args.subject]
  17. volume_frame['subfield_segmentation_mode'] = volume_frame.apply(
  18. lambda row: ' & '.join(filter(None, ('T1' if row['T1_input'] else None,
  19. abbreviate_analysis_id(row['analysis_id'])))),
  20. axis=1,
  21. )
  22. volume_frame.to_csv('hippocampal_subfield_volumes_{}.csv'.format(args.subject), index=False)
  23. volume_frame.drop(columns=['subject', 'T1_input', 'analysis_id'], inplace=True)
  24. print(volume_frame)
  25. volume_frame.sort_values(['subfield_segmentation_mode', 'subfield'], inplace=True)
  26. seaborn.set(font_scale=0.3)
  27. volume_frame_subfields = volume_frame[volume_frame['subfield'] != 'Whole_hippocampus']
  28. for hemisphere in ['left', 'right']:
  29. pyplot.ylim(0, 750)
  30. ax = seaborn.barplot(data=volume_frame_subfields[volume_frame_subfields['hemisphere'] == hemisphere],
  31. x='subfield', y='volume_mm^3',
  32. hue='subfield_segmentation_mode')
  33. 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. ax = seaborn.barplot(data=volume_frame[volume_frame['subfield'] == 'Whole_hippocampus'],
  39. x='hemisphere', y='volume_mm^3',
  40. hue='subfield_segmentation_mode')
  41. 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()