Esempio n. 1
0
def get_total_treatment_volume_of_study(study_instance_uid):
    """
    Calculate combined PTV for the provided study_instance_uid
    """
    ptv_coordinates_strings = query('dvhs', 'roi_coord_string',
                                    "study_instance_uid = '%s' and roi_type like 'PTV%%'" % study_instance_uid)

    ptvs = [roi_form.get_planes_from_string(ptv[0]) for ptv in ptv_coordinates_strings]

    return roi_geom.union(ptvs)
Esempio n. 2
0
def get_total_treatment_volume_of_study(study_instance_uid, ptvs=None):
    """
    Calculate combined PTV for the provided study_instance_uid
    """

    condition = "study_instance_uid = '%s' and roi_type like 'PTV%%'" % study_instance_uid
    if ptvs:
        condition += " and roi_name in ('%s')" % "','".join(ptvs)
    ptv_coordinates_strings = query('dvhs', 'roi_coord_string', condition)

    ptvs = [
        roi_form.get_planes_from_string(ptv[0])
        for ptv in ptv_coordinates_strings
    ]

    return roi_geom.union(ptvs)
Esempio n. 3
0
def min_distances(study_instance_uid, roi_name, pre_calc=None):
    """
    Recalculate the min, mean, median, and max PTV distances an roi based on data in the SQL DB.
    Optionally provide coordinates of combined PTV, return from get_treatment_volume_coord
    """

    oar_coordinates_string = query('dvhs', 'roi_coord_string',
                                   "study_instance_uid = '%s' and roi_name = '%s'" % (study_instance_uid, roi_name))

    treatment_volume_coord = pre_calc
    if treatment_volume_coord is None:
        with DVH_SQL() as cnx:
            ptv_coordinates_strings = cnx.query('dvhs', 'roi_coord_string',
                                                "study_instance_uid = '%s' and roi_type like 'PTV%%'"
                                                % study_instance_uid)

        ptvs = [roi_form.get_planes_from_string(ptv[0]) for ptv in ptv_coordinates_strings]
        treatment_volume_coord = roi_form.get_roi_coordinates_from_planes(roi_geom.union(ptvs))

    oar_coordinates = roi_form.get_roi_coordinates_from_string(oar_coordinates_string[0][0])

    treatment_volume_coord = sample_roi(treatment_volume_coord)
    oar_coordinates = sample_roi(oar_coordinates)

    try:
        data = roi_geom.min_distances_to_target(oar_coordinates, treatment_volume_coord)
    except MemoryError:
        try:
            treatment_volume_coord = sample_roi(treatment_volume_coord, max_point_count=3000)
            oar_coordinates = sample_roi(oar_coordinates,  max_point_count=3000)
            data = roi_geom.min_distances_to_target(oar_coordinates, treatment_volume_coord)
        except MemoryError as e:
            print("Memory Error: ", e)
            print('Error reported for %s with study_instance_uid %s' % (roi_name, study_instance_uid))
            print('Skipping PTV distance and DTH calculations for this ROI.')
            data = None
        except Exception as e:
            print('Error: ', e)
            print('Error reported for %s with study_instance_uid %s' % (roi_name, study_instance_uid))
            print('Skipping PTV distance and DTH calculations for this ROI.')
            data = None

    if data is not None:
        try:
            dth = roi_geom.dth(data)
            dth_string = ','.join(['%.3f' % num for num in dth])

            data_map = {'dist_to_ptv_min': round(float(np.min(data)), 2),
                        'dist_to_ptv_mean': round(float(np.mean(data)), 2),
                        'dist_to_ptv_median': round(float(np.median(data)), 2),
                        'dist_to_ptv_max': round(float(np.max(data)), 2),
                        'dth_string': dth_string}
        except MemoryError as e:
            print("Memory Error: ", e)
            print('Error reported for %s with study_instance_uid %s' % (roi_name, study_instance_uid))
            print('Skipping PTV distance and DTH calculations for this ROI.')
            data_map = None

        if data_map:
            for key, value in data_map.items():
                update_dvhs_table(study_instance_uid, roi_name, key, value)