def reduce_expanded_mask(expanded_mask, img_size, expansion): return np.mean( np.mean( np.reshape(expanded_mask, (img_size, expansion, img_size, expansion)), axis=1, ), axis=2, )
def to_minimise_edge_agreement(centre): x, y = points_to_check_edge_agreement(centre) total_minimisation = 0 for current_mask in dist_mask[1::]: current_layer = field(x[current_mask], y[current_mask]) total_minimisation += np.mean( (current_layer - np.mean(current_layer))**2) return total_minimisation / (len(dist_mask) - 1)
def plot_gamma_hist(gamma, percent, dist): valid_gamma = gamma[~np.isnan(gamma)] plt.hist(valid_gamma, 50, density=True) pass_ratio = np.sum(valid_gamma <= 1) / len(valid_gamma) plt.title( "Local Gamma ({0}%/{1}mm) | Percent Pass: {2:.2f} % | Mean Gamma: {3:.2f} | Max Gamma: {4:.2f}" .format(percent, dist, pass_ratio * 100, np.mean(valid_gamma), np.max(valid_gamma)))
def running_mean(x, N): out = np.zeros_like(x, dtype=np.float64) dim_len = x.shape[0] for i in range(dim_len): if N % 2 == 0: a, b = i - (N - 1) // 2, i + (N - 1) // 2 + 2 else: a, b = i - (N - 1) // 2, i + (N - 1) // 2 + 1 # cap indices to min and max indices a = max(0, a) b = min(dim_len, b) out[i] = np.mean(x[a:b]) return out
def penumbra_width( self, side: str = "left", lower: int = 20, upper: int = 80, interpolate: bool = False, ): """Return the penumbra width of the profile. This is the standard "penumbra width" calculation that medical physics talks about in radiation profiles. Standard is the 80/20 width, although 90/10 is sometimes used. Parameters ---------- side : {'left', 'right', 'both'} Which side of the profile to determined penumbra. If 'both', the left and right sides are averaged. lower : int The "lower" penumbra value used to calculate penumbra. Must be lower than upper. upper : int The "upper" penumbra value used to calculate penumbra. interpolate : bool Whether to interpolate the profile to get more accurate values. Raises ------ ValueError If lower penumbra is larger than upper penumbra """ if lower > upper: raise ValueError( "Upper penumbra value must be larger than the lower penumbra value" ) if side in (LEFT, RIGHT): li = self._penumbra_point(side, lower, interpolate) ui = self._penumbra_point(side, upper, interpolate) pen = np.abs(ui - li) elif side == BOTH: li = self._penumbra_point(LEFT, lower, interpolate) ui = self._penumbra_point(LEFT, upper, interpolate) lpen = np.abs(ui - li) li = self._penumbra_point(RIGHT, lower, interpolate) ui = self._penumbra_point(RIGHT, upper, interpolate) rpen = np.abs(ui - li) pen = np.mean([lpen, rpen]) return pen
def find_modulation_factor(sinogram): """ read sinogram from csv file Calculate the ratio of the maximum leaf open time (assumed fully open) to the mean leaf open time, as determined over all non-zero leaf open times, where zero is interpreted as blocked versus modulated. Parameters ---------- sinogram : np.array Returns ------- modulation factor : float """ lfts = [lft for lft in sinogram.flatten() if lft > 0.0] modulation_factor = max(lfts) / np.mean(lfts) return modulation_factor
def _calc_open_fraction(mlc_open, jaw_open): open_t = mlc_open * jaw_open[:, :, None] open_fraction = np.mean(open_t, axis=0) return open_fraction
def to_minimise(rotation): all_field_points = transform_rotation_field_points( points_at_origin, centre, rotation) return np.mean(field(*all_field_points)**2)
def gamma_filter_numpy(axes_reference, dose_reference, axes_evaluation, dose_evaluation, distance_mm_threshold, dose_threshold, lower_dose_cutoff=0, **_): coord_diffs = [ coord_ref[:, None] - coord_eval[None, :] for coord_ref, coord_eval in zip(axes_reference, axes_evaluation) ] all_in_vicinity = [ np.where(np.abs(diff) < distance_mm_threshold) for diff in coord_diffs ] ref_coord_points = create_point_combination( [in_vicinity[0] for in_vicinity in all_in_vicinity]) eval_coord_points = create_point_combination( [in_vicinity[1] for in_vicinity in all_in_vicinity]) distances = np.sqrt( np.sum( [ coord_diff[ref_points, eval_points]**2 for ref_points, eval_points, coord_diff in zip( ref_coord_points, eval_coord_points, coord_diffs) ], axis=0, )) within_distance_threshold = distances < distance_mm_threshold distances = distances[within_distance_threshold] ref_coord_points = ref_coord_points[:, within_distance_threshold] eval_coord_points = eval_coord_points[:, within_distance_threshold] dose_diff = ( dose_evaluation[eval_coord_points[0, :], eval_coord_points[1, :], eval_coord_points[2, :]] - dose_reference[ref_coord_points[0, :], ref_coord_points[1, :], ref_coord_points[2, :]]) gamma = np.sqrt((dose_diff / dose_threshold)**2 + (distances / distance_mm_threshold)**2) gamma_pass = gamma < 1 eval_pass = eval_coord_points[:, gamma_pass] ravel_index = convert_to_ravel_index(eval_pass) gamma_pass_array = np.zeros_like(dose_evaluation).astype(np.bool) gamma_pass_array = np.ravel(gamma_pass_array) dose_above_cut_off = np.ravel(dose_evaluation) > lower_dose_cutoff gamma_pass_array[ravel_index] = True gamma_pass_percentage = np.mean(gamma_pass_array[dose_above_cut_off]) * 100 return gamma_pass_percentage