-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinning.py
362 lines (288 loc) · 11.4 KB
/
binning.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Import necessary libraries
from datetime import datetime
startTime = datetime.now()
import tracemalloc
#from scippnexus import data
#import scippnexus as snx
import numpy as np
import math
#qimport mantid_args
#from scippnexus import NXdetector
from scippneutron.conversion import graph
import scipp as sc
import scipp.binning
import scippneutron as scn
import scippnexus as snx
#%matplotlib widget
import matplotlib.pyplot as plt
import ipywidgets as ipw
from matplotlib.colors import LogNorm
#import multiprocessing as mp
import argparse, sys
import h5py
# h5py.enable_ipython_completer()
# starting the momory monitoring
tracemalloc.start()
# Define command-line arguments
parser=argparse.ArgumentParser()
#parser.add_argument('--res', help='input res file',required=True)
parser.add_argument('--nex', help='input nexus file',required=True)
parser.add_argument('--bins', help='number of time binns (must be integer)',required=False,default=100)
parser.add_argument('--maxprop', help='nomralisation to maximum probility',required=False,default=1)
# Retrieve command-line arguments
args=parser.parse_args()
if args.nex != None:
filename=args.nex
print("the used file is: %s" % filename)
if args.bins != None:
t_step=int(args.bins)
print("number of time bins:", t_step)
max_prop = 1
if args.maxprop != None:
max_prop=int(args.maxprop)
print("nomalisation of max propailitytie:", max_prop)
# Number of pixels per detector dimension
pix = 1280
# Number of detectors
n_det = 3
def CDist2(A,B):
"""
Calculate the distance between two points.
"""
dist = len3dvec(twoP_to_vec(A, B))
return dist
def len3dvec(vec):
"""
Calculate the length of a 3D vector.
"""
a = np.sqrt(vec[0]**2 + vec[1]**2 + vec[2]**2)
return a
def twoP_to_vec(A,B):
"""
Create a vector between two points.
"""
vec = np.array([B[0]-A[0], B[1]-A[1], B[2]-A[2]])
return vec
def rotation_matrix(axis, theta):
"""
Return the rotation matrix associated with counterclockwise rotation about
the given axis by theta radians.
"""
axis = np.asarray(axis)
axis = axis / math.sqrt(np.dot(axis, axis))
a = math.cos(theta / 2.0)
b, c, d = -axis * math.sin(theta / 2.0)
aa, bb, cc, dd = a * a, b * b, c * c, d * d
bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
return np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
[2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
[2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])
def read_in_data(filename):
"""
Read data from an HDF5 file and prepare it for processing.
"""
f = h5py.File(filename)
a = f['entry1/data']['bank01_events_dat_list_p_x_y_n_id_t']['events'][...]
#a[0]
d = np.matrix.transpose(a)
print("shape of event list (p_x_y_n_id_t)", d.shape)
print("start extracting data")
# Allocate units to events and create separate lists for each parameter
t_list = sc.array(dims=['x'], unit='s', values=d[5])
id_list = sc.array(dims=['x'], unit=None, values=d[4], dtype='int64')
#print(x_list.shape, y_list.shape, t_list.shape,id_list.shape)
weights = sc.array(dims=['x'], unit='counts', values=d[0]) #change to integer for measured data
# Normalize weights to the maximum probability
weights = weights * (max_prop/weights.max()) #delete for actual data
# weights = sc.ones_like(x_list)
# weights.unit = 'counts'
da = sc.DataArray(data=weights, coords={ 't': t_list, 'id': id_list})
# Ensure all IDs are recognized
print("id min",id_list.values.min())
print("id max",id_list.values.max())
ids1 = sc.arange('id', 1, 1638401, unit=None)
ids2 = sc.arange('id', 2000001, 3638401, unit=None)
ids3 = sc.arange('id', 4000001, 5638401, unit=None)
ids = sc.concat([ids1, ids2, ids3], 'id')
return da, ids
print("Read in data")
da, ids = read_in_data(filename)
print("Begin grouping")
grouped = da.group(ids)
del da
print("Begin histogram")
group_t = grouped.hist(t=t_step)
del grouped
# Extract geometry information from the input file
print("Extracting geometry information")
f = h5py.File(filename)
origen = f['entry1/data']['bank01_events_dat_list_p_x_y_n_id_t']['distance'][0].decode()
origen = list(np.float_(origen.split()))
xml = str(f['entry1/instrument/instrument_xml/data'][...][0]).split('\\n')
comp = False
det = False
source = False
sample = False
sample_pos = [0,0,0]
source_pos = [0,0,0]
d_list = []
rot_l1 = []
fast_l = []
slow_l = []
# Parse XML data to extract component information
for i in range(len(xml)):
ls = xml[i].replace('<t',' ').replace('>',' ').replace('"',' ').replace('<',' ').replace('\\t',' ').split()
# print(xml[i])
if len(ls) >= 1:
if ls[0] == 'component':
det = False
source = False
sample = False
comp = True
if ls[2].split('-')[0] == 'MonNDtype':
det = True
d_list.append([int(ls[2].split('-')[1])])
elif ls[2] == 'sourceMantid-type':
source = True
elif ls[2] == 'sampleMantid-type':
sample = True
comp = True
# if ls[1].split('-')[0] == 'type="MonNDtype':
# print("1",ls)
if len(ls) >= 1:
if ls[0] == 'type':
comp = False
if len(ls) >= 1:
if ls[0] == 'location' or ls[0] == 'location':
# print("3",ls)
if comp == True and det == True:
# print("2",ls)
xyz = [float(ls[2]),float(ls[4]),float(ls[6])]
print("xyz", xyz)
rot =float(ls[8])
rot_xyz =[ float(ls[8]), float(ls[10]),float(ls[12]),float(ls[14])]
print("rotation of detector",rot, rot_xyz)
d_list[len(d_list)-1].append(xyz)
rot_l1.append(rot_xyz)
elif comp == True and source == True:
source_pos = [float(ls[2]),float(ls[4]),float(ls[6])]
elif comp == True and sample == True:
sample_pos = np.array([float(ls[2]),float(ls[4]),float(ls[6])])
# print(len(d_list))
# print("sample and source position",sample_pos,source_pos)
# print("distance between sample and source",CDist2(source_pos, sample_pos))
# print("detector positions, relative to source at 0,0,0:",d_list)
# print("rotation list",rot_l)
# Shift from relative position to source to relative to sample
ds_l = []
sample_pos = sample_pos * [-1,-1,-1]
print("sample_pos",sample_pos)
for i in range(len(d_list)):
det_pos = np.array([d_list[i][1][0],d_list[i][1][1],d_list[i][1][2]]) * [-1.0, -1.0,-1.0]
rel_xyz= np.round(twoP_to_vec(sample_pos,det_pos),2)
print("rel_xyz",rel_xyz)
rel_xyz = rel_xyz # * [-1,-1,-1]
print("sample to detector dist",len3dvec(rel_xyz))
# rel_pos= twoP_to_vec(det_pos, sample_pos)
# print("detector position",sample_pos,det_pos,rel_pos)
# rel_xyz = [ d_list[i][1][0]-sample_pos[0], d_list[i][1][1]-sample_pos[1], (d_list[i][1][2]-sample_pos[2])]
# print("relative position",rel_xyz)
# rel_xyz = [ sample_pos[0]-d_list[i][1][0],sample_pos[1]-d_list[i][1][1], sample_pos[2]-d_list[i][1][2]]
# print("position",rel_xyz,rel_pos)
# print("relative position",rel_xyz)
ds_l.append(rel_xyz)
vec_f = [-1,0,-0]
vec_s = [0,-1,0]
rot_l = [rot_l1[0],rot_l1[2],rot_l1[1]] #not corect but a temporary work around
for i in range(len(rot_l)):
# if rot_l[i][2] < 0:
# theta = np.radians(rot_l[i][0])
# else:
theta = np.radians(-rot_l[i][0])
v = [rot_l[i][1],rot_l[i][2],rot_l[i][3]]
v1 = [0,0,1]
print("v",i,v,"theta", theta,rot_l[i][0])
fast_vec = np.round(np.dot(rotation_matrix(v, theta), vec_f),2)
fast_v_round = np.array([np.round(fast_vec[0],1), np.round(fast_vec[1],1), np.round(fast_vec[2],1)])
fast_l.append(fast_vec)
slow_l.append(np.dot(rotation_matrix(v, theta), vec_s))
# slow_l.append([0,1,0])
print("relative to sample position",ds_l)
print("fast axis",fast_l)
print("slow axis", slow_l)
print(rot_l)
print((f['entry1']['simulation']['Param'].keys()))
print((f['entry1']['simulation']['Param']['XtalPhiX']))
phix=float(list(str(f['entry1']['simulation']['Param']['XtalPhiX'][...][0]))[2])
phiy=float(list(str(f['entry1']['simulation']['Param']['XtalPhiY'][...][0]))[2])
phiz=float(list(str(f['entry1']['simulation']['Param']['XtalPhiZ'][...][0]))[2])
#str(phix[0])
#int(list(str(phix[0]))[2])
print(phix,phiy,phiz)
cor=[phix,phiy, phiz]
cryst_or = np.array(cor)
cryst_or
# Extract file name for output
no = filename.split('/')
print(no)
name_out= no[-1].split('.')[0]
print(name_out)
file_out = './'+name_out+'_'+str(t_step)+'_out.h5'
print(file_out)
print("Start writing to file")
# Write data to an output Nexus file
with h5py.File(file_out, 'w') as fo:
## create output nexus file
fo.attrs['default'] = 'NMX_data'
nxentry = fo.create_group('NMX_data')
nxentry.attrs["NX_class"] = 'NXentry'
nxentry.attrs['default'] = 'data'
nxentry.attrs['name'] = 'NMX1'
#nxentry.__setitem__('beamline','NMX')
nxentry.__setitem__('name','NMX')
nxentry.__setitem__('definition','TOFRAW')
nxentry.attrs['name'] = "NMX"
#nxentry.attrs['name'].__setattr__('name','NMX')
# Create NXsample group
nx_sample = nxentry.create_group('NXsample')
nx_sample.__setitem__('name','Single_crystal')
# Create NXinstrument group
nx_instrument = nxentry.create_group('NXinstrument')
nx_detector = nxentry.create_group('NXdetector')
# Create dataset for detector origin
det_origen = nx_detector.create_dataset('origen', data=ds_l)
det_origen.attrs['units'] = 'm'
# Create datasets for fast and slow axes
fast_axis = nx_detector.create_dataset('fast_axis', data=fast_l)
slow_axis = nx_detector.create_dataset('slow_axis', data=slow_l)
nx_beam = nxentry.create_group('NXbeam')
proton = nxentry.create_dataset('proton_charge', data=1)
nx_det1 = nxentry.create_group('detector_1')
counts = nx_det1.create_dataset('counts', data=[group_t.values], compression="gzip", compression_opts=4)
t_spectrum = nx_det1.create_dataset('t_bin', data=group_t.coords['t'].values, compression="gzip", compression_opts=4)
t_spectrum.attrs['units'] = 'ms'
t_spectrum.attrs['long_name'] = 't_bin TOF (ms)'
pixel_id = nx_det1.create_dataset('pix_id', data=group_t.coords['id'].values, compression="gzip", compression_opts=4)
pixel_id.attrs['units'] = ''
pixel_id.attrs['long_name'] = 'pixel ID'
# Create NXsource group
nx_inst = nxentry.create_group('instrument')
nx_inst.attrs['nr_detector'] = len(d_list)
nx_source = nxentry.create_group('NXsource')
nx_source.__setitem__('name','European Spallation Source')
nx_source.__setitem__('short_name','ESS')
nx_source.__setitem__('type','Spallation Neutron Source')
nx_source.__setitem__('distance',-CDist2(source_pos, sample_pos))
nx_source.__setitem__('probe','neutron')
nx_source.__setitem__('target_material','W')
# c_or = nxinst.create_dataset('crystal_orientation',data=cryst_or)
# c_or.attrs['unit'] = 'degrees'
# c_or.attrs['long_name'] = 'crystal orientation in Phi (degrees)'
fo.close()
del fo
print("Done writing to file")
# Memory usage tracking
current, peak = tracemalloc.get_traced_memory()
print('Current memory [GB]: {}, peak memory [GB]: {}'.format(round((current / (1024 * 1024 * 1024)), 4), round((peak / (1024 * 1024 * 1024)), 4)))
tracemalloc.stop()
print("Elapsed time (h:mm:ss):", datetime.now() - startTime)