Ejemplo n.º 1
0
def get_recently_completed_qcls_across_sites(cursors,
                                             servers,
                                             centres,
                                             locations,
                                             days=7):
    now = datetime.datetime.now()
    days_ago = now - datetime.timedelta(days=days)
    tomorrow = now + datetime.timedelta(days=1)

    days_ago_string = "{} 00:00:00".format(days_ago.strftime("%Y-%m-%d"))
    tomorrow_string = "{} 00:00:00".format(tomorrow.strftime("%Y-%m-%d"))

    results = pd.DataFrame()

    for centre in centres:
        cursor = cursors[servers[centre]]

        qcls = get_qcls_by_date(cursor, locations[centre], days_ago_string,
                                tomorrow_string)

        qcls["centre"] = [centre] * len(qcls)

        results = results.append(qcls)

    results = results.sort_values(by="actual_completed_time", ascending=False)

    return results
Ejemplo n.º 2
0
def display_deliveries(deliveries):
    if not deliveries:
        return 0
    """
    #### Overview of selected deliveries
    """

    data = []
    for delivery in deliveries:
        num_control_points = len(delivery.mu)

        if num_control_points != 0:
            total_mu = delivery.mu[-1]
        else:
            total_mu = 0

        data.append([total_mu, num_control_points])

    columns = ["MU", "Number of Data Points"]
    df = pd.DataFrame(data=data, columns=columns)
    df

    total_mu = round(df["MU"].sum(), 1)

    f"Total MU: `{total_mu}`"

    return total_mu
Ejemplo n.º 3
0
def batch_process(image_paths,
                  edge_lengths,
                  bb_diameter=8,
                  penumbra=2,
                  display_figure=True):
    bb_centres = []
    field_centres = []
    field_rotations = []

    for image_path in image_paths:
        bb_centre, field_centre, field_rotation = iview_find_bb_and_field(
            image_path,
            edge_lengths,
            bb_diameter=bb_diameter,
            penumbra=penumbra,
            display_figure=display_figure,
        )

        bb_centres.append(bb_centre)
        field_centres.append(field_centre)
        field_rotations.append(field_rotation)

        if display_figure:
            plt.show()

    data = np.concatenate(
        [bb_centres, field_centres,
         np.array(field_rotations)[:, None]],
        axis=1)
    return pd.DataFrame(
        data=data, columns=["BB x", "BB y", "Field x", "Field y", "Rotation"])
Ejemplo n.º 4
0
def get_staff_name(cursor, staff_id):
    data = execute_sql(
        cursor,
        """
        SELECT
            Staff.Initials,
            Staff.User_Name,
            Staff.Type,
            Staff.Category,
            Staff.Last_Name,
            Staff.First_Name
        FROM Staff
        WHERE
            Staff.Staff_ID = %(staff_id)s
        """,
        {"staff_id": staff_id},
    )

    results = pd.DataFrame(
        data=data,
        columns=[
            "initials",
            "user_name",
            "type",
            "category",
            "last_name",
            "first_name",
        ],
    )

    return results
Ejemplo n.º 5
0
def get_treatments(cursor, start, end, machine):
    treatment_results = execute_sql(
        cursor,
        """
        SELECT
            Ident.IDA,
            Patient.Last_Name,
            Patient.First_Name,
            TxField.FLD_ID,
            TxField.Field_Label,
            TxField.Field_Name,
            TxField.Type_Enum,
            TxField.Meterset,
            TxField.Version,
            Tracktreatment.WasQAMode,
            Tracktreatment.WasBeamComplete,
            TrackTreatment.Create_DtTm,
            TrackTreatment.Edit_DtTm
        FROM TrackTreatment, Ident, Patient, TxField, Staff
        WHERE
            TrackTreatment.Pat_ID1 = Ident.Pat_ID1 AND
            Patient.Pat_ID1 = Ident.Pat_ID1 AND
            TrackTreatment.FLD_ID = TxField.FLD_ID AND
            Staff.Staff_ID = TrackTreatment.Machine_ID_Staff_ID AND
            REPLACE(Staff.Last_Name, ' ', '') = %(machine)s AND
            TrackTreatment.Edit_DtTm >= %(start)s AND
            TrackTreatment.Create_DtTm <= %(end)s
        """,
        {
            "machine": machine,
            "start": start,
            "end": end
        },
    )

    table = pd.DataFrame(
        data=treatment_results,
        columns=[
            "patient_id",
            "last_name",
            "first_name",
            "field_id",
            "field_label",
            "field_name",
            "field_type",
            "monitor_units",
            "field_version",
            "qa_mode",
            "completed",
            "start",
            "end",
        ],
    )

    table["field_type"] = [FIELD_TYPES[item] for item in table["field_type"]]

    table = table.sort_values("start")

    return table
