Skip to content
This repository was archived by the owner on Oct 21, 2018. It is now read-only.

Commit d6e2d3d

Browse files
author
Anastomose
committed
Updated readme.md, annotation function
1 parent e9e3a90 commit d6e2d3d

File tree

5 files changed

+93
-35
lines changed

5 files changed

+93
-35
lines changed

Students/ebuer/termp/createplot.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
"""createplot.py
2+
file creates class object then uses makeSampdict and
3+
plotSampdict to generate the visualization.
14
5+
This file can be further refined in the future to initialized with a
6+
datafile create a full dictionary of samples and then plot each
7+
sample/method pair in sequence.
8+
"""
29

310
import sys
411
sys.path.append(r"D:\GitHub\IntroToPython\Students\ebuer\termp")
@@ -13,7 +20,8 @@
1320
import matplotlib.pyplot as plt
1421

1522

16-
# create the instance from the data file
23+
# create the class instance from the data file
24+
# script could be enhaced with sys.argv argument for initializing object
1725

1826

1927
def callclass():
@@ -22,7 +30,6 @@ def callclass():
2230

2331
# use the class object and methods to select and prepare data for plotting
2432
# this could easily be turned into an fx w/dict arg for efficient looping
25-
# with either sys.argv or an additional script to feed values
2633

2734

2835
def makeSampdict(**kwargs):
@@ -64,20 +71,28 @@ def plotSampdict(**kwargs):
6471
ax1.set_ylim(plot_dict['y_lims'])
6572
ax1.set_ylabel('Concentration in {u_units}'.format(**kwargs), rotation='vertical')
6673

67-
# create annotation
74+
# create title and annotation
6875
anno_text = 'Results for Sample: {u_sample} Analytical Method: {u_method}'.format(**kwargs)
69-
# ax1.annotate(anno_text, xy=(kwargs['x_ticks'][0]-0.5, kwargs['y_lims'][1]))
7076
ax1.set_title(anno_text)
7177

78+
anno_list = zip(kwargs['flag'], kwargs['x_ticks'], kwargs['y_value'])
79+
for a in anno_list:
80+
l_text = ''
81+
if type(a[0]) is not float:
82+
l_text = a[0]
83+
x = a[1] + 0.15
84+
y = a[2] + 1
85+
ax1.annotate(l_text, xy = (x, y))
86+
7287

7388
# in principle visualization is expensive, so we save it for the end
7489
d = {'pad': 2, 'w_pad': 1, 'h_pad': 1} # keyword dict for padding
7590
fig.set_tight_layout(d)
7691

7792
fig.canvas.draw()
78-
# plt.savefig('test_figure.pdf')
79-
fig.show() # after many trial runs and tests screen vis not needed
93+
plt.savefig('test_figure.pdf')
94+
# fig.show() # after many trial runs screen vis not needed
8095
return (fig, ax1)
8196

82-
# call function with dict
97+
# call function with dict generated by makeSampledict
8398
fig1, ax1 = plotSampdict(**plot_dict)

Students/ebuer/termp/readme.md

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
1-
###Term Project Readme
2-
Basic project construct: use newfound python knowledge combined with pandas and matplotlib modules to create a simple set of data visualization tools for environmental chemistry and monitoring data.
3-
4-
**Desired Key Elements**
5-
* Read in environmental data from format delivered
6-
* Visualize detected results on a single image
7-
* Apply estimate flag to qualified results
8-
* Add axis and constituent labeling as appropriate
9-
* Plot multiple sets of data on shared axis graphs or a single image for monitoring/environmental data
10-
11-
####Part 1: Read in data
12-
Environmental chemistry data is delivered in csv or xlsx format. After validation it is almost always delivered as an xlsx file. Use pandas module along with other techniques to create a data set for plotting.
13-
14-
Several points to work out:
15-
* multiple sample names per sheet -- need unique list of samples
16-
* create list (or tuple?) of parameters analyzed in each sample
17-
* take advantage of index feature to pull out flags
18-
* make dict of plotting parameters to pass to chart
1+
##Term Project Readme
2+
Basic project construct: use newfound python knowledge combined with pandas and matplotlib modules to create a simple data visualization tool for environmental chemistry data.
3+
4+
###plotprep Class Attributes
5+
All attributes at this time are mutable to allow for tinkering.
6+
7+
_raw_data_
8+
Raw import of the data used in class initialization
9+
10+
_selected_data_
11+
Columns selected from raw_data that are processed as part of the plotprep. Original raw_data is left untouched in the event it needs to be examined later.
12+
13+
_samples_
14+
A list of unique sample identifiers pulled from selected_data.
15+
16+
_qc_samples_
17+
A llist of unique qc sample identifiers pulled from selected_data
18+
19+
_sampleData_
20+
Analytical results selected based on the sample id handed to the selectSample method.
21+
22+
_plotData_
23+
Analytical results for the sample identified using selectSample and the method provided to selectForPlot. Results do not include spiked sample recoveries.
24+
25+
_plot_dict_
26+
Plotting dictionary created by makePlotobj method, requires that both plotData already have been selected and assigned as an attribute of the class instance
27+
28+
29+
####Class Methods
30+
_showOptions_
31+
Automatically called by other methods to display a unique list of options for the user such as sample identifiers or methods used on a specific sample.
32+
33+
_selectSample_
34+
Returns sampleData attribute to the class instance. sampleData are the columns of selected_data reduced down to only the selected sample.
35+
36+
Syntax: selectSample(sample_id)
37+
38+
Note: type(sample_id) must match what is in the raw_data.
39+
40+
_selectForPlot_
41+
Returns plotData attribute to the class instance. plotData is the set of analytical results from the selected_data reduced down by sample in _selectSample_ and by the method entered.
42+
43+
Syntax: selectForPlot(analytical_method)
44+
45+
_makePlotobj_
46+
Returns plotting dictionary attribute to the class instance. Method takes no arguments, but requires that selectSample and selectForPlot have already been run to be fully populated
47+
48+
###Createplot.py
49+
Script initializes plotprep object and creates plotting dictionary for sample-method pair passed in as test_samp dictonary using _makeSampdict_.
50+
51+
_plotSampdict(**kwargs)_
52+
Takes plotting dictionary created in the plotprep instance of _makeSampdict_ and renders a bar chart of results with labeling and laboratory flags applied to the top of each column. Returns figure and subplot objects for further tinkering.

Students/ebuer/termp/termp_funcs.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(self, dfile):
5656

5757
# samples and qc samples each in their own class attribute
5858
self.samples = temp_array[mask]
59-
self.qc_samples = temp_array[i_mask]
59+
self.qc_samples = temp_array[i_mask] # might be useful later
6060

6161
# create a unique list of sample names and print list of samples
6262
s_dict = {'name': 'samples', 'o_list': self.samples.unique()}
@@ -113,7 +113,7 @@ def makePlotobj(self):
113113

114114
# get values set up for plotting
115115
x_label = [x for x in self.plotData['chemical_name']]
116-
y_value = [float(y.replace(',', ''))
116+
y_value = [float(y.replace(',', ''))
117117
for y in self.plotData['result_value']
118118
]
119119
flag = [f for f in self.plotData['lab_flag']]
@@ -128,16 +128,11 @@ def makePlotobj(self):
128128
'y_lims': (0, max(y_value)+5),
129129
'flag': flag,
130130
'u_sample': self.plotData['sample_name'].unique()[0],
131-
'u_method': self.plotData['std_anl_method_name'].unique()[0],
131+
'u_method':
132+
self.plotData['std_anl_method_name'].unique()[0],
133+
132134
'u_units': self.plotData['result_unit'].unique()[0]
133135
# add dict entry for results units, call in labeling
134136
}
135137

136138
return self.plot_dict
137-
138-
139-
140-
141-
142-
143-
2.96 KB
Binary file not shown.

Students/ebuer/termp/test_termp_funcs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,17 @@ def test_makePlotobj():
7272
assert sample_dict['y_value'][0] is not None
7373
assert type(sample_dict['y_value'][0]) is float
7474
assert sample_dict['u_units'] == 'ug/kg'
75+
76+
77+
def test_annoStr():
78+
# created to get at flag data types for logic in termp_funcs
79+
p_test = callclass()
80+
p_test.selectSample('14051305')
81+
p_test.selectForPlot('SW8082A')
82+
# pdb.set_trace()
83+
sample_dict = p_test.makePlotobj()
84+
85+
f = sample_dict['flag']
86+
for fl in f:
87+
print type(fl)
88+
assert True

0 commit comments

Comments
 (0)