Ejemplo n.º 1
0
def readCT(planTrialFile):
    """
    Read a CT cube for a plan
    """
    fp1 = open(os.path.join(os.path.dirname(planTrialFile), 'plan.defaults'))
    imFile = fp1.readline().split(':')[1].strip()
    fp1.close()

    imFile = os.path.join(os.path.dirname(planTrialFile), imFile)
    imHdr = pinn.read(imFile + '.header')

    # Read the data from the file
    imData = np.fromfile(imFile + '.img', dtype='int16')

    # Reshape to a 3D array
    imData = imData.reshape((imHdr.z_dim, imHdr.y_dim, imHdr.x_dim))

    # Solaris uses big endian schema. Almost everything else is little endian
    if sys.byteorder == 'little':
        if imHdr.byte_order == 1:
            imData = imData.byteswap(True)
    else:
        if imHdr.byte_order == 0:
            imData = imData.byteswap(True)

    return imData, imHdr
Ejemplo n.º 2
0
def readPlanTrial(patientPlanDir):
    """
	Read all the information out of a plan.Trial file for a given patient plan directory.
	"""
    planTrialFile = os.path.join(patientPlanDir, 'plan.Trial')
    planTrial = pinn.read(planTrialFile)
    return planTrial
Ejemplo n.º 3
0
def readDose(planTrialFile, trNum, chooseBmInd=-1):
    """
    Read a dose cube for a trial in a given plan and return as a numpy array

    Need to test reading dose for a variety of different prescriptions

    Currently tested for:
            Dose is prescribed to a norm point,
                    beam weights are proportional to point dose
                    and control point dose is not stored.
    """

    pln1 = pinn.read(os.path.join(
        os.path.dirname(planTrialFile), 'plan.Trial'))
    pts = pinn.read(os.path.join(
        os.path.dirname(planTrialFile), 'plan.Points'))

    nTrials = 1
    if pln1.has_key('TrialList'):
        nTrials = len(pln1.TrialList)

    assert(trNum >= 0)
    assert(trNum < nTrials)

    if pln1.has_key('TrialList'):
        curTr = pln1.TrialList[trNum]
    else:
        curTr = pln1.Trial

    doseHdr = curTr.DoseGrid

    dose = np.zeros(
        (doseHdr.Dimension.Z, doseHdr.Dimension.Y, doseHdr.Dimension.X))

    prescriptionPoint = []
    prescriptionDose = []
    prescriptionPointDose = []
    prescriptionPointDoseFactor = []

    for bInd, bm in enumerate(curTr.BeamList):
        if chooseBmInd >= 0 and bInd != chooseBmInd:
            continue
        try:
            # Get the name of the file where the beam dose is saved - PREVIOUSLY USED DoseVarVolume ?
            doseFile = os.path.join(os.path.dirname(planTrialFile),
                                    "plan.Trial.binary.%03d" % int(bm.DoseVolume.split('-')[1]))

            # Read the dose from the file
            bmDose = np.fromfile(doseFile, dtype='float32')

            if bmDose.nbytes == 0:
                raise DoseInvalidException('')
        except:
            raise DoseInvalidException('Beam %d in trial %d has no stored dose. Try other trial [0-%d]'
                                       % (bInd, trNum, nTrials - 1))

        # Reshape to a 3D array
        bmDose = bmDose.reshape(
            (doseHdr.Dimension.Z, doseHdr.Dimension.Y, doseHdr.Dimension.X))

        # Solaris uses big endian schema. Almost everything else is little endian
        if sys.byteorder == 'little':
            bmDose = bmDose.byteswap(True)

        doseFactor = 1.0

        # Weight the dose cube by the beam weight
        # Assume dose is prescribed to a norm point and beam weights are proportional to point dose
        doseAtPoint = 0.0

        for pp in curTr.PrescriptionList:
            if pp.Name == bm.PrescriptionName:
                prescriptionDose.append(
                    pp.PrescriptionDose * pp.NumberOfFractions)
                if pp.WeightsProportionalTo == 'Point Dose':
                    for pt in pts.PoiList:
                        if pt.Name == pp.PrescriptionPoint:
                            doseAtPoint = doseAtCoord(
                                bmDose, doseHdr, pt.XCoord, pt.YCoord, pt.ZCoord)
                            doseFactor = pp.PrescriptionDose * \
                                pp.NumberOfFractions * \
                                (bm.Weight * 0.01 / doseAtPoint)

                            prescriptionPoint.append(
                                [pt.XCoord, pt.YCoord, pt.ZCoord])
                            prescriptionPointDose.append(doseAtPoint)
                            prescriptionPointDoseFactor.append(doseFactor)

        dose += (bmDose * doseFactor)

    for bm, pD, pp in zip(range(len(prescriptionPointDose)), prescriptionPointDose, prescriptionPoint):
        indPP = coordToIndex(doseHdr, pp[0], pp[1], pp[2])

    return dose, doseHdr