1

I have the following function which I'm using to run analysis.

When I set run_analysis_1 = True, it should run the whole first piece of analysis, which outputs plots 1, 2 and 3. When I set run_analysis_2 = True, it should run the whole second piece of analysis, which outputs plots 4, 5 and 6.

def save_locally(self,
                 run_all_analysis: bool = False,
                 run_analysis_1: bool = False,
                 run_analysis_2: bool = False):

    if run_analysis_1 = True
        plot_1 # some function that creates plot 1 + saves to local folder
        plot_2 # some function that creates plot 2 + saves to local folder
        plot_3 # some function that creates plot 3 + saves to local folder
    
    if run_analysis_2 = True
        plot_4 # some function that creates plot 4 + saves to local folder
        plot_5 # some function that creates plot 5 + saves to local folder
        plot_6 # some function that creates plot 6 + saves to local folder

I would like a lot of flexibility in choosing what plots I would like to run, such that, I am able to:

  1. Run the whole of a piece of analysis (for example, run all components of analysis 1 to output plots 1,2 and 3)
  2. Run a part of the analysis (for example, just run plot 1 from analysis 1)

So it looks something like the below...

save_locally(run_analysis_1.plot_1=True, run_analysis_2.all=True)

Is there a way to do this?

1 Answer 1

1

Something like this might work for you;

  • Store all plots from an analysis in its own class
  • Custom all attribute to store all defined plots in one list
  • Change signature of save_locally to take *args

Would let you call your function pretty cleanly like this:

save_locally(Analysis1.plot1, Analysis1.plot2, Analysis2.all)


from itertools import chain

class _Analysis:
    all = ...
    def __init_subclass__(cls, **kwargs):
        cls.all = [value for key, value in cls.__dict__.items() if not key.startswith("_")]

class Analysis1(_Analysis):
    plot1 = "a"
    plot2 = "b"
    plot3 = "c"

class Analysis2(_Analysis):
    plot4 = "d"
    plot5 = "e"
    plot6 = "f"


def save_locally(*plots):
    plots = chain(*plots)  # Flatten - lets us write Analysis.all without *
    for plot in plots:
        print(plot, end=" ")  # Do whatever with plot

save_locally(Analysis1.plot1, Analysis1.plot2, Analysis2.all)

>>> a b d e f 
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing, that is what I was after!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.