Ejemplo n.º 6
0
 def __init__(self, ip):
     self.ip = ip
     self.port = 8123
     self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # UDP
     self.sock.settimeout(3)
     self.MSG = b""
     self.raw_MSG = ""
     self.measurements = pd.DataFrame()
     self.data = ""
     self.raw_data = ""
     self.connected = False
Ejemplo n.º 7
0
def get_incomplete_qcls_across_sites(cursors, servers, centres, locations):
    results = pd.DataFrame()

    for centre in centres:
        cursor = cursors[servers[centre]]

        incomplete_qcls = get_incomplete_qcls(cursor, locations[centre])
        incomplete_qcls["centre"] = [centre] * len(incomplete_qcls)

        results = results.append(incomplete_qcls)

    results = results.sort_values(by="due")

    return results
Ejemplo n.º 8
0
def get_qcls_by_date(cursor, location, start, end):
    data = execute_sql(
        cursor,
        """
        SELECT
            Ident.IDA,
            Patient.Last_Name,
            Patient.First_Name,
            Chklist.Due_DtTm,
            Chklist.Act_DtTm,
            Chklist.Instructions,
            Chklist.Notes,
            QCLTask.Description
        FROM Chklist, Staff, QCLTask, Ident, Patient
        WHERE
            Chklist.Pat_ID1 = Ident.Pat_ID1 AND
            Patient.Pat_ID1 = Ident.Pat_ID1 AND
            QCLTask.TSK_ID = Chklist.TSK_ID AND
            Staff.Staff_ID = Chklist.Rsp_Staff_ID AND
            Staff.Last_Name = %(location)s AND
            Chklist.Act_DtTm >= %(start)s AND
            Chklist.Act_DtTm < %(end)s
        """,
        {
            "location": location,
            "start": start,
            "end": end
        },
    )

    results = pd.DataFrame(
        data=data,
        columns=[
            "patient_id",
            "last_name",
            "first_name",
            "due",
            "actual_completed_time",
            "instructions",
            "comment",
            "task",
        ],
    )

    results = results.sort_values(by=["actual_completed_time"])

    return results
Ejemplo n.º 9
0
    def get_measurements(self):
        self.send_quickcheck("MEASCNT")
        if "MEASCNT" not in self.data:
            self.send_quickcheck("MEASCNT")
        m = self.parse_measurements()
        n_meas = m["MEASCNT"]
        print("Receiving Quickcheck measurements")
        meas_list = []
        for m in tqdm.tqdm(range(n_meas)):
            control = False
            while not control:
                self.send_quickcheck("MEASGET;INDEX-MEAS=" + "%d" % (m, ))
                control = self.raw_MSG in self.data

            meas = self.parse_measurements()
            meas_list.append(meas)
        self.measurements = pd.DataFrame(meas_list)
Ejemplo n.º 10
0
def get_treatment_times(cursor, field_id):
    treatment_time_results = execute_sql(
        cursor,
        """
        SELECT
            TrackTreatment.Create_DtTm,
            TrackTreatment.Edit_DtTm,
            Tracktreatment.WasBeamComplete,
            Tracktreatment.WasQAMode
        FROM TrackTreatment
        WHERE
            TrackTreatment.FLD_ID = %(field_id)s
        """,
        {"field_id": field_id},
    )

    return pd.DataFrame(data=treatment_time_results,
                        columns=["start", "end", "completed", "qa_mode"])
Ejemplo n.º 11
0
def get_patient_fields(cursor, patient_id):
    """Returns all of the patient fields for a given Patient ID.
    """
    patient_id = str(patient_id)

    patient_field_results = execute_sql(
        cursor,
        """
        SELECT
            TxField.FLD_ID,
            TxField.Field_Label,
            TxField.Field_Name,
            TxField.Version,
            TxField.Meterset,
            TxField.Type_Enum,
            Site.Site_Name
        FROM Ident, TxField, Site
        WHERE
            TxField.Pat_ID1 = Ident.Pat_ID1 AND
            TxField.SIT_Set_ID = Site.SIT_Set_ID AND
            Ident.IDA = %(patient_id)s
        """,
        {"patient_id": patient_id},
    )

    table = pd.DataFrame(
        data=patient_field_results,
        columns=[
            "field_id",
            "field_label",
            "field_name",
            "field_version",
            "monitor_units",
            "field_type",
            "site",
        ],
    )

    table.drop_duplicates(inplace=True)

    table["field_type"] = [FIELD_TYPES[item] for item in table["field_type"]]

    return table
