-
Notifications
You must be signed in to change notification settings - Fork 0
/
accumulation_analysis_2D_140520.py
293 lines (168 loc) · 6.56 KB
/
accumulation_analysis_2D_140520.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 30 16:15:49 2020
@author: feldaher
"""
# Aim: extract x,y,z init and final for each track from MTrackJ files
# input: track and points csv files
import os
import itertools
import pandas as pd
#import numpy as np
import matplotlib.pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import shapely.geometry as geom
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from sympy import Point, Line
import numpy as np
import mpl_scatter_density
import math
x_ref=59
y_ref=204
ImageDim_y=224.5
ImageDim_x=219
intertectal_angle=-3
'''
@@@@@@@@@@@@@@@@@ Preprocess MTrackJ exported files add apply transformations
'''
# Read track.csv files
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filenamePts= askopenfilename() # show an "Open" dialog box and return the path to the selected file
directory = os.path.split(filenamePts)[0]
#read skin outline coordinates from ImageJ manual freehand ROI
microglia_coords = np.loadtxt(filenamePts)
x=microglia_coords[:,0]
y=microglia_coords[:,1]
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filenameSkin= askopenfilename() # show an "Open" dialog box and return the path to the selected file
#read skin outline coordinates from ImageJ manual freehand ROI
skin_coords = np.loadtxt(filenameSkin)
# Flip vertical
y=ImageDim_y-y
y_ref=ImageDim_y-y_ref
skin_coords[:,1]=ImageDim_y-skin_coords[:,1]
#intertectal_angle=-intertectal_angle
#Rotate to orientate the frame of reference verticaly on LSM880 images
x0=ImageDim_x/2
y0=ImageDim_y/2
x1=(y-y0)+x0
y1=-(x-x0)+y0
x_ref2=(y_ref-y0)+x0
y_ref2=-(x_ref-x0)+y0
#Rotate skin outline
new_skin_coords=skin_coords.copy()
new_skin_coords[:,0]=(skin_coords[:,1]-y0)+x0
new_skin_coords[:,1]=-(skin_coords[:,0]-x0)+y0
#intertectal_angle=intertectal_angle-90
#Flip vertical
y1=ImageDim_x-y1
y_ref2=ImageDim_x-y_ref2
new_skin_coords[:,1]=ImageDim_x-new_skin_coords[:,1]
#intertectal_angle=-intertectal_angle
# y_ref2=ImageDim_x-y_ref2
# new_skin_coords[:,1]=ImageDim_x-new_skin_coords[:,1]
# Convert into radians
intertectal_angle=math.radians(-intertectal_angle)
truncate_filename=os.path.splitext(os.path.basename(filenamePts))
'''
@@@@@@@@@@@@@@@@@ Compute CoG and graph intersection points + CoG
'''
cgx = np.sum(x1)/len(x1)
cgy = np.sum(y1)/len(y1)
#Save the coordinates of the center of gravity
d = {'xCoG': [cgx], 'yCoG': [cgy]}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'_CoG.csv', index = False, header=True)
#Display scatter plot and save it as SVG file
plt.scatter(x1,y1);
plt.scatter(cgx, cgy, color='k', marker='+', s=1e4);
plt.savefig(directory+'/'+truncate_filename[0]+'_accumulation_pts.svg', format='svg', dpi=600)
plt.show()
'''
@@@@@@@@@@@@@@@@@ Compute distance and angle CoG - skin intercept
'''
#print(new_skin_coords[:,0])
line = geom.LineString(new_skin_coords)
point_CoG = geom.Point(cgx, cgy)
# Note that "line.distance(point_CoG)" would be identical
distance=point_CoG.distance(line)
point_on_line = line.interpolate(line.project(point_CoG))
x_skin=point_on_line.x
y_skin=point_on_line.y
cgx=float(cgx)
cgy=float(cgy)
# !! angle inverted on purpose angle = - arctan
angle=np.degrees(np.arctan2((cgy-point_on_line.y),(cgx-point_on_line.x)))
d = {'x_skin': [x_skin], 'y_skin': [y_skin],'distance_CoG_skin': [distance],'angle':[angle]}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'_CoG_analysis.csv', index = False, header=True)
'''
@@@@@@@@@@@@@@@@@ Transform coordinates of CoG to be at the same origin (intertecal junction caudal point), with the skin perpendicular to the x axis
'''
# Translation and rotation
cgx2=(cgx-x_ref2)*math.cos(intertectal_angle)-(cgy-y_ref2)*math.sin(intertectal_angle)
cgy2=(cgx-x_ref2)*math.sin(intertectal_angle)+(cgy-y_ref2)*math.cos(intertectal_angle)
# Convert to polar coordinates
r_cog=math.sqrt(cgx2**2+cgy2**2)
theta_cog=np.degrees(np.arctan2(cgy2,cgx2))
# Save the coordinates of the tranformed center of gravity
d = {'xCoG2': [cgx2], 'yCoG2': [cgy2]}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'_CoG2.csv', index = False, header=True)
d = {'r_cog': [r_cog], 'theta_cog': [theta_cog]}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'__CoG2_polar.csv', index = False, header=True)
'''
@@@@@@@@@@@@@@@@@ Transform coordinates of accumulation points
'''
x2=(x1-x_ref2)*math.cos(intertectal_angle)-(y1-y_ref2)*math.sin(intertectal_angle)
y2=(x1-x_ref2)*math.sin(intertectal_angle)+(y1-y_ref2)*math.cos(intertectal_angle)
# Convert to polar coordinates
r=x2**2+y2**2
r=np.sqrt(r)
x2=x2.astype(float)
y2=y2.astype(float)
theta =np.degrees(np.arctan2(y2,x2))
# save data to file
d = {'x_inter2': x2, 'y_inter2': y2}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'_accumulation_transf.csv', index = False, header=True)
d = {'r': r, 'theta': theta}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'_accumulation_transf_polar.csv', index = False, header=True)
#Display scatter plot and save it as SVG file
plt.scatter(x2,y2);
plt.scatter(cgx, cgy, color='k', marker='+', s=1e4);
plt.savefig(directory+'/'+truncate_filename[0]+'_accumulation_pts_rot.svg', format='svg', dpi=600)
plt.show()
'''
@@@@@@@@@@@@@@@@@ Scaling to take into account interindividual variation in tectum size
'''
# measure distance new origin to skin intersection pt
point_Ref = geom.Point(x_ref2, y_ref2)
distanceRef=point_Ref.distance(line)
print(distanceRef)
# scale using the distance origin pt-skin intersection pt
x2=x2/distanceRef
y2=y2/distanceRef
d = {'x_inter2': x2, 'y_inter2': y2}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'_accumulation_transf_Scaled.csv', index = False, header=True)
r=x2**2+y2**2
r=np.sqrt(r)
d = {'r': r, 'theta': theta}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'_accumulation_transf_polar_scaled.csv', index = False, header=True)
# Scale center of gravity
cgx2=cgx2/distanceRef
cgy2=cgy2/distanceRef
# Save the coordinates of the tranformed center of gravity
d = {'xCoG2': [cgx2], 'yCoG2': [cgy2]}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'_CoG2_scaled.csv', index = False, header=True)
r_cog=math.sqrt(cgx2**2+cgy2**2)
d = {'r_cog': [r_cog], 'theta_cog': [theta_cog]}
df = pd.DataFrame(data=d)
df.to_csv(directory+'/'+truncate_filename[0]+'__CoG2_polar_scaled.csv', index = False, header=True)