Skip to content

Commit 2d3e4ab

Browse files
authored
Merge pull request #123 from ODM2/sfresults
Sfresults
2 parents ab50ca8 + 817a78e commit 2d3e4ab

File tree

3 files changed

+85
-90
lines changed

3 files changed

+85
-90
lines changed

Examples/Sample.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
#connect to database
1818
# createconnection (dbtype, servername, dbname, username, password)
1919
# session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0)#sqlite
20-
session_factory = dbconnection.createConnection('postgresql', 'localhost', 'odm2', 'ODM', 'odm')
21-
# session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm')#mysql
2220

23-
# session_factory= dbconnection.createConnection('mssql', "(local)", "ODM2", "ODM", "odm")#win MSSQL
2421

22+
# session_factory = dbconnection.createConnection('postgresql', 'localhost', 'odm2', 'ODM', 'odm')
23+
# session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm')#mysql
24+
session_factory= dbconnection.createConnection('mssql', "(local)", "ODM2", "ODM", "odm")#win MSSQL
2525
# session_factory= dbconnection.createConnection('mssql', "arroyoodm2", "", "ODM", "odm")#mac/linux MSSQL
26-
# session_factory = dbconnection.createConnection('sqlite', '/Users/stephanie/DEV/YODA-Tools/tests/test_files/XL_specimen.sqlite', 2.0)
26+
# session_factory = dbconnection.createConnection('sqlite', 'path/to/ODM2.sqlite', 2.0)
2727

2828

2929

@@ -61,6 +61,7 @@
6161
try:
6262
print ("\n-------- Information about Sites ---------")
6363
siteFeatures = read.getSamplingFeatures(type= 'site')
64+
6465
# siteFeatures = read.getSamplingFeatures(type='Site')
6566
numSites = len(siteFeatures)
6667
print ("Successful query")
@@ -144,7 +145,7 @@
144145
# Get the values for a particular TimeSeriesResult
145146
print("\n-------- Example of Retrieving Time Series Result Values ---------")
146147

147-
tsValues = read.getResultValues(resultid = 1) # Return type is a pandas datafram
148+
tsValues = read.getResultValues(resultids = [1]) # Return type is a pandas datafram
148149

149150
# Print a few Time Series Values to the console
150151
# tsValues.set_index('ValueDateTime', inplace=True)

