def run_model(self,model_list, constituent, match_time=30, min_samples=None): db_path = '/said/{}/'.format(self.site['id']) iv_path = db_path + 'iv' qwdata_path = db_path + 'qwdata' try: sur_df = self.store.get(iv_path) con_df = self.store.get(qwdata_path) except KeyError: print('site {} not found'.format(self.site['name'])) if min_samples is None: min_samples = self.min_samples model = HierarchicalModel(con_df, sur_df, model_list, match_time=match_time, min_samples=min_samples) predictions = model.get_prediction() sur_df = update_merge(sur_df, predictions) self.store.put(iv_path, sur_df) temp_csv = StringIO(model.summary().as_csv()) model_summary = pd.read_csv(temp_csv, sep=',') model_summary.columns=['model','# obs','adjusted r^2','p-value'] self.summary_table = self.summary_table.append(model_summary) report = model.report() path = f"report/{constituent}_long_report.txt" open(path, 'w').close() with open(path,"a") as report_file: report_file.write(report) model.plot_model_pred_vs_obs(savepath=f"report/{constituent}_pred_vs_obs_plot.png", dpi=150)
def _apply_proxy(self, service, proxy_id): #import pdb; pdb.set_trace() with NWISStore(self._store_path) as store: #import pdb; pdb.set_trace() station = store.get_station(self.id()) #try::w # station = store.get_station(self.site_id) ## #except: # print('station not found') if not proxy_id: return station.get(service) else: proxy = store.get_station(proxy_id) # check if the station even has a service try: df = station.get(service) except: return proxy.get(service) # if it does, check for the proxy try: proxy_df = proxy.get(service) #import pdb; pdb.set_trace() #return update_merge(df, proxy_df) return update_merge(df, proxy_df, na_only=True) except: return station.get(service)
def run_model(self, constituent, model_list): model = HierachicalModel(con_df, sur_df, model_list) predictions = model.get_predictions() iv = self.get('iv') iv = update_merge(iv, predictions) self.put('iv')
def update_table(store, path, df): """ """ if path in store.keys(): old_df = store.get(path) old_df = update_merge(old_df, df) store.put(path, old_df) else: store.put(path, df)
def un_model(store, site, model_list, constituent): db_path = '/said/{}/'.format(site['id']) iv_path = db_path + 'iv' qwdata_path = db_path + 'qwdata' try: sur_df = store.get(iv_path) con_df = store.get(qwdata_path) except KeyError: print('site {} not found'.format(site['name'])) model = HierarchicalModel(con_df, sur_df, model_list) predictions = model.get_prediction() sur_df = update_merge(sur_df, predictions) store.put(iv_path, sur_df) print(model.summary())
def phos_load(model1, model2, wy=None): """ """ predicted_data_1 = model1._model.predict_response_variable( explanatory_data=model1._surrogate_data, raw_response=True, bias_correction=True, prediction_interval=True) obs = model1.get_model_dataset() df = model1._surrogate_data.get_data() if model2: pvalue = model2._model._model.fit().f_pvalue if pvalue < 0.05: obs = obs[~obs['Missing']] obs2 = model2.get_model_dataset() obs2.drop(obs.index, axis=0) #drop anything thats in obs1 from obs2 obs = obs.append(obs2).sort_index() predicted_data_2 = model2._model.predict_response_variable( explanatory_data=model2._surrogate_data, raw_response=True, bias_correction=True, prediction_interval=True) predicted_data_1 = update_merge(predicted_data_2, predicted_data_1, na_only=True) discharge = df['Discharge'] constituent = predicted_data_1['TP'] #if wy: # return wy_load(wy, discharge, constituent) # #else: return mean_annual_load(discharge, constituent, wy=wy)