Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ script:
fi

- if [[ $TEST_TARGET == 'coding_standards' ]]; then
find . -type f -name "*.py" ! -name 'conf.py' ! -name '_version.py' ! -name 'versioneer.py' | xargs flake8 --max-line-length=110 ;
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 ;
fi

- if [[ $TEST_TARGET == 'docs' ]]; then
Expand Down
110 changes: 10 additions & 100 deletions odm2api/ODM2/models.py

Large diffs are not rendered by default.

100 changes: 51 additions & 49 deletions odm2api/ODM2/services/readService.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import (absolute_import, division, print_function)

import warnings

from odm2api.ODM2 import serviceBase
from odm2api.ODM2.models import (
ActionAnnotations, ActionDirectives, ActionExtensionPropertyValues, Actions,
Expand Down Expand Up @@ -29,7 +31,7 @@
SpatialReferenceExternalIdentifiers, SpatialReferences, SpecimenBatchPositions,
SpectraResultValueAnnotations, SpectraResultValues, TaxonomicClassifierExternalIdentifiers,
TaxonomicClassifiers, TimeSeriesResultValueAnnotations, TimeSeriesResultValues,
TimeSeriesResults, TrajectoryResultValueAnnotations, TrajectoryResultValues,
TrajectoryResultValueAnnotations, TrajectoryResultValues,
TransectResultValueAnnotations, TransectResultValues, Units, VariableExtensionPropertyValues,
VariableExternalIdentifiers, Variables,
)
Expand All @@ -38,8 +40,6 @@

from sqlalchemy import distinct, exists

import warnings

__author__ = 'sreeder'


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


class SamplingFeatureDataSet():
datasets={}
related_features={}
datasets = {}
related_features = {}

def __init__(self, samplingfeature, datasetresults, relatedfeatures):
sf = samplingfeature

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


print(self.datasets)

def assignDatasets(self, datasetresults):
self.datasets = {}
if datasetresults:
for dsr in datasetresults:
if dsr.DataSetObj not in self.datasets:
#if the dataset is not in the dictionary, add it and the first result
self.datasets[dsr.DataSetObj]=[]
# if the dataset is not in the dictionary, add it and the first result
self.datasets[dsr.DataSetObj] = []
res = dsr.ResultObj
# res.FeatureActionObj = None
self.datasets[dsr.DataSetObj].append(res)
else:
#if the dataset is in the dictionary, append the result object to the list
# if the dataset is in the dictionary, append the result object to the list
res = dsr.ResultObj
# res.FeatureActionObj = None
self.datasets[dsr.DataSetObj].append(res)


def assignRelatedFeatures(self, relatedfeatures):
self.related_features = {}
if relatedfeatures:
Expand Down Expand Up @@ -169,7 +168,8 @@ def resultExists(self, result):
)
return ret.scalar()

except:
except Exception as e:
print('Error running Query: {}'.format(e))
return None

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

except:
except Exception as e:
print('Error running Query: {}'.format(e))
return None

# CV
Expand Down Expand Up @@ -384,15 +385,16 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
variables = [
x[0] for x in
self._session.query(distinct(Results.VariableID))
.filter(Results.FeatureActionID == FeatureActions.FeatureActionID)
.filter(FeatureActions.SamplingFeatureID == SamplingFeatures.SamplingFeatureID)
.filter(SamplingFeatures.SamplingFeatureCode == sitecode).all()
.filter(Results.FeatureActionID == FeatureActions.FeatureActionID)
.filter(FeatureActions.SamplingFeatureID == SamplingFeatures.SamplingFeatureID)
.filter(SamplingFeatures.SamplingFeatureCode == sitecode).all()
]
if ids:
ids = list(set(ids).intersection(variables))
else:
ids = variables
except:
except Exception as e:
print('Error running Query: {}'.format(e))
pass

if results:
Expand All @@ -402,7 +404,8 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
ids = list(set(ids).intersection(variables))
else:
ids = variables
except:
except Exception as e:
print('Error running Query: {}'.format(e))
pass

