Home > Net >  python: extract variables values from `json` and use in `jupyter` notebook for a function throwing a
python: extract variables values from `json` and use in `jupyter` notebook for a function throwing a

Time:11-17

I have a par.json file were i stored the variable values. And fetched the values in .py file successfully. I'm trying to run all function of .py file in jupyter notebook (all is in same location) where .py file already getting values from json file but while running in jupyter notebook it is throwing an error called as 'b_threshold' is not defined
json file I have

{
    "mul": 200,
    "b_threshold": 2,
    "a_threshold": 5,

}

the multi.py file snippet is below

def h_b_acc(data, har={}): 
    trr = data.tt.unique() 
    # global b_threshold, a_threshold, mul
    for tr in trr:
        temp_dict = {}
        
        temp_df = data.loc[data['tt']==tr].copy()
        temp_df['b_event'] = temp_df['aci'].apply(lambda x: 1 if x < b_threshold else 0)
        temp_df['a_event'] = temp_df['aci'].apply(lambda x: 1 if x > a_threshold else 0)
  

    
        total_d_dis = temp_df['dd'].max() - temp_df['dd'].min()

        temp_dict['b_s'] = (2015* mul)/ total_d_dis 
   


        temp_dict['a_s']= (2016 * mul)/ total_d_dis 

        har['tr_'   str(tr)] =   temp_dict  
#         print(temp_dict)
    return har

if __name__ =='__main__':
    try:
        with open(r'path\par.json', "r") as prm_mp:
            pp = json.load(prm_mp)
            
            
        b_threshold = pp.get('b_threshold', 2)
        a_threshold = pp.get('a_threshold', 5) 
        mul = pp.get(['mul'],200)

   except:
        print("Parameter mapping file not parsed")

loaded json file in following way in jupyter notebook

with open(r'path\par.json', "r") as prm_mp:
            pp = json.load(prm_mp)
            
            
        b_threshold = pp.get('b_threshold', 2)
        a_threshold = pp.get('a_threshold', 5) 
        mul = pp.get(['mul'],200)


har_1 = multi.h_b_acc(tm)
har_1

showing following error .. how can I import it ? or may I wrong in writing function.

enter image description here

CodePudding user response:

Your imported module multi and its function h_b_acc() do not have access to your variables a_threshold and b_threshold. I recommend that you modify multi.h_b_acc() to pass them as optional arguments, so you can use the function in the Jupyer Notebook, as well as use your global variables in multi.py when needed. (It's also probably a good idea to ALL-CAPS your global variables)

def h_b_acc(data, a_threshold=A_THRESHOLD, b_threshold=B_THRESHOLD, mul=MUL, har={}): 
    trr = data.tt.unique() 
    for tr in trr:
        temp_dict = {}
        
        temp_df = data.loc[data['tt']==tr].copy()
        temp_df['b_event'] = temp_df['aci'].apply(lambda x: 1 if x < b_threshold else 0)
        temp_df['a_event'] = temp_df['aci'].apply(lambda x: 1 if x > a_threshold else 0)
  

    
        total_d_dis = temp_df['dd'].max() - temp_df['dd'].min()

        temp_dict['b_s'] = (2015* mul)/ total_d_dis 
   


        temp_dict['a_s']= (2016 * mul)/ total_d_dis 

        har['tr_'   str(tr)] =   temp_dict  
#         print(temp_dict)
    return har

This, way, in your Jupyter Notebook, you can do the following:

with open(r'path\par.json', "r") as prm_mp:
            pp = json.load(prm_mp)
            
            
        b_threshold = pp.get('b_threshold', 2)
        a_threshold = pp.get('a_threshold', 5) 
        mul = pp.get(['mul'],200)


har_1 = multi.h_b_acc(tm, a_threshold, b_threshold, mul)
har_1
  • Related