-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyRate_AnalyseData.py
More file actions
188 lines (154 loc) · 7.21 KB
/
Copy pathPyRate_AnalyseData.py
File metadata and controls
188 lines (154 loc) · 7.21 KB
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
import numpy as np
import math
from datetime import datetime
def interpolate(target_dose, dose, previous_dose, time, previous_time):
# interpolate
delta_dose = dose - previous_dose
delta_time = time - previous_time
target_dose_past_previous = target_dose - previous_dose
proportion_of_delta = target_dose_past_previous / delta_dose
interpolated_time = previous_time + (delta_time * proportion_of_delta)
return interpolated_time
def write_data(data, out_file, dose_map_dose_target):
# [0%, 5%, 10%, 15%, 85%, 90%, 95%, 100%]
dose_threshhold_fractions = [0.00, 0.05, 0.10, 0.15, 0.85, 0.90, 0.95, 1.00, 1.10]
dose_threshhold = [x * dose_map_dose_target for x in dose_threshhold_fractions]
time_to_target_data = [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00]
dose_threshold_index = 0
max_dose = 0.0
previous_time = 0.
previous_dose = 0.
for line in data:
#########################
# Write out data to file
#
# dose = dose[point_of_interest[0], point_of_interest[1]] * dose_per_signal
for i in range(len(line)):
if i != len(line)-1:
out_file.write(f'{line[i]}, ')
else:
out_file.write(f'{line[i]}\n')
#############################################
# Check threshold dose values and store times
#
while line[1] > dose_threshhold[dose_threshold_index]:
interpolated_time = interpolate(dose_threshhold[dose_threshold_index],
line[1],
previous_dose,
line[0],
previous_time)
time_to_target_data[dose_threshold_index] = interpolated_time
dose_threshold_index += 1
if line[1] > max_dose:
max_dose = line[1]
time_to_target_data[7] = line[0]
previous_time = line[0]
previous_dose = line[1]
return time_to_target_data
def get_times_to_dose(x, y, dose, dose_threshold_index_map, dose_threshold_map, previous_dose_map, time, previous_time, time_to_dose):
# Loop over the extent of the dose map
if dose != 0.0:
# Get the latest dose threshold index for this pixel
dose_threshold_index = dose_threshold_index_map[y][x]
if dose_threshold_index < 8:
while dose >= dose_threshold_map[dose_threshold_index][y][x]:
# Store the time taken to get to this threshold of dose
interpolated_time = interpolate(dose_threshold_map[dose_threshold_index][y][x],
dose,
previous_dose_map[y][x],
time,
previous_time)
time_to_dose[dose_threshold_index][y][x] = interpolated_time
if dose_threshold_index < 8:
# Increment dose threshold index
dose_threshold_index += 1
# Store the last
dose_threshold_index_map[y][x] = dose_threshold_index
previous_dose_map[y][x] = dose
def dose_map_into_dose_rate(dose_map_history):
min_dose = 0.1
dose_threshhold_fraction = [0.00, 0.05, 0.10, 0.15, 0.85, 0.90, 0.95, 1.00, 1.10]
size = dose_map_history[0][1].shape
time_to_dose = np.zeros((8, size[0], size[1]))
dose_rate_map = np.zeros((4, size[0], size[1]))
previous_dose_map = np.zeros((size[0], size[1]))
previous_time = 0
dose_threshold_index_map = np.zeros((size[0], size[1]), dtype=int)
max_dose = dose_map_history[len(dose_map_history) - 1][1]
# Pre-calculating dose thresholds
dose_threshold_map = np.zeros((9, size[0], size[1]), dtype=float)
for pc_i in range(9):
pc_fraction = dose_threshhold_fraction[pc_i]
dose_threshold_map[pc_i] = max_dose * pc_fraction
start = datetime.now()
for time, dose_map in dose_map_history:
[[get_times_to_dose(x, y, dose,
dose_threshold_index_map,
dose_threshold_map,
previous_dose_map,
time,
previous_time,
time_to_dose)
for (x, dose) in enumerate(column)]
for (y, column) in enumerate(dose_map)]
previous_time = time
print(f'\t\t-- took {(datetime.now() - start).seconds} seconds')
print("> Converting Times into Dose Rates...")
for y in range(size[0]):
for x in range(size[1]):
if max_dose[y][x] > min_dose:
time100 = time_to_dose[7][y][x] - time_to_dose[0][y][x]
time90 = time_to_dose[6][y][x] - time_to_dose[1][y][x]
time80 = time_to_dose[5][y][x] - time_to_dose[2][y][x]
time70 = time_to_dose[4][y][x] - time_to_dose[3][y][x]
dose_rate_map[0][y][x] = (max_dose[y][x] * 1.0) / time100
dose_rate_map[1][y][x] = (max_dose[y][x] * 0.9) / time90
dose_rate_map[2][y][x] = (max_dose[y][x] * 0.8) / time80
dose_rate_map[3][y][x] = (max_dose[y][x] * 0.7) / time70
else:
dose_rate_map[0][y][x] = 0.
dose_rate_map[1][y][x] = 0.
dose_rate_map[2][y][x] = 0.
dose_rate_map[3][y][x] = 0.
return time_to_dose, dose_rate_map
def calculate_statistics(map, target, spot_pixels_max, overscan, resolution):
target_horizontal_size = target[0]
target_vertical_size = target[1]
shape = target[2]
h = math.ceil(target_horizontal_size / resolution)
v = math.ceil(target_vertical_size / resolution)
size = map.shape
list_of_values = []
bottom_corner_horizontal = spot_pixels_max + overscan[0]
bottom_corner_vertical = spot_pixels_max + overscan[1]
if shape == 'rectangle':
min_x = bottom_corner_horizontal
min_y = bottom_corner_vertical
max_x = bottom_corner_horizontal + h
max_y = bottom_corner_vertical + v
for y in range(size[0]):
if y <= max_y and y >= min_y:
for x in range(size[1]):
if x <= max_x and x >= min_x:
dose = map[y][x]
list_of_values.append(dose)
elif shape == 'circle':
centre_x = bottom_corner_horizontal + math.ceil(h / 2)
centre_y = bottom_corner_vertical + math.ceil(v / 2)
radius = math.ceil(target_horizontal_size / 2 / resolution)
for y in range(size[0]):
for x in range(size[1]):
vector = [x - centre_x, y - centre_y]
distance = pow(pow(vector[0], 2) + pow(vector[1], 2), 0.5)
if distance <= radius:
dose = map[y][x]
list_of_values.append(dose)
average = np.average(list_of_values)
stdev = np.std(list_of_values)
homogeneity_index = calculate_HI(list_of_values)
return average, stdev, homogeneity_index
def calculate_HI(list):
max_value = max(list)
min_value = min(list)
homogeneity_index = ((max_value - min_value) / (max_value + min_value))*100
return homogeneity_index