Mercurial > repos > jay > gaiac_precision
annotate gaiac_bias_calculation/gaiac_bias_calculation.py @ 3:1f9e67edde6a draft default tip
planemo upload for repository https://github.com/jaidevjoshi83/gaiac commit e9587f93346c7b55e1be00bad5844bf2db3ed03d-dirty
| author | jay | 
|---|---|
| date | Thu, 10 Jul 2025 19:42:02 +0000 | 
| parents | 287d6cc86582 | 
| children | 
| rev | line source | 
|---|---|
| 0 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 1 import scipy | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 2 #from scipy.stats.distributions import chi2,t | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 3 from scipy.stats import t | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 4 from scipy import stats | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 5 import pandas as pd | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 6 import numpy as np | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 7 import argparse | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 8 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 9 def Bias_abs(infile, clm1, clm2, Out): | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 10 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 11 df = pd.read_csv(infile, sep="\t") | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 12 lOc = int(df.shape[0]) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 13 cl = df.columns.tolist() | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 14 #making a column of absolute di as per us epa | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 15 df['PM10_OPC_DIFF']=abs(((df[cl[int(clm1)-1]])-(df[cl[int(clm2)-1]]))/(df[cl[int(clm2)-1]]))*100 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 16 #square of di | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 17 df['PM10_OPC_DIFFs']=(df['PM10_OPC_DIFF'])*(df['PM10_OPC_DIFF']) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 18 #summation of the columns | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 19 d10=df.PM10_OPC_DIFF.sum() | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 20 d10_2=df.PM10_OPC_DIFFs.sum() | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 21 #AB and AS calculations | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 22 AB= d10/lOc | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 23 AS=(np.sqrt((lOc*(d10_2)-(d10)**2)/(2*lOc*(lOc-1)))) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 24 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 25 #T distribution calculation | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 26 T=stats.t.ppf(1-0.05, lOc) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 27 #Absolute bias calculation | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 28 Bias_abs = abs(AB + T*AS/(np.sqrt(lOc))) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 29 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 30 #di column with sign | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 31 df['Di']=(((df[cl[int(clm1)-1]])-(df[cl[int(clm2)-1]]))/(df[cl[int(clm2)-1]]))*100 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 32 #quantiles of di without sign | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 33 q1=df['Di'].quantile([0.25]) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 34 q2=df['Di'].quantile([0.75]) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 35 #assigning sign to absolute bias based on q1 and q2 values | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 36 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 37 if (q1[0.25] < 0) & (q2[0.75] < 0): | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 38 Bias_abs=Bias_abs*-1 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 39 elif (q1[0.25] > 0) & (q2[0.75] > 0): | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 40 Bias_abs=Bias_abs | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 41 elif (q1[0.25] >0) & (q2[0.75]<0): | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 42 Bias_abs=(u"\u00B1"+str(Bias_abs)) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 43 elif (q1[0.25] <0) & (q2[0.75]>0): | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 44 Bias_abs=(u"\u00B1"+str(Bias_abs)) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 45 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 46 #output file | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 47 df1 = pd.DataFrame([Bias_abs], columns=['Percent Bias']) | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 48 df1.round(4).to_csv(Out, sep="\t") | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 49 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 50 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 51 if __name__=="__main__": | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 52 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 53 parser = argparse.ArgumentParser() | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 54 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 55 parser.add_argument("-I", "--infile", required=True, default=None, help="Input file") | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 56 parser.add_argument("-c1", "--column_1", required=True, default=None, help="First column") | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 57 parser.add_argument("-c2", "--column_2", required=True, default=None, help="Second column") | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 58 parser.add_argument("-o", "--output", required=True, default=None, help="OutFile") | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 59 args = parser.parse_args() | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 60 | 
| 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 61 Bias_abs(args.infile, args.column_1, args.column_2, args.output) | 
