-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphometrics.py
executable file
·235 lines (188 loc) · 7.75 KB
/
morphometrics.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
225
226
227
228
229
230
231
232
233
import os
import glob
import pandas as pd
from pathlib import Path
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
def plot_parameter_histogram(folder_path, par, ax):
# Get list of all CSV files in the given directory
if folder_path.endswith('\\'):
csv_files = glob.glob(folder_path + "**\*.csv")
else:
csv_files = glob.glob(folder_path + "**\*.csv")
# Create an empty DataFrame
data = pd.DataFrame()
# Read each csv file and concatenate into the data DataFrame
for file in csv_files:
df = pd.read_csv(file)
data = pd.concat([data, df])
# clean data
data = data[data['minor_ax'] != 0] # this shouldn't exist
data = data[data['area'] >= 80] # these cells are suspiciously small
data = data[data['area'] <= 600] # these cells are suspiciously big
# create aspect ratio
data['aspect_ratio'] = data['major_ax'] / data['minor_ax']
# Check if the parameter exists in the DataFrame columns
if par not in data.columns:
print(f"Parameter {par} not found in the data.")
return
# Plot histogram
ax.hist(data[par], bins=30, edgecolor='black')
ax.set_xlabel(par)
ax.set_ylabel('Frequency')
# ax.set_yscale('log')
def overlay_parameter_on_image(csv_file, par, ax):
# Read the data from the CSV file
df = pd.read_csv(csv_file)
# Clean data
df = df[df['minor_ax'] != 0] # this shouldn't exist
df = df[df['area'] >= 80] # these cells are suspiciously small
df = df[df['area'] <= 600] # these cells are suspiciously big
# Create aspect ratio
df['aspect_ratio'] = df['major_ax'] / df['minor_ax']
# Check if the parameter exists in the DataFrame columns
if par not in df.columns:
print(f"Parameter {par} not found in the data.")
return
# Construct image file path
image_file = csv_file.replace("_2Dresults.csv", "")
# Load the image
image = mpimg.imread(image_file)
im_gray = image.mean(axis=2)
# Show the image
ax.imshow(im_gray, cmap=plt.cm.gray)
# Create scatter plot overlay
magic_number = 0.85
s = df[par].to_numpy()
s = s / s.max()
ind = np.argwhere(s > magic_number).squeeze()
scatter = ax.scatter(df['centroid_y'].to_numpy()[ind], df['centroid_x'].to_numpy()[ind], s=s[ind], c='magenta', alpha=0.5)
ind = np.argwhere(s <= magic_number).squeeze()
scatter = ax.scatter(df['centroid_y'].to_numpy()[ind], df['centroid_x'].to_numpy()[ind], s=s[ind], c='cyan', alpha=0.5)
# Set the title of the plot to the image file name
ax.set_title(image_file)
# Return the scatter plot (in case further customization is needed)
return scatter
def plot_2d_histogram(csv_file, par1, par2, ax):
# Read the data from the CSV file
df = pd.read_csv(csv_file)
# Clean data
df = df[df['minor_ax'] != 0] # this shouldn't exist
df = df[df['area'] >= 80] # these cells are suspiciously small
df = df[df['area'] <= 600] # these cells are suspiciously big
# Create aspect ratio
df['aspect_ratio'] = df['major_ax'] / df['minor_ax']
# Check if the parameters exist in the DataFrame columns
if par1 not in df.columns or par2 not in df.columns:
print(f"One of the parameters {par1} or {par2} not found in the data.")
return
# Plot 2D histogram
hb = ax.hist2d(df[par1], df[par2], bins=20, cmap='hot')
ax.set_xlabel(par1)
ax.set_ylabel(par2)
# Colorbar
cb = plt.colorbar(hb[3], ax=ax)
cb.set_label('counts in bin')
def create_scatter_plots(folder_path, par_1, par_2, ax):
# Get list of all CSV files in the given directory
if folder_path.endswith('\\'):
csv_files = glob.glob(folder_path + "**\*.csv")
else:
csv_files = glob.glob(folder_path + "**\*.csv")
# Create an empty DataFrame
data = pd.DataFrame()
# Read each csv file and concatenate into the data DataFrame
for file in csv_files:
df = pd.read_csv(file)
data = pd.concat([data, df])
# clean data
data = data[data['minor_ax'] != 0] # this shouldn't exist
data = data[data['area'] >= 80] # these cells are suspiciously small
data = data[data['area'] <= 600] # these cells are suspiciously big
# create aspect ratio
data['aspect_ratio'] = data['major_ax'] / data['minor_ax']
# Check if the parameter exists in the DataFrame columns
if par_1 not in data.columns:
print(f"Parameter {par} not found in the data.")
return
# Check if the parameter exists in the DataFrame columns
if par_2 not in data.columns:
print(f"Parameter {par} not found in the data.")
return
# Plot histogram
ax.scatter(data[par_1], data[par_2])
ax.set_xlabel(par_1)
ax.set_ylabel(par_2)
plt.show()
# ax.set_yscale('log')
if __name__ == '__main__':
parameters = ['area', 'perimeter', 'major_ax', 'minor_ax', 'aspect_ratio', 'eccentricity', 'convexity', 'dist_from_edge']
# ### histograms of pars
#
# number_of_folders = 2
#
# fig, ax = plt.subplots(len(parameters), number_of_folders, figsize=(10, 20), dpi=200)
#
# for n, parameter in enumerate(parameters):
#
# path = '/HERE UPDATE ME'
# if n == 0:
# ax[n, 0].set_title(f"{Path(path).name}")
# plot_parameter_histogram(path, parameter, ax[n, 0]) # <--- hisutogram plots made here for path=='path'
#
# path = 'HERE UPDATE ME'
# if n == 0:
# ax[n, 1].set_title(f"{Path(path).name}")
# plot_parameter_histogram(path, parameter, ax[n, 1])
#
# # copy me for # of folders
# # path = 'HERE'
# # if n == 0:
# # ax[n, 1].set_title(f"{Path(path).name}")
# # plot_parameter_histogram(path, parameter, ax[n, 1])
#
# plt.savefig('histograms.png')
# plt.show()
# ### image of pars
# folder_path = '/Users/peternewman/Drive/Python/plot/BL morphometrics/timepoint a'
# csv_files = glob.glob(folder_path + "/*.csv")
#
# sqrt_len = np.ceil(len(csv_files) ** 0.5).astype(int)
#
# for par in parameters:
# fig, ax = plt.subplots(sqrt_len, sqrt_len, figsize=(20, 20), dpi=80)
#
# for n, file in enumerate(csv_files):
# n, m = np.unravel_index(n, ax.shape)
# overlay_parameter_on_image(file, par, ax[n, m]) # <--------- image over plots made here
#
# plt.savefig(f'{folder_path[-1]}_scatter_overlaid_{par}.png')
# plt.show()
# ### image of pars
# folder_path = '/Users/peternewman/Drive/Python/plot/BL morphometrics/timepoint b'
# csv_files = glob.glob(folder_path + "/*.csv")
#
# sqrt_len = np.ceil(len(csv_files) ** 0.5).astype(int)
#
# for par in parameters:
# fig, ax = plt.subplots(sqrt_len, sqrt_len, figsize=(20, 20), dpi=80)
#
# for n, file in enumerate(csv_files):
# n, m = np.unravel_index(n, ax.shape)
# overlay_parameter_on_image(file, par, ax[n, m])
#
# plt.savefig(f'{folder_path[-1]}_scatter_overlaid_{par}.png')
# plt.show()
# ## 2D histograms
# csv_file = '/Users/peternewman/Drive/Python/plot/BL morphometrics/timepoint a/20x FGF8 RA- 1_Processed001.tif_2Dresults.csv'
# fig, ax = plt.subplots(figsize=(10, 8), dpi=200)
# plot_2d_histogram(csv_file, 'major_ax', 'eccentricity', ax)
# plt.savefig('2D_histogram.png')
# plt.show()
### scatter plots
folder_path = r"D:\Ben\Fluorescent Images\cellpose_wrapper\resources\im"
par_1 = 'dist_from_edge' # Replace 'column_name_x' with the actual column name for x-axis data
par_2 = 'eccentricity' # Replace 'column_name_y' with the actual column name for y-axis data
fig, ax = plt.subplots(figsize=(10, 8), dpi=200)
create_scatter_plots(folder_path, par_1, par_2, ax)