def __init__(self, path, tally, precision): """ Constructor """ self.__path = path self.__precision = precision self.__func = LinearRegression(tally, True)
class SampleRegression(object): """ classdocs """ # ---------------------------------------------------------------------------------------------------------------- def __init__(self, path, tally, precision): """ Constructor """ self.__path = path self.__precision = precision self.__func = LinearRegression(tally, True) # ---------------------------------------------------------------------------------------------------------------- def datum(self, sample): if not sample.has_path(self.__path): return None try: rec_node = sample.node('rec') except KeyError: return None rec = LocalizedDatetime.construct_from_jdict(rec_node) value = sample.node(self.__path) self.__func.append(rec, value) if not self.__func.has_regression(): return None slope, intercept = self.__func.line() if slope is None: return None target = PathDict() if sample.has_path('rec'): target.copy(sample, 'rec') target.append(self.__path + '.src', value) target.append(self.__path + '.slope', round(slope, self.__precision)) target.append(self.__path + '.intercept', round(intercept, self.__precision)) return target.node() # ---------------------------------------------------------------------------------------------------------------- def __str__(self, *args, **kwargs): return "SampleRegression:{path:%s, func:%s}" % (self.__path, self.__func)
class SampleMidpoint(object): """ classdocs """ # ---------------------------------------------------------------------------------------------------------------- def __init__(self, path, tally, precision): """ Constructor """ self.__path = path self.__precision = precision self.__func = LinearRegression(tally, False) # ---------------------------------------------------------------------------------------------------------------- def datum(self, sample): if not sample.has_path(self.__path): return None try: rec_node = sample.node('rec') except KeyError: return None rec = LocalizedDatetime.construct_from_jdict(rec_node) value = sample.node(self.__path) self.__func.append(rec, value) if not self.__func.has_midpoint(): return None mid_rec, mid = self.__func.midpoint() if rec is None: return None target = PathDict() target.append('rec', rec.as_iso8601()) target.append('mid-rec', mid_rec.as_iso8601()) target.append(self.__path + '.src', value) target.append(self.__path + '.mid', round(mid, self.__precision)) return target.node() # ---------------------------------------------------------------------------------------------------------------- def __str__(self, *args, **kwargs): return "SampleMidpoint:{path:%s, precision:%s, func:%s}" % ( self.__path, self.__precision, self.__func)
class SampleRegression(object): """ classdocs """ # ---------------------------------------------------------------------------------------------------------------- def __init__(self, path, tally): """ Constructor """ self.__path = path self.__func = LinearRegression(tally, True) # ---------------------------------------------------------------------------------------------------------------- def datum(self, sample): if not sample.has_path(self.__path): return None rec = LocalizedDatetime.construct_from_jdict(sample.node('rec')) value = sample.node(self.__path) self.__func.append(rec, value) if not self.__func.has_tally(): return None slope, intercept = self.__func.compute() if slope is None: return None target = PathDict() target.copy(datum, 'rec') target.append(self.__path + '.src', value) target.append(self.__path + '.slope', round(slope * 60 * 60, 6)) # x-scale is hours target.append(self.__path + '.intercept', round(intercept, 6)) return target.node() # ---------------------------------------------------------------------------------------------------------------- def __str__(self, *args, **kwargs): return "SampleRegression:{path:%s, func:%s}" % (self.__path, self.__func)
def append(self, datetime: LocalizedDatetime, sample: PathDict): # initialise... if not self.__initialised: for node in self.__nodes: try: paths = sample.paths(node) except KeyError: continue except IndexError as ex: paths = None print("sample_aggregate: %s: IndexError: %s" % (node, ex), file=sys.stderr) sys.stderr.flush() exit(1) for path in paths: if path == 'rec': continue self.__precisions[path] = Precision() self.__regressions[path] = LinearRegression() if Datum.is_numeric(sample.node(path)) else \ CategoricalRegression() self.__initialised = True # values... for path in self.__precisions.keys(): try: value = sample.node(path) except KeyError: continue if value is None: continue try: self.__precisions[path].widen(value) self.__regressions[path].append(datetime, value) except InvalidOperation: continue
def __init__(self, path, tally): """ Constructor """ self.__path = path self.__func = LinearRegression(tally, False)
j1 = '{"rec": "2016-10-14T14:19:15.677+01:00", "val": {"opc_n2": {"pm1": 1.5, "pm2p5": 2.7, "pm10": 3.3, ' \ '"per": 5.0, "bin": [95, 30, 11, 1, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], ' \ '"mtf1": 31, "mtf3": 29, "mtf5": 0, "mtf7": 0}}}' j2 = '{"rec": "2016-10-14T14:19:20.680+01:00", "val": {"opc_n2": {"pm1": 1.8, "pm2p5": 4.3, "pm10": 6.9, ' \ '"per": 5.0, "bin": [76, 36, 19, 6, 1, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], ' \ '"mtf1": 26, "mtf3": 35, "mtf5": 26, "mtf7": 35}}}' j3 = '{"rec": "2016-10-14T14:19:25.680+01:00", "val": {"opc_n2": {"pm1": 0.9, "pm2p5": 1.7, "pm10": 22.4, ' \ '"per": 5.0, "bin": [62, 23, 8, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], ' \ '"mtf1": 20, "mtf3": 24, "mtf5": 0, "mtf7": 0}}}' # -------------------------------------------------------------------------------------------------------------------- lr = LinearRegression() print(lr) print("-") d1 = PathDict.construct_from_jstr(j1) print(d1) print("-") rec = LocalizedDatetime.construct_from_iso8601(d1.node('rec')) val = d1.node('val.opc_n2.pm10') lr.append(rec, val) print(lr) print("-") d2 = PathDict.construct_from_jstr(j2)