Ejemplo n.º 12
0
def file_output(output_directory, distance, relative_dose, scan_curvetype,
                scan_depth):
    """Store the loaded mephysto data into csv files for easy user confirmation
    and use.
    """
    # Determines the filepaths for the output
    filepaths = determine_output_filepaths(output_directory, scan_curvetype,
                                           scan_depth)

    columns = ["distance (mm)", "relative dose"]

    # Loop over each curvetype and save the data to csv
    for i, _ in enumerate(scan_curvetype):
        # Stacks the data into one array and transposes into column orientation
        data = np.vstack([distance[i], relative_dose[i]]).T

        # Use pandas to save data to csv
        df = pd.DataFrame(data, columns=columns)
        df.to_csv(filepaths[i])
Ejemplo n.º 13
0
def get_incomplete_qcls(cursor, location):
    data = execute_sql(
        cursor,
        """
        SELECT
            Ident.IDA,
            Patient.Last_Name,
            Patient.First_Name,
            Chklist.Due_DtTm,
            Chklist.Instructions,
            Chklist.Notes,
            QCLTask.Description
        FROM Chklist, Staff, QCLTask, Ident, Patient
        WHERE
            Chklist.Pat_ID1 = Ident.Pat_ID1 AND
            Patient.Pat_ID1 = Ident.Pat_ID1 AND
            QCLTask.TSK_ID = Chklist.TSK_ID AND
            Staff.Staff_ID = Chklist.Rsp_Staff_ID AND
            Staff.Last_Name = %(location)s AND
            Chklist.Complete = 0
        """,
        {"location": location},
    )

    results = pd.DataFrame(
        data=data,
        columns=[
            "patient_id",
            "last_name",
            "first_name",
            "due",
            "instructions",
            "comment",
            "task",
        ],
    )

    results = results.sort_values(by=["due"], ascending=False)

    return results
Ejemplo n.º 14
0
def get_patient_name(cursor, patient_id):
    patient_id = str(patient_id)

    patient_name_results = execute_sql(
        cursor,
        """
        SELECT
            Patient.Last_Name,
            Patient.First_Name
        FROM Ident, Patient
        WHERE
            Patient.Pat_ID1 = Ident.Pat_ID1 AND
            Ident.IDA = %(patient_id)s
        """,
        {"patient_id": patient_id},
    )

    table = pd.DataFrame(data=patient_name_results,
                         columns=["last_name", "first_name"])
    table.drop_duplicates(inplace=True)

    if len(table.index) < 1:
        raise ValueError("No patient found with that ID")

    if len(table.index) > 1:
        raise ValueError("Multiple patients were found with that ID")

    series = table.iloc[0]

    last_name = series["last_name"]
    first_name = series["first_name"]

    patient_name = pymedphys._utilities.patient.convert_patient_name_from_split(  # pylint: disable = protected-access
        last_name, first_name)

    return patient_name
Ejemplo n.º 15
0
def create_dataframe(data, column_names, time_increment):
    """Converts the provided data into a pandas dataframe."""
    dataframe = pd.DataFrame(data=data, columns=column_names)
    dataframe.index = np.round(dataframe.index * time_increment, 2)

    return dataframe
