#!/usr/bin/env python3

import sys, argparse, os
import pandas as pd
from scipy import stats

parser = argparse.ArgumentParser(description=r'Perform linear regression on specified columns of CSV. Returns linear regression.', epilog=r'')

# Positional (i.e. required) arguments
parser.add_argument('csv', help='CSV file to read data from.')

# Optional arguments
parser.add_argument('-x', '--xcol', help='Column name of independent variable. Default first column.')
parser.add_argument('-y', '--ycol', help='Column name of dependent variable. Default second column.')
parser.add_argument('-r', '--round', help='Number of decimal places desired. Default 2.', default=2, type=int)

# Parse arguments
args = parser.parse_args()

# Script behavior goes below

data = pd.read_csv(args.csv)

if args.xcol == None:
    args.xcol = data.columns[0]

if args.ycol == None:
    args.ycol = data.columns[1]

x = data[args.xcol]
y = data[args.ycol]

slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)

print("y = {0}⋅x + {1}".format(round(slope,args.round),round(intercept,args.round)))
