Mercurial > repos > jay > gaiac_precision
annotate gaiac_regression_plot/gaiac_regression_plot.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 numpy as np |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
2 import pandas as pd |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
3 import matplotlib.pyplot as plt |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
4 import seaborn as sns |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
5 #%matplotlib inline |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
6 from scipy import stats |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
7 from sklearn import linear_model |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
8 import statsmodels.formula.api as smf |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
9 import statsmodels.api as sm |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
10 import argparse |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
11 def regression (infile,clm_list_y, clm_list_x, outfile, plottitle,fig_height, fig_aspect, clm_lab_y, clm_lab_x): |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
12 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
|
13 cl = df.columns.tolist() |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
14 |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
15 clms_x = cl[int(clm_list_x)-1] |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
16 clms_y = cl[int(clm_list_y)-1] |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
17 |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
18 |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
19 #results = smf.ols('clms_y~clms_x', data=df).fit() |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
20 regr = linear_model.LinearRegression() |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
21 X = df[clms_x].values.reshape(-1,1) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
22 y = df[clms_y].values.reshape(-1,1) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
23 regr.fit(X, y) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
24 slope, intercept, r_value, p_value, std_err = stats.mstats.linregress(df[clms_x],df[clms_y]) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
25 a='{0:0.2f}'.format(intercept) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
26 b='{0:0.2f}'.format(slope) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
27 c='{0:0.2f}'.format(r_value) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
28 |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
29 g = sns.lmplot(x=clms_x,y=clms_y, data=df, fit_reg=True,height= int(fig_height),aspect= float(fig_aspect),ci=None, scatter_kws={"s": int(fig_height)*8}) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
30 g.set(xlabel=clm_lab_x, ylabel=clm_lab_y) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
31 props = dict(boxstyle='round', alpha=0.25,color=sns.color_palette()[0]) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
32 #textstr = '$y=1.42 + 1.27x$' |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
33 textstr='y= {} + {}x , r={}'.format(a,b,c) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
34 g.ax.text(0.4, 1, textstr,transform=g.ax.transAxes, fontsize=int(fig_height)*2, bbox=props) |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
35 plt.savefig(outfile,dpi=300,bbox_inches="tight") |
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 |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
38 if __name__=="__main__": |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
39 |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
40 parser = argparse.ArgumentParser() |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
41 parser.add_argument("-I", "--infile", required=True, default=None, help="Input data frame as a tab-separated (.tsv) file.") |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
42 parser.add_argument("-Cy", "--column_list_y", required=False, default=False, help="Comma-separated list of column names to plot on the Y-axis.") |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
43 parser.add_argument("-Cx", "--column_list_x", required=False, default=False, help="Comma-separated list of column names to plot on the X-axis.") |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
44 parser.add_argument("-O", "--output", required=False, default='Out.png', help="Output file name for the saved figure (default: 'Out.png').") |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
45 parser.add_argument("-T", "--title", required=False, default='Time Series plot', help="Title of the figure (default: 'Time Series plot').") |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
46 parser.add_argument("-H", "--height", required=False, default='14', help="Figure height in inches (default: 14).") |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
47 parser.add_argument("-A", "--aspect", required=False, default='12', help="Aspect ratio or figure width in inches (default: 12).") |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
48 parser.add_argument("-Y", "--ylab", required=False, default='Y label', help="Label for the Y-axis (default: 'Y label').") |
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
jay
parents:
diff
changeset
|
49 parser.add_argument("-X", "--xlab", required=False, default='X label(time)', help="Label for the X-axis (default: 'X label(time)').") |
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 args = parser.parse_args() |
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 regression(args.infile, args.column_list_y, args.column_list_x, args.output, args.title, args.height, args.aspect, args.ylab, args.xlab) |