forked from Kafri-Lab/Cell-t-SNE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·224 lines (189 loc) · 8.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
import os
import numpy as np
import pylab
import mahotas as mh
import glob
import watershed # label image by calling watershed.py
import utils # crop cell by calling utils.py
import plot
# import tsne1
from PIL import Image
import skimage
import skimage.io
import scipy
import pandas as pd
import click
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches
from IPython import embed
if os.name != 'nt':
from tsne import bh_sne
def everything():
'''
# crop and label images
for filename in list(glob.glob('*.tif')):
img = skimage.io.imread(filename) # read and save image as an array (img.shape is (1024, 1360))
y_value = watershed.label(img) # watershed images; y_value stores the labels
utils.crop (img, y_value) #crop image
'''
#put together a list of x value for tsne to process
filenames=list(glob.glob('*.jpg'))
x_value = np.zeros((4900, len(filenames))) # Dimension of the image: 70*70=4900; x_value will store images in 2d array
for imageName in filenames :
count = 0
image1d = scipy.misc.imresize(skimage.io.imread(imageName), (70,70)) #reshape size to 70,70 for every image
image1d = image1d.flatten() #image1d stores a 1d array for each image
x_value[:,count] = image1d # add a row of values
count += 1
tsne1.tSNE(x_value) #plot data using tsne
@click.command()
@click.option('--csv', help='The csv file that contains single cell data.', required=True)
@click.option('--channel', help='The image channel to crop.', default=1)
@click.option('--resize', help='Size of the image', default=False)
@click.option('--square/--rectangle', help='Crop the cell in into a square box rather than a rectangle.', default=False)
def crop_images(csv,channel,resize,square):
#./main.py crop_images --csv ResultTable\ -\ 2\ wells\,\ 1\ fields\,\ thresh\ 160_with_Traces_full_curated.csv
#./main.py crop_images --csv ResultTable\ -\ 2\ wells\,\ 1\ fields\,\ thresh\ 160_with_Traces_full_curated.csv --resize 200
df = utils.read_csv(csv)
print('Found number of cells: %s' % df.shape[0])
#image_dir = './images/Ron/'
image_dir = './images/LFS images/'
# Loop by image
for image_filename in df.FileName.unique():
cells_in_img = df.loc[df['FileName'] == image_filename]
cell_ids = []
# Set the channel number in the image filename to load
s = list(image_filename)
s[15]=str(channel)
ch_image_filename = "".join(s)
# Load image
image = skimage.io.imread(image_dir + ch_image_filename)
# Build labelled image
labelled = np.zeros(image.shape)
count = 1
for row_index, row in cells_in_img.iterrows():
cyto_px_ids = map(int, row.cyto_boundaries.split()) # get locations of this cell's boundries as a list of ints
labelled[np.unravel_index(cyto_px_ids, labelled.shape, order='F')] = count # set this cell in the labelled image
cell_ids.append(row.CellID)
count+=1
save_location = './images/cropped_images/ch%s-' % channel
utils.crop_and_save(image, labelled, save_location, filenames=cell_ids, square=square, resize=resize)
print('Saved number of cropped cells: %s' % df.shape[0])
@click.command()
@click.option('--csv', help='The csv file that contains single cell data.', required=True)
@click.option('--color-by', help='The measurement name to color the boxes by.', default='Trace')
@click.option('-x', help='The measurement name on the X axis.', required=True)
@click.option('-y', help='The measurement name on the Y axis.', required=True)
@click.option('--dpi', help='The resolution to save the output image.', default=200)
@click.option('--channel', help='The image channel to display.', default=1)
def image_scatter(csv,color_by,x,y,dpi,channel):
#./main.py image_scatter --csv ResultTable\ -\ 2\ wells\,\ 1\ fields\,\ thresh\ 160_with_Traces_full_curated.csv -x tsne1 -y tsne2 --color-by CellLine --dpi 650
df = utils.read_csv(csv)
print('Found number of cells: %s' % df.shape[0])
#image_dir = './images/cropped_images/Ron/'
image_dir = './images/cropped_images/'
cell_imgs = []
colors = []
xx = np.array([])
yy = np.array([])
cmap='gist_rainbow'
color_list = utils.get_colors(len(np.unique(df[color_by])),cmap=cmap)
# blue is better when there are only 3 colours
if len(color_list) == 3 and cmap == 'gist_rainbow':
color_list[2] = (0.05, 0.529, 1, 1.0)
for row_id, row in df.iterrows():
cell_id = row.CellID
image_filename = image_dir + 'ch' + str(channel) + '-' + cell_id + '.jpg'
if not os.path.exists(image_filename):
print('[WARN] no image found %s'% image_filename)
continue
print('Loading image %s' % image_filename)
image = skimage.io.imread(image_filename)
image = utils.gray_to_color(image)
cell_imgs.append(image)
xx = np.append(xx,row[x])
yy = np.append(yy,row[y])
color_id = np.where(np.unique(df[color_by])==row[color_by])[0][0] # find position where this value appears
c = color_list[color_id]
c = (int(c[0]*255),int(c[1]*255),int(c[2]*255)) # convert value range, ex. 1.0 -> 255 or 0.0 -> 0
colors.append(c)
if len(cell_imgs)==0:
print('[ERROR] 0 cropped single cell images found.')
return
canvas = plot.image_scatter(yy, xx, cell_imgs, colors, min_canvas_size=4000)
plt.imshow(canvas,origin='lower')
plt.title('%s vs %s' % (x,y))
plt.xlabel('%s' % x)
plt.ylabel('%s' % y)
plt.xticks([])
plt.yticks([])
patches=[]
for i in range(len(np.unique(df[color_by]))):
label = '%s %s' % (color_by, np.unique(df[color_by])[i])
if color_by == 'Dend.cat':
label = 'Detected Category %s' % (i+1)
# Plot additional data that can't be in the main csv
# extra_datafile = 'PCaxes.csv'
# if os.path.exists(extra_datafile):
# df_extra = utils.read_csv(extra_datafile)
# from IPython import embed
# embed() # drop into an IPython session
# plt.scatter(avg_x, avg_y,c=color,marker='*')
patch = mpatches.Patch(color=color_list[i], label=label)
patches.append(patch)
plt.legend(handles=patches,bbox_to_anchor=(1.04,0.5), loc="center left", borderaxespad=0, frameon=False)
save_location = './images/%s_image_scatter_by_%s_dpi%s.jpg' % (csv, color_by, dpi)
plt.savefig(save_location,dpi=dpi,pad_inches=1,bbox_inches='tight')
# plt.show()
print('Saved image scatter to %s' % save_location)
@click.command()
@click.option('--csv', help='The csv file that contains single cell data.', required=True)
@click.option('--res', help='The resolution of image.', default=70)
@click.option('--perplexity', help='The perplexcity of tsne plot.', default=5)
def tsne_images(csv,res,perplexity):
image_dir = './images/cropped_images/'
filenames=list(glob.glob(image_dir+'*.jpg'))
'''
x_value = np.zeros((4900, len(filenames))) # Dimension of the image: 70*70=4900; x_value will store images in 2d array
for imageName in filenames:
count = 0
image1d = scipy.misc.imresize(skimage.io.imread(imageName), (70,70)) #reshape size to 70,70 for every image
image1d = image1d.flatten() #image1d stores a 1d array for each image
x_value[:,count] = image1d # add a row of values
count += 1
'''
total_res = res**2
x_value = np.zeros((len(filenames),total_res)) # Dimension of the image: 70*70=4900; x_value will store images in 2d array
print filenames
count = 0
for imageName in filenames:
image1d = scipy.misc.imresize(skimage.io.imread(imageName), (res,res)) #reshape size to 70,70 for every image
image1d = image1d.flatten() #image1d stores a 1d array for each image
x_value[count,:] = image1d # add a row of values
#embed()
count += 1
if count>50:
break
print x_value.shape
vis_data = bh_sne(x_value,perplexity=perplexity)# tsne embedding
print vis_data.shape
vis_x = vis_data[:, 0]
vis_y = vis_data[:, 1]
df = utils.read_csv(csv)
print df.shape
df['tsne1']=pd.Series (vis_x)
df['tsne2']=pd.Series (vis_y)
df.to_csv(csv)
# Setup group of command line commands
@click.group()
def cli():
pass
cli.add_command(crop_images)
cli.add_command(image_scatter)
cli.add_command(tsne_images)
if __name__ == '__main__':
cli() # make command line commands available
# everything()
# import pylab
# pylab.imshow(labelled);pylab.show()