/
get_well_data.py
146 lines (109 loc) · 5.54 KB
/
get_well_data.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
"""
This is the main scrunching tracking script
It assumes that the raw data has been preprocessed: individuals wells cropped
and the images of individual wells were saved in the corresponding folders
Output: the following data is generated for each of the respective wells
1) MAL (txt), 2) MAL vs time plot, 3) COM (txt), Aspect Ratio (txt)
4) AVI movies showing the tracking results
"""
import numpy as np
from numpy import asarray
from numpy import savetxt
import visualize_results
#matplotlib.use('Qt5Agg') # Apple doesn't like Tkinter (TkAgg backend) so I needed to change the backend to 'Qt5Agg'
from matplotlib import pyplot as plt
import cv2 as cv
import os
from os.path import exists
from os import makedirs
import scrunching_track
### CHANGE THIS TO YOUR PLATE FOLDER PATH
# Note: the path should NOT have a slash at the end
plateFolder = "/Users/arina/Desktop/Collins/2021_08_12 Arina 3 chem scrunching/18"
outputPath = plateFolder + "/results"
wellDataFolder = outputPath + '/well_data'
wells = list(np.arange(1, 49, 1))
wells = [1]
start_frame=1
end_frame=1500
centermost_arr, mals_arr, coms_arr, asp_ratios_arr = [], [], [], []
for ind in wells:
curr_centermost_arr, curr_mal_arr, curr_com_arr, curr_asp_ratio_arr = \
scrunching_track.analyze(wellNum=ind, plateFolder=plateFolder, start_frame=1, end_frame=1500)
centermost_arr.append(curr_centermost_arr)
mals_arr.append(curr_mal_arr)
coms_arr.append(curr_com_arr)
asp_ratios_arr.append(curr_asp_ratio_arr)
if exists(outputPath) is False:
makedirs(outputPath)
wellDataFolder = outputPath + '/well_data'
if exists(wellDataFolder) is False:
makedirs(wellDataFolder)
wellVidsFolder = outputPath + '/well_vids'
if exists(wellVidsFolder) is False:
makedirs(wellVidsFolder)
# this creates a movie visualizing the results of worm trackig
# the `centermost_arr` is a binary mask showing the outline of the worm
visualize_results.displayVideo(filtered_imgs=np.array(curr_centermost_arr),outpath=wellVidsFolder + "/" + "binary_well" + str(ind) + '.avi')
visualize_results.plotMAL(curr_mal_arr, MAL=True, title=("well" + str(ind)),
outpath=(wellDataFolder + "/MAL plot" + str(ind)), show=False)
# visualize_results.displayOrigVideo(start_frame=start_frame, last_frame=end_frame, filepath=plateFolder, wellNum=wellNum, outpath=wellVidsFolder + "/" + "orig_well" + str(wellNum) + '.avi')
data = asarray(curr_mal_arr)
path = os.path.expanduser(wellDataFolder + '/MAL_well' + str(ind) + '.csv')
savetxt(path, data, delimiter=',', fmt='%1.3f')
x = [tup[0] for tup in curr_com_arr] # reshaping the array to store x and y coordinates conveniently in a .csv file
y = [tup[1] for tup in curr_com_arr]
xy = np.stack((x, y))
xy = np.transpose(xy)
path = os.path.expanduser(wellDataFolder + '/COM_well'+ str(ind)+'.csv')
savetxt(path, xy, delimiter=',', fmt='%1.3f')
filename = os.path.expanduser(wellDataFolder + "/centermost_well" + str(ind) + ".csv")
curr_centermost_arr = np.array(curr_centermost_arr)
arrReshaped = curr_centermost_arr.reshape(curr_centermost_arr.shape[0], -1)
# saving reshaped array (binary mask) to a txt file
# this information can be used for calculating the worm's area, etc
np.savetxt(filename, arrReshaped)
data = asarray(curr_asp_ratio_arr)
path = os.path.expanduser(wellDataFolder + '/AspRatio_well' + str(ind) + '.csv')
savetxt(path, data, delimiter=',', fmt='%1.3f')
#data = asarray(tracked_areas)
#path = os.path.expanduser('Areas well'+ str(wellNum)+'.csv')
#savetxt(path, data, delimiter=',', fmt='%1.3f')
"""
centermost_arr = []
wellVidsFolder = outputPath + '/well_vids'
for ind in wells:
curr_centermost_arr, _, _, curr_asp_ratio_arr = \
scrunching_track.analyze(wellNum=ind, plateFolder=plateFolder, start_frame=1, end_frame=1500)
#visualize_results.displayVideo(filtered_imgs=curr_centermost_arr,
# outpath=wellVidsFolder + "/" + "binary_well" + str(ind) + '.avi')
#data = asarray(curr_centermost_arr)
#path = os.path.expanduser(wellDataFolder + '/centermost_well' + str(ind) + '.csv')
#savetxt(path, data)
filename = os.path.expanduser(wellDataFolder + "/centermost_well" + str(ind) + ".csv")
curr_centermost_arr = np.array(curr_centermost_arr)
arrReshaped = curr_centermost_arr.reshape(curr_centermost_arr.shape[0], -1)
# saving reshaped array to file.
np.savetxt(filename, arrReshaped)
data = asarray(curr_asp_ratio_arr)
path = os.path.expanduser(wellDataFolder + '/AspRatio_well' + str(ind) + '.csv')
savetxt(path, data, delimiter=',', fmt='%1.3f')
#loadedArr = np.loadtxt(filename)
# loadedOriginal = loadedArr.reshape(loadedArr.shape[0], loadedArr.shape[1] // arr.shape[2], arr.shape[2])
asp_ratio_arr = []
for centermost in curr_centermost_arr:
if sum(sum(centermost)) != 0:
contours, _ = cv.findContours(centermost, 1, 2)
cnt = contours[0]
#print(cnt)
(x, y), (width, height), angle = cv.minAreaRect(cnt)
aspect_ratio = min(width, height) / max(width, height)
if aspect_ratio<1:
aspect_ratio= 1/aspect_ratio
asp_ratio_arr.append(aspect_ratio)
else:
asp_ratio_arr.append(np.nan)
data = asarray(asp_ratio_arr)
path = os.path.expanduser(wellDataFolder + '/AspRatio_well' + str(ind) + '.csv')
savetxt(path, data, delimiter=',', fmt='%1.3f')
"""