From baee8b5d1720d1d5aa768d26e2a9e3f87242fddc Mon Sep 17 00:00:00 2001 From: lsetiawan Date: Fri, 5 Jan 2018 10:51:20 -0800 Subject: [PATCH 1/3] Fix dataframe column names --- odm2api/ODM2/services/readService.py | 36 +++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index a7ed9f9..46c6c2f 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -120,10 +120,23 @@ def assignRelatedFeatures(self, relatedfeatures): self.related_features = related +class ReadODM2(serviceBase): + def _get_columns(self, model): + """Internal helper function to get a dictionary of a model column properties. + Args: + model (object): Sqlalchemy object, Ex. ODM2 model. + Returns: + dict: Dictionary of column properties Ex. {'resultid': 'ResultID'} + + """ + from sqlalchemy.orm.properties import ColumnProperty + columns = [(prop.key.lower(), prop.key) for prop in model.__mapper__.iterate_properties if + isinstance(prop, ColumnProperty)] + + return dict(columns) -class ReadODM2(serviceBase): # Exists functions def resultExists(self, result): """ @@ -1275,25 +1288,25 @@ def getResultValues(self, resultids, starttime=None, endtime=None): * Pass an endtime - Returns a dataframe with the values before the given end time """ - type = self._session.query(Results).filter_by(ResultID=resultids[0]).first().ResultTypeCV + restype = self._session.query(Results).filter_by(ResultID=resultids[0]).first().ResultTypeCV ResultType = TimeSeriesResultValues - if 'categorical' in type.lower(): + if 'categorical' in restype.lower(): ResultType = CategoricalResultValues - elif 'measurement' in type.lower(): + elif 'measurement' in restype.lower(): ResultType = MeasurementResultValues - elif 'point' in type.lower(): + elif 'point' in restype.lower(): ResultType = PointCoverageResultValues - elif 'profile' in type.lower(): + elif 'profile' in restype.lower(): ResultType = ProfileResultValues - elif 'section' in type.lower(): + elif 'section' in restype.lower(): ResultType = SectionResults - elif 'spectra' in type.lower(): + elif 'spectra' in restype.lower(): ResultType = SpectraResultValues - elif 'time' in type.lower(): + elif 'time' in restype.lower(): ResultType = TimeSeriesResultValues - elif 'trajectory' in type.lower(): + elif 'trajectory' in restype.lower(): ResultType = TrajectoryResultValues - elif 'transect' in type.lower(): + elif 'transect' in restype.lower(): ResultType = TransectResultValues q = self._session.query(ResultType).filter(ResultType.ResultID.in_(resultids)) @@ -1310,6 +1323,7 @@ def getResultValues(self, resultids, starttime=None, endtime=None): con=self._session_factory.engine, params=query.params ) + df.columns = [self._get_columns(ResultType)[c] for c in df.columns] return df except Exception as e: print('Error running Query: {}'.format(e)) From 32519f6a7aabc20ff9906419a93dc9edfde112ee Mon Sep 17 00:00:00 2001 From: lsetiawan Date: Fri, 5 Jan 2018 11:07:35 -0800 Subject: [PATCH 2/3] Update docstrings --- odm2api/ODM2/services/readService.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index 46c6c2f..04145b3 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -1274,18 +1274,26 @@ def getResultDerivationEquations(self): """ return self._session.query(ResultDerivationEquations).all() - # Results - # ResultValues def getResultValues(self, resultids, starttime=None, endtime=None): """ - getResultValues(self, resultids, starttime=None, endtime=None) - * Pass in a list of ResultID - Returns a pandas dataframe object of type - that is specific to the result type - The resultids must be associated - with the same value type - * Pass a ResultID and a date range - returns a pandas dataframe object - of type that is specific to the result type with values between the input date range - * Pass a starttime - Returns a dataframe with the values after the given start time - * Pass an endtime - Returns a dataframe with the values before the given end time + Retrieve result values associated with the given result. + + **The resultids must be associated with the same result type** + Args: + resultids (list): List of SamplingFeatureIDs. + starttime (object, optional): Start time to filter by as datetime object. + endtime (object, optional): End time to filter by as datetime object. + + Returns: + DataFrame: Pandas dataframe of result values. + + Examples: + >>> READ = ReadODM2(SESSION_FACTORY) + >>> READ.getResultValues(resultids=[10, 11]) + >>> READ.getResultValues(resultids=[100, 20, 34], starttime=datetime.today()) + >>> READ.getResultValues(resultids=[1, 2, 3, 4], + >>> starttime=datetime(2000, 01, 01), + >>> endtime=datetime(2003, 02, 01)) """ restype = self._session.query(Results).filter_by(ResultID=resultids[0]).first().ResultTypeCV From b87dab25a581a764c42df0d231b16545600fdb4d Mon Sep 17 00:00:00 2001 From: lsetiawan Date: Fri, 5 Jan 2018 12:34:07 -0800 Subject: [PATCH 3/3] change ResultType to ResultValues --- odm2api/ODM2/services/readService.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index 04145b3..eabcaf3 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -1297,31 +1297,31 @@ def getResultValues(self, resultids, starttime=None, endtime=None): """ restype = self._session.query(Results).filter_by(ResultID=resultids[0]).first().ResultTypeCV - ResultType = TimeSeriesResultValues + ResultValues = TimeSeriesResultValues if 'categorical' in restype.lower(): - ResultType = CategoricalResultValues + ResultValues = CategoricalResultValues elif 'measurement' in restype.lower(): - ResultType = MeasurementResultValues + ResultValues = MeasurementResultValues elif 'point' in restype.lower(): - ResultType = PointCoverageResultValues + ResultValues = PointCoverageResultValues elif 'profile' in restype.lower(): - ResultType = ProfileResultValues + ResultValues = ProfileResultValues elif 'section' in restype.lower(): - ResultType = SectionResults + ResultValues = SectionResults elif 'spectra' in restype.lower(): - ResultType = SpectraResultValues + ResultValues = SpectraResultValues elif 'time' in restype.lower(): - ResultType = TimeSeriesResultValues + ResultValues = TimeSeriesResultValues elif 'trajectory' in restype.lower(): - ResultType = TrajectoryResultValues + ResultValues = TrajectoryResultValues elif 'transect' in restype.lower(): - ResultType = TransectResultValues + ResultValues = TransectResultValues - q = self._session.query(ResultType).filter(ResultType.ResultID.in_(resultids)) + q = self._session.query(ResultValues).filter(ResultValues.ResultID.in_(resultids)) if starttime: - q = q.filter(ResultType.ValueDateTime >= starttime) + q = q.filter(ResultValues.ValueDateTime >= starttime) if endtime: - q = q.filter(ResultType.ValueDateTime <= endtime) + q = q.filter(ResultValues.ValueDateTime <= endtime) try: # F841 local variable 'vals' is assigned to but never used # vals = q.order_by(ResultType.ValueDateTime) @@ -1331,7 +1331,7 @@ def getResultValues(self, resultids, starttime=None, endtime=None): con=self._session_factory.engine, params=query.params ) - df.columns = [self._get_columns(ResultType)[c] for c in df.columns] + df.columns = [self._get_columns(ResultValues)[c] for c in df.columns] return df except Exception as e: print('Error running Query: {}'.format(e))