Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_axes(image, dpcm=100):
shape = np.shape(image)
x_span = (np.arange(0, shape[0]) - shape[0] // 2) * 10 / dpcm
y_span = (np.arange(0, shape[1]) - shape[1] // 2) * 10 / dpcm
return x_span, y_span
def _interp_coords(coord):
return scipy.interpolate.interp1d(np.arange(len(coord)), coord)
Returns
-------
histogram : list of tuples: [(bin, count)...]
bin is a 2-element array
"""
lfts = sinogram.flatten()
bin_inc = (max(lfts) - min(lfts)) / num_bins
bin_min = min(lfts)
bin_max = max(lfts)
bins_strt = np.arange(bin_min, bin_max, bin_inc)
bins_stop = np.arange(bin_inc, bin_max + bin_inc, bin_inc)
bins = np.dstack((bins_strt, bins_stop))[0]
counts = [0 for b in bins]
for lft in lfts:
for idx, bin in enumerate(bins):
if lft >= bin[0] and lft < bin[1]:
counts[idx] = counts[idx] + 1
histogram = list(zip(bins, counts))
return histogram
)
file_hashes_vmat = not_yet_compared[field_types == "VMAT"]
vmat_filepaths = np.array(
[
os.path.join(
config["linac_logfile_data_directory"],
"indexed",
index[file_hash]["filepath"],
)
for file_hash in file_hashes_vmat
]
)
shuffle_index = np.arange(len(vmat_filepaths))
np.random.shuffle(shuffle_index)
return file_hashes_vmat[shuffle_index], vmat_filepaths[shuffle_index]
grid_reference_position = _determine_reference_grid_position(
top_of_reference_leaf, grid_resolution
)
top_grid_pos = (
np.round((max_y - grid_reference_position) / grid_resolution)
) * grid_resolution + grid_reference_position
bot_grid_pos = (
grid_reference_position
- (np.round((-min_y + grid_reference_position) / grid_resolution))
* grid_resolution
)
grid = dict()
grid["jaw"] = np.arange(
bot_grid_pos, top_grid_pos + grid_resolution, grid_resolution
).astype("float")
grid_leaf_map = np.argmin(
np.abs(grid["jaw"][:, None] - leaf_centres[None, :]), axis=1
)
adjusted_grid_leaf_map = grid_leaf_map - np.min(grid_leaf_map)
leaves_to_be_calced = np.unique(grid_leaf_map)
adjusted_mlc = mlc[:, leaves_to_be_calced, :]
min_x = np.round(np.min(-adjusted_mlc[:, :, 0]) / grid_resolution) * grid_resolution
max_x = np.round(np.max(adjusted_mlc[:, :, 1]) / grid_resolution) * grid_resolution
grid["mlc"] = np.arange(min_x, max_x + grid_resolution, grid_resolution).astype(
dose_evaluation,
distance_mm_threshold,
dose_threshold,
lower_dose_cutoff=0,
**_
):
xx_ref, yy_ref, zz_ref = np.meshgrid(*axes_reference, indexing="ij")
gamma_array = np.ones_like(dose_evaluation).astype(np.float) * np.nan
mesh_index = np.meshgrid(
*[np.arange(len(coord_eval)) for coord_eval in axes_evaluation]
)
eval_index = np.reshape(np.array(mesh_index), (3, -1))
run_index = np.arange(np.shape(eval_index)[1])
np.random.shuffle(run_index)
sys.stdout.write(" ")
for counter, point_index in enumerate(run_index):
i, j, k = eval_index[:, point_index]
eval_x = axes_evaluation[0][i]
eval_y = axes_evaluation[1][j]
eval_z = axes_evaluation[2][k]
if dose_evaluation[i, j, k] < lower_dose_cutoff:
continue
distance = np.sqrt(
(xx_ref - eval_x) ** 2 + (yy_ref - eval_y) ** 2 + (zz_ref - eval_z) ** 2
)
def _calc_blocked_by_device(grid, positions, grid_resolution, time_steps):
blocked_by_device = {}
for device, value in positions.items():
blocked_by_device[device] = dict()
for multiplier, (start, end) in value.items():
dt = (end - start) / (time_steps - 1)
travel = start[None, :] + np.arange(0, time_steps)[:, None] * dt[None, :]
travel_diff = multiplier * (
grid[device][None, None, :] - travel[:, :, None]
)
blocked_by_device[device][multiplier] = _calc_blocked_t(
travel_diff, grid_resolution
)
return blocked_by_device
def gamma_filter_brute_force(
axes_reference,
dose_reference,
axes_evaluation,
dose_evaluation,
distance_mm_threshold,
dose_threshold,
lower_dose_cutoff=0,
**_
):
xx_ref, yy_ref, zz_ref = np.meshgrid(*axes_reference, indexing="ij")
gamma_array = np.ones_like(dose_evaluation).astype(np.float) * np.nan
mesh_index = np.meshgrid(
*[np.arange(len(coord_eval)) for coord_eval in axes_evaluation]
)
eval_index = np.reshape(np.array(mesh_index), (3, -1))
run_index = np.arange(np.shape(eval_index)[1])
np.random.shuffle(run_index)
sys.stdout.write(" ")
for counter, point_index in enumerate(run_index):
i, j, k = eval_index[:, point_index]
eval_x = axes_evaluation[0][i]
eval_y = axes_evaluation[1][j]
eval_z = axes_evaluation[2][k]
if dose_evaluation[i, j, k] < lower_dose_cutoff:
continue
# DIMENSIONS TO AVG ACROSS DIFFERENT FOR HORIZ VS VERT IMG
if image_array.shape[0] > 5 * image_array.shape[1]: # VERT
image_vector = np.average(image_array, axis=(1, 2))
pixel_size_in_cm = 2.54 / dpi_vert
elif image_array.shape[1] > 5 * image_array.shape[0]: # HORIZ
image_vector = np.average(image_array, axis=(0, 2))
pixel_size_in_cm = 2.54 / dpi_horiz
else:
raise ValueError("The PNG file is not a narrow strip.")
assert step_size > 5 * pixel_size_in_cm, "step size too small"
if image_vector.shape[0] % 2 == 0:
image_vector = image_vector[:-1] # SO ZERO DISTANCE IS MID-PIXEL
length_in_cm = image_vector.shape[0] * pixel_size_in_cm
full_resolution_distances = np.arange(
-length_in_cm / 2, length_in_cm / 2, pixel_size_in_cm
)
# TO MOVE FROM FILM RESOLUTION TO DESIRED PROFILE RESOLUTION
num_pixels_to_avg_over = int(step_size / pixel_size_in_cm)
sample_indices = np.arange(
num_pixels_to_avg_over / 2,
len(full_resolution_distances),
num_pixels_to_avg_over,
).astype(int)
downsampled_distances = list(full_resolution_distances[sample_indices])
downsampled_density = []
for idx in sample_indices: # AVERAGE OVER THE SAMPLING WINDOW
avg_density = np.average(
image_vector[
beam_collimation = (
collimation.BeamSequence[0]
.ControlPointSequence[0]
.BeamLimitingDevicePositionSequence
)
gantry_step_size = 15.0
gantry_beam_1 = from_bipolar(np.arange(-180, 181, gantry_step_size))
gantry_beam_1[0] = 180.1
gantry_beam_1[-1] = 179.9
coll_beam_1 = from_bipolar(np.arange(-180, 1, gantry_step_size / 2))
coll_beam_1[0] = 180.1
gantry_beam_2 = from_bipolar(np.arange(180, -181, -gantry_step_size))
gantry_beam_2[-1] = 180.1
gantry_beam_2[0] = 179.9
coll_beam_2 = from_bipolar(np.arange(180, -1, -gantry_step_size / 2))
coll_beam_2[0] = 179.9
gant_directions = ["CW", "CC"]
coll_directions = ["CC", "CW"]
control_point_sequence_beam1 = create_control_point_sequence(
vmat_example.BeamSequence[0],
beam_collimation,
coll_directions[0],
dose_rate,
gantry_beam_1,
coll_beam_1,