def plot_result(self): if not os.path.exists("./predict/"): self.train_data() loss = np.load("./predict/{}.npz".format("loss")) accuracy = np.load("./predict/{}.npz".format("accuracy")) plt.figure(figsize = (16, 6), dpi = 80) plt.interactive(True) plt.subplot(121) plt.title("loss") plt.xlabel("epoch") plt.ylabel("loss") plt.plot(list(range(1, len(loss["data"]) + 1)), loss["data"], "-o") plt.xticks(list(range(1, len(loss["data"]) + 1, 2))) plt.subplot(122) plt.title("accuracy") plt.xlabel("epoch") plt.ylabel("accuracy") plt.plot(list(range(1, len(accuracy["data"]) + 1)), accuracy["data"], "-o") plt.xticks(list(range(1, len(accuracy["data"]) + 1, 2))) plt.tight_layout() plt.show()
def ensemble_demo(): utc = Calendar() t_start = utc.time(YMDhms(2011, 9, 1)) t_fc_ens_start = utc.time(YMDhms(2015, 7, 26)) disp_start = utc.time(YMDhms(2015, 7, 20)) dt = deltahours(1) n_obs = int(round((t_fc_ens_start - t_start)/dt)) n_fc_ens = 30 n_disp = int(round(t_fc_ens_start - disp_start)/dt) + n_fc_ens + 24*7 obs_time_axis = Timeaxis(t_start, dt, n_obs + 1) fc_ens_time_axis = Timeaxis(t_fc_ens_start, dt, n_fc_ens) display_time_axis = Timeaxis(disp_start, dt, n_disp) q_obs_m3s_ts = observed_tistel_discharge(obs_time_axis.total_period()) ptgsk = create_tistel_simulator(PTGSKOptModel, tistel.geo_ts_repository(tistel.grid_spec.epsg())) initial_state = burn_in_state(ptgsk, t_start, utc.time(YMDhms(2012, 9, 1)), q_obs_m3s_ts) ptgsk.run(obs_time_axis, initial_state) current_state = adjust_simulator_state(ptgsk, t_fc_ens_start, q_obs_m3s_ts) q_obs_m3s_ts = observed_tistel_discharge(display_time_axis.total_period()) ens_repos = tistel.arome_ensemble_repository(tistel.grid_spec) ptgsk_fc_ens = create_tistel_simulator(PTGSKModel, ens_repos) sims = ptgsk_fc_ens.create_ensembles(fc_ens_time_axis, t_fc_ens_start, current_state) for sim in sims: sim.simulate() plt.hold(1) percentiles = [10, 25, 50, 75, 90] plot_percentiles(sims, percentiles, obs=q_obs_m3s_ts) plt.interactive(1) plt.show()
def drawall(self, **kwargs): if not self.n == self.drawax.shape: self.drawax = np.ones(self.n, dtype='bool') if 'lw' in kwargs.keys(): kwargs['linewidth'] = kwargs.pop('lw', self.linewidth) if 'linewidth' not in kwargs.keys(): kwargs['linewidth'] = self.linewidth else: self.linewidth = kwargs['linewidth'] inter = pylab.isinteractive() pylab.interactive(False) # wait to draw the axes, until they've all been # created. for iv, ih in self._iter_axinds(): if self.drawax[iv, ih]: self.ax[iv, ih] = axes(self.axPlacer(iv, ih), sharex=self.sharex(iv, ih), sharey=self.sharey(iv, ih), **kwargs) self._xlabel_ax = self.ax[-1, 0] self._ylabel_ax = self._xlabel_ax pylab.interactive(inter) pylab.draw_if_interactive() return self.ax
def drawall(self, **kwargs): if not self.n == self.drawax.shape: self.drawax = np.ones(self.n, dtype='bool') if not self.n[1] == self.hrel.shape[0]: self.hrel = np.ones(self.n[1], dtype='float32') if not self.n[0] == self.vrel.shape[0]: self.vrel = np.ones(self.n[0], dtype='float32') if kwargs.has_key('lw'): kwargs['linewidth'] = kwargs.pop('lw') if not kwargs.has_key('linewidth'): kwargs['linewidth'] = self.linewidth else: self.linewidth = kwargs['linewidth'] forcesharex = False forcesharey = False if kwargs.has_key('sharex'): forcesharex = True if kwargs.has_key('sharey'): forcesharey = True inter = pylab.isinteractive() pylab.interactive( False) # wait to draw the axes, until they've all been created. axg = self.axgrid() for iv in range(self.n[0]): for ih in range(self.n[1]): if forcesharex: # I should put this functionality into a func. pass elif self.sharex[iv, ih] and self._sharex_ax[self.sharex[iv, ih]]: kwargs['sharex'] = self._sharex_ax[self.sharex[iv, ih]] elif kwargs.has_key('sharex'): kwargs.pop('sharex') if forcesharey: pass elif self.sharey[iv, ih] and self._sharey_ax[self.sharey[iv, ih]]: kwargs['sharey'] = self._sharey_ax[self.sharey[iv, ih]] elif kwargs.has_key('sharey'): kwargs.pop('sharey') if self.drawax[iv, ih]: #self.ax[iv,ih]=myaxes(axg[iv,ih,:],**kwargs) self.ax[iv, ih] = axes(axg[iv, ih, :], **kwargs) self.ax[iv, ih].hold(True) if self.sharex[ iv, ih] and not self._sharex_ax[self.sharex[iv, ih]]: self._sharex_ax[self.sharex[iv, ih]] = self.ax[iv, ih] if self.sharey[ iv, ih] and not self._sharey_ax[self.sharey[iv, ih]]: self._sharey_ax[self.sharey[iv, ih]] = self.ax[iv, ih] flag = True self._xlabel_ax = self.ax[-1, 0] self._ylabel_ax = self._xlabel_ax pylab.interactive(inter) pylab.draw_if_interactive() return self.ax
def ensemble_demo(): utc = Calendar() t_start = utc.time(YMDhms(2011, 9, 1)) t_fc_ens_start = utc.time(YMDhms(2015, 7, 26)) disp_start = utc.time(YMDhms(2015, 7, 20)) dt = deltahours(1) n_obs = int(round((t_fc_ens_start - t_start) / dt)) n_fc_ens = 30 n_disp = int(round(t_fc_ens_start - disp_start) / dt) + n_fc_ens + 24 * 7 obs_time_axis = Timeaxis(t_start, dt, n_obs + 1) fc_ens_time_axis = Timeaxis(t_fc_ens_start, dt, n_fc_ens) display_time_axis = Timeaxis(disp_start, dt, n_disp) q_obs_m3s_ts = observed_tistel_discharge(obs_time_axis.total_period()) ptgsk = create_tistel_simulator( PTGSKOptModel, tistel.geo_ts_repository(tistel.grid_spec.epsg())) initial_state = burn_in_state(ptgsk, t_start, utc.time(YMDhms(2012, 9, 1)), q_obs_m3s_ts) ptgsk.run(obs_time_axis, initial_state) current_state = adjust_simulator_state(ptgsk, t_fc_ens_start, q_obs_m3s_ts) q_obs_m3s_ts = observed_tistel_discharge(display_time_axis.total_period()) ens_repos = tistel.arome_ensemble_repository(tistel.grid_spec) ptgsk_fc_ens = create_tistel_simulator(PTGSKModel, ens_repos) sims = ptgsk_fc_ens.create_ensembles(fc_ens_time_axis, t_fc_ens_start, current_state) for sim in sims: sim.simulate() plt.hold(1) percentiles = [10, 25, 50, 75, 90] plot_percentiles(sims, percentiles, obs=q_obs_m3s_ts) plt.interactive(1) plt.show()
def pm(a, ls=None): pylab.interactive(True) for i in range(len(a)): if ls: pylab.plot(range(len(a[i])), numpy.abs(a[i]), ls) else: pylab.plot(numpy.abs(a[i])) pylab.show()
def continuous_calibration(): utc = Calendar() t_start = utc.time(YMDhms(2011, 9, 1)) t_fc_start = utc.time(YMDhms(2015, 10, 1)) dt = deltahours(1) n_obs = int(round((t_fc_start - t_start)/dt)) obs_time_axis = Timeaxis(t_start, dt, n_obs + 1) q_obs_m3s_ts = observed_tistel_discharge(obs_time_axis.total_period()) ptgsk = create_tistel_simulator(PTGSKOptModel, tistel.geo_ts_repository(tistel.grid_spec.epsg())) initial_state = burn_in_state(ptgsk, t_start, utc.time(YMDhms(2012, 9, 1)), q_obs_m3s_ts) num_opt_days = 30 # Step forward num_opt_days days and store the state for each day: recal_start = t_start + deltahours(num_opt_days*24) t = t_start state = initial_state opt_states = {t: state} while t < recal_start: ptgsk.run(Timeaxis(t, dt, 24), state) t += deltahours(24) state = ptgsk.reg_model_state opt_states[t] = state recal_stop = utc.time(YMDhms(2011, 10, 30)) recal_stop = utc.time(YMDhms(2012, 5, 30)) curr_time = recal_start q_obs_avg = TsTransform().to_average(t_start, dt, n_obs + 1, q_obs_m3s_ts) target_spec = TargetSpecificationPts(q_obs_avg, IntVector([0]), 1.0, KLING_GUPTA) target_spec_vec = TargetSpecificationVector([target_spec]) i = 0 times = [] values = [] p, p_min, p_max = construct_calibration_parameters(ptgsk) while curr_time < recal_stop: print(i) i += 1 opt_start = curr_time - deltahours(24*num_opt_days) opt_state = opt_states.pop(opt_start) p = ptgsk.region_model.get_region_parameter() p_opt = ptgsk.optimize(Timeaxis(opt_start, dt, 24*num_opt_days), opt_state, target_spec_vec, p, p_min, p_max, tr_stop=1.0e-5) ptgsk.region_model.set_region_parameter(p_opt) corr_state = adjust_simulator_state(ptgsk, curr_time, q_obs_m3s_ts) ptgsk.run(Timeaxis(curr_time, dt, 24), corr_state) curr_time += deltahours(24) opt_states[curr_time] = ptgsk.reg_model_state discharge = ptgsk.region_model.statistics.discharge([0]) times.extend(discharge.time(i) for i in range(discharge.size())) values.extend(list(np.array(discharge.v))) plt.plot(utc_to_greg(times), values) plot_results(None, q_obs=observed_tistel_discharge(UtcPeriod(recal_start, recal_stop))) set_calendar_formatter(Calendar()) plt.interactive(1) plt.title("Continuously recalibrated discharge vs observed") plt.xlabel("Time in UTC") plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$", verticalalignment="top", rotation="horizontal") plt.gca().yaxis.set_label_coords(0, 1.1)
def drawall(self, **kwargs): if not self.n == self.drawax.shape: self.drawax = np.ones(self.n, dtype='bool') if not self.n[1] == self.hrel.shape[0]: self.hrel = np.ones(self.n[1], dtype='float32') if not self.n[0] == self.vrel.shape[0]: self.vrel = np.ones(self.n[0], dtype='float32') if 'lw' in kwargs.keys(): kwargs['linewidth'] = kwargs.pop('lw', self.linewidth) if 'linewidth' not in kwargs.keys(): kwargs['linewidth'] = self.linewidth else: self.linewidth = kwargs['linewidth'] forcesharex = False forcesharey = False if 'sharex' in kwargs.keys(): forcesharex = True if 'sharey' in kwargs.keys(): forcesharey = True inter = pylab.isinteractive() pylab.interactive(False) # wait to draw the axes, until they've all been # created. axg = self.axgrid() for iv in range(self.n[0]): for ih in range(self.n[1]): if forcesharex: # I should put this functionality into a func. pass elif (self.sharex[iv, ih] and self._sharex_ax[self.sharex[iv, ih]]): kwargs['sharex'] = self._sharex_ax[self.sharex[iv, ih]] elif 'sharex' in kwargs.keys(): kwargs.pop('sharex') if forcesharey: pass elif (self.sharey[iv, ih] and self._sharey_ax[self.sharey[iv, ih]]): kwargs['sharey'] = self._sharey_ax[self.sharey[iv, ih]] elif 'sharey' in kwargs.keys(): kwargs.pop('sharey') if self.drawax[iv, ih]: # self.ax[iv,ih]=myaxes(axg[iv,ih,:],**kwargs) self.ax[iv, ih] = axes(axg[iv, ih,:], **kwargs) self.ax[iv, ih].hold(True) if self.sharex[iv, ih] and not\ self._sharex_ax[self.sharex[iv, ih]]: self._sharex_ax[self.sharex[iv, ih]] = self.ax[iv, ih] if self.sharey[iv, ih] and not\ self._sharey_ax[self.sharey[iv, ih]]: self._sharey_ax[self.sharey[iv, ih]] = self.ax[iv, ih] self._xlabel_ax = self.ax[-1, 0] self._ylabel_ax = self._xlabel_ax pylab.interactive(inter) pylab.draw_if_interactive() return self.ax
def simple_matching_game(in_list): end_game = False total = 0 corr = 0 while ~end_game: r = random.sample(in_list, 3) correct_name = r[0]['firstName'] + ' ' + r[0]['lastName'] img = Image.open(urllib.urlopen(r[0]['imageURL'])) fig = plt.figure(2) axs = plt.gca() axs.xaxis.set_ticks([]) axs.yaxis.set_ticks([]) plt.imshow(img) plt.interactive(True) plt.show() random.shuffle(r) choice1 = r[0]['firstName'] + ' ' + r[0]['lastName'] choice2 = r[1]['firstName'] + ' ' + r[1]['lastName'] choice3 = r[2]['firstName'] + ' ' + r[2]['lastName'] if total <= 0: event, values = simpleGUI.Window( f'Choose who is in the picture (Press Cancel to Stop)', [[ simpleGUI.Text('Select one->'), simpleGUI.Listbox( [choice1, choice2, choice3], size=(50, 3), key='LB') ], [simpleGUI.Button('OK'), simpleGUI.Button('Cancel')]]).read(close=True) else: event, values = simpleGUI.Window( f'Choose who is in the picture Current Score: {round(corr / total * 100, 3)}', [[ simpleGUI.Text('Select one->'), simpleGUI.Listbox( [choice1, choice2, choice3], size=(50, 3), key='LB') ], [simpleGUI.Button('OK'), simpleGUI.Button('Cancel')]]).read(close=True) if event == 'OK': selected_choice = values["LB"][0] if selected_choice == correct_name: simpleGUI.popup(f'You are correct!') corr += 1 else: simpleGUI.popup(f'You are wrong!') if event == simpleGUI.WIN_CLOSED or event == 'Cancel': plt.close(2) break total += 1 return
def plot(data, labels, title ="", cbarlabel = "Cosine Distance"): plt.rcParams["axes.grid"] = False plt.interactive(False) fig, ax = plt.subplots(figsize=(20, 20)) im, cbar = heatmap(data, labels, labels, ax=ax, cmap="BuPu_r", title = title, cbarlabel = cbarlabel) fig.tight_layout() print("Show plot") plt.show(block=True) print("Done")
def save_images(self, tensor, filename, discriminator=None): plt.interactive(False) if not isinstance(tensor, list): plt.style.use('ggplot') plt.clf() fig = plt.figure() ax1 = fig.add_subplot(111) data = tensor.data.cpu().numpy() if hasattr( tensor, 'data') else tensor.cpu().numpy() x, y = np.split(data, 2, axis=1) x = x.flatten() y = y.flatten() for i in range(len(x)): rand_x = random.gauss(mu=x, sigma=0.1) rand_y = random.gauss(mu=y, sigma=0.1) ax1.scatter(rand_x, rand_y, c='red', marker='.', s=1) x_original, y_original = self.xs, self.ys #self.points() ax1.scatter(x_original, y_original, c='lime') else: if GaussianCircularToyDataSet.colors is None: GaussianCircularToyDataSet.colors = [ np.random.rand(3, ) for _ in tensor ] plt.style.use('ggplot') fig = plt.figure() ax1 = fig.add_subplot(111) # Plot generator x_original, y_original = self.xs, self.ys ax1.scatter(x_original, y_original, zorder=len(tensor) + 1, color='b') cm = plt.get_cmap('gist_rainbow') number_of_colors = 10 ax1.set_prop_cycle('color', [ cm(1. * i / number_of_colors) for i in range(number_of_colors) ]) for i, element in enumerate(tensor): data = element.data.cpu().numpy() if hasattr( element, 'data') else element.cpu().numpy() x, y = np.split(data, 2, axis=1) ax1.scatter(x.flatten(), y.flatten(), color=GaussianCircularToyDataSet.colors[i], zorder=len(tensor) - i, marker='x') plt.savefig(filename)
def save_images(tensor, filename, discriminator=None): plt.interactive(False) fig, ax = plt.subplots(nrows=10, ncols=10) plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0.001, hspace=0.001) for i in range(len(ax)): for j in range(len(ax[i])): ax[i][j].hist(tensor[i + j].data.numpy(), bins=50) ax[i][j].set_xticks([]) ax[i][j].set_yticks([]) plt.savefig(filename)
def showImage(self): if self.transfer_images == []: self.image_reconstruction() size = 16 plt.subplot(4, 15, 1) plt.ylabel("origin", fontsize=size) plt.subplot(4, 15, 16) plt.ylabel("reconstruction", fontsize=size) plt.subplot(4, 15, 31) plt.ylabel("origin", fontsize=size) plt.subplot(4, 15, 46) plt.ylabel("reconstruction", fontsize=size) for i in range(15): plt.subplot(4, 15, i + 1) plt.xticks([]) plt.yticks([]) plt.imshow(self.origin_images[i]) plt.subplot(4, 15, i + 16) plt.xticks([]) plt.yticks([]) plt.imshow( np.clip(self.transfer_images[i], 0, 255).astype(np.uint8)) for i in range(15, 30): plt.subplot(4, 15, i + 16) plt.xticks([]) plt.yticks([]) plt.imshow(self.origin_images[i]) plt.subplot(4, 15, i + 31) plt.xticks([]) plt.yticks([]) plt.imshow( np.clip(self.transfer_images[i], 0, 255).astype(np.uint8)) plt.get_current_fig_manager().window.showMaximized() plt.interactive(True) plt.show()
def launch_shell(self, argv=[]): # Configure prompts and messages in_template = 'L2A: In <\\#>: ' in_template2 = ' .\\D.:' out_template = 'L2A: Out<\\#>: ' banner = '*** Launcing L2 Analysis Shell ***\nAvailable analysis routines:\n%s' % self.formatted_routine_names( ) exit_msg = '*** Bye ***' # Set pylab environment as interactive pylab.interactive(True) # Try using an older version of IPython first try: argv += [ '-pi1', in_template, '-pi2', in_template2, '-po', out_template ] from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed(argv, banner=banner, exit_msg=exit_msg) ipshell(local_ns=self._local_namespace, global_ns=self._global_namespace) except ImportError as imp_err: # Newer version of IPython, 0.11 onward use this interface from IPython.config.loader import Config cfg = Config() prompt_config = cfg.PromptManager prompt_config.in_template = in_template prompt_config.in2_template = in_template2 prompt_config.out_template = out_template from IPython.frontend.terminal.embed import InteractiveShellEmbed ipshell = InteractiveShellEmbed(config=cfg, banner1=banner, exit_msg=exit_msg) # There is no access to global namespace in this version of IPython # put everything into the local namespace namespace = {} namespace.update(self._local_namespace) namespace.update(self._global_namespace) ipshell(local_ns=namespace)
def plot_Cifa10_images(self): plt.figure(figsize = (8, 6), dpi = 100) plt.interactive(True) for index in range(9): image, target = self.trainset[index] image = image.data.numpy().transpose((1, 2, 0)) image = (image + 1) / 2 plt.subplot(331 + index) plt.title(self.targetTable[int(target)]) plt.axis('off') plt.imshow(image) plt.tight_layout() plt.show()
def plot(data, labels, title="", cbarlabel="Cosine Distance"): plt.rcParams["axes.grid"] = False plt.interactive(False) fig, ax = plt.subplots(figsize=(20, 20)) im, cbar = heatmap(data, labels, labels, ax=ax, cmap="BuPu_r", title=title, cbarlabel=cbarlabel) fig.tight_layout() print("Show plot") plt.show(block=True) print("Done")
def test_data(self, index): if index == "": index = "1" if index.isdigit() == False: return index = int(index) - 1 if not 0 <= index < len(self.testset): return if not os.path.exists("./model/"): self.train_data() model = torch.load("./model/model.pkl", map_location=torch.device(self.device)) shape = np.shape(self.testset[index][0]) image, target = self.testset[index] output = model( torch.tensor(image.data.numpy().reshape( 1, shape[0], shape[1], shape[2])).float().to(self.device)) output = nn.functional.softmax(output, dim=1) output = output.data.cpu().numpy() plt.figure(figsize=(16, 6), dpi=80) plt.interactive(True) image = (image + 1) / 2 plt.subplot(121) plt.title(self.targetTable[target]) plt.axis('off') plt.imshow(image.data.numpy().transpose((1, 2, 0))) plt.subplot(122) plt.title("kind of probability") plt.bar(self.targetTable.values(), output[0]) plt.ylabel("probability") plt.tight_layout() plt.show()
def forecast_demo(): """Simple forecast demo using arome data from met.no. Initial state is bootstrapped by simulating one hydrological year (starting Sept 1. 2011), and then calculating the state August 31. 2012. This state is then used as initial state for simulating Sept 1, 2011, after scaling with observed discharge. The validity of this approach is limited by the temporal variation of the spatial distribution of the discharge state, q, in the Kirchner method. The model is then stepped forward until Oct 1, 2015, and then used to compute the discharge for 65 hours using Arome data. At last, the results are plotted as simple timeseries. """ utc = Calendar() t_start = utc.time(YMDhms(2011, 9, 1)) t_fc_start = utc.time(YMDhms(2015, 10, 1)) dt = deltahours(1) n_obs = int(round((t_fc_start - t_start) / dt)) n_fc = 65 obs_time_axis = Timeaxis(t_start, dt, n_obs) fc_time_axis = Timeaxis(t_fc_start, dt, n_fc) total_time_axis = Timeaxis(t_start, dt, n_obs + n_fc) q_obs_m3s_ts = observed_tistel_discharge(total_time_axis.total_period()) ptgsk = create_tistel_simulator( PTGSKOptModel, tistel.geo_ts_repository(tistel.grid_spec.epsg())) initial_state = burn_in_state(ptgsk, t_start, utc.time(YMDhms(2012, 9, 1)), q_obs_m3s_ts) ptgsk.run(obs_time_axis, initial_state) plot_results(ptgsk, q_obs_m3s_ts) current_state = adjust_simulator_state(ptgsk, t_fc_start, q_obs_m3s_ts) ptgsk_fc = create_tistel_simulator( PTGSKModel, tistel.arome_repository(tistel.grid_spec, t_fc_start)) ptgsk_fc.run(fc_time_axis, current_state) plt.figure() q_obs_m3s_ts = observed_tistel_discharge(fc_time_axis.total_period()) plot_results(ptgsk_fc, q_obs_m3s_ts) plt.interactive(1) plt.show()
def launch_shell(self, argv=[]): # Configure prompts and messages in_template = 'L2A: In <\\#>: ' in_template2 = ' .\\D.:' out_template = 'L2A: Out<\\#>: ' banner = '*** Launcing L2 Analysis Shell ***\nAvailable analysis routines:\n%s' % self.formatted_routine_names() exit_msg = '*** Bye ***' # Set pylab environment as interactive pylab.interactive(True) # Try using an older version of IPython first try: argv += [ '-pi1', in_template, '-pi2', in_template2, '-po', out_template ] from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed(argv,banner=banner,exit_msg=exit_msg) ipshell(local_ns=self._local_namespace, global_ns=self._global_namespace) except ImportError as imp_err: # Newer version of IPython, 0.11 onward use this interface from IPython.config.loader import Config cfg = Config() prompt_config = cfg.PromptManager prompt_config.in_template = in_template prompt_config.in2_template = in_template2 prompt_config.out_template = out_template from IPython.frontend.terminal.embed import InteractiveShellEmbed ipshell = InteractiveShellEmbed(config=cfg, banner1=banner, exit_msg=exit_msg) # There is no access to global namespace in this version of IPython # put everything into the local namespace namespace = {} namespace.update(self._local_namespace) namespace.update(self._global_namespace) ipshell(local_ns=namespace)
def forecast_demo(): """Simple forecast demo using arome data from met.no. Initial state is bootstrapped by simulating one hydrological year (starting Sept 1. 2011), and then calculating the state August 31. 2012. This state is then used as initial state for simulating Sept 1, 2011, after scaling with observed discharge. The validity of this approach is limited by the temporal variation of the spatial distribution of the discharge state, q, in the Kirchner method. The model is then stepped forward until Oct 1, 2015, and then used to compute the discharge for 65 hours using Arome data. At last, the results are plotted as simple timeseries. """ utc = Calendar() t_start = utc.time(YMDhms(2011, 9, 1)) t_fc_start = utc.time(YMDhms(2015, 10, 1)) dt = deltahours(1) n_obs = int(round((t_fc_start - t_start)/dt)) n_fc = 65 obs_time_axis = Timeaxis(t_start, dt, n_obs) fc_time_axis = Timeaxis(t_fc_start, dt, n_fc) total_time_axis = Timeaxis(t_start, dt, n_obs + n_fc) q_obs_m3s_ts = observed_tistel_discharge(total_time_axis.total_period()) ptgsk = create_tistel_simulator(PTGSKOptModel, tistel.geo_ts_repository(tistel.grid_spec.epsg())) initial_state = burn_in_state(ptgsk, t_start, utc.time(YMDhms(2012, 9, 1)), q_obs_m3s_ts) ptgsk.run(obs_time_axis, initial_state) plot_results(ptgsk, q_obs_m3s_ts) current_state = adjust_simulator_state(ptgsk, t_fc_start, q_obs_m3s_ts) ptgsk_fc = create_tistel_simulator(PTGSKModel, tistel.arome_repository(tistel.grid_spec, t_fc_start)) ptgsk_fc.run(fc_time_axis, current_state) plt.figure() q_obs_m3s_ts = observed_tistel_discharge(fc_time_axis.total_period()) plot_results(ptgsk_fc, q_obs_m3s_ts) plt.interactive(1) plt.show()
def continuous_calibration(): utc = Calendar() t_start = utc.time(YMDhms(2011, 9, 1)) t_fc_start = utc.time(YMDhms(2015, 10, 1)) dt = deltahours(1) n_obs = int(round((t_fc_start - t_start) / dt)) obs_time_axis = Timeaxis(t_start, dt, n_obs + 1) q_obs_m3s_ts = observed_tistel_discharge(obs_time_axis.total_period()) ptgsk = create_tistel_simulator( PTGSKOptModel, tistel.geo_ts_repository(tistel.grid_spec.epsg())) initial_state = burn_in_state(ptgsk, t_start, utc.time(YMDhms(2012, 9, 1)), q_obs_m3s_ts) num_opt_days = 30 # Step forward num_opt_days days and store the state for each day: recal_start = t_start + deltahours(num_opt_days * 24) t = t_start state = initial_state opt_states = {t: state} while t < recal_start: ptgsk.run(Timeaxis(t, dt, 24), state) t += deltahours(24) state = ptgsk.reg_model_state opt_states[t] = state recal_stop = utc.time(YMDhms(2011, 10, 30)) recal_stop = utc.time(YMDhms(2012, 5, 30)) curr_time = recal_start q_obs_avg = TsTransform().to_average(t_start, dt, n_obs + 1, q_obs_m3s_ts) target_spec = TargetSpecificationPts(q_obs_avg, IntVector([0]), 1.0, KLING_GUPTA) target_spec_vec = TargetSpecificationVector([target_spec]) i = 0 times = [] values = [] p, p_min, p_max = construct_calibration_parameters(ptgsk) while curr_time < recal_stop: print(i) i += 1 opt_start = curr_time - deltahours(24 * num_opt_days) opt_state = opt_states.pop(opt_start) p = ptgsk.region_model.get_region_parameter() p_opt = ptgsk.optimize(Timeaxis(opt_start, dt, 24 * num_opt_days), opt_state, target_spec_vec, p, p_min, p_max, tr_stop=1.0e-5) ptgsk.region_model.set_region_parameter(p_opt) corr_state = adjust_simulator_state(ptgsk, curr_time, q_obs_m3s_ts) ptgsk.run(Timeaxis(curr_time, dt, 24), corr_state) curr_time += deltahours(24) opt_states[curr_time] = ptgsk.reg_model_state discharge = ptgsk.region_model.statistics.discharge([0]) times.extend(discharge.time(i) for i in range(discharge.size())) values.extend(list(np.array(discharge.v))) plt.plot(utc_to_greg(times), values) plot_results(None, q_obs=observed_tistel_discharge( UtcPeriod(recal_start, recal_stop))) set_calendar_formatter(Calendar()) plt.interactive(1) plt.title("Continuously recalibrated discharge vs observed") plt.xlabel("Time in UTC") plt.ylabel(r"Discharge in $\mathbf{m^3s^{-1}}$", verticalalignment="top", rotation="horizontal") plt.gca().yaxis.set_label_coords(0, 1.1)
def save_images(self, tensor, filename, discriminator=None): plt.interactive(False) if not isinstance(tensor, list): plt.style.use('ggplot') plt.clf() fig = plt.figure() ax1 = fig.add_subplot(111) data = self.data.cpu().numpy() x, y = np.split(data, 2, axis=1) x = x.flatten() y = y.flatten() ax1.scatter(x, y, c='lime', s=1) data = tensor.data.cpu().numpy() if hasattr( tensor, 'data') else tensor.cpu().numpy() x, y = np.split(data, 2, axis=1) x = x.flatten() y = y.flatten() ax1.scatter(x, y, c='red', marker='.', s=1) else: if GridToyDataSet.colors is None: GridToyDataSet.colors = [np.random.rand(3, ) for _ in tensor] plt.style.use('ggplot') fig = plt.figure() ax1 = fig.add_subplot(111) GridToyDataSet._plot_discriminator(discriminator, ax1) # Plot generator x_original, y_original = self.points() ax1.scatter(x_original, y_original, zorder=len(tensor) + 1, color='b') cm = plt.get_cmap('gist_rainbow') ax1.set_prop_cycle('color', [cm(1. * i / 10) for i in range(10)]) for i, element in enumerate(tensor): data = element.data.cpu().numpy() if hasattr( element, 'data') else element.cpu().numpy() x, y = np.split(data, 2, axis=1) ax1.scatter(x.flatten(), y.flatten(), color=GridToyDataSet.colors[i], zorder=len(tensor) - i, marker='x') plt.savefig(filename) @staticmethod def _plot_discriminator(discriminator, ax): if discriminator is not None: alphas = [] for x in np.linspace(-1, 1, 8, endpoint=False): for y in np.linspace(-1, 1, 8, endpoint=False): center = torch.zeros(2) center[0] = x + 0.125 center[1] = y + 0.125 alphas.append( float( discriminator.net( to_pytorch_variable(center)))) alphas = np.asarray(alphas) normalized = (alphas - min(alphas)) / (max(alphas) - min(alphas)) plt.text(0.1, 0.9, 'Min: {}\nMax: {}'.format(min(alphas), max(alphas)), transform=ax.transAxes) k = 0 for x in np.linspace(-1, 1, 8, endpoint=False): for y in np.linspace(-1, 1, 8, endpoint=False): center = torch.zeros(2) center[0] = x + 0.125 center[1] = y + 0.125 ax.fill([x, x + 0.25, x + 0.25, x], [y, y, y + 0.25, y + 0.25], 'r', alpha=normalized[k], zorder=0) k += 1
P.stem(np.arange(i_max + 1 - i_min), mtx[i_min:i_max + 1, :].sum(1)) ttl = 'Note Frequency' if tstr: ttl += ': ' + tstr P.title(ttl, fontsize=16) t = P.xticks(np.arange(0, i_max + 1 - i_min, 3), pc_labels[i_min:i_max + 1:3], fontsize=14) P.xlabel('Pitch Class', fontsize=14) P.ylabel('Frequency', fontsize=14) ax = P.axis() P.axis(xmin=-0.5) P.grid() if __name__ == "__main__": P.interactive(True) a = np.loadtxt('01.ascii') P.figure() # Plot piano roll: MIDI pitch by beats P.subplot(211) plot_mtx(a, cmap=P.cm.gray_r, cbar=False) P.axis('tight') P.title('WTC 1 "Prelude in C": Piano Roll') # Plot dissonance by (integrated) beats P.subplot(212) win_len = 8 # Number of beats to integrate, non-overlapping a = win_mtx(a, win_len) d = dissonance_fun(a) P.plot(np.arange(len(d)) * win_len, d, 'r', linewidth=1) P.axis('tight')
for year in INTERVAL_1: y.append(raw_data.get_daily_temp('BOSTON', 1, 10, year)) models = generate_models(x, y, [1]) evaluate_models_on_training(x, y, models) def prob4(raw_data): # Problem 4: FILL IN MISSING CODE TO GENERATE y VALUES for yearly average x1 = INTERVAL_1 x2 = INTERVAL_2 y = [] # MISSING LINES for year in INTERVAL_1: y.append(np.mean(raw_data.get_yearly_temp('BOSTON', year))) models = generate_models(x1, y, [1]) evaluate_models_on_training(x1, y, models) def main(): ### Begining of program raw_data = Climate('data.csv') print("Calling for prob3 model") prob3(raw_data) print("Calling for prob4 model") prob4(raw_data) if __name__ == '__main__': pylab.interactive(False) main()
def drawnow(): pylab.show() pylab.interactive(False)
def demo(test_data_path): """ Edge Detection demo script. The input parameter is the pathname to the 'test_data' directory of the gemini_python SVN repository. It will display images in DS9 and plot in your terminal. Please make sure ds9 is running already. Using Python shell: >>> import demo >>> demo.demo('/data1/gemini_python/test_data') Using Unix shell: demo.py --tdir=<test_data_path> run ' """ import os #from gempy.science import extract as extr import extract as extr from astrodata import AstroData # Set test_dir path to the test files; they are available in the # test_data directory in the SVN repository: # http://chara.hi.gemini.edu/svn/DRSoftware/gemini_python/test_data/edge_detection/ if not os.access(test_data_path,os.F_OK): print "\n >>>> ERROR in test_data_path",test_data_path return test_data_path = os.path.join(test_data_path, 'edge_detection') if test_data_path == None: print "..... ERROR: Please edit this script and set the 'test_data_path'" gnirs_file = os.path.join(test_data_path, 'nN20101215S0475_comb.fits') f2_file = os.path.join(test_data_path, 'fS20100220S0035_comb.fits') gmos_file = os.path.join(test_data_path, 'mgS20100113S0110.fits') # EDIT the for loop to include gmos. GMOS is SLOW demo (~4 mins) #for fname in [gnirs_file,f2_file,gmos_file]: print "Starting time:",time.asctime() pylab.interactive(True) for fname in [gnirs_file,f2_file]: ad = AstroData(fname) print ".... 1) Finding slit edges for",ad.filename,' (',ad.instrument(),')' adout = extr.trace_slits(ad,debug=True) print ".... 2) Cutting slits... \n" ad_cuts = extr.cut_slits(adout,debug=True) bname = os.path.basename(fname) # Writing to FITS file. ad_cuts[0].filename='cuts_'+bname ad_cuts[0].write(clobber=True) print ".... 3) Writing FITS file: ",ad_cuts[0].filename," .. in your working directory." print ".... 4) Displayinig the whole frame in frame: 1" print "Ending time:",time.asctime()
def pm(a, ls=None): pylab.interactive(True)
'''Helper function to demonstrate some graphics''' for i in range(1, n_series + 1): if i % 3 == 0: x = np.linspace(0, 14, 10) plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip, '-o', label='curve %d' % i) else: x = np.linspace(0, 14, 100) plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip, label='curve %d' % i) plt.title("Sample sine plot") # don't use **axtitleparams to demonstrate the default behaviour plt.legend() plt.xlabel('this is X') plt.ylabel('this is Y\nlabel') # don't use **ylabelparams to demonstrate the default behaviour cleanup() return plt.gcf() a8c_style = a8c_style_gray sns.set_style(a8c_style) if __name__ == '__main__': plt.interactive(False) plt.figure() _ = sinplot(n_series=4) plt.figure() _ = sinplot(n_series=10) plt.show()
for n in range(n_max): z0[1] = s[1] z = rk4(f, z0, x) phi1 = z[N, 0] - beta ds = error_func(phi0, phi1, s[0], s[1]) s[0] = s[1] s[1] += ds phi0 = phi1 print(phi0) print('n = {} s1 = {} and ds = {}'.format(n, s[1], ds)) plt.plot(x, z[:, 0]) if abs(ds) <= eps: print('Solution converged for eps = {} and s1 ={} and ds = {}. \n'. format(eps, s[1], ds)) break plt.interactive(False) #Oppgave 1 b) s_start, s_end = -0.5, -1.23 sList = np.linspace(s_start, s_end, 51) phiList = np.zeros_like(sList) for n, s in enumerate(sList): Y_0 = [2, s] Y_shoot = rk4(dydx, Y_0, x) y_shoot = Y_shoot[:, 0] # extract y, and not y' phiList[n] = y_shoot[-1] - beta plt.figure() plt.plot(sList, phiList) plt.plot(sList, np.zeros_like(sList), 'k--')
import ttk import matplotlib matplotlib.use("TkAgg") from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg from matplotlib import style import numpy as np import numpy.ma as ma from astropy.io import fits from scipy.optimize import curve_fit from scipy import asarray as ar,exp #from PyAstronomy import pyasl #module not standard with python, not used in code yet import matplotlib.pylab as plt from matplotlib.ticker import AutoMinorLocator plt.interactive(True) import tkFileDialog LARGE_FONT = ("Verdana", 12) ; STND_FONT = ("Helvetica", 8) ; style.use("ggplot") ; class SpectraApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) ; tk.Tk.iconbitmap(self, default = "TexasIcon.ico") ; tk.Tk.wm_title(self, "Spectra Fitting Application") ;