import numpy as np
import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats
from src.ground_truth import AnnotationReader, GroundTruth
set_matplotlib_formats('svg')
annotations_folder = '/Users/jcboyd/Documents/hcs/input/annotations/'
ch5_folder = '/Users/jcboyd/Documents/hcs/input/hdf5/'
classes = ['interphase', 'large', 'prometaphase', 'metaphase', 'bright',
'anaphase', 'early anaphase', 'polylobed', 'apoptosis']
ar = AnnotationReader(annotations_folder, ch5_folder)
ar.read_annotation_data(read_file=False)
ar.df_ground_truth.head()
ar.save_data()
ar.df_ground_truth.groupby('class')['class'].count()
annotations_folder = '/Users/jcboyd/Documents/hcs/input/annotations_control/'
ar2 = AnnotationReader(annotations_folder, ch5_folder)
ar2.read_annotation_data()
ar2.df_ground_truth.groupby('class')['class'].count()
comparison = ar2.make_comparison(ar)
N = 7
agrees = [comparison[key][0] for key in comparison.keys()]
disagrees = [comparison[key][1] for key in comparison.keys()]
ind = np.arange(N)
width = 0.35
p1 = plt.bar(ind, agrees, width, color='red')
p2 = plt.bar(ind, disagrees, width, bottom=agrees, color='blue')
plt.ylabel('Total')
plt.title('Comparison of annotations by class')
plt.xticks(ind, np.array(classes)[[0, 1, 2, 3, 4, 7, 8]], rotation='vertical')
plt.legend((p1[0], p2[0]), ('Agree', 'Disagree'))
plt.gca().set_ylim([0, 180])
plt.show()
heights = [sample['bottom'] - sample['top'] for _, sample in ar.df_ground_truth.iterrows()]
widths = [sample['right'] - sample['left'] for _, sample in ar.df_ground_truth.iterrows()]
fig = plt.figure(figsize=(6, 3))
for i, data in enumerate([heights, widths]):
fig.add_subplot(1, 2, i + 1)
plt.title(['heights', 'widths'][i])
plt.hist(heights, bins=20, color=['red', 'blue'][i])
plt.tight_layout()
plt.show()
percentile = 99
np.percentile(heights, percentile), np.percentile(widths, percentile)
IMG_SIZE = 112
DOWNSCALE = 4
output = './output/ground_truth/%dd%d' % (IMG_SIZE, DOWNSCALE)
gg = GroundTruth(ar.df_ground_truth, ch5_folder)
gg.export_ground_truth(output, IMG_SIZE=IMG_SIZE, downsize_factor=DOWNSCALE)