def populateIdxMap(self, slacNum, kind=None, cavNum=None):
        # type: (int, str, int) -> None

        if kind == "q0":
            if slacNum not in self.q0IdxMap:
                sessionCSV = self.genSessionFile(slacNum, kind, cavNum)
                if not isfile(sessionCSV):
                    compatibleMkdirs(sessionCSV)
                    with open(sessionCSV, "w+") as f:
                        headerWriter = writer(f)
                        header = self.q0Header
                        headerWriter.writerow(header)
                        self.populate(f, slacNum, header=header, kind=kind)

                with open(sessionCSV) as csvFile:
                    self.populate(csvFile, slacNum, kind=kind)
        else:
            if slacNum not in self.idxMap:
                sessionCSV = self.genSessionFile(slacNum, kind, cavNum)
                if not isfile(sessionCSV):
                    compatibleMkdirs(sessionCSV)
                    with open(sessionCSV, "w+") as f:
                        headerWriter = writer(f)
                        header = self.calibHeader
                        headerWriter.writerow(header)
                        self.populate(f, slacNum, header=header, kind=kind)

                with open(sessionCSV) as csvFile:
                    self.populate(csvFile, slacNum, kind=kind)
 def saveFigs(self, cryoModule):
     lastFigNum = len(plt.get_fignums()) + 1
     for i in range(self.figStartIdx, lastFigNum):
         plt.figure(i)
         figFile = ("figures/cm{NUM}/{CM}_{FIG}.png".format(
             NUM=cryoModule.cryModNumSLAC, CM=cryoModule.name, FIG=i))
         compatibleMkdirs(figFile)
         plt.savefig(figFile)
     self.figStartIdx = lastFigNum
