From 82ff4a871d674fe8605814c1ec33eddcf4e4afc8 Mon Sep 17 00:00:00 2001 From: robertsilen Date: Thu, 23 Nov 2017 12:58:41 +0200 Subject: [PATCH] testing --- groupanalysis.py | 25 +++++++++++++++++++++++++ timeseries_plot.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 timeseries_plot.py diff --git a/groupanalysis.py b/groupanalysis.py index 28e3016..c3e0d05 100644 --- a/groupanalysis.py +++ b/groupanalysis.py @@ -57,8 +57,11 @@ def stop(self): # Read data from file inputfiles = [] +<<<<<<< HEAD timeseries = True +======= +>>>>>>> parent of 040a575... removed timeseries_plt.py and updated plotting to groupanalysis.py if len(sys.argv) < 2: sys.argv.append('example.csv') @@ -73,6 +76,7 @@ def stop(self): timeseries_medians = pd.DataFrame() timeseries_results = pd.DataFrame() +<<<<<<< HEAD df_timeseries @@ -84,15 +88,23 @@ def stop(self): # Loop through all files for j, element in enumerate(inputfiles): progress_bar = ProgressBar() +======= +# Loop through all files +for j, element in enumerate(inputfiles): +>>>>>>> parent of 040a575... removed timeseries_plt.py and updated plotting to groupanalysis.py try: input = element print("\nReading: "+str(input)) +<<<<<<< HEAD if timeseries==False: df = pd.read_csv(input) else: df = df_timeseries +======= + df = pd.read_csv(input) +>>>>>>> parent of 040a575... removed timeseries_plt.py and updated plotting to groupanalysis.py if input.endswith('.csv'): input = input[:-4] @@ -117,6 +129,10 @@ def stop(self): else: print ('Groups are NOT equal size: '+str(a)) +<<<<<<< HEAD +======= + progress_bar = ProgressBar() +>>>>>>> parent of 040a575... removed timeseries_plt.py and updated plotting to groupanalysis.py progress_bar.start() # Prepare to capture results @@ -227,12 +243,21 @@ def stop(self): except: progress_bar.stop() +<<<<<<< HEAD + + print "\nUnexpected error:", sys.exc_info()[0] +timeseries_results = pd.concat([timeseries_medians, timeseries_values], axis=1) +timeseries_results = pd.concat([timeseries_results.loc[['Group'],:], timeseries_results.drop('Group', axis=0)], axis=0) + +======= + progress_bar.join() print "\nUnexpected error:", sys.exc_info()[0] timeseries_results = pd.concat([timeseries_medians, timeseries_values], axis=1) timeseries_results = pd.concat([timeseries_results.loc[['Group'],:], timeseries_results.drop('Group', axis=0)], axis=0) +>>>>>>> parent of 040a575... removed timeseries_plt.py and updated plotting to groupanalysis.py output = 'medians'+time.strftime("-%d-%m-%Y")+".csv" print "\nWriting summary of median values: "+str(output)+"\n" timeseries_results.to_csv(output) diff --git a/timeseries_plot.py b/timeseries_plot.py new file mode 100644 index 0000000..a679eeb --- /dev/null +++ b/timeseries_plot.py @@ -0,0 +1,39 @@ +import sys +import pandas as pd +import matplotlib.pyplot as plt + +# load all input files into array of dataframes +data = [] +for j, element in enumerate(sys.argv[1:]): + data.append(pd.read_csv(element)) + data[j].set_index('Person',inplace=True) + input = element + if input.endswith('.csv'): + input = input[:-4] + data[j] = data[j].T + data[j].insert(loc=1, column='TimeSeries', value=input) + +# merge dataframes, change cells to numeric, not objects +df = pd.concat(data, axis=0, join='inner') +df = pd.concat([df.ix[:,:2], df.ix[:,2:].apply(pd.to_numeric)], axis=1) +df['Group'] = df['Group'].astype(int) + +# move groups and timeseries to first columns +cols = list(df) +cols.insert(0, cols.pop(cols.index('Group'))) +cols.insert(0, cols.pop(cols.index('TimeSeries'))) +df = df.ix[:, cols] + +# plotting +for column in df.ix[:,2:]: + fig, ax = plt.subplots() + labels = [] + for key, grp in df.groupby(['TimeSeries', 'Group'])[[column]].median().groupby(['TimeSeries']): + ax = grp.reset_index().plot(ax=ax, x='Group', y=column, xticks=grp.reset_index()['Group'], kind='line', title=column) + labels.append(key) + lines, _ = ax.get_legend_handles_labels() + ax.legend(lines, labels, loc='best') + plt.savefig('fig/'+column+'.png') + plt.clf() + +df.to_csv('test.csv')