1
|
|
|
import sys |
2
|
|
|
import matplotlib |
3
|
|
|
matplotlib.use('Agg') |
4
|
|
|
import skimage.io as sio |
5
|
|
|
import matplotlib.pyplot as plt |
6
|
|
|
import numpy as np |
7
|
|
|
import pandas as pd |
8
|
|
|
import diff_classifier.aws as aws |
9
|
|
|
from skimage.filters import roberts, sobel, scharr, prewitt, median, rank |
10
|
|
|
from skimage import img_as_ubyte |
11
|
|
|
from skimage.morphology import erosion, dilation, opening, closing, white_tophat, disk, reconstruction |
12
|
|
|
from scipy.ndimage.morphology import distance_transform_edt as EuclideanTransform |
13
|
|
|
from operator import itemgetter |
14
|
|
|
|
15
|
|
|
to_track = [] |
16
|
|
|
result_futures = {} |
17
|
|
|
|
18
|
|
|
remote_folder = 'Cell_Studies/10_16_18_cell_study' #Folder in AWS S3 containing files to be analyzed |
19
|
|
|
bucket = 'ccurtis.data' |
20
|
|
|
vids = 5 |
21
|
|
|
types = ['PS', 'PEG'] |
22
|
|
|
slices = [1, 2] |
23
|
|
|
|
24
|
|
|
for typ in types: |
25
|
|
|
for slic in slices: |
26
|
|
|
for num in range(1, vids+1): |
|
|
|
|
27
|
|
|
#to_track.append('100x_0_4_1_2_gel_{}_bulk_vid_{}'.format(vis, num)) |
28
|
|
|
to_track.append('{}_{}_XY{}'.format(typ, slic, num)) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def binary_BF(image, meanse=disk(10), edgefilt='prewitt', opense=disk(10), |
32
|
|
|
fill_first=False, bi_thresh=0.000025, tophatse=disk(20)): |
33
|
|
|
|
34
|
|
|
#convertim = img_as_ubyte(image) |
35
|
|
|
meanim = rank.mean(image, meanse) |
36
|
|
|
if edgefilt is 'prewitt': |
37
|
|
|
edgeim = prewitt(meanim) |
38
|
|
|
elif edgefilt is 'sobel': |
39
|
|
|
edgeim = sobel(meanim) |
40
|
|
|
elif edgefilt is 'scharr': |
41
|
|
|
edgeim = scharr(meanim) |
42
|
|
|
elif edgefilt is 'roberts': |
43
|
|
|
edgeim = roberts(meanim) |
44
|
|
|
|
45
|
|
|
closeim = closing(edgeim, opense) |
|
|
|
|
46
|
|
|
openim = opening(closeim, opense) |
47
|
|
|
if fill_first: |
48
|
|
|
seed = np.copy(openim) |
49
|
|
|
seed[1:-1, 1:-1] = openim.max() |
50
|
|
|
mask = openim |
51
|
|
|
filledim = reconstruction(seed, mask, method='erosion') |
52
|
|
|
binarim = filledim > bi_thresh |
53
|
|
|
else: |
54
|
|
|
binarim = openim > bi_thresh*np.mean(openim) |
55
|
|
|
seed = np.copy(binarim) |
56
|
|
|
seed[1:-1, 1:-1] = binarim.max() |
57
|
|
|
mask = binarim |
58
|
|
|
filledim = reconstruction(seed, mask, method='erosion') |
59
|
|
|
|
60
|
|
|
tophim = filledim - closing(white_tophat(filledim, tophatse), opense)>0.01 |
61
|
|
|
|
62
|
|
|
fig, ax = plt.subplots(nrows=2, ncols=4, figsize=(16, 8)) |
63
|
|
|
ax[0][0].imshow(image, cmap='gray') |
64
|
|
|
ax[0][1].imshow(meanim, cmap='gray') |
65
|
|
|
ax[0][2].imshow(edgeim, cmap='gray', vmax=4*np.mean(edgeim)) |
66
|
|
|
ax[0][3].imshow(closeim, cmap='gray', vmax=4*np.mean(closeim)) |
67
|
|
|
ax[1][0].imshow(openim, cmap='gray', vmax=4*np.mean(openim)) |
68
|
|
|
ax[1][1].imshow(binarim, cmap='gray') |
69
|
|
|
ax[1][2].imshow(filledim, cmap='gray') |
70
|
|
|
ax[1][3].imshow(tophim, cmap='gray') |
71
|
|
|
for axes in ax: |
72
|
|
|
for axe in axes: |
73
|
|
|
axe.axis('off') |
74
|
|
|
fig.tight_layout() |
75
|
|
|
|
76
|
|
|
return tophim |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def BF_cell_features(prefix, folder, bucket='ccurtis.data'): |
80
|
|
|
|
81
|
|
|
ffilename = 'features_{}.csv'.format(prefix) |
82
|
|
|
mfilename = 'msd_{}.csv'.format(prefix) |
83
|
|
|
bffilename = 'BF_cells_{}.tif'.format(prefix) |
84
|
|
|
biim = 'bi_BF_cells_{}.tif'.format(prefix) |
85
|
|
|
bimages = 'biproc_BF_cells_{}.png'.format(prefix) |
86
|
|
|
|
87
|
|
|
aws.download_s3('{}/{}'.format(folder, ffilename), ffilename, bucket_name=bucket) |
88
|
|
|
aws.download_s3('{}/{}'.format(folder, mfilename), mfilename, bucket_name=bucket) |
89
|
|
|
aws.download_s3('{}/{}'.format(folder, bffilename), bffilename, bucket_name=bucket) |
90
|
|
|
print('Successfully downloaded files') |
91
|
|
|
|
92
|
|
|
fstats = pd.read_csv(ffilename, encoding = "ISO-8859-1") |
93
|
|
|
msds = pd.read_csv(mfilename, encoding = "ISO-8859-1") |
94
|
|
|
bfimage = plt.imread(bffilename) |
95
|
|
|
tophimage = binary_BF(bfimage, opense=disk(12), bi_thresh=1.2, tophatse=disk(20)) |
96
|
|
|
plt.savefig(bimages) |
97
|
|
|
euimage = EuclideanTransform(tophimage)+EuclideanTransform(~tophimage) |
98
|
|
|
print('Successfully performed image processing') |
99
|
|
|
|
100
|
|
|
xa = -np.reshape(np.clip((fstats.Y.values-1).astype(int), a_min=0, a_max=2043), newshape=(fstats.Y.shape[0], 1)) |
101
|
|
|
ya = np.reshape(np.clip((fstats.X.values-1).astype(int), a_min=0, a_max=2043), newshape=(fstats.X.shape[0], 1)) |
102
|
|
|
xya = [tuple(l) for l in np.concatenate((xa, ya), axis=1).tolist()] |
103
|
|
|
fstats['Cell Status'] = itemgetter(*xya)(tophimage) |
104
|
|
|
fstats['Cell Distance'] = itemgetter(*xya)(euimage) |
105
|
|
|
|
106
|
|
|
print('Successfully calculated Cell Status Params') |
107
|
|
|
|
108
|
|
|
frames = 651 |
109
|
|
|
xb = -np.reshape(np.clip((msds.Y.values-1).astype(int), a_min=0, a_max=2043), newshape=(int(msds.Y.shape[0]), 1)) |
110
|
|
|
yb = np.reshape(np.clip((msds.X.values-1).astype(int), a_min=0, a_max=2043), newshape=(int(msds.X.shape[0]), 1)) |
111
|
|
|
xyb = [tuple(l) for l in np.concatenate((xb, yb), axis=1).tolist()] |
112
|
|
|
msds['Cell Status'] = itemgetter(*xyb)(tophimage) |
113
|
|
|
msds['Cell Distance'] = itemgetter(*xyb)(euimage) |
114
|
|
|
|
115
|
|
|
msds_cell_status = np.reshape(msds['Cell Status'].values, newshape=(int(msds.X.shape[0]/frames), frames)) |
116
|
|
|
msds_cell_distance = np.reshape(msds['Cell Distance'].values, newshape=(int(msds.X.shape[0]/frames), frames)) |
117
|
|
|
fstats['Membrane Xing'] = np.sum(np.diff(msds_cell_status, axis=1) == True, axis=1) |
118
|
|
|
fstats['Distance Towards Cell'] = np.sum(np.diff(msds_cell_distance, axis=1), axis=1) |
119
|
|
|
fstats['Percent Towards Cell'] = np.mean(np.diff(msds_cell_distance, axis=1) > 0, axis=1) |
120
|
|
|
print('Successfully calculated Membrane Xing Params') |
121
|
|
|
|
122
|
|
|
fstats.to_csv(ffilename, sep=',', encoding = "ISO-8859-1") |
123
|
|
|
msds.to_csv(mfilename, sep=',', encoding = "ISO-8859-1") |
124
|
|
|
plt.imsave(biim, tophimage, cmap='gray') |
125
|
|
|
|
126
|
|
|
aws.upload_s3(ffilename, '{}/{}'.format(folder, ffilename), bucket_name=bucket) |
127
|
|
|
aws.upload_s3(mfilename, '{}/{}'.format(folder, mfilename), bucket_name=bucket) |
128
|
|
|
aws.upload_s3(biim, '{}/{}'.format(folder, biim), bucket_name=bucket) |
129
|
|
|
aws.upload_s3(bimages, '{}/{}'.format(folder, bimages, bucket_name=bucket)) |
130
|
|
|
print('Successfully uploaded files') |
131
|
|
|
|
132
|
|
|
return fstats |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
for prefix in to_track[int(sys.argv[1]):int(sys.argv[2])]: |
136
|
|
|
fstats = BF_cell_features(prefix, remote_folder, bucket=bucket) |
137
|
|
|
print('Successfully output cell features for {}'.format(prefix)) |
138
|
|
|
|
139
|
|
|
|