dimanche 13 septembre 2020

Improving computational speed for overlapping piecewise functions written with if-else statements

I have a function that looks like this:

overlapping function

where m is read in from a directory of many .json files.

The Python code I wrote for it looks like this:

for i in os.listdir(PATH):
    if i.endswith(".json"):
    
        # Read in m value from json files 
        posterior_files = bilby.result.read_in_result(PATH + "/" + i)
        m = np.asarray(posterior_files.posterior['mass_1'].values)
        p = np.asarray(posterior_files.posterior['log_prior'].values)

        m = [] # one big float array per each json file 
        A = []
        B = []
        C = []

            
        for j in range(0, len(m)):
            if m1[j]<5. and m1[j]>10.:
                A = 0
            else:
                A = m**(-1.) # some algebraic expression

            if m1[j]<3. and m1[j]>10.:
                B = 0
            else:
                B = m**(-2.) # some algebraic expression

            if m1[j]<5. and m1[j]>10.:
                C = 0
            else:
                C = m**(-3.) # some algebraic expression

       num = 1.0/len(m) * np.sum((A+B)/np.exp(p))
       den = 1.0/len(m) * np.sum((C)/np.exp(p))

       factor = num/den
       print(factor)

(I've omitted the real algebraic expressions since it isn't important for the question itself.)

This piece of code works but the problem is that it takes a really long time to run. With the actual data and expressions, it took over a day to run on a supercomputer. I need to rewrite this such that it takes a lesser amount of time.

I've tried using piecewise functions but the issue is that the conditions are overlapping which leads to other errors.

I would be extremely thankful for any suggestions!

Aucun commentaire:

Enregistrer un commentaire