def profile(displacements, depth, direction, dose_dataset, plan_dataset): """Interpolates dose for cardinal angle horizontal profiles within a DICOM dose dataset. Since the DICOM dose dataset is in CT coordinates the corresponding DICOM plan is also required in order to calculate the conversion between CT coordinate space and depth and horizontal displacement. Currently, `profile()` only supports a `dose_dataset` for which the patient orientation is HFS and that any beams in `plan_dataset` have gantry angle equal to 0 (head up). Depth is assumed to be purely in the y axis direction in DICOM coordinates. Parameters ---------- displacements : numpy.ndarray An array of displacements to interpolate within the DICOM dose file. 0 is defined in the DICOM z or x directions based either upon the ``SurfaceEntryPoint`` or the ``IsocenterPosition`` depending on what is available within the DICOM plan file. depth : float The depth at which to interpolate within the DICOM dose file. 0 is defined as the surface of the phantom using either the ``SurfaceEntryPoint`` parameter or a combination of ``SourceAxisDistance``, ``SourceToSurfaceDistance``, and ``IsocentrePosition``. direction : str, one of ('inplane', 'inline', 'crossplane', 'crossline') Corresponds to the axis upon which to apply the displacements. - 'inplane' or 'inline' converts to DICOM z direction - 'crossplane' or 'crossline' converts to DICOM x direction dose_dataset : pydicom.dataset.Dataset The RT DICOM dose dataset to be interpolated plan_dataset : pydicom.dataset.Dataset The RT DICOM plan used to extract surface and isocentre parameters and verify gantry angle 0 beams are used. """ require_patient_orientation(dose_dataset, "HFS") require_gantries_be_zero(plan_dataset) displacements = np.array(displacements, copy=False) surface_entry_point = get_surface_entry_point_with_fallback(plan_dataset) depth_adjust = surface_entry_point.y y = [depth + depth_adjust] if direction in ("inplane", "inline"): coords = (displacements + surface_entry_point.z, y, [surface_entry_point.x]) elif direction in ("crossplane", "crossline"): coords = ([surface_entry_point.z], y, displacements + surface_entry_point.x) else: raise ValueError("Expected direction to be equal to one of " "'inplane', 'inline', 'crossplane', or 'crossline'") extracted_dose = np.squeeze(dicom_dose_interpolate(coords, dose_dataset)) return extracted_dose
def _from_mosaiq_base(cls, cursor, field_id): txfield_results, txfieldpoint_results = fetch_and_verify_mosaiq_sql( cursor, field_id) total_mu = np.array(txfield_results[0]).astype(float) cumulative_percentage_mu = txfieldpoint_results[:, 0].astype(float) if np.shape(cumulative_percentage_mu) == (): mu_per_control_point = [0, total_mu] else: cumulative_mu = cumulative_percentage_mu * total_mu / 100 mu_per_control_point = np.concatenate([[0], np.diff(cumulative_mu)]) monitor_units = np.cumsum(mu_per_control_point).tolist() mlc_a = np.squeeze( decode_msq_mlc(txfieldpoint_results[:, 1].astype(bytes))).T mlc_b = np.squeeze( decode_msq_mlc(txfieldpoint_results[:, 2].astype(bytes))).T msq_gantry_angle = txfieldpoint_results[:, 3].astype(float) msq_collimator_angle = txfieldpoint_results[:, 4].astype(float) coll_y1 = txfieldpoint_results[:, 5].astype(float) coll_y2 = txfieldpoint_results[:, 6].astype(float) mlc, jaw = collimation_to_bipolar_mm(mlc_a, mlc_b, coll_y1, coll_y2) gantry = convert_IEC_angle_to_bipolar(msq_gantry_angle) collimator = convert_IEC_angle_to_bipolar(msq_collimator_angle) # TODO Tidy up this axis swap mlc = np.swapaxes(mlc, 0, 2) jaw = np.swapaxes(jaw, 0, 1) mosaiq_delivery_data = cls(monitor_units, gantry, collimator, mlc, jaw) return mosaiq_delivery_data
def depth_dose(depths, dose_dataset, plan_dataset): """Interpolates dose for defined depths within a DICOM dose dataset. Since the DICOM dose dataset is in CT coordinates the corresponding DICOM plan is also required in order to calculate the conversion between CT coordinate space and depth. Currently, `depth_dose()` only supports a `dose_dataset` for which the patient orientation is HFS and that any beams in `plan_dataset` have gantry angle equal to 0 (head up). Depth is assumed to be purely in the y axis direction in DICOM coordinates. Parameters ---------- depths : numpy.ndarray An array of depths to interpolate within the DICOM dose file. 0 is defined as the surface of the phantom using either the ``SurfaceEntryPoint`` parameter or a combination of ``SourceAxisDistance``, ``SourceToSurfaceDistance``, and ``IsocentrePosition``. dose_dataset : pydicom.dataset.Dataset The RT DICOM dose dataset to be interpolated plan_dataset : pydicom.dataset.Dataset The RT DICOM plan used to extract surface parameters and verify gantry angle 0 beams are used. """ require_patient_orientation(dose_dataset, "HFS") require_gantries_be_zero(plan_dataset) depths = np.array(depths, copy=False) surface_entry_point = get_surface_entry_point_with_fallback(plan_dataset) depth_adjust = surface_entry_point.y y = depths + depth_adjust x, z = [surface_entry_point.x], [surface_entry_point.z] coords = (z, y, x) extracted_dose = np.squeeze(dicom_dose_interpolate(coords, dose_dataset)) return extracted_dose
def visual_alignment_of_equivalent_ellipse(x, y, width, length, callback): """Visually align the equivalent ellipse to the insert.""" insert = shapely_insert(x, y) unit_circle = shapely.geometry.Point(0, 0).buffer(1) initial_ellipse = shapely.affinity.scale(unit_circle, xfact=width / 2, yfact=length / 2) def minimising_function(optimiser_input): x_shift, y_shift, rotation_angle = optimiser_input rotated = shapely.affinity.rotate(initial_ellipse, rotation_angle, use_radians=True) translated = shapely.affinity.translate(rotated, xoff=x_shift, yoff=y_shift) disjoint_area = (translated.difference(insert).area + insert.difference(translated).area) return disjoint_area / 400 x0 = np.append(np.squeeze(insert.centroid.coords), np.pi / 4) niter = 10 T = insert.area / 40000 stepsize = 3 niter_success = 2 output = scipy.optimize.basinhopping( minimising_function, x0, niter=niter, T=T, stepsize=stepsize, niter_success=niter_success, callback=callback, ) x_shift, y_shift, rotation_angle = output.x return x_shift, y_shift, rotation_angle
def search_for_centre_of_largest_bounded_circle(x, y, callback=None): """Find the centre of the largest bounded circle within the insert.""" insert = shapely_insert(x, y) boundary = insert.boundary centroid = insert.centroid furthest_distance = np.hypot(np.diff(insert.bounds[::2]), np.diff(insert.bounds[1::2])) def minimising_function(optimiser_input): x, y = optimiser_input point = shapely.geometry.Point(x, y) if insert.contains(point): edge_distance = point.distance(boundary) else: edge_distance = -point.distance(boundary) return -edge_distance x0 = np.squeeze(centroid.coords) niter = 200 T = furthest_distance / 3 stepsize = furthest_distance / 2 niter_success = 50 output = scipy.optimize.basinhopping( minimising_function, x0, niter=niter, T=T, stepsize=stepsize, niter_success=niter_success, callback=callback, ) circle_centre = output.x return circle_centre