Example #1
0
File: fit.py Project: op3/hdtv
 def Refresh(self):
     """
     Refresh
     """
     if self.spec is None:
         return
     # repeat the fits
     if self.dispPeakFunc:
         # this includes the background fit
         self.FitPeakFunc(self.spec)
     elif self.dispBgFunc:
         # maybe there was only a background fit
         self.FitBgFunc(self.spec)
     if not self.viewport:
         return
     with LockViewport(self.viewport):
         # draw fit func, if available
         if self.dispPeakFunc:
             self.dispPeakFunc.Draw(self.viewport)
         if self.dispBgFunc:
             self.dispBgFunc.Draw(self.viewport)
         # draw peaks
         for peak in self.peaks:
             peak.Draw(self.viewport)
         # draw the markers (do this after the fit,
         # because the fit updates the position of the peak markers)
         self.peakMarkers.Refresh()
         self.regionMarkers.Refresh()
         self.bgMarkers.Refresh()
         self.Show()
Example #2
0
File: marker.py Project: op3/hdtv
 def Clear(self):
     """
     Remove all markers from this collection
     """
     with LockViewport(self.viewport):
         while self:
             self.pop()
Example #3
0
 def Hide(self):
     if self.viewport is None:
         return
     with LockViewport(self.viewport):
         DrawableManager.Hide(self)
         if self.hist:
             self.hist.Hide()
Example #4
0
    def Draw(self, viewport):
        """
        Draw this spectrum to the viewport
        """

        if self.viewport is not None and not self.viewport == viewport:
            # Unlike the DisplaySpec object of the underlying implementation,
            # Spectrum() objects can only be drawn on a single viewport
            raise RuntimeError(
                "Spectrum can only be drawn on a single viewport")
        self.viewport = viewport
        # Lock updates
        with LockViewport(self.viewport):
            # Show spectrum
            if self.displayObj is None and self._hist is not None:
                if self.active:
                    color = self._activeColor
                else:
                    color = self._passiveColor
                self.displayObj = ROOT.HDTV.Display.DisplaySpec(
                    self._hist, color)
                self.displayObj.SetNorm(self.norm)
                self.displayObj.Draw(self.viewport)
                # add calibration
                if self.cal:
                    self.displayObj.SetCal(self.cal)
                # and ID
                if self.ID is not None:
                    ID = str(self.ID).strip(".")
                    self.displayObj.SetID(ID)
Example #5
0
File: fit.py Project: op3/hdtv
 def ShowAsPassive(self):
     if not self.viewport:
         return
     with LockViewport(self.viewport):
         # marker in passive State and not dashed
         for mc in [self.regionMarkers, self.peakMarkers, self.bgMarkers]:
             mc.active = False
             mc.dashed = False
         # only show peakMarkers
         self.regionMarkers.Hide()
         self.peakMarkers.Show()
         self.bgMarkers.Hide()
         # coloring
         if self.dispPeakFunc:
             self.dispPeakFunc.SetColor(self.color)
             self.dispPeakFunc.Show()
         if self.dispBgFunc:
             self.dispBgFunc.SetColor(self.color)
             self.dispBgFunc.Show()
         # peak list
         for peak in self.peaks:
             peak.color = self.color
             if self._showDecomp:
                 peak.Show()
             else:
                 peak.Hide()
Example #6
0
File: fit.py Project: op3/hdtv
 def Draw(self, viewport):
     """
     Draw
     """
     if self.viewport and not self.viewport == viewport:
         # Unlike the Display object of the underlying implementation,
         # python objects can only be drawn on a single viewport
         raise RuntimeError("Object can only be drawn on a single viewport")
     self.viewport = viewport
     with LockViewport(self.viewport):
         # draw the markers (do this after the fit,
         # because the fit updates the position of the peak markers)
         self.peakMarkers.Draw(self.viewport)
         self.regionMarkers.Draw(self.viewport)
         self.bgMarkers.Draw(self.viewport)
         # draw fit func, if available
         if self.dispPeakFunc:
             self.dispPeakFunc.Draw(self.viewport)
         if self.dispBgFunc:
             self.dispBgFunc.Draw(self.viewport)
         # draw peaks
         for peak in self.peaks:
             peak.color = self.color
             peak.Draw(self.viewport)
         self.Show()
