Skip to content

Commit 5164ebe

Browse files
committed
Merge branch 'fix_grv' of https://github.com/lsetiawan/ODM2PythonAPI into fix_grv
2 parents badb498 + dbca706 commit 5164ebe

File tree

7 files changed

+99
-226
lines changed

7 files changed

+99
-226
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ script:
7474
fi
7575

7676
- if [[ $TEST_TARGET == 'coding_standards' ]]; then
77-
find . -type f -name "*.py" ! -name 'conf.py' ! -name '_version.py' ! -name 'versioneer.py' | xargs flake8 --max-line-length=110 ;
77+
find . -type f -name "*.py" ! -name 'conf.py' ! -name '_version.py' ! -name 'versioneer.py' ! -name '*DBConfig.py' ! -name 'Sample.py' | xargs flake8 --max-line-length=110 ;
7878
fi
7979

8080
- if [[ $TEST_TARGET == 'docs' ]]; then

odm2api/ODM2/models.py

Lines changed: 10 additions & 100 deletions
Large diffs are not rendered by default.

odm2api/ODM2/services/readService.py

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import (absolute_import, division, print_function)
22

3+
import warnings
4+
35
from odm2api.ODM2 import serviceBase
46
from odm2api.ODM2.models import (
57
ActionAnnotations, ActionDirectives, ActionExtensionPropertyValues, Actions,
@@ -29,7 +31,7 @@
2931
SpatialReferenceExternalIdentifiers, SpatialReferences, SpecimenBatchPositions,
3032
SpectraResultValueAnnotations, SpectraResultValues, TaxonomicClassifierExternalIdentifiers,
3133
TaxonomicClassifiers, TimeSeriesResultValueAnnotations, TimeSeriesResultValues,
32-
TimeSeriesResults, TrajectoryResultValueAnnotations, TrajectoryResultValues,
34+
TrajectoryResultValueAnnotations, TrajectoryResultValues,
3335
TransectResultValueAnnotations, TransectResultValues, Units, VariableExtensionPropertyValues,
3436
VariableExternalIdentifiers, Variables,
3537
)
@@ -38,8 +40,6 @@
3840

3941
from sqlalchemy import distinct, exists
4042

41-
import warnings
42-
4343
__author__ = 'sreeder'
4444

4545

@@ -75,8 +75,9 @@ def __init__(self, affiliation, person, org):
7575

7676

7777
class SamplingFeatureDataSet():
78-
datasets={}
79-
related_features={}
78+
datasets = {}
79+
related_features = {}
80+
8081
def __init__(self, samplingfeature, datasetresults, relatedfeatures):
8182
sf = samplingfeature
8283

@@ -94,26 +95,24 @@ def __init__(self, samplingfeature, datasetresults, relatedfeatures):
9495
self.assignDatasets(datasetresults)
9596
self.assignRelatedFeatures(relatedfeatures)
9697

97-
9898
print(self.datasets)
9999

100100
def assignDatasets(self, datasetresults):
101101
self.datasets = {}
102102
if datasetresults:
103103
for dsr in datasetresults:
104104
if dsr.DataSetObj not in self.datasets:
105-
#if the dataset is not in the dictionary, add it and the first result
106-
self.datasets[dsr.DataSetObj]=[]
105+
# if the dataset is not in the dictionary, add it and the first result
106+
self.datasets[dsr.DataSetObj] = []
107107
res = dsr.ResultObj
108108
# res.FeatureActionObj = None
109109
self.datasets[dsr.DataSetObj].append(res)
110110
else:
111-
#if the dataset is in the dictionary, append the result object to the list
111+
# if the dataset is in the dictionary, append the result object to the list
112112
res = dsr.ResultObj
113113
# res.FeatureActionObj = None
114114
self.datasets[dsr.DataSetObj].append(res)
115115

116-
117116
def assignRelatedFeatures(self, relatedfeatures):
118117
self.related_features = {}
119118
if relatedfeatures:
@@ -169,7 +168,8 @@ def resultExists(self, result):
169168
)
170169
return ret.scalar()
171170

172-
except:
171+
except Exception as e:
172+
print('Error running Query: {}'.format(e))
173173
return None
174174

175175
# Annotations
@@ -225,7 +225,8 @@ def getAnnotations(self, annottype=None, codes=None, ids=None, **kwargs):
225225
query = query.filter(Annotations.AnnotationID.in_(ids))
226226
return query.all()
227227

228-
except:
228+
except Exception as e:
229+
print('Error running Query: {}'.format(e))
229230
return None
230231