Exemple #3
0
    def generateDataFile(self) -> Optional[Path]:

        print(self.filePath)

        if self.filePath.is_file():
            return self.filePath

        pvs = self.cryomodulePVs.asList()

        try:
            data = getArchiverData(self.timeParams.startTime, self.numPoints,
                                   pvs, self.timeParams.timeInterval)

        except TypeError:
            raise AssertionError("Data not retrieved from Archiver")

        # We're collapsing the readback for each cavity's desired and actual
        # electric heat load into two sum columns (instead of 16 individual
        # columns)
        compatibleMkdirs(self.filePath)

        # itemgetter searches the data.values dictionary for entries that match
        # keys in cryomodulePVs.heaterDesPVs and returns the values as a tuple
        # of lists. Then we call zip to collapse those into a list of tuples,
        # after which we just sum up all the values in each tuple so that we
        # end up with a list of ints
        heatLoadDes = [
            sum(x) for x in zip(*itemgetter(
                *self.cryomodulePVs.heaterDesPVs)(data.values))
        ]
        heatLoadAct = [
            sum(x) for x in zip(*itemgetter(
                *self.cryomodulePVs.heaterActPVs)(data.values))
        ]
        gradient = ([
            sum(x)
            for x in zip(*itemgetter(*self.cryomodulePVs.gradPVs)(data.values))
        ] if self.cryomodulePVs.gradPVs else None)

        jsonData = dumps({
            "Total Heater Setpoint": heatLoadDes,
            "Total Heater Readback": heatLoadAct,
            "Whole Module Gradient": gradient,
            "Timestamps": data.timeStamps
        }.update(data.values))

        # open file for writing, "w"
        f = open(self.filePath, "w")

        # write json object to file
        f.write(jsonData)

        # close file
        f.close()

        return self.filePath
    def getRowsAndFileReader(self, slacNum, kind=None, cavNum=None):
        # type: (int, str, int) -> Tuple

        sessionCSV = self.genSessionFile(slacNum, kind, cavNum)
        rows = open(compatibleMkdirs(sessionCSV)).readlines()
        # Reversing to get in chronological order (the program appends the most
        # recent sessions to the end of the file)
        rows.reverse()
        compatibleNext(reader([rows.pop()]))
        fileReader = reader(rows)
        return fileReader, rows
    def populateIdxMap(self, slacNum):
        # type: (int) -> None
        def populate(fileObj, header=None):
            if not header:
                csvReader = reader(fileObj)
                header = compatibleNext(csvReader)
            indices = {}
            for key, column in self.idxKeys:
                indices[key] = header.index(column)
            self.idxMap[slacNum] = indices

        if slacNum not in self.idxMap:
            sessionCSV = self.fileFormatter.format(CM_SLAC=slacNum)
            if not isfile(sessionCSV):
                compatibleMkdirs(sessionCSV)
                with open(sessionCSV, "w+") as f:
                    headerWriter = writer(f)
                    headerWriter.writerow(self.header)
                    populate(f, self.header)

            with open(sessionCSV) as csvFile:
                populate(csvFile)
    def parse(self):
        for row in self.csvReader:
            slacNum = int(row[self.slacNumIdx])

            calibSess, desiredGradients = self.getCalibAndDesGrads(
                row, slacNum)

            cryoModule = self.cryoModules[slacNum]

            calibSess.printSessionReport()

            calibCutoffs = [
                str(run.diagnostics["Cutoff"]) for run in calibSess.dataRuns
            ]

            if desiredGradients:

                options = {
                    1: "multi-cavity Q0 measurement",
                    2: "single-cavity Q0 measurements"
                }

                prompt = "Please select a type of measurement for row: {ROW}".format(
                    ROW=row)
                printOptions(options)

                selectedType = getNumInputFromLst(prompt, options.keys(), int,
                                                  True)

                if selectedType == 2:
                    for cavNum, gradDes in desiredGradients.items():
                        if not gradDes:
                            continue

                        fname = (
                            "results/cm{CM}/cav{CAV}/resultsCM{CM}CAV{CAV}.csv"
                            .format(CM=slacNum, CAV=cavNum))

                        if not isfile(fname):
                            compatibleMkdirs(fname)
                            with open(fname, "w+") as f:
                                csvWriter = writer(f, delimiter=',')
                                csvWriter.writerow([
                                    "Cavity", "Gradient", "Q0", "Calibration",
                                    "Q0 Measurement", "Calibration Cutoffs",
                                    "Q0 Cutoffs"
                                ])

                        print("\n---- Cavity {CAV} @ {GRAD} MV/m ----".format(
                            CM=slacNum, CAV=cavNum, GRAD=gradDes))

                        cavity = self.cryoModules[slacNum].cavities[cavNum]

                        q0Sess = self.genQ0Session(desiredGradient=gradDes,
                                                   slacNum=slacNum,
                                                   cavity=cavity,
                                                   calibSession=calibSess)

                        q0s = [
                            q0Sess.dataRuns[runIdx].q0
                            for runIdx in q0Sess.rfRunIdxs
                        ]
                        q0Cutoffs = [
                            str(run.diagnostics["Cutoff"])
                            for run in q0Sess.dataRuns
                        ]

                        with open(fname, "a") as f:
                            csvWriter = writer(f, delimiter=',')
                            csvWriter.writerow([
                                cavity.cavNum, gradDes,
                                '{:.2e}'.format(Decimal(mean(q0s))),
                                str(calibSess),
                                str(q0Sess), " | ".join(calibCutoffs),
                                " | ".join(q0Cutoffs)
                            ])
                else:
                    # desiredGradients = {}
                    # for _, cavity in cryoModule.cavities.items():
                    #     cavGradIdx = self.header.index("Cavity {NUM} Gradient"
                    #                                    .format(NUM=cavity.cavNum))
                    #     try:
                    #         gradDes = float(row[cavGradIdx])
                    #     except ValueError:
                    #         continue
                    #
                    #     desiredGradients[cavity.cavNum] = gradDes

                    fname = "results/cm{CM}/resultsCM{CM}.csv".format(
                        CM=slacNum)

                    if not isfile(fname):
                        compatibleMkdirs(fname)
                        with open(fname, "w+") as f:
                            csvWriter = writer(f, delimiter=',')
                            csvWriter.writerow([
                                "Cavity 1 Gradient", "Cavity 2 Gradient",
                                "Cavity 3 Gradient", "Cavity 4 Gradient",
                                "Cavity 5 Gradient", "Cavity 6 Gradient",
                                "Cavity 7 Gradient", "Cavity 8 Gradient",
                                "Cumulative Gradient", "Q0", "Calibration",
                                "Q0 Measurement", "Calibration Cutoffs",
                                "Q0 Cutoffs"
                            ])

                    q0Sess = self.genMultiQ0Session(
                        desiredGradients=desiredGradients,
                        slacNum=slacNum,
                        cryomodule=cryoModule,
                        calibSession=calibSess)

                    q0s = [
                        q0Sess.dataRuns[runIdx].q0
                        for runIdx in q0Sess.rfRunIdxs
                    ]
                    q0Cutoffs = [
                        str(run.diagnostics["Cutoff"])
                        for run in q0Sess.dataRuns
                    ]

                    with open(fname, "a") as f:
                        csvWriter = writer(f, delimiter=',')

                        # TODO fix this...
                        csvWriter.writerow([
                            slacNum, desiredGradients,
                            '{:.2e}'.format(Decimal(mean(q0s))),
                            str(calibSess),
                            str(q0Sess), " | ".join(calibCutoffs),
                            " | ".join(q0Cutoffs)
                        ])
            self.saveFigs(cryoModule)

        drawAndShow()
    def parse(self):
        for row in self.csvReader:
            slacNum = int(row[self.slacNumIdx])

            if slacNum not in self.cryoModules:
                calibSess = self.cryModManager.addNewCryMod(slacNum=slacNum)

            else:
                options = {}
                idx = 1
                idx2session = {}

                # Multiple rows in the input file may have the same SLAC
                # cryomodule number. However, the user might want to use
                # different calibrations. This is where we give the user the
                # option to reuse a calibration we've already loaded up and
                # processed.
                dataSessions = self.cryoModules[slacNum].dataSessions
                for _, dataSession in dataSessions.items():
                    options[idx] = str(dataSession)
                    idx2session[idx] = dataSession
                    idx += 1

                options[idx] = "Use a different calibration"
                printOptions(options)

                prompt = ("Please select a calibration option"
                          " (hit enter for option 1): ")
                selection = getNumInputFromLst(prompt, options.keys(), int,
                                               True)

                reuseCalibration = (selection != max(options))

                if not reuseCalibration:
                    cryMod = self.cryoModules[slacNum]
                    calibSess = self.cryModManager.addToCryMod(slacNum=slacNum,
                                                               cryMod=cryMod)

                else:
                    calibSess = idx2session[selection]

            cryoModule = self.cryoModules[slacNum]

            calibSess.printSessionReport()

            calibCutoffs = [
                str(run.diagnostics["Cutoff"]) for run in calibSess.dataRuns
            ]

            fname = "results/cm{CM}.csv".format(CM=slacNum)

            if not isfile(fname):
                compatibleMkdirs(fname)
                with open(fname, "w+") as f:
                    csvWriter = writer(f, delimiter=',')
                    csvWriter.writerow([
                        "Cavity", "Gradient", "Q0", "Calibration",
                        "Q0 Measurement", "Calibration Cutoffs", "Q0 Cutoffs"
                    ])

            for _, cavity in cryoModule.cavities.items():
                cavGradIdx = self.header.index(
                    "Cavity {NUM} Gradient".format(NUM=cavity.cavNum))

                try:
                    gradDes = float(row[cavGradIdx])
                except ValueError:
                    continue

                print("\n---- Cavity {CAV} @ {GRAD} MV/m ----".format(
                    CM=slacNum, CAV=cavity.cavNum, GRAD=gradDes))

                q0Sess = self.genQ0Session(refGradVal=gradDes,
                                           slacNum=slacNum,
                                           cavity=cavity,
                                           calibSession=calibSess)

                q0s = [
                    q0Sess.dataRuns[runIdx].q0 for runIdx in q0Sess.rfRunIdxs
                ]
                q0Cutoffs = [
                    str(run.diagnostics["Cutoff"]) for run in q0Sess.dataRuns
                ]

                with open(fname, "a") as f:
                    csvWriter = writer(f, delimiter=',')
                    csvWriter.writerow([
                        cavity.cavNum, gradDes,
                        '{:.2e}'.format(Decimal(mean(q0s))),
                        str(calibSess),
                        str(q0Sess), " | ".join(calibCutoffs),
                        " | ".join(q0Cutoffs)
                    ])

            self.saveFigs(cryoModule)

        drawAndShow()