Example #7
0
 def Refresh(self):
     if self.viewport is None:
         return
     with LockViewport(self.viewport):
         DrawableManager.Refresh(self)
         if self.hist:
             self.hist.Refresh()
Example #8
0
 def Show(self):
     if self.viewport is None:
         return
     with LockViewport(self.viewport):
         DrawableManager.Show(self)
         if self.hist:
             self.hist.Show()
Example #9
0
File: drawable.py Project: op3/hdtv
    def ShowObjects(self, ids, clear=True):
        """
        Show objects on the display.

        If the clear parameter is True, the display is cleared first.
        Otherwise the objects are shown in addition to the ones, that
        are already visible.
        """
        if self.viewport is None:
            return
        with LockViewport(self.viewport):
            # check if just single id
            try:
                iter(ids)
            except BaseException:
                ids = [ids]
            if clear:
                # hide all other objects except in ids
                # do not use HideAll, because if the active objects is among
                # the objects that should be shown, its state would be lost
                others = set(self.dict.keys()) - set(ids)
                self.HideObjects(others)
            for ID in ids:
                try:
                    self.dict[ID].Show()
                    self.visible.add(ID)
                except KeyError:
                    hdtv.ui.warning("ID %s not found" % ID)
            return ids
Example #10
0
 def _set_color(self, color):
     # we only need the passive color for fits
     self._passiveColor = hdtv.color.Highlight(color, active=False)
     with LockViewport(self.viewport):
         self.regionMarkers.color = color
         self.bgMarkers.color = color
         if hasattr(self, "spec") and self.spec is not None:
             self.spec.color = color
Example #11
0
File: fit.py Project: op3/hdtv
 def _set_active(self, state):
     with LockViewport(self.viewport):
         self._active = state
         self.peakMarkers.active = state
         self.regionMarkers.active = state
         self.bgMarkers.active = state
         for peak in self.peaks:
             peak.active = state
         self.Show()
Example #12
0
File: fit.py Project: op3/hdtv
 def _set_color(self, color):
     # we only need the passive color for fits
     self._passiveColor = hdtv.color.Highlight(color, active=False)
     with LockViewport(self.viewport):
         self.peakMarkers.color = color
         self.regionMarkers.color = color
         self.bgMarkers.color = color
         for peak in self.peaks:
             peak.color = color
         self.Show()
Example #13
0
File: fit.py Project: op3/hdtv
 def _set_cal(self, cal):
     self._cal = hdtv.cal.MakeCalibration(cal)
     with LockViewport(self.viewport):
         self.peakMarkers.cal = self._cal
         self.regionMarkers.cal = self._cal
         self.bgMarkers.cal = self._cal
         if self.dispPeakFunc:
             self.dispPeakFunc.SetCal(self._cal)
         if self.dispBgFunc:
             self.dispBgFunc.SetCal(self._cal)
         for peak in self.peaks:
             peak.cal = self._cal
Example #14
0
File: fit.py Project: op3/hdtv
 def Hide(self):
     if not self.viewport:
         return
     with LockViewport(self.viewport):
         self.peakMarkers.Hide()
         self.regionMarkers.Hide()
         self.bgMarkers.Hide()
         if self.dispPeakFunc:
             self.dispPeakFunc.Hide()
         if self.dispBgFunc:
             self.dispBgFunc.Hide()
         for peak in self.peaks:
             peak.Hide()
Example #15
0
File: drawable.py Project: op3/hdtv
 def Draw(self, viewport):
     """
     Draw function (sets the viewport and draws all components)
     """
     if not self.viewport is None and not self.viewport == viewport:
         # Unlike the Display object of the underlying implementation,
         # python objects can only be drawn on a single viewport
         raise RuntimeError("Object can only be drawn on a single viewport")
     self.viewport = viewport
     with LockViewport(self.viewport):
         for ID in self.dict.keys():
             self.dict[ID].Draw(self.viewport)
             self.visible.add(ID)