231232
# CV
@@ -384,15 +385,16 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
384385
variables = [
385386
x[0] for x in
386387
self._session.query(distinct(Results.VariableID))
387-
.filter(Results.FeatureActionID == FeatureActions.FeatureActionID)
388-
.filter(FeatureActions.SamplingFeatureID == SamplingFeatures.SamplingFeatureID)
389-
.filter(SamplingFeatures.SamplingFeatureCode == sitecode).all()
388+
.filter(Results.FeatureActionID == FeatureActions.FeatureActionID)
389+
.filter(FeatureActions.SamplingFeatureID == SamplingFeatures.SamplingFeatureID)
390+
.filter(SamplingFeatures.SamplingFeatureCode == sitecode).all()
390391
]
391392
if ids:
392393
ids = list(set(ids).intersection(variables))
393394
else:
394395
ids = variables
395-
except:
396+
except Exception as e:
397+
print('Error running Query: {}'.format(e))
396398
pass
397399

398400
if results:
@@ -402,7 +404,8 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
402404
ids = list(set(ids).intersection(variables))
403405
else:
404406
ids = variables
405-
except:
407+
except Exception as e:
408+
print('Error running Query: {}'.format(e))
406409
pass
407410

408411
query = self._session.query(Variables)
@@ -520,10 +523,10 @@ def getSamplingFeatures(self, ids=None, codes=None, uuids=None,
520523
if results:
521524
try:
522525
fas = [x[0] for x in self._session.query(distinct(Results.FeatureActionID)).all()]
523-
except:
526+
except Exception as e:
527+
print('Error running Query: {}'.format(e))
524528
return None
525-
sf = [x[0] for x in self._session.query(distinct(FeatureActions.SamplingFeatureID))
526-
.filter(FeatureActions.FeatureActionID.in_(fas)).all()]
529+
sf = [x[0] for x in self._session.query(distinct(FeatureActions.SamplingFeatureID)).filter(FeatureActions.FeatureActionID.in_(fas)).all()] # noqa
527530
if ids:
528531
ids = list(set(ids).intersection(sf))
529532
else:
@@ -557,8 +560,8 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid=None, relationshiptype=None
557560
558561
"""
559562

560-
sf = self._session.query(distinct(SamplingFeatures.SamplingFeatureID))\
561-
.select_from(RelatedFeatures)
563+
sf = self._session.query(distinct(SamplingFeatures.SamplingFeatureID)) \
564+
.select_from(RelatedFeatures)
562565

563566
if sfid:
564567
sf = sf.join(RelatedFeatures.RelatedFeatureObj).filter(RelatedFeatures.SamplingFeatureID == sfid)
@@ -576,8 +579,6 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid=None, relationshiptype=None
576579
print('Error running Query: {}'.format(e))
577580
return None
578581

579-
580-
581582
# Action
582583
def getActions(self, ids=None, acttype=None, sfid=None, **kwargs):
583584
"""
@@ -656,7 +657,6 @@ def getUnits(self, ids=None, name=None, unittype=None, **kwargs):
656657
print('Error running Query: {}'.format(e))
657658
return None
658659

659-
660660
# Organization
661661
def getOrganizations(self, ids=None, codes=None):
662662
"""
@@ -738,7 +738,7 @@ def getAffiliations(self, ids=None, personfirst=None, personlast=None, orgcode=N
738738
return None
739739

740740
# Results
741-
def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simulationid=None,
741+
def getResults(self, ids=None, restype=None, uuids=None, actionid=None, simulationid=None,
742742
variableid=None, siteid=None, sfids=None, sfuuids=None, sfcodes=None, **kwargs):
743743

744744
# TODO what if user sends in both type and actionid vs just actionid
@@ -791,10 +791,10 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
791791
if uuids:
792792
query = query.filter(Results.ResultUUID.in_(uuids))
793793
if simulationid:
794-
query = query.join(FeatureActions)\
795-
.join(Actions)\
796-
.join(Simulations)\
797-
.filter_by(SimulationID=simulationid)
794+
query = query.join(FeatureActions) \
795+
.join(Actions) \
796+
.join(Simulations) \
797+
.filter_by(SimulationID=simulationid)
798798
if actionid:
799799
query = query.join(FeatureActions).filter_by(ActionID=actionid)
800800
if 'sfid' in kwargs:
@@ -811,16 +811,15 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
811811
query = query.join(FeatureActions).filter(FeatureActions.SamplingFeatureID.in_(sfids))
812812

813813
if siteid:
814-
815814
sfids = [x[0] for x in self._session.query(
816815
distinct(SamplingFeatures.SamplingFeatureID))
817816
.select_from(RelatedFeatures)
818817
.join(RelatedFeatures.SamplingFeatureObj)
819818
.filter(RelatedFeatures.RelatedFeatureID == siteid)
820819
.all()
821-
]
820+
]
822821

823-
#TODO does this code do the same thing as the code above?
822+
# TODO does this code do the same thing as the code above?
824823
# sf_list = self.getRelatedSamplingFeatures(rfid=siteid)
825824
# sfids = []
826825
# for sf in sf_list:
@@ -835,7 +834,7 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
835834
return None
836835

837836
# Datasets
838-
def getDataSets(self, ids= None, codes=None, uuids=None, dstype=None):
837+
def getDataSets(self, ids=None, codes=None, uuids=None, dstype=None):
839838
"""
840839
Retrieve a list of Datasets
841840
@@ -907,7 +906,7 @@ def getDataSetsResults(self, ids=None, codes=None, uuids=None, dstype=None):
907906
if all(v is None for v in [ids, codes, uuids]):
908907
raise ValueError('Expected DataSetID OR DataSetUUID OR DataSetCode argument')
909908

910-
q = self._session.query(DataSetsResults)\
909+
q = self._session.query(DataSetsResults) \
911910
.join(DataSets)
912911
if ids:
913912
q = q.filter(DataSets.DataSetID.in_(ids))
@@ -963,7 +962,6 @@ def getDataSetsValues(self, ids=None, codes=None, uuids=None, dstype=None, lower
963962
print('Error running Query {}'.format(e))
964963
return None
965964

966-
967965
def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None, sftype=None):
968966
"""
969967
Retrieve a list of Datasets associated with the given sampling feature data.
@@ -993,11 +991,12 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
993991
994992
"""
995993

996-
997994
# make sure one of the three arguments has been sent in
998995
if all(v is None for v in [ids, codes, uuids, sftype]):
999-
raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode OR samplingFeatureType '
1000-
'argument')
996+
raise ValueError(
997+
'Expected samplingFeatureID OR samplingFeatureUUID '
998+
'OR samplingFeatureCode OR samplingFeatureType '
999+
'argument')
10011000