query = self._session.query(Variables)
Expand Down Expand Up @@ -520,10 +523,10 @@ def getSamplingFeatures(self, ids=None, codes=None, uuids=None,
if results:
try:
fas = [x[0] for x in self._session.query(distinct(Results.FeatureActionID)).all()]
except:
except Exception as e:
print('Error running Query: {}'.format(e))
return None
sf = [x[0] for x in self._session.query(distinct(FeatureActions.SamplingFeatureID))
.filter(FeatureActions.FeatureActionID.in_(fas)).all()]
sf = [x[0] for x in self._session.query(distinct(FeatureActions.SamplingFeatureID)).filter(FeatureActions.FeatureActionID.in_(fas)).all()] # noqa
if ids:
ids = list(set(ids).intersection(sf))
else:
Expand Down Expand Up @@ -557,8 +560,8 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid=None, relationshiptype=None

"""

sf = self._session.query(distinct(SamplingFeatures.SamplingFeatureID))\
.select_from(RelatedFeatures)
sf = self._session.query(distinct(SamplingFeatures.SamplingFeatureID)) \
.select_from(RelatedFeatures)

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



# Action
def getActions(self, ids=None, acttype=None, sfid=None, **kwargs):
"""
Expand Down Expand Up @@ -656,7 +657,6 @@ def getUnits(self, ids=None, name=None, unittype=None, **kwargs):
print('Error running Query: {}'.format(e))
return None


# Organization
def getOrganizations(self, ids=None, codes=None):
"""
Expand Down Expand Up @@ -738,7 +738,7 @@ def getAffiliations(self, ids=None, personfirst=None, personlast=None, orgcode=N
return None

# Results
def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simulationid=None,
def getResults(self, ids=None, restype=None, uuids=None, actionid=None, simulationid=None,
variableid=None, siteid=None, sfids=None, sfuuids=None, sfcodes=None, **kwargs):

# TODO what if user sends in both type and actionid vs just actionid
Expand Down Expand Up @@ -791,10 +791,10 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
if uuids:
query = query.filter(Results.ResultUUID.in_(uuids))
if simulationid:
query = query.join(FeatureActions)\
.join(Actions)\
.join(Simulations)\
.filter_by(SimulationID=simulationid)
query = query.join(FeatureActions) \
.join(Actions) \
.join(Simulations) \
.filter_by(SimulationID=simulationid)
if actionid:
query = query.join(FeatureActions).filter_by(ActionID=actionid)
if 'sfid' in kwargs:
Expand All @@ -810,16 +810,15 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
query = query.join(FeatureActions).filter(FeatureActions.SamplingFeatureID.in_(sfids))

if siteid:

sfids = [x[0] for x in self._session.query(
distinct(SamplingFeatures.SamplingFeatureID))
.select_from(RelatedFeatures)
.join(RelatedFeatures.SamplingFeatureObj)
.filter(RelatedFeatures.RelatedFeatureID == siteid)
.all()
]
]

#TODO does this code do the same thing as the code above?
# TODO does this code do the same thing as the code above?
# sf_list = self.getRelatedSamplingFeatures(rfid=siteid)
# sfids = []
# for sf in sf_list:
Expand All @@ -834,7 +833,7 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
return None

# Datasets
def getDataSets(self, ids= None, codes=None, uuids=None, dstype=None):
def getDataSets(self, ids=None, codes=None, uuids=None, dstype=None):
"""
Retrieve a list of Datasets

Expand Down Expand Up @@ -906,7 +905,7 @@ def getDataSetsResults(self, ids=None, codes=None, uuids=None, dstype=None):
if all(v is None for v in [ids, codes, uuids]):
raise ValueError('Expected DataSetID OR DataSetUUID OR DataSetCode argument')

q = self._session.query(DataSetsResults)\
q = self._session.query(DataSetsResults) \
.join(DataSets)
if ids:
q = q.filter(DataSets.DataSetID.in_(ids))
Expand Down Expand Up @@ -955,12 +954,11 @@ def getDataSetsValues(self, ids=None, codes=None, uuids=None, dstype=None):
resids.append(ds.ResultID)

try:
return self.getResultValues(resultids = resids)
return self.getResultValues(resultids=resids)
except Exception as e:
print('Error running Query {}'.format(e))
return None


def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None, sftype=None):
"""
Retrieve a list of Datasets associated with the given sampling feature data.
Expand Down Expand Up @@ -990,11 +988,12 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No

"""


# make sure one of the three arguments has been sent in
if all(v is None for v in [ids, codes, uuids, sftype]):
raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode OR samplingFeatureType '
'argument')
raise ValueError(
'Expected samplingFeatureID OR samplingFeatureUUID '
'OR samplingFeatureCode OR samplingFeatureType '
'argument')

sf_query = self._session.query(SamplingFeatures)
if sftype:
Expand All @@ -1011,18 +1010,17 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
sf_list.append(sf)

try:
sfds=[]
sfds = []
for sf in sf_list:

q = self._session.query(DataSetsResults)\
.join(Results)\
.join(FeatureActions)\
q = self._session.query(DataSetsResults) \
.join(Results) \
.join(FeatureActions) \
.filter(FeatureActions.SamplingFeatureID == sf.SamplingFeatureID)

if dstype:
q = q.filter_by(DatasetTypeCV=dstype)


vals = q.all()

related = self.getRelatedSamplingFeatures(sf.SamplingFeatureID)
Expand Down Expand Up @@ -1081,17 +1079,21 @@ def getEquipment(self, codes=None, equiptype=None, sfid=None, actionid=None, **k
warnings.warn('The parameter \'type\' is deprecated. Please use the equiptype parameter instead.',
DeprecationWarning, stacklevel=2)
equiptype = kwargs['type']

# NOTE: Equiptype currently unused!
if equiptype:
pass
e = self._session.query(Equipment)
if sfid:
e = e.join(EquipmentUsed) \
.join(Actions) \
.join(FeatureActions) \
.filter(FeatureActions.SamplingFeatureID == sfid)
.join(Actions) \
.join(FeatureActions) \
.filter(FeatureActions.SamplingFeatureID == sfid)
if codes:
e = e.filter(Equipment.EquipmentCode.in_(codes))
if actionid:
e = e.join(EquipmentUsed).join(Actions) \
.filter(Actions.ActionID == actionid)
.filter(Actions.ActionID == actionid)
return e.all()

def CalibrationReferenceEquipment(self):
Expand Down
Loading