def write_SMG_regresjon(variable, region, ps): """This function writes pandas series (pandas series) to smg series (ts) chosen according to the chosen region.""" global ts smg = TimeSeriesRepositorySmg(SMG_PROD) # define ts to write to if variable == 'tilsig': ts = 'RegEstimatTilsig' + region + '-U9100S0BT0105' if variable == 'magasin': ts = 'RegEstimatMag' + region + '...-U9104S0BT0132' print('\nSkriver estimerte verdier til: ', ts) # get metainfo from ts minfo = smg.get_meta_info_by_name([ts]) # we need to attach meta information to the Pandas series since it is needed to create the TimeSeries object ps.meta_info = minfo[0] print(ps) print(ps[-1]) # convert the Pandas series to a time series time_series_to_write = ts_from_pandas_series(ps, filter_out_nans=True) # writing result = smg.write([time_series_to_write])
def read_forecast(self, fc_names, period): if not period.valid(): raise SmgDataError("Period should be valid() of type api.UtcPeriod") fc_series = {} fcIdentities = self._namelist_to_ListOf_TsIdentities(fc_names) ssa_period = self._make_ssa_Period_from_shyft_period(period) tsIdentities = [] with TimeSeriesRepositorySmg(self.env) as tsr: tsIdentities = tsr.repo.GetIdentities(tsr.repo.FindMetaInfo(fcIdentities)) fcr = ForecastRepositorySmg(self.fc_env) # TODO: implement __exit__ to use 'with as' fc_reads = fcr.repo.ReadForecast(tsIdentities, ssa_period) if tsIdentities.Count != len(fc_reads): print("WARNING: Could only find {} out of {} requested timeseries".format(fc_reads.Count, tsIdentities.Count)) for fcTs in fc_reads: fc_series[fcTs.Name] = self._make_shyft_ts_from_xts(fcTs) return fc_series
def read(self, ts_names, period): """Open a connection to the SMG database and fetch all the time series given in ts_names. ts_id is currently the full unique name of the smg-ts. We could/should also support using unique number/keys instead. -more efficient, and more robust to name changes. Return the result as a dictionary of shyft_ts.""" if not period.valid(): raise SmgDataError("Period should be valid() of type api.UtcPeriod") shyft_series = {} tsIdentities = self._namelist_to_ListOf_TsIdentities(ts_names) ssa_period = self._make_ssa_Period_from_shyft_period(period) tsReads = [] with TimeSeriesRepositorySmg(self.env) as tsr: tsReads = tsr.repo.ReadRawPoints(tsIdentities, ssa_period) if len(ts_names) != tsReads.Count: print("WARNING: Could only find {} out of {} requested timeseries".format(tsReads.Count, len(ts_names))) for ts in tsReads: # TODO: check ts.Name is valid else use ts.Info.Id shyft_series[ts.Name] = self._make_shyft_ts_from_xts(ts) return shyft_series
def store(self, ts_dict, is_forecast=False): """ Input the list of Shyft result ts_dict, where the keys are the wanted SmG ts-path names and the values are Shyft result api.shyft_timeseries_double, time-series. If the named time-series does not exist, create it. Then store time-series data to the named entities. """ # Create list of TS identities used by repository tsIds = self._namelist_to_ListOf_TsIdentities(ts_dict.keys()) res = False with TimeSeriesRepositorySmg(self.env) as tsr: self._create_missing_ts(tsr, tsIds, is_forecast) tsIds = tsr.repo.GetIdentities(tsr.repo.FindMetaInfo(tsIds)) ts_names = {x.Name: x for x in tsIds} # Create list of TS used by repository tsList = List[TimeSeriesPointSegments]([]) for name, shyft_ts in iter(ts_dict.items()): ts_1h = self._resample_1h(shyft_ts) xts = self._make_ssa_tsps_from_shyft_ts(ts_names[name], ts_1h) tsList.Add(xts) errors = tsr.repo.Write(tsList, False) res = errors is None return res
def write_V_SMG_Regresjon(results, chosen_p, fasit_key, r2_modelled, r2_modelled_long, short_period_beste, MagKap_mag=False): start_time = time.time() now = pd.to_datetime(time.strftime("%Y.%m.%d %H:%M"), format="%Y.%m.%d %H:%M", errors='ignore') expression = str( '! Sist oppdatert {}\n!R2 (modell vs fasit) for periode tilbake til 08.06.2015: {:0.6f}\n!R2 (modell vs fasit) for periode tilbake {} uker: {:0.6f}\n' .format(now, r2_modelled_long, short_period_beste, r2_modelled)) vekt_serie = "F{tall}={serie}*{vekt}\n" region = str(fasit_key[6:9]) if str(fasit_key[-3:]) == '132': sum_av_vekter = '## = ({alle_vekter})/100' for tall, serie, vekt in sorted( zip(range(len(chosen_p)), chosen_p, results.params)): MagKap = MagKap_mag[serie] if ('-U' not in serie) and (MagKap != 0): serie = "(100*@TRANSFORM(%'{}','WEEK','FIRST')/{})".format( serie, MagKap) elif ('-U' in serie) and (MagKap != 0): serie = "(100*%'{}'/{})".format(serie, MagKap) elif ('-U' not in serie) and (MagKap == 0): serie = "@TRANSFORM(%'{}','WEEK','FIRST')".format(serie) elif ('-U' in serie) and (MagKap == 0): serie = "%'{}'".format(serie) expression += (str((vekt_serie.format(tall=tall + 1, serie=serie, vekt=vekt)))) expression += str( sum_av_vekter.format( alle_vekter='F' + '+F'.join([str(tall + 1) for tall in range(len(chosen_p))]))) expression += '\n@SET_TS_VALUNIT(##,132)' ts = 'RegresjonMag' + region + '.......-U9104S0BT0132' if str(fasit_key[-3:]) == '105': sum_av_vekter = '## = {alle_vekter}' for tall, serie, vekt in sorted( zip(range(len(chosen_p)), chosen_p, results.params)): serie = "@TRANSFORM(%'{}','WEEK','SUM')".format(serie) expression += (str((vekt_serie.format(tall=tall + 1, serie=serie, vekt=vekt)))) expression += str( sum_av_vekter.format( alle_vekter='F' + '+F'.join([str(tall + 1) for tall in range(len(chosen_p))]))) expression += '\n@SET_TS_VALUNIT(##,105)' if 'No' in region: ts = 'RegresjonTilsigNO' + region[-1] + '.-U9100S0BT0105' elif 'Se' in region: ts = 'RegresjonTilsigSE' + region[-1] + '.-U9100S0BT0105' smg = TimeSeriesRepositorySmg(SMG_PROD) print('Skriver formel for siste regresjon til: ', ts) info = smg.get_meta_info_by_name([ts]) # print('-----------------------------------------------------------------------') # print(expression) # print('-----------------------------------------------------------------------') smg.update_virtual({info[0]: expression}) end_time = time.time() print('Time to run write_V_SMG_Regresjon: ', end_time - start_time)