Skip to content

Commit cb30fbe

Browse files
authored
Merge pull request #136 from lsetiawan/fix_getresultvalues
Fix getresultvalues
2 parents ae0bdfd + b87dab2 commit cb30fbe

File tree

1 file changed

+59
-37
lines changed

1 file changed

+59
-37
lines changed

odm2api/ODM2/services/readService.py

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,23 @@ def assignRelatedFeatures(self, relatedfeatures):
120120
self.related_features = related
121121

122122

123+
class ReadODM2(serviceBase):
124+
def _get_columns(self, model):
125+
"""Internal helper function to get a dictionary of a model column properties.
123126
127+
Args:
128+
model (object): Sqlalchemy object, Ex. ODM2 model.
124129
130+
Returns:
131+
dict: Dictionary of column properties Ex. {'resultid': 'ResultID'}
132+
133+
"""
134+
from sqlalchemy.orm.properties import ColumnProperty
135+
columns = [(prop.key.lower(), prop.key) for prop in model.__mapper__.iterate_properties if
136+
isinstance(prop, ColumnProperty)]
137+
138+
return dict(columns)
125139

126-
class ReadODM2(serviceBase):
127140
# Exists functions
128141
def resultExists(self, result):
129142
"""
@@ -1261,46 +1274,54 @@ def getResultDerivationEquations(self):
12611274
"""
12621275
return self._session.query(ResultDerivationEquations).all()
12631276

1264-
# Results
1265-
# ResultValues
12661277
def getResultValues(self, resultids, starttime=None, endtime=None):
12671278
"""
1268-
getResultValues(self, resultids, starttime=None, endtime=None)
1269-
* Pass in a list of ResultID - Returns a pandas dataframe object of type
1270-
that is specific to the result type - The resultids must be associated
1271-
with the same value type
1272-
* Pass a ResultID and a date range - returns a pandas dataframe object
1273-
of type that is specific to the result type with values between the input date range
1274-
* Pass a starttime - Returns a dataframe with the values after the given start time
1275-
* Pass an endtime - Returns a dataframe with the values before the given end time
1276-
1277-
"""
1278-
type = self._session.query(Results).filter_by(ResultID=resultids[0]).first().ResultTypeCV
1279-
ResultType = TimeSeriesResultValues
1280-
if 'categorical' in type.lower():
1281-
ResultType = CategoricalResultValues
1282-
elif 'measurement' in type.lower():
1283-
ResultType = MeasurementResultValues
1284-
elif 'point' in type.lower():
1285-
ResultType = PointCoverageResultValues
1286-
elif 'profile' in type.lower():
1287-
ResultType = ProfileResultValues
1288-
elif 'section' in type.lower():
1289-
ResultType = SectionResults
1290-
elif 'spectra' in type.lower():
1291-
ResultType = SpectraResultValues
1292-
elif 'time' in type.lower():
1293-
ResultType = TimeSeriesResultValues
1294-
elif 'trajectory' in type.lower():
1295-
ResultType = TrajectoryResultValues
1296-
elif 'transect' in type.lower():
1297-
ResultType = TransectResultValues
1298-
1299-
q = self._session.query(ResultType).filter(ResultType.ResultID.in_(resultids))
1279+
Retrieve result values associated with the given result.
1280+
1281+
**The resultids must be associated with the same result type**
1282+
Args:
1283+
resultids (list): List of SamplingFeatureIDs.
1284+
starttime (object, optional): Start time to filter by as datetime object.
1285+
endtime (object, optional): End time to filter by as datetime object.
1286+
1287+
Returns:
1288+
DataFrame: Pandas dataframe of result values.
1289+
1290+
Examples:
1291+
>>> READ = ReadODM2(SESSION_FACTORY)
1292+
>>> READ.getResultValues(resultids=[10, 11])
1293+
>>> READ.getResultValues(resultids=[100, 20, 34], starttime=datetime.today())
1294+
>>> READ.getResultValues(resultids=[1, 2, 3, 4],
1295+
>>> starttime=datetime(2000, 01, 01),
1296+
>>> endtime=datetime(2003, 02, 01))
1297+
1298+
"""
1299+
restype = self._session.query(Results).filter_by(ResultID=resultids[0]).first().ResultTypeCV
1300+
ResultValues = TimeSeriesResultValues
1301+
if 'categorical' in restype.lower():
1302+
ResultValues = CategoricalResultValues
1303+
elif 'measurement' in restype.lower():
1304+
ResultValues = MeasurementResultValues
1305+
elif 'point' in restype.lower():
1306+
ResultValues = PointCoverageResultValues
1307+
elif 'profile' in restype.lower():
1308+
ResultValues = ProfileResultValues
1309+
elif 'section' in restype.lower():
1310+
ResultValues = SectionResults
1311+
elif 'spectra' in restype.lower():
1312+
ResultValues = SpectraResultValues
1313+
elif 'time' in restype.lower():
1314+
ResultValues = TimeSeriesResultValues
1315+
elif 'trajectory' in restype.lower():
1316+
ResultValues = TrajectoryResultValues
1317+
elif 'transect' in restype.lower():
1318+
ResultValues = TransectResultValues
1319+
1320+
q = self._session.query(ResultValues).filter(ResultValues.ResultID.in_(resultids))
13001321
if starttime:
1301-
q = q.filter(ResultType.ValueDateTime >= starttime)
1322+
q = q.filter(ResultValues.ValueDateTime >= starttime)
13021323
if endtime:
1303-
q = q.filter(ResultType.ValueDateTime <= endtime)
1324+
q = q.filter(ResultValues.ValueDateTime <= endtime)
13041325
try:
13051326
# F841 local variable 'vals' is assigned to but never used
13061327
# vals = q.order_by(ResultType.ValueDateTime)
@@ -1310,6 +1331,7 @@ def getResultValues(self, resultids, starttime=None, endtime=None):
13101331
con=self._session_factory.engine,
13111332
params=query.params
13121333
)
1334+
df.columns = [self._get_columns(ResultValues)[c] for c in df.columns]
13131335
return df
13141336
except Exception as e:
13151337
print('Error running Query: {}'.format(e))

0 commit comments

Comments
 (0)