odm2api/ODM2/services/readService.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,8 @@ def getAffiliations(self, ids=None, personfirst=None, personlast=None, orgcode=N
614614
return None
615615

616616
# Results
617-
def getResults(self, ids=None, type=None, uuids=None, actionid=None, simulationid=None, sfid=None,
618-
variableid=None, siteid=None):
617+
def getResults(self, ids=None, type=None, restype = None, uuids=None, actionid=None, simulationid=None, sfid=None,
618+
variableid=None, siteid=None, sfids=None, sfuuids=None, sfcodes=None):
619619

620620
# TODO what if user sends in both type and actionid vs just actionid
621621
"""Retrieve a list of Result objects.
@@ -625,7 +625,7 @@ def getResults(self, ids=None, type=None, uuids=None, actionid=None, simulationi
625625
626626
Args:
627627
ids (list, optional): List of ResultIDs.
628-
type (str, optional): Type of Result from
628+
restype (str, optional): Type of Result from
629629
`controlled vocabulary name <http://vocabulary.odm2.org/resulttype/>`_.
630630
uuids (list, optional): List of UUIDs string.
631631
actionid (int, optional): ActionID.
@@ -634,14 +634,17 @@ def getResults(self, ids=None, type=None, uuids=None, actionid=None, simulationi
634634
variableid (int, optional): VariableID.
635635
siteid (int, optional): SiteID. - goes through related features table and finds all of results
636636
recorded at the given site
637+
sfids(list, optional): List of Sampling Feature IDs integer.
638+
sfuuids(list, optional): List of Sampling Feature UUIDs string.
639+
sfcodes=(list, optional): List of Sampling Feature codes string.
637640
638641
Returns:
639642
list: List of Result objects
640643
641644
Examples:
642645
>>> ReadODM2.getResults(ids=[39,40])
643-
>>> ReadODM2.getResults(type='Time series coverage')
644-
>>> ReadODM2.getResults(sfid=65)
646+
>>> ReadODM2.getResults(restype='Time series coverage')
647+
>>> ReadODM2.getResults(sfids=[65])
645648
>>> ReadODM2.getResults(uuids=['a6f114f1-5416-4606-ae10-23be32dbc202',
646649
... '5396fdf3-ceb3-46b6-aaf9-454a37278bb4'])
647650
>>> ReadODM2.getResults(simulationid=50)
@@ -653,7 +656,12 @@ def getResults(self, ids=None, type=None, uuids=None, actionid=None, simulationi
653656
query = self._session.query(Results)
654657

655658
if type:
659+
import warnings
660+
warnings.warn(
661+
"The parameter 'type' is no longer be supported. Please use the restype parameter instead.")
656662
query = query.filter_by(ResultTypeCV=type)
663+
if restype:
664+
query = query.filter_by(ResultTypeCV=restype)
657665
if variableid:
658666
query = query.filter_by(VariableID=variableid)
659667
if ids:
@@ -668,16 +676,32 @@ def getResults(self, ids=None, type=None, uuids=None, actionid=None, simulationi
668676
if actionid:
669677
query = query.join(FeatureActions).filter_by(ActionID=actionid)
670678
if sfid:
679+
import warnings
680+
warnings.warn("The parameter 'sfid' is no longer be supported. Please use the sfids parameter and send in a list.")
671681
query = query.join(FeatureActions).filter_by(SamplingFeatureID=sfid)
682+
if sfids or sfcodes or sfuuids:
683+
sf_list = self.getSamplingFeatures(ids=sfids, codes=sfcodes, uuids=sfuuids)
684+
sfids = []
685+
for sf in sf_list:
686+
sfids.append(sf.SamplingFeatureID)
687+
query = query.join(FeatureActions).filter(FeatureActions.SamplingFeatureID.in_(sfids))
672688

673689
if siteid:
690+
674691
sfids = [x[0] for x in self._session.query(
675692
distinct(SamplingFeatures.SamplingFeatureID))
676693
.select_from(RelatedFeatures)
677694
.join(RelatedFeatures.SamplingFeatureObj)
678695
.filter(RelatedFeatures.RelatedFeatureID == siteid)
679696
.all()
680697
]
698+
699+
#TODO does this code do the same thing as the code above?
700+
# sf_list = self.getRelatedSamplingFeatures(rfid=siteid)
701+
# sfids = []
702+
# for sf in sf_list:
703+
# sfids.append(sf.SamplingFeatureID)
704+
681705
query = query.join(FeatureActions).filter(FeatureActions.SamplingFeatureID.in_(sfids))
682706

683707
try:

tests/test_odm2/test_readservice.py

+50-80
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def setup(self):
7676
self.db = globals_vars['db']
7777

7878

79-
# Sampling Features
79+
# Sampling Features
8080
def test_getAllSamplingFeatures(self):
8181
# get all models from the database
8282
res = self.engine.execute('SELECT * FROM SamplingFeatures').fetchall()
@@ -91,6 +91,14 @@ def test_getSamplingFeatureByID(self):
9191
# get all simulations using the api
9292
resapi = self.reader.getSamplingFeatures(ids=[sfid])
9393
assert resapi is not None
94+
95+
def test_getSamplingFeatureByCode(self):
96+
# get all models from the database
97+
res = self.engine.execute('SELECT * FROM SamplingFeatures').fetchone()
98+
code = res[2]
99+
# get all simulations using the api
100+
resapi = self.reader.getSamplingFeatures(codes=[code])
101+
assert resapi is not None
94102

95103
#DataSets
96104
def test_getDataSets(self):
@@ -121,8 +129,6 @@ def test_getDataSetsValues(self):
121129
assert len(values) > 0
122130

123131

124-
125-
#ToDo figure out how to actually test this function
126132
def test_getSamplingFeatureDataSets(self):
127133

128134
#find a sampling feature that is associated with a dataset
@@ -151,18 +157,48 @@ def test_getSamplingFeatureDataSets(self):
151157
assert len(dsapi) > 0
152158
assert ds[1] == dsapi[0].DataSetID
153159

160+
# Results
161+
def test_getAllResults(self):
162+
# get all results from the database
163+
res = self.engine.execute('SELECT * FROM Results').fetchall()
164+
print(res)
165+
# get all results using the api
166+
resapi = self.reader.getResults()
167+
assert len(res) == len(resapi)
154168

155-
# Models
156-
"""
157-
TABLE Models
158-
ModelID INTEGER NOT NULL PRIMARY KEY,
159-
ModelCode VARCHAR (50) NOT NULL,
160-
ModelName VARCHAR (255) NOT NULL,
161-
ModelDescription VARCHAR (500) NULL,
162-
Version VARCHAR (255) NULL,
163-
ModelLink VARCHAR (255) NULL
164-
"""
169+
def test_getResultsByID(self):
170+
# get a result from the database
171+
res = self.engine.execute('SELECT * FROM Results').fetchone()
172+
resultid = res[1]
173+
174+
# get the result using the api
175+
resapi = self.reader.getResults(ids=[resultid])
176+
assert resapi is not None
165177

178+
def test_getResultsBySFID(self):
179+
sf = self.engine.execute(
180+
'SELECT * from SamplingFeatures as sf '
181+
'inner join FeatureActions as fa on fa.SamplingFeatureID == sf.SamplingFeatureID '
182+
'inner join Results as r on fa.FeatureActionID == r.FeatureActionID '
183+
).fetchone()
184+
assert len(sf) > 0
185+
sfid = sf[0]
186+
187+
res = self.engine.execute(
188+
'SELECT * from Results as r '
189+
'inner join FeatureActions as fa on fa.FeatureActionID == r.FeatureActionID '
190+
'where fa.SamplingFeatureID = ' + str(sfid)
191+
).fetchone()
192+
193+
assert len(res) > 0
194+
195+
# get the result using the api
196+
resapi = self.reader.getResults(sfids=[sfid])
197+
assert resapi is not None
198+
assert len(resapi) > 0
199+
assert resapi[0].ResultID == res[0]
200+
201+
# Models
166202
def test_getAllModels(self):
167203
# get all models from the database
168204
res = self.engine.execute('SELECT * FROM Models').fetchall()
@@ -179,19 +215,7 @@ def test_getModelByCode(self):
179215
assert resapi is not None
180216

181217

182-
# RelatedModels
183-
"""
184-
TABLE RelatedModels (
185-
RelatedID INTEGER NOT NULL PRIMARY KEY,
186-
ModelID INTEGER NOT NULL,
187-
RelationshipTypeCV VARCHAR (255) NOT NULL,
188-
RelatedModelID INTEGER NOT NULL,
189-
FOREIGN KEY (RelationshipTypeCV) REFERENCES CV_RelationshipType (Name)
190-
ON UPDATE NO ACTION ON DELETE NO ACTION,
191-
FOREIGN KEY (ModelID) REFERENCES Models (ModelID)
192-
ON UPDATE NO ACTION ON DELETE NO ACTION
193-
"""
194-
218+
# RelatedModels
195219
def test_getRelatedModelsByID(self):
196220
# get related models by id using the api
197221
resapi = self.reader.getRelatedModels(id=1)
@@ -214,61 +238,7 @@ def test_getRelatedModelsByCode(self):
214238
# test invalid argument
215239
resapi = self.reader.getRelatedModels(code=234123)
216240
assert not resapi
217-
218-
219-
# Results
220-
"""
221-
TABLE Results (
222-
ResultID INTEGER NOT NULL PRIMARY KEY,
223-
ResultUUID VARCHAR(36) NOT NULL,
224-
FeatureActionID INTEGER NOT NULL,
225-
ResultTypeCV VARCHAR (255) NOT NULL,
226-
VariableID INTEGER NOT NULL,
227-
UnitsID INTEGER NOT NULL,
228-
TaxonomicClassifierID INTEGER NULL,
229-
ProcessingLevelID INTEGER NOT NULL,
230-
ResultDateTime DATETIME NULL,
231-
ResultDateTimeUTCOffset INTEGER NULL,
232-
ValidDateTime DATETIME NULL,
233-
ValidDateTimeUTCOffset INTEGER NULL,
234-
StatusCV VARCHAR (255) NULL,
235-
SampledMediumCV VARCHAR (255) NOT NULL,
236-
ValueCount INTEGER NOT NULL
237-
"""
238-
def test_getAllResults(self):
239-
# get all results from the database
240-
res = self.engine.execute('SELECT * FROM Results').fetchall()
241-
# print(res)
242-
# get all results using the api
243-
resapi = self.reader.getResults()
244-
assert len(res) == len(resapi)
245-
246-
def test_getResultsByID(self):
247-
# get a result from the database
248-
res = self.engine.execute('SELECT * FROM Results').fetchone()
249-
resultid = res[1]
250-
251-
# get the result using the api
252-
resapi = self.reader.getResults(ids=[resultid])
253-
assert resapi is not None
254-
255241
# Simulations
256-
"""
257-
TABLE Simulations (
258-
SimulationID INTEGER NOT NULL PRIMARY KEY,
259-
ActionID INTEGER NOT NULL,
260-
SimulationName VARCHAR (255) NOT NULL,
261-
SimulationDescription VARCHAR (500) NULL,
262-
SimulationStartDateTime DATETIME NOT NULL,
263-
SimulationStartDateTimeUTCOffset INTEGER NOT NULL,
264-
SimulationEndDateTime DATETIME NOT NULL,
265-
SimulationEndDateTimeUTCOffset INTEGER NOT NULL,
266-
TimeStepValue FLOAT NOT NULL,
267-
TimeStepUnitsID INTEGER NOT NULL,
268-
InputDataSetID INTEGER NULL,
269-
ModelID INTEGER NOT NULL,
270-
"""
271-
272242
def test_getAllSimulations(self):
273243
# get all simulation from the database
274244
res = self.engine.execute('SELECT * FROM Simulations').fetchall()

0 commit comments

Comments
 (0)