-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
136 lines (99 loc) · 3.53 KB
/
functions.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
import numpy as np
import sys
import os
import xpcs
# parameters
distance = 4.0 # m
pix = 55e-6 # m
photon_energy = 10.0e3 #eV
h = 4.135667516e-15
c = 2.99792458e8
wavelength = h*c/photon_energy
center_x, center_y = 1069.5, 201.5 # tristan center
center = center_y, center_x
def calc_Iq(Q_map, I_map, mask, downsample, nbins, n_max, n_min, threshold):
# --- calculate I(q) based on a Q-map
# roi
smallx1 = np.int((center_x-n_max)//downsample)
smallx2 = np.int((center_x+n_max)//downsample)
smally1 = np.int((center_y-n_min)//downsample)
smally2 = np.int((center_y+n_max)//downsample)
# mask
if mask is None:
mask=np.ones(Q_map.shape) # unitary mask
Q_map = Q_map*mask
Q_map = Q_map[smally1:smally2,smallx1:smallx2]
I_map = I_map*mask
I_map = I_map[smally1:smally2,smallx1:smallx2]
# flatten 2D maps
Q_map_flat = Q_map.flatten()
I_map_flat = I_map.flatten()
# sort into bins with similar intensity
ind = np.argsort(Q_map_flat)
Q_map_flat = Q_map_flat[ind]
I_map_flat = I_map_flat[ind]
# Q-bins
edges = np.linspace(Q_map_flat[Q_map_flat>threshold].min(), Q_map_flat.max(), nbins+1)
#print(edges[0], edges[-1])
# group indices in different Q-bins
inds = np.digitize(Q_map_flat, edges)
Q_av = np.array([Q_map_flat[inds == i].mean() for i in range(0, nbins+1)])
I_av = np.array([I_map_flat[inds == i].mean() for i in range(0, nbins+1)])
return Q_av, I_av
def calc_q1d(Q_map, mask, downsample, nbins, n_max, n_min, threshold):
# --- calculate the 1D Q based on a 2D Q-map
# roi
smallx1 = np.int((center_x-n_max)/downsample)
smallx2 = np.int((center_x+n_max)/downsample)
smally1 = np.int((center_y-n_min)/downsample)
smally2 = np.int((center_y+n_max)/downsample)
# mask
if mask is None:
mask=np.ones(Q_map.shape) # unitary mask
Q_map = Q_map*mask
Q_map = Q_map[smally1:smally2,smallx1:smallx2]
# flatten 2D maps
Q_map_flat = Q_map.flatten()
# sort into bins with similar intensity
ind = np.argsort(Q_map_flat)
Q_map_flat = Q_map_flat[ind]
# Q-bins
edges = np.linspace(Q_map_flat[Q_map_flat>threshold].min(), Q_map_flat.max(), nbins+1)
print(edges[0], edges[-1])
# group indices in different Q-bins
inds = np.digitize(Q_map_flat, edges)
Q_av = np.array([Q_map_flat[inds == i].mean() for i in range(0, nbins+1)])
return Q_av
def make_rois_qmap(Q_map, mask, nbins, roi_min, roi_max, threshold, downsample):
# mask
if mask is None:
mask=np.ones(Q_map.shape) # unitary mask
Q_map = Q_map*mask
Q_map = Q_map
# flatten 2D maps
Q_map_flat = Q_map.flatten()
# sort into bins with similar intensity
ind = np.argsort(Q_map_flat)
Q_map_flat = Q_map_flat[ind]
# Q-bins
edges = np.linspace(Q_map_flat[Q_map_flat>threshold].min(), Q_map_flat.max(), nbins+1)
print(edges[0], edges[-1], edges.shape)
# group indices in different Q-bins
rois = np.digitize(Q_map, edges)
rois[(rois<roi_min) | (rois>roi_max)] = 0
inds = np.digitize(Q_map_flat, edges)
Q_av = np.array([Q_map_flat[inds == i].mean() for i in range(roi_min, roi_max+1)])
print(len(Q_av))
return rois, Q_av
def shift_image(X, dx, dy): # shift image and replace with zeros
X = np.roll(X, dy, axis=0)
X = np.roll(X, dx, axis=1)
if dy>0:
X[:dy, :] = 0
elif dy<0:
X[dy:, :] = 0
if dx>0:
X[:, :dx] = 0
elif dx<0:
X[:, dx:] = 0
return X