Example #16
0
File: drawable.py Project: op3/hdtv
 def RefreshObjects(self, ids):
     """
     Refresh objects with ids
     """
     with LockViewport(self.viewport):
         try:
             iter(ids)
         except BaseException:
             ids = [ids]
         for ID in ids:
             try:
                 self.dict[ID].Refresh()
             except KeyError:
                 hdtv.ui.warning("ID %d not found" % ID)
         return ids
Example #17
0
File: drawable.py Project: op3/hdtv
 def HideObjects(self, ids):
     """
     Hide objects
     """
     if self.viewport is None:
         return
     with LockViewport(self.viewport):
         # check if just single id
         try:
             iter(ids)
         except BaseException:
             ids = [ids]
         for ID in ids:
             try:
                 self.dict[ID].Hide()
                 self.visible.discard(ID)
             except KeyError:
                 hdtv.ui.warning("ID %d not found" % ID)
         return ids
Example #18
0
File: drawable.py Project: op3/hdtv
 def ActivateObject(self, ID=None):
     """
     Activates the object with ID
     """
     if ID is not None and ID not in self.ids:
         raise KeyError
     with LockViewport(self.viewport):
         # change state of former active object
         if self.activeID is not None:
             self.GetActiveObject().active = False
         # activate new object
         self.activeID = ID
         if self.activeID is not None:
             # reset iterator
             self._iteratorID = self.activeID
             # change state of object
             self.GetActiveObject().active = self.active
             # call ShowObject, to make sure the new active object is visible
             self.ShowObjects(ID, clear=False)
Example #19
0
File: fit.py Project: op3/hdtv
 def ShowAsWorkFit(self):
     if not self.viewport:
         return
     with LockViewport(self.viewport):
         # all markers in active state and solid
         for mc in [self.regionMarkers, self.peakMarkers, self.bgMarkers]:
             mc.active = True
             mc.dashed = False
             mc.Show()
         # coloring
         if self.dispPeakFunc:
             self.dispPeakFunc.SetColor(hdtv.color.region)
             self.dispPeakFunc.Show()
         if self.dispBgFunc:
             self.dispBgFunc.SetColor(hdtv.color.bg)
             self.dispBgFunc.Show()
         # peak list
         for peak in self.peaks:
             peak.color = hdtv.color.peak
             if self._showDecomp:
                 peak.Show()
             else:
                 peak.Hide()
Example #20
0
 def Hide(self):
     if not self.viewport:
         return
     with LockViewport(self.viewport):
         self.regionMarkers.Hide()
         self.bgMarkers.Hide()
Example #21
0
File: fitxml.py Project: op3/hdtv
    def ReadFitlist(
        self,
        file_object,
        sid=None,
        calibrate=False,
        refit=False,
        interactive=True,
        fname=None,
    ):
        """
        Reads fitlist from xml files
        """

        with LockViewport(self.spectra.viewport):
            if sid is None:
                sid = self.spectra.activeID
            if sid not in self.spectra.ids:
                raise HDTVCommandError("No spectrum with id %s loaded." % sid)
            count = 0
            try:
                fname = "'{}'".format(fname or file_object.name)
            except AttributeError:
                fname = "fitlist"
            try:
                tree = ET.parse(file_object)
                root = tree.getroot()
                if not root.tag == "hdtv" or root.get("version") is None:
                    e = "this is not a valid hdtv file"
                    raise SyntaxError(e)
                # current version
                if root.get("version") == self.version:
                    count, fits = self.RestoreFromXml(
                        root,
                        sid,
                        calibrate=calibrate,
                        refit=refit,
                        interactive=interactive,
                    )
                else:
                    # old versions
                    oldversion = root.get("version")
                    hdtv.ui.warning(
                        "The XML version of this file (%s) is outdated." % oldversion
                    )
                    if oldversion == "1.4":
                        hdtv.ui.msg(
                            "But this version should be fully compatible with the new version."
                        )
                        count, fits = self.RestoreFromXml_v1_4(
                            root, sid, calibrate=calibrate, refit=refit
                        )
                    if oldversion == "1.3":
                        hdtv.ui.msg(
                            "But this version should be fully compatible with the new version."
                        )
                        count, fits = self.RestoreFromXml_v1_3(
                            root, sid, calibrate=calibrate, refit=refit
                        )
                    if oldversion == "1.2":
                        hdtv.ui.msg(
                            "But this version should be fully compatible with the new version."
                        )
                        count, fits = self.RestoreFromXml_v1_2(
                            root, sid, calibrate=calibrate, refit=refit
                        )
                    if oldversion == "1.1":
                        hdtv.ui.msg(
                            "But this version should be fully compatible with the new version."
                        )
                        count, fits = self.RestoreFromXml_v1_1(
                            root, sid, calibrate=calibrate, refit=refit
                        )
                    if oldversion == "1.0":
                        hdtv.ui.msg(
                            "Restoring only fits belonging to spectrum %s" % sid
                        )
                        hdtv.ui.msg(
                            "There may be fits belonging to other spectra in this file."
                        )
                        if interactive:
                            input("Please press enter to continue...\n")
                        count, fits = self.RestoreFromXml_v1_0(
                            root, [sid], calibrate=False, refit=refit
                        )
                    if oldversion.startswith("0"):
                        hdtv.ui.msg(
                            "Only the fit markers have been saved in this file."
                        )
                        hdtv.ui.msg("All the fits therefore have to be repeated.")
                        hdtv.ui.msg("This will take some time...")
                        if interactive:
                            input("Please press enter to continue...\n")
                        count, fits = self.RestoreFromXml_v0(root, True)
            except SyntaxError as e:
                raise HDTVCommandError(f"Error reading fits from {fname}.")
            else:
                msg = "%s loaded: " % (fname)
                if count == 1:
                    msg += "1 fit restored."
                else:
                    msg += "%d fits restored." % count
                hdtv.ui.msg(msg)
                return count, fits