10021001
sf_query = self._session.query(SamplingFeatures)
10031002
if sftype:
@@ -1014,18 +1013,17 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
10141013
sf_list.append(sf)
10151014

10161015
try:
1017-
sfds=[]
1016+
sfds = []
10181017
for sf in sf_list:
10191018

1020-
q = self._session.query(DataSetsResults)\
1021-
.join(Results)\
1022-
.join(FeatureActions)\
1019+
q = self._session.query(DataSetsResults) \
1020+
.join(Results) \
1021+
.join(FeatureActions) \
10231022
.filter(FeatureActions.SamplingFeatureID == sf.SamplingFeatureID)
10241023

10251024
if dstype:
10261025
q = q.filter_by(DatasetTypeCV=dstype)
10271026

1028-
10291027
vals = q.all()
10301028

10311029
related = self.getRelatedSamplingFeatures(sf.SamplingFeatureID)
@@ -1084,17 +1082,21 @@ def getEquipment(self, codes=None, equiptype=None, sfid=None, actionid=None, **k
10841082
warnings.warn('The parameter \'type\' is deprecated. Please use the equiptype parameter instead.',
10851083
DeprecationWarning, stacklevel=2)
10861084
equiptype = kwargs['type']
1085+
1086+
# NOTE: Equiptype currently unused!
1087+
if equiptype:
1088+
pass
10871089
e = self._session.query(Equipment)
10881090
if sfid:
10891091
e = e.join(EquipmentUsed) \
1090-
.join(Actions) \
1091-
.join(FeatureActions) \
1092-
.filter(FeatureActions.SamplingFeatureID == sfid)
1092+
.join(Actions) \
1093+
.join(FeatureActions) \
1094+
.filter(FeatureActions.SamplingFeatureID == sfid)
10931095
if codes:
10941096
e = e.filter(Equipment.EquipmentCode.in_(codes))
10951097
if actionid:
10961098
e = e.join(EquipmentUsed).join(Actions) \
1097-
.filter(Actions.ActionID == actionid)
1099+
.filter(Actions.ActionID == actionid)
10981100
return e.all()
10991101

11001102
def CalibrationReferenceEquipment(self):

0 commit comments

Comments
 (0)