Exemplo n.º 1
0
 def temp_html_output(self, plot_object, backend=None):
     """This function plots a object, by saving the plot as html file in a temporary file and returning the path
     to the file"""
     save_plot(
         "temp_plot", plot_object, self.rootdir, save_as=["html"], backend=backend
     )
     return os.path.join(self.rootdir, "html", "temp_plot.html")
Exemplo n.º 2
0
    def save_to(self, progress_queue=None, backend=None, to_call=None):
        """This function saves all plots from every analysis for each datasets"""

        # Generate base folder
        save_dir = os.path.normpath(self.config.get("Output", "Plots"))
        try:
            os.mkdir(save_dir)
        except:
            self.log.warning(
                "The directory: {} already exists, files inside can/will be overwritten!"
                .format(save_dir))

        self.log.info("Saving data...")
        from forge.tools import save_data

        if progress_queue:
            progress_queue.put({"STATE": "Saving Data..."})
        save_data(self,
                  self.config.get("Save_as", []),
                  save_dir,
                  to_call=to_call)
        if progress_queue:
            progress_queue.put({"STATE": "IDLE"})

        # check if any plot should be saved
        anyplot = False
        for ploti in ["png", "html", "svg"]:
            if ploti in self.config.get("Save_as", []):
                anyplot = True

        if not anyplot:
            self.log.info("No plot type specified for saving...")
            return

        self.log.critical("Saving plots...")
        progress_steps = 0
        saved = 0

        if os.path.exists(os.path.normpath(save_dir)):
            if progress_queue:
                progress_queue.put({"STATE": "Saving plots..."})
            for plot in self.plotObjects:
                if "All" in plot:
                    self.log.info("Saving all subplots from the 'All' plot...")
                    Allplots = plot["All"]
                    try:
                        plotslist_tuple = Allplots.keys()
                        progress_steps += len(plotslist_tuple)
                        for path in plotslist_tuple:
                            if progress_queue:
                                progress_queue.put(
                                    {"PROGRESS": saved / (progress_steps + 1)})
                            plots = Allplots
                            for attr in path:
                                plots = getattr(plots, attr)
                            try:
                                label = "_".join(path)
                            except:
                                label = plots._label
                            save_plot(
                                label,
                                plots,
                                save_dir,
                                save_as=self.config.get("Save_as", ["png"]),
                                backend=backend,
                            )
                            saved += 1
                    except:
                        save_plot(
                            Allplots.group,
                            Allplots,
                            save_dir,
                            save_as=self.config.get("Save_as", ["png"]),
                            backend=backend,
                        )
                        saved += 1

                else:
                    self.log.info(
                        "Saving all subplots from the 'All' not possible due to missing key..."
                    )
                    self.log.info(
                        "Saving all plots across the dictionary instead...")
                    progress_steps = len(plot.items())
                    for key, subplot in plot.items():
                        if progress_queue:
                            progress_queue.put(
                                {"progress": saved / (progress_steps + 1)})
                        save_plot(key, subplot, save_dir, backend=backend)
                        saved += 1
                try:
                    if "html" in self.config.get(
                            "Save_as", []) or "png" in self.config.get(
                                "Save_as", []):
                        self.log.info("Export the 'All' plot...")
                        save_plot(
                            plot.get("Name", "All Plots"),
                            #self.config.get("plot_name", ["pippo"]),
                            plot["All"],
                            save_dir,
                            #save_as=self.config.get("Save_as", ["png"]),
                            backend=self.backend,
                        )
                        if progress_queue:
                            progress_queue.put({"PROGRESS": 1})
                    else:
                        progress_queue.put({"PROGRESS": 1})
                except:
                    self.log.warning("'All plots' could not be saved....",
                                     exc_info=True)

        else:
            self.log.error("The path: {} does not exist...".format(save_dir))
        if progress_queue:
            progress_queue.put({"STATE": "IDLE"})
Exemplo n.º 3
0
    def save_to(self, progress_queue=None, backend=None):
        """This function saves all plots from every analysis for each datasets"""
        self.log.critical("Saving plots...")
        progress_steps = 0
        saved = 0
        # Generate base folder
        save_dir = os.path.normpath(self.config.get("Output", "Plots"))
        try:
            #Todo:directory tree generation
            os.mkdir(save_dir)
        except:
            self.log.warning(
                "The directory: {} already exists, files inside can/will be overwritten!"
                .format(save_dir))
        if os.path.exists(os.path.normpath(save_dir)):
            if progress_queue:
                progress_queue.put({"STATE": "Saving plots..."})
            for plot in self.plotObjects:
                if "All" in plot:
                    self.log.info("Saving all subplots from the 'All' plot...")
                    Allplots = plot["All"]
                    try:
                        plotslist_tuple = Allplots.keys()
                        progress_steps += len(plotslist_tuple)
                        for path in plotslist_tuple:
                            if progress_queue:
                                progress_queue.put(
                                    {"PROGRESS": saved / (progress_steps + 1)})
                            plots = Allplots
                            for attr in path:
                                plots = getattr(plots, attr)
                            try:
                                label = "_".join(path)
                            except:
                                label = plots._label

                            save_plot(label,
                                      plots,
                                      save_dir,
                                      save_as=self.config.get(
                                          "Save_as", ["png"]),
                                      backend=backend)
                            saved += 1
                    except:
                        save_plot(Allplots.group,
                                  Allplots,
                                  save_dir,
                                  save_as=self.config.get("Save_as", ["png"]),
                                  backend=backend)
                        saved += 1

                else:
                    self.log.info(
                        "Saving all subplots from the 'All' not possible due to missing key..."
                    )
                    self.log.info(
                        "Saving all plots across the dictionary instead...")
                    progress_steps = len(plot.items())
                    for key, subplot in plot.items():
                        if progress_queue:
                            progress_queue.put(
                                {"progress": saved / (progress_steps + 1)})
                        save_plot(key, subplot, save_dir, backend=backend)
                        saved += 1
                try:
                    if self.backend == "bokeh":
                        self.log.info("Export the 'All' html plot...")
                        save_plot(plot.get("Name", "All Plots"), plot["All"],
                                  save_dir)
                    if progress_queue:
                        progress_queue.put({"PROGRESS": 1})
                except:
                    self.log.warning("'All plots' could not be saved....")

        else:
            self.log.error("The path: {} does not exist...".format(save_dir))
        if progress_queue:
            progress_queue.put({"STATE": "IDLE"})