Example #22
0
    def LoadSpectra(self, patterns, ID=None):
        """
        Load spectra from files matching patterns.

        If ID is specified, the spectrum is stored with id ID, possibly
        replacing a spectrum that was there before.

        Returns:
            A list of the loaded spectra
        """
        with LockViewport(self.window.viewport if self.window else None):
            # only one filename is given
            if isinstance(patterns, str):
                patterns = [patterns]

            if ID is not None and len(patterns) > 1:
                raise hdtv.cmdline.HDTVCommandError(
                    "If you specify an ID, you can only give one pattern")

            loaded = []
            for p in patterns:
                # put fmt if available
                p = p.rsplit("'", 1)
                if len(p) == 1 or not p[1]:
                    (fpat, fmt) = (p[0], None)
                else:
                    (fpat, fmt) = p

                files = glob.glob(os.path.expanduser(fpat))

                if len(files) == 0:
                    hdtv.ui.warning("%s: no such file" % fpat)
                elif ID is not None and len(files) > 1:
                    raise hdtv.cmdline.HDTVCommandAbort(
                        "pattern %s is ambiguous and you specified an ID" %
                        fpat)
                    break

                files.sort()

                for fname in files:
                    try:
                        # Create spectrum object
                        spec = Spectrum(FileHistogram(fname, fmt))
                    except (OSError, SpecReaderError):
                        hdtv.ui.warning("Could not load %s'%s" % (fname, fmt))
                    else:
                        sid = self.spectra.Insert(spec, ID)
                        spec.color = hdtv.color.ColorForID(sid.major)
                        if spec.name in list(self.spectra.caldict.keys()):
                            spec.cal = self.spectra.caldict[spec.name]
                        loaded.append(spec)
                        if fmt is None:
                            hdtv.ui.msg("Loaded %s into %s" % (fname, sid))
                        else:
                            hdtv.ui.msg("Loaded %s'%s into %s" %
                                        (fname, fmt, sid))

            if loaded:
                # activate last loaded spectrum
                self.spectra.ActivateObject(loaded[-1].ID)
            # Expand window if it is the only spectrum
            if len(self.spectra) == 1:
                if self.window:
                    self.window.Expand()
            return loaded
Example #23
0
 def Show(self):
     if not self.viewport:
         return
     with LockViewport(self.viewport):
         self.regionMarkers.Show()
         self.bgMarkers.Show()
Example #24
0
 def Draw(self, viewport):
     self.viewport = viewport
     with LockViewport(self.viewport):
         DrawableManager.Draw(self, viewport)
         if self.hist:
             self.hist.Draw(viewport)