Ejemplo n.º 16
0
def get_all_treatment_data(cursor, mrn):

    dataframe_column_to_sql_reference = collections.OrderedDict([
        ("mrn", "Ident.IDA"),
        ("first_name", "Patient.First_Name"),
        ("last_name", "Patient.Last_Name"),
        ("dob", "Patient.Birth_DtTm"),
        ("machine", "Staff.Last_Name"),
        ("field_id", "TxField.FLD_ID"),
        ("field_label", "TxField.Field_Label"),
        ("field_name", "TxField.Field_Name"),
        ("target", "Site.Target"),
        ("rx_depth", "Site.Rx_Depth"),
        ("target_units", "Site.Target_Units"),
        ("technique", "Site.Technique"),
        ("modality", "Site.Modality"),
        ("energy [MV]", "TxFieldPoint.Energy"),
        ("fraction_dose [cGy]", "Site.Dose_Tx"),
        ("total_dose [cGy]", "Site.Dose_Ttl"),
        ("fractions", "Site.Fractions"),
        ("fraction_pattern", "Site.Frac_Pattern"),
        ("notes", "Site.Notes"),
        ("field_version", "TxField.Version"),
        ("monitor_units", "TxField.Meterset"),
        ("meterset_rate", "TxFieldPoint.Meterset_Rate"),
        ("field_type", "TxField.Type_Enum"),
        ("gantry_angle", "TxFieldPoint.Gantry_Ang"),
        ("collimator_angle", "TxFieldPoint.Coll_Ang"),
        ("ssd [cm]", "TxField.Ssd"),
        ("sad [cm]", "TxField.SAD"),
        ("site", "Site.Site_Name"),
        ("dyn_wedge", "TxField.Dyn_Wedge"),
        ("wdg_appl", "TxField.Wdg_Appl"),
        ("block", "TxField.Block"),
        ("blk_desc", "TxField.Blk_Desc"),
        ("comp_fda", "TxField.Comp_Fda"),
        ("fda_desc", "TxField.FDA_Desc"),
        ("bolus", "TxField.Bolus"),
        ("iso_x [cm]", "SiteSetup.Isocenter_Position_X"),
        ("iso_y [cm]", "SiteSetup.Isocenter_Position_Y"),
        ("iso_z [cm]", "SiteSetup.Isocenter_Position_Z"),
        ("position", "SiteSetup.Patient_Orient"),
        ("field_x [cm]", "TxFieldPoint.Field_X"),
        ("coll_x1 [cm]", "TxFieldPoint.Coll_X1"),
        ("coll_x2 [cm]", "TxFieldPoint.Coll_X2"),
        ("field_y [cm]", "TxFieldPoint.Field_Y"),
        ("coll_y1 [cm]", "TxFieldPoint.Coll_Y1"),
        ("coll_y2 [cm]", "TxFieldPoint.Coll_Y2"),
        ("couch_vrt [cm]", "TxFieldPoint.Couch_Vrt"),
        ("couch_lat [cm]", "TxFieldPoint.Couch_Lat"),
        ("couch_lng [cm]", "TxFieldPoint.Couch_Lng"),
        ("couch_ang", "TxFieldPoint.Couch_Ang"),
        ("tolerance", "TxField.Tol_Tbl_ID"),
        ("time", "TxField.BackupTimer"),
        ("site_setup_status", "SiteSetup.Status_Enum"),
        ("site_status", "Site.Status_Enum"),
        ("hidden", "TxField.IsHidden"),
        ("site version", "Site.Version"),
        ("create_id", "Site.Create_ID"),
        ("field_approval", "TxField.Sanct_ID"),
    ])

    columns = list(dataframe_column_to_sql_reference.keys())
    select_string = "SELECT " + ",\n\t\t    ".join(
        dataframe_column_to_sql_reference.values())

    sql_string = (select_string + """
                FROM Ident, TxField, Site, Patient, SiteSetup, TxFieldPoint, Staff
                WHERE
                    TxField.Pat_ID1 = Ident.Pat_ID1 AND
                    TxField.Machine_ID_Staff_ID = Staff.Staff_ID AND
                    TxFieldPoint.FLD_ID = TxField.FLD_ID AND
                    TxFieldPoint.Point = 0 AND
                    Patient.Pat_ID1 = Ident.Pat_ID1 AND
                    SiteSetup.SIT_Set_ID = TxField.SIT_Set_ID AND
                    TxField.SIT_Set_ID = Site.SIT_Set_ID AND
                    Site.Version = 0 AND
                    SiteSetup.Version = 0 AND
                    Ident.IDA = %(patient_id)s
                """)

    table = execute_sql(cursor=cursor,
                        sql_string=sql_string,
                        parameters={"patient_id": mrn})

    mosaiq_fields = pd.DataFrame(data=table, columns=columns)

    mosaiq_fields.drop_duplicates(inplace=True)
    mosaiq_fields["field_type"] = [
        FIELD_TYPES[item] for item in mosaiq_fields["field_type"]
    ]

    mosaiq_fields["position"] = [
        ORIENTATION[item] for item in mosaiq_fields["position"]
    ]

    # reformat some fields to create the 'rx' field
    rx = []
    for i in mosaiq_fields.index:
        rx.append(
            str(mosaiq_fields["target"][i] + " " +
                str(mosaiq_fields["rx_depth"][i]) +
                str(mosaiq_fields["target_units"][i])) +
            str(mosaiq_fields["modality"][i]))
    mosaiq_fields["rx"] = rx

    return mosaiq_fields
Ejemplo n.º 17
0
def get_all_treatment_data(cursor, mrn):
    table = execute_sql(
        cursor,
        """
        SELECT
            Ident.IDA,
            Patient.First_Name,
            Patient.Last_Name,
            Patient.Birth_DtTm,
            Staff.Last_Name,
            TxField.FLD_ID,
            TxField.Field_Label,
            TxField.Field_Name,
            Site.Target,
            Site.Rx_Depth,
            Site.Target_Units,
            Site.Technique,
            Site.Modality,
            TxFieldPoint.Energy,
            Site.Dose_Tx,
            Site.Dose_Ttl,
            Site.Fractions,
            Site.Notes,
            TxField.Version,
            TxField.Meterset,
            TxFieldPoint.Meterset_Rate,
            TxField.Type_Enum,
            TxFieldPoint.Gantry_Ang,
            TxFieldPoint.Coll_Ang,
            TxField.Ssd,
            TxField.SAD,
            Site.Site_Name,
            TxField.Dyn_Wedge,
            TxField.Wdg_Appl,
            TxField.Block,
            TxField.Blk_Desc,
            TxField.Comp_Fda,
            TxField.FDA_Desc,
            TxField.Bolus,
            SiteSetup.Isocenter_Position_X,
            SiteSetup.Isocenter_Position_Y,
            SiteSetup.Isocenter_Position_Z,
            TxFieldPoint.Field_X,
            TxFieldPoint.Coll_X1,
            TxFieldPoint.Coll_X2,
            TxFieldPoint.Field_Y,
            TxFieldPoint.Coll_Y1,
            TxFieldPoint.Coll_Y2,
            TxFieldPoint.Couch_Vrt,
            TxFieldPoint.Couch_Lat,
            TxFieldPoint.Couch_Lng,
            TxFieldPoint.Couch_Ang,
            TxField.Tol_Tbl_ID,
            TxField.BackupTimer

        FROM Ident, TxField, Site, Patient, SiteSetup, TxFieldPoint, Staff
        WHERE
            TxField.Pat_ID1 = Ident.Pat_ID1 AND
            TxField.Machine_ID_Staff_ID = Staff.Staff_ID AND
            TxFieldPoint.FLD_ID = TxField.FLD_ID AND
            TxFieldPoint.Point = 0 AND
            Patient.Pat_ID1 = Ident.Pat_ID1 AND
            SiteSetup.SIT_Set_ID = TxField.SIT_Set_ID AND
            TxField.SIT_Set_ID = Site.SIT_Set_ID AND
            Ident.IDA = %(patient_id)s
        """,
        {"patient_id": mrn},
    )

    mosaiq_fields = pd.DataFrame(
        data=table,
        columns=[
            "mrn",
            "first_name",
            "last_name",
            "dob",
            "machine",
            "field_id",
            "field_label",
            "field_name",
            "target",
            "rx_depth",
            "target_units",
            "technique",
            "modality",
            "energy",
            "fraction_dose",
            "total_dose",
            "fractions",
            "notes",
            "field_version",
            "monitor_units",
            "meterset_rate",
            "field_type",
            "gantry_angle",
            "collimator_angle",
            "ssd",
            "sad",
            "site",
            "dyn_wedge",
            "wdg_appl",
            "block",
            "blk_desc",
            "comp_fda",
            "fda_desc",
            "bolus",
            "iso_x",
            "iso_y",
            "iso_z",
            "field_x",
            "coll_x1",
            "coll_x2",
            "field_y",
            "coll_y1",
            "coll_y2",
            "couch_vrt",
            "couch_lat",
            "couch_lng",
            "couch_ang",
            "tolerance",
            "time",
        ],
    )

    mosaiq_fields.drop_duplicates(inplace=True)
    mosaiq_fields["field_type"] = [
        FIELD_TYPES[item] for item in mosaiq_fields["field_type"]
    ]

    return mosaiq_fields
Ejemplo n.º 18
0
def header_as_dataframe(trf_header_contents):
    header = decode_header(trf_header_contents)

    return pd.DataFrame([header], columns=Header._fields)