-
Notifications
You must be signed in to change notification settings - Fork 39
/
classify.py
executable file
·405 lines (340 loc) · 21.1 KB
/
classify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python
"""
Script to execute steps in the classification of measurements including:
1. Labeling specific MeasuredParameters
2. Tagging MeasuredParameters based on a model
Mike McCann
MBARI 16 June 2014
"""
import os
import sys
# Insert Django App directory (parent of config) into python path
sys.path.insert(0, os.path.abspath(os.path.join(
os.path.dirname(__file__), "../../")))
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings.local'
# django >=1.7
try:
import django
django.setup()
except AttributeError:
pass
import matplotlib as mpl
mpl.use('Agg') # Force matplotlib to not use any Xwindows backend
import matplotlib.pyplot as plt
import numpy as np
import warnings
from datetime import datetime
from django.db.utils import IntegrityError
from textwrap import wrap
from stoqs.models import (Activity, ResourceType, Resource, Measurement, MeasuredParameter,
MeasuredParameterResource, ResourceResource)
from utils.STOQSQManager import LABEL, DESCRIPTION, COMMANDLINE
from contrib.analysis import BiPlot, NoPPDataException
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA
import pickle
LABELED = 'Labeled'
TRAIN = 'Train'
TEST = 'Test'
class Classifier(BiPlot):
'''
To hold methods and data to support classification of measurements in a STOQS database.
See http://scikit-learn.org/stable/auto_examples/plot_classifier_comparison.html
'''
classifiers = { 'Nearest_Neighbors': KNeighborsClassifier(3),
'Linear_SVM': SVC(kernel="linear", C=0.025),
'RBF_SVM': SVC(gamma=2, C=1),
'Decision_Tree': DecisionTreeClassifier(max_depth=5),
'Random_Forest': RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
'AdaBoost': AdaBoostClassifier(),
'Naive_Bayes': GaussianNB(),
'LDA': LDA(),
'QDA': QDA()
}
def getActivity(self, mpx, mpy):
'''
Return activity object which MeasuredParameters mpx and mpy belong to
'''
acts = Activity.objects.using(self.args.database).filter(
instantpoint__measurement__measuredparameter__id__in=(mpx,mpy)).distinct()
if not acts:
print("acts = %s" % acts)
raise Exception('Not exactly 1 activity returned with SQL = \n%s' % str(acts.query))
else:
return acts[0]
def saveCommand(self):
'''
Save the command executed to a Resource and return it for the doXxxx() method to associate it with the resources it creates
'''
rt, _ = ResourceType.objects.using(self.args.database).get_or_create(name=LABEL, description='metadata')
r, _ = Resource.objects.using(self.args.database).get_or_create(name=COMMANDLINE, value=self.commandline, resourcetype=rt)
return r
def saveLabelSet(self, clResource, label, x_ids, y_ids, description, typeName, typeDescription):
'''
Save the set of labels in MeasuredParameterResource. Accepts 2 input vectors. (TODO: generalize to N input vectors);
description is used to describe the criteria for assigning this label. The typeName and typeDecription may be used to
refer to the grouping, and associate via the grouping the other labels made in the heuristic applied.
'''
try:
# Label
rt, _ = ResourceType.objects.using(self.args.database).get_or_create(name=typeName, description=typeDescription)
r, _ = Resource.objects.using(self.args.database).get_or_create(name=LABEL, value=label, resourcetype=rt)
# Label's description
rdt, _ = ResourceType.objects.using(self.args.database).get_or_create(name=LABEL, description='metadata')
rd, _ = Resource.objects.using(self.args.database).get_or_create(name=DESCRIPTION, value=description, resourcetype=rdt)
rr = ResourceResource(fromresource=r, toresource=rd)
rr.save(using=self.args.database)
# Associate with commandlineResource
ResourceResource.objects.using(self.args.database).get_or_create(fromresource=r, toresource=clResource)
except IntegrityError as e:
print(str(e))
print("Ignoring")
# Associate MeasuredParameters with Resource
if self.args.verbose:
print(" Saving %d values of '%s' with type '%s'" % (len(x_ids), label, typeName))
for x_id,y_id in zip(x_ids, y_ids):
a = self.getActivity(x_id, y_id)
mp_x = MeasuredParameter.objects.using(self.args.database).get(pk=x_id)
mp_y = MeasuredParameter.objects.using(self.args.database).get(pk=y_id)
MeasuredParameterResource.objects.using(self.args.database).get_or_create(
activity=a, measuredparameter=mp_x, resource=r)
MeasuredParameterResource.objects.using(self.args.database).get_or_create(
activity=a, measuredparameter=mp_y, resource=r)
def removeLabels(self, labeledGroupName, label=None, description=None, commandline=None): # pragma: no cover
'''
Delete labeled MeasuredParameterResources that have ResourceType.name=labeledGroupName (such as 'Labeled Plankton').
Restrict deletion to the other passed in options, if specified: label is like 'diatom', description is like
'Using Platform dorado, Parameter {'salinity': ('33.65', '33.70')} from 20130916T124035 to 20130919T233905'
(commandline is too long to show in this doc string - see examples in usage note). Note: Some metadatda
ResourceTypes will not be removed even though the Resources that use them will be removed.
'''
# Remove MeasuredParameter associations with Resource (Labeled data)
mprs = MeasuredParameterResource.objects.using(self.args.database).filter(resource__resourcetype__name=labeledGroupName
).select_related('resource')
if label:
mprs = mprs.filter(resource__name=LABEL, resource__value=label)
if self.args.verbose > 1:
print(" Removing MeasuredParameterResources with type = '%s' and label = %s" % (labeledGroupName, label))
rs = []
for mpr in mprs:
rs.append(mpr.resource)
mpr.delete(using=self.args.database)
# Remove Resource associations with Resource (label metadata), make rs list distinct with set() before iterating on the delete()
if label and description and commandline:
try:
rrs = ResourceResource.objects.using(self.args.database).filter(
(QDA(fromresource__name=LABEL) & QDA(fromresource__value=label)) &
((QDA(toresource__name=DESCRIPTION) & QDA(toresource__value=description)) |
(QDA(toresource__name=COMMANDLINE) & QDA(toresource__value=commandline)) ) )
if self.args.verbose > 1:
print(" Removing ResourceResources with fromresource__value = '%s' and toresource__value = '%s'" % (label, description))
for rr in rrs:
rr.delete(using=self.args.database)
except TypeError:
# Likely TypeError: __init__() got an unexpected keyword argument 'fromresource__name'
if self.args.verbose > 1:
print(" Previous Resource associations not found.")
else:
if self.args.verbose > 1:
print(" Removing Resources associated with labeledGroupName = %s'" % labeledGroupName)
for r in set(rs):
r.delete(using=self.args.database)
def createLabels(self, labeledGroupName):
'''
Using discriminator, mins, and maxes label MeasuredParameters in the database so that we can do supervised learning
'''
sdt = datetime.strptime(self.args.start, '%Y%m%dT%H%M%S')
edt = datetime.strptime(self.args.end, '%Y%m%dT%H%M%S')
commandlineResource = self.saveCommand()
for label, dmin, dmax in zip(self.args.labels, self.args.mins, self.args.maxes):
# Multiple discriminators are possible...
pvDict = {self.args.discriminator: (dmin, dmax)}
if self.args.verbose:
print("Making label '%s' with discriminator %s" % (label, pvDict))
try:
x_ids, y_ids, _, _, _ = self._getPPData(sdt, edt, self.args.platform, self.args.inputs[0],
self.args.inputs[1], pvDict, returnIDs=True, sampleFlag=False)
except NoPPDataException as e:
print(str(e))
if self.args.verbose:
print(" (%d, %d) MeasuredParameters returned from database %s" % (len(x_ids), len(y_ids), self.args.database))
description = 'Using Platform %s, Parameter %s from %s to %s' % (self.args.platform, pvDict, self.args.start, self.args.end)
if self.args.clobber:
self.removeLabels(labeledGroupName, label, description, commandlineResource.value)
self.saveLabelSet(commandlineResource, label, x_ids, y_ids, description, labeledGroupName,
'Labeled with %s as discriminator' % self.args.discriminator)
def loadLabeledData(self, labeledGroupName, classes): # pragma: no cover
'''
Retrieve from the database to set of Labeled data and return the standard X, and y arrays that the scikit-learn package uses
'''
if len(classes) > 2:
raise Exception('Maximum classes length is 2')
f0 = np.array(0)
f1 = np.array(0)
y = np.array(0, dtype=int)
target = 0
for label in classes:
mprs = MeasuredParameterResource.objects.using(self.args.database).filter(
resource__name=LABEL, resource__resourcetype__name=labeledGroupName,
resource__value=label
).values_list('measuredparameter__datavalue', flat=True)
count = mprs.filter(measuredparameter__parameter__name=self.args.inputs[0]).count()
if self.args.verbose:
print('count = {} for label = {}'.format(count, label))
if count == 0:
warnings.warn('count = 0 for label = {}'.format(label))
f0 = np.append(f0, mprs.filter(measuredparameter__parameter__name=self.args.inputs[0]))
f1 = np.append(f1, mprs.filter(measuredparameter__parameter__name=self.args.inputs[1]))
y = np.append(y, np.ones(count) * target)
target += 1
# Form the feature vectors into the X matrix that sklearn wants
X = np.concatenate((f0.reshape(-1,1), f1.reshape(-1,1)), axis=1)
return X, y
def doModelsScore(self, labeledGroupName):
'''
Print scores for several different classifiers
'''
X, y = self.loadLabeledData(labeledGroupName, classes=self.args.classes)
X = StandardScaler().fit_transform(X)
if X.any() and y.any():
for name, clf in list(self.classifiers.items()):
scores = cross_val_score(clf, X, y, cv=5)
print("%-18s accuracy: %0.2f (+/- %0.2f)" % (name, scores.mean(), scores.std() * 2))
else:
raise Exception('No data returned for labeledGroupName = %s' % labeledGroupName)
def createClassifier(self, labeledGroupName): # pragma: no cover
'''
Query the database for labeled training data, fit a model to it, and save the pickled
model back to the database. Follow the pattern in the example at
http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html
and learn about Learning at https://www.youtube.com/watch?v=4ONBVNm3isI (see at time 2:33 and
following - though the whole tutorial is worth watching).
'''
clf = self.classifiers[self.args.classifier]
X, y = self.loadLabeledData(labeledGroupName, classes=self.args.classes)
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=self.args.test_size, train_size=self.args.train_size)
import pdb
pdb.set_trace()
# TODO: Implement graphical evaluation as in http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
if self.args.verbose:
print(" score = %f" % score)
self._saveModel(labeledGroupName, clf)
def _saveModel(self, labeledGroupName, clf):
'''
Pickle and save the model in the database
'''
# Save pickled mode to the database and relate it to the LABELED data resource
if self.args.modelBaseName:
rt, _ = ResourceType.objects.using(self.args.database).get_or_create(name='FittedModel', description='SVC(gamma=2, C=1)')
labeledResource = Resource.objects.using(self.args.database).filter(resourcetype__name=labeledGroupName)[0]
modelValue = pickle.dumps(clf).encode("zip").encode("base64").strip()
modelResource = Resource(name=self.args.modelBaseName, value=modelValue, resourcetype=rt)
modelResource.save(using=self.args.database)
rr = ResourceResource(fromresource=labeledResource, toresource=modelResource)
rr.save(using=self.args.database)
if self.args.verbose:
print('Saved fitted model to the database with name = %s' % self.args.modelBaseName)
print('Retrieve with "clf = pickle.loads(r.value.decode("base64").decode("zip"))"')
def getFileName(self, figCount):
'''
Construct plot file name
'''
fileName = 'cpBiPlot_%02d' % figCount
if self.args.daytime:
fileName += '_day'
if self.args.nighttime:
fileName += '_night'
fileName += '.png'
fileName = os.path.join(self.args.plotDir, self.args.plotPrefix + fileName)
return fileName
def saveFigure(self, fig, figCount):
'''
Save this page
'''
provStr = 'Created with STOQS command ' + '\\\n'.join(wrap(self.commandline, width=160)) + ' on ' + datetime.now().ctime()
plt.figtext(0.0, 0.0, provStr, size=7, horizontalalignment='left', verticalalignment='bottom')
plt.tight_layout()
if self.args.title:
fig.text(0.5, 0.975, self.args.title, horizontalalignment='center', verticalalignment='top')
fileName = self.getFileName(figCount)
if self.args.verbose:
print(' Saving file', fileName)
fig.savefig(fileName)
def process_command_line(self):
'''
The argparse library is included in Python 2.7 and is an added package for STOQS.
'''
import argparse
from argparse import RawTextHelpFormatter
examples = 'Example machine learning workflow:' + '\n\n'
examples += "Step 1: Create Labeled features in the database using salinity as a discriminator:\n"
examples += sys.argv[0] + (" --createLabels --groupName Plankton --database stoqs_september2013_t"
" --platform dorado --start 20130916T124035 --end 20130919T233905"
" --inputs bbp700 fl700_uncorr --discriminator salinity"
" --labels diatom dino1 dino2 sediment --mins 33.33 33.65 33.70 33.75"
" --maxes 33.65 33.70 33.75 33.93 --clobber -v\n\n")
examples += "Step 2: Evaluate classifiers using the labels created in Step 1\n"
examples += sys.argv[0] + (" --doModelsScore --groupName Plankton --database stoqs_september2013_t"
" --classes diatom sediment --inputs bbp700 fl700_uncorr\n\n")
examples += "Step 3: Create a prediction model using the labels created in Step 1\n"
examples += sys.argv[0] + (" --createClassifier --groupName Plankton --database stoqs_september2013_t"
" --classifier Nearest_Neighbors --classes diatom sediment"
" --modelBaseName Nearest_Neighbors_1\n\n")
examples += "Step 4: Use a model to classify new measurements\n"
examples += '\nIf running from cde-package replace ".py" with ".py.cde" in the above list.'
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter,
description='Script to execute steps in the classification of measurements',
epilog=examples)
parser.add_argument('-p', '--platform', action='store', help='STOQS Platform name for training data access')
parser.add_argument('-d', '--database', action='store', help='Database alias', default='stoqs_september2013_o', required=True)
##parser.add_argument('--minDepth', action='store', help='Minimum depth for data queries', default=None, type=float)
##parser.add_argument('--maxDepth', action='store', help='Maximum depth for data queries', default=None, type=float)
parser.add_argument('--createLabels', action='store_true', help='Label data with --discriminator, --groupName --labels, --mins, and --maxes options')
parser.add_argument('--removeLabels', action='store_true', help='Remove Labels with --groupName option')
parser.add_argument('--createClassifier', action='store_true', help='Fit a model to Labeled data with --classifier to labels in --labels and save in database as --modelBaseName')
parser.add_argument('--doModelsScore', action='store_true', help='Print scores for fits of various models for --groupName')
parser.add_argument('--inputs', action='store', help='List of STOQS Parameter names to use as features, separated by spaces', nargs='*')
parser.add_argument('--start', action='store', help='Start time in YYYYMMDDTHHMMSS format', default='19000101T000000')
parser.add_argument('--end', action='store', help='End time in YYYYMMDDTHHMMSS format', default='22000101T000000')
parser.add_argument('--discriminator', action='store', help='Parameter name to use to discriminate the data')
parser.add_argument('--groupName', action='store', help='Name to follow "Labeled" in UI describing the group of --labels for --createLabels option')
parser.add_argument('--labels', action='store', help='List of labels to create separated by spaces', nargs='*')
parser.add_argument('--mins', action='store', help='List of labels to create separated by spaces', nargs='*')
parser.add_argument('--maxes', action='store', help='List of labels to create separated by spaces', nargs='*')
parser.add_argument('--test_size', action='store', help='Proportion of discriminated sample to save as Test set', default=0.4, type=float)
parser.add_argument('--train_size', action='store', help='Proportion of discriminated sample to save as Train set', default=0.4, type=float)
parser.add_argument('--classifier', choices=list(self.classifiers.keys()), help='Specify classifier to use with --createClassifier option')
parser.add_argument('--modelBaseName', action='store', help='Base name of the model to store in the database')
parser.add_argument('--classes', action='store', help='Labels to load from the database for --doModelsScore and --createClassifier', nargs='*')
parser.add_argument('--clobber', action='store_true', help='Remove existing MeasuredParameterResource records before adding new classification')
parser.add_argument('-v', '--verbose', nargs='?', choices=[1,2,3], type=int, help='Turn on verbose output. Higher number = more output.', const=1, default=0)
self.args = parser.parse_args()
self.commandline = ' '.join(sys.argv)
# Conditional tests
if self.args.doModelsScore:
if not c.args.classes:
parser.error('--doModelsScore requires --classes')
if not c.args.inputs:
parser.error('--doModelsScore requires --inputs')
if __name__ == '__main__':
c = Classifier()
c.process_command_line()
if c.args.createLabels:
c.createLabels(' '.join((LABELED, c.args.groupName)))
if c.args.removeLabels:
c.removeLabels(' '.join((LABELED, c.args.groupName)))
elif c.args.doModelsScore:
c.doModelsScore(' '.join((LABELED, c.args.groupName)))
elif c.args.createClassifier:
c.createClassifier(' '.join((LABELED, c.args.groupName)))