def limit_patient_set(patients: pd.DataFrame, limit_args: LimitArguments, sites: SiteArguments = None): if limit_args is None or limit_args.limit is None: return patients if not limit_args.order_by: raise exc.RSError('order required when limit is specified') # Closest YMCA is a synthetic column based on the YMCA sites included in the query if limit_args.order_by == C.QK_LIMIT_CLOSEST_YMCA: if sites is None or sites.sites is None: sites = list(model.cdm.ymca_site_keys) else: sites = sites.sites closest_site = patients[sites].min(axis=1) patients[C.QK_LIMIT_CLOSEST_YMCA] = closest_site if limit_args.order_by not in patients.columns: raise exc.RSError("missing order column: '{}'".format( limit_args.order_by)) patients = patients.sort_values(by=[limit_args.order_by], ascending=limit_args.order_asc) return patients.head(limit_args.limit)
def _validate_filter_value(self, value): if not isinstance(value, str): raise exc.RSError( "invalid filter value: filter value must be a string") if value not in self.allowed_values: raise exc.RSError( "invalid filter value '{}'; must be one of [{}]".format( value, ', '.join(self.allowed_values)))
def validate_filter_value(self, value): if not isinstance(value, dict): raise exc.RSError( 'invalid filter value: structure must contain min and/or max') minval = self._get_boundary_value(value, 'min') maxval = self._get_boundary_value(value, 'max') if (minval is not None) and (maxval is not None): if minval > maxval: raise exc.RSError( 'invalid filter value: min cannot be greater than max: {} > {}' .format(minval, maxval)) elif minval is None and maxval is None: raise exc.RSError( 'invalid filter value: structure must contain min and/or max')
def validate_filter_value(self, value): if not isinstance(value, dict): self._validate_filter_value(value) else: if 'value' not in value: raise exc.RSError( "filter value structure must contain 'value' element") self._validate_filter_value(value['value'])
def validate_filter_value(self, value): if not (isinstance(value, str) or isinstance(value, list)): raise exc.RSError( "invalid filter value: filter value must be a string or a list of strings" ) subvalues = self._ensure_list(value) for subvalue in subvalues: self._validate_filter_value(subvalue)
def _get_boundary_value(self, value, key): if key not in value: return None try: return float(value[key]) except ValueError: raise exc.RSError( "invalid filter value: {} must be an integer: '{}'".format( key, value))
def parse_limit_arguments(args: dict) -> LimitArguments: limit = None order_by = None order_asc = False if C.QK_EXPORT_LIMIT in args: if C.QK_EXPORT_ORDER_BY not in args: raise exc.RSError('export limit requires {} argument'.format( C.QK_EXPORT_ORDER_BY)) limit = args.pop(C.QK_EXPORT_LIMIT) order_by = args.pop(C.QK_EXPORT_ORDER_BY) order_asc = parse_boolean(args.pop(C.QK_EXPORT_ORDER_ASC, False)) try: limit = int(limit) except ValueError: raise exc.RSError("invalid export limit '{}'".format(limit)) if order_by not in C.QK_EXPORT_ORDER_VALUES: raise exc.RSError("invalid order field '{}'".format(order_by)) return LimitArguments(limit, order_by, order_asc)
def _create_site_metadata(self, sites): metadata = dict() if sites.sites is not None and sites.maxdists is not None: metadata[C.YMCA_SITES] = { site: { C.QK_SITE_MINDIST: mindist, C.QK_SITE_MAXDIST: maxdist } for site, maxdist, mindist in zip(sites.sites, sites.maxdists, sites.mindists) } elif sites.sites != sites.maxdists: raise exc.RSError('sites and maxdists must both be present or both be None') return metadata
def _validate_filter_value(self, value): if value not in self._VALID_FILTER_VALUES: raise exc.RSError( "invalid filter value '{}'; must be one of [{}]".format( value, ', '.join(self._VALID_FILTER_VALUES)))