-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.py
298 lines (183 loc) · 6.48 KB
/
image.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
291
292
293
294
295
296
297
298
from include import *
from utils import *
def get_image(self, plot_option, refined_field, width, length_unit, npixels, args):
# Extract parameters
if args:
zoom = args[0]
move = args[1]
rotate = args[2]
else:
zoom = 0
move = 0
rotate = 0
# Initialize some additional variables
center_mode = 0
npoints = npixels**2
if refined_field == 'gamma':
log_plot = 0
else:
log_plot = 1
# Get particle positions
flag, pos = self.get_refined_field('pos')
if flag:
return
flag, vel = self.get_refined_field('vel')
if flag:
return
flag, mass = self.get_refined_field('mass')
if flag:
return
pos, vel = self.center_and_rotate(pos, vel, mass)
# Do this the first time the plot is created
if self.flag_plot_first:
# Center box
flag, nh = self.get_refined_field('nh')
if flag:
return
flag = self.get_center(0)
if flag:
return
self.norm_center = np.zeros(3)
# Get normalized positions
self.norm_pos = np.zeros([int(pos.size / 3), 3])
for i in np.arange(3):
self.norm_pos[:, i] = (pos[:, i] - self.center[i]) / self.header.boxsize_cgs
# Create tree
if plot_option == 'Slice':
self.tree = spatial.cKDTree(self.norm_pos)
# Initialize rotation angles
self.rot_alpha = 0.
self.rot_beta = 0.
# It is no longer the first time the plot is created
self.flag_plot_first = 0
# Do zoom
if zoom:
if zoom == 1:
width *= self.zoom_fac
elif zoom == 2:
width /= self.zoom_fac
# Determine normalized width
norm_width = width * self.length_units[length_unit] / self.header.boxsize_cgs
# Do move
if move:
delta = self.move_fac * norm_width
if move == 1:
self.norm_center[0] -= delta
elif move == 2:
self.norm_center[0] += delta
elif move == 3:
self.norm_center[1] -= delta
elif move == 4:
self.norm_center[1] += delta
# Get rotation angles
if rotate:
if rotate == 1:
self.rot_beta += self.rot_delta
elif rotate == 2:
self.rot_beta -= self.rot_delta
elif rotate == 3:
self.rot_alpha += self.rot_delta
elif rotate == 4:
self.rot_alpha -= self.rot_delta
# Do plot
if plot_option == 'Slice':
# Determine list of evenly spaced points
x1 = -norm_width / 2. + self.norm_center[0]
x2 = norm_width / 2. + self.norm_center[0]
y1 = -norm_width / 2. + self.norm_center[1]
y2 = norm_width / 2. + self.norm_center[1]
px = np.linspace(x1, x2, npixels)
py = np.linspace(y1, y2, npixels)
xv, yv = np.meshgrid(px, py, indexing='ij')
xv = xv.ravel()
yv = yv.ravel()
points = np.empty([npoints, 3])
points[:, 0] = xv
points[:, 1] = yv
points[:, 2] = 0.
# Do rotation
points = do_rotation(points, self.rot_alpha, self.rot_beta, self.norm_center)
# Walk tree
dist, idx = self.tree.query(points)
# Select nearest neigbours in target field
flag, vals = self.get_refined_field(refined_field)
if flag:
return
vals = vals[idx]
if log_plot:
vals = np.log10(vals)
elif plot_option == 'Projection':
# Get index list for particles within r = 2 * sub box (due to rotation)
rel_pos = np.zeros([int(self.norm_pos.size / 3), 3])
for i in np.arange(3):
rel_pos[:, i] = (self.norm_pos[:, i] - self.norm_center[i]) / norm_width
idx_list = []
for i in np.arange(3):
idx_list.append(np.abs(rel_pos[:, i]) < 1.)
idx = np.logical_and(idx_list[0], idx_list[1])
idx = np.logical_and(idx, idx_list[2])
# Get positions of sub box
sub_pos = np.zeros([rel_pos[idx, 0].size, 3])
for i in np.arange(3):
sub_pos[:, i] = rel_pos[idx, i]
# Do rotation
rot_pos = do_rotation(sub_pos, self.rot_alpha, self.rot_beta, np.zeros(3))
# Get fields required for projection
x = np.array(rot_pos[:, 0])
y = np.array(rot_pos[:, 1])
mass = mass[idx]
flag, rho = self.get_refined_field('rho')
if flag:
return
rho = rho[idx]
flag, ref = self.get_refined_field(refined_field)
if flag:
return
ref = ref[idx]
if log_plot:
ref = np.log10(ref)
hsml = (3. / 4. / np.pi * mass / rho)**( 1. / 3.)
hsml /= (self.header.boxsize_cgs * norm_width)
# Initialize result arrays
vals = np.zeros(npoints, dtype = np.dtype('float64'))
sums = np.zeros(npoints, dtype = np.dtype('float64'))
# Load projection library
arr = npct.ndpointer(dtype = np.dtype('float64'), ndim = 1, flags = 'CONTIGUOUS')
lib = npct.load_library("projection", ".")
lib.restype = None
lib.projection.argtypes = [arr, arr, arr, arr, arr, c_int, arr, arr, c_int]
# Do projection
lib.projection(x, y, rho, hsml, ref, x.size, vals, sums, npixels)
# Get valid values
idx = sums > 0.
vals[idx] /= sums[idx]
idx = sums == 0.
vals[idx] = np.nan
# Reshape
vals = np.reshape(vals, (npixels, npixels))
vals = np.swapaxes(vals, 0, 1)
# Draw image
bottom = 0.05
top = 0.05
frac_cbar = 0.03
frac = 1. - bottom - top - frac_cbar
left = (1. - frac) / 2.
cmap = get_color_map(refined_field)
ax = self.fig.add_axes([left, bottom, frac, frac])
ax.set_xticks([])
ax.set_yticks([])
im = ax.imshow(vals, cmap = cmap)
# Add labels
offset_x = 0.01
offset_y = 0.05
self.fig.text(left + frac + offset_x, bottom + frac + offset_y, get_label(refined_field), rotation = 270)
offset = 0.02
text = 'Width: ' + str(width) + ' ' + length_unit
self.fig.text(0.5, offset, text, horizontalalignment = 'center')
# Add color bar
cax = self.fig.add_axes([left, bottom + frac, frac, frac_cbar])
self.fig.colorbar(im, cax = cax, orientation = 'horizontal')
cax.xaxis.set_ticks_position('top')
cax.yaxis.set_label_position('right')
# Return width (used for zooming)
return width