def flag(self, vis, vis_mask, li, gi, tbl, ts, **kwargs): """Function that does the actual flag.""" sigma = self.params['sigma'] time_window = self.params['time_window'] nt = vis.shape[0] abs_vis = np.abs(np.ma.array(vis, mask=vis_mask)) # mask all if valid values less than the given threshold if abs_vis.count() < 0.1 * nt or abs_vis.count() <= 3: vis_mask[:] = True return if np.ma.count_masked(abs_vis) > 0: # has masked value abs_vis_valid = abs_vis[~abs_vis.mask] inds_valid = np.arange(nt)[~abs_vis.mask] itp = InterpolatedUnivariateSpline(inds_valid, abs_vis_valid) abs_vis_itp = itp(np.arange(nt)) abs_vis1 = abs_vis_itp.copy() else: abs_vis1 = abs_vis.copy() for cnt in xrange(10): if cnt != 0: abs_vis1[inds] = smooth[inds] smooth = savitzky_golay(abs_vis1, time_window, 3) # flage RFI diff = abs_vis1 - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where(np.abs(diff - mean) > sigma*std)[0] if len(inds) == 0: break diff = abs_vis - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where(np.abs(diff - mean) > sigma*std)[0] # masked inds # Addtional threshold # inds1 = np.where(np.abs(diff[inds]) > 1.0e-2*np.abs(smooth[inds]))[0] # inds = inds[inds1] vis_mask[inds] = True # set mask
def flag(self, vis, vis_mask, li, gi, bl, ts, **kwargs): """Function that does the actual flag.""" freq_window = self.params['freq_window'] time_window = self.params['time_window'] freq_sigma = self.params['freq_sigma'] time_sigma = self.params['time_sigma'] plot_fit = self.params['plot_fit'] freq_fig_prefix = self.params['freq_fig_name'] time_fig_prefix = self.params['time_fig_name'] tag_output_iter = self.params['tag_output_iter'] iteration = self.iteration freq_flag = kwargs.get('freq_flag') time_flag = kwargs.get('time_flag') time = ts.time[:] nt = len(time) freq = ts.freq[:] nfreq = len(freq) if isinstance(ts, Timestream): # for Timestream pol = bl[0] bl = tuple(bl[1]) elif isinstance(ts, RawTimestream): # for RawTimestream pol = None bl = tuple(bl) else: raise ValueError('Need either a RawTimestream or Timestream') if freq_flag: # time integration tm_vis = np.ma.mean(np.ma.array(vis, mask=vis_mask), axis=0) abs_vis = np.abs(tm_vis) if np.ma.count_masked(tm_vis) > 0: # has masked value abs_vis_valid = abs_vis[~abs_vis.mask] inds_valid = np.arange(nfreq)[~abs_vis.mask] itp = InterpolatedUnivariateSpline(inds_valid, abs_vis_valid) abs_vis_itp = itp(np.arange(nfreq)) abs_vis1 = abs_vis_itp.copy() else: abs_vis1 = abs_vis.copy() for cnt in xrange(10): if cnt != 0: abs_vis1[inds] = smooth[inds] smooth = savitzky_golay(abs_vis1, freq_window, 3) # flage RFI diff = abs_vis1 - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where(np.abs(diff - mean) > freq_sigma * std)[0] if len(inds) == 0: break diff = abs_vis - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where( np.abs(diff - mean) > freq_sigma * std)[0] # masked inds vis_mask[:, inds] = True # set mask if plot_fit: plt.figure() plt.plot(freq, abs_vis, label='data') plt.plot(freq[inds], abs_vis[inds], 'ro', label='flag') plt.plot(freq, smooth, label='smooth') plt.xlabel(r'$\nu$ / MHz') plt.legend(loc='best') if pol is None: fig_name = '%s_%d_%d.png' % (freq_fig_prefix, bl[0], bl[1]) else: fig_name = '%s_%d_%d_%s.png' % (freq_fig_prefix, bl[0], bl[1], pol) if tag_output_iter: fig_name = output_path(fig_name, iteration=iteration) else: fig_name = output_path(fig_name) plt.savefig(fig_name) plt.close() if time_flag: # freq integration fm_vis = np.ma.mean(np.ma.array(vis, mask=vis_mask), axis=1) abs_vis = np.abs(fm_vis) if np.ma.count_masked(fm_vis) > 0: # has masked value abs_vis_valid = abs_vis[~abs_vis.mask] inds_valid = np.arange(nt)[~abs_vis.mask] itp = InterpolatedUnivariateSpline(inds_valid, abs_vis_valid) abs_vis_itp = itp(np.arange(nt)) abs_vis1 = abs_vis_itp.copy() else: abs_vis1 = abs_vis.copy() for cnt in xrange(10): if cnt != 0: abs_vis1[inds] = smooth[inds] smooth = savitzky_golay(abs_vis1, time_window, 3) # flage RFI diff = abs_vis1 - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where(np.abs(diff - mean) > time_sigma * std)[0] if len(inds) == 0: break diff = abs_vis - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where( np.abs(diff - mean) > time_sigma * std)[0] # masked inds # Addtional threshold # inds1 = np.where(np.abs(diff[inds]) > 1.0e-2*np.abs(smooth[inds]))[0] # inds = inds[inds1] vis_mask[inds] = True # set mask if plot_fit: plt.figure() plt.plot(time, abs_vis, label='data') plt.plot(time[inds], abs_vis[inds], 'ro', label='flag') plt.plot(time, smooth, label='smooth') plt.xlabel(r'$t$ / Julian Date') plt.legend(loc='best') if pol is None: fig_name = '%s_%d_%d.png' % (time_fig_prefix, bl[0], bl[1]) else: fig_name = '%s_%d_%d_%s.png' % (time_fig_prefix, bl[0], bl[1], pol) if tag_output_iter: fig_name = output_path(fig_name, iteration=iteration) else: fig_name = output_path(fig_name) plt.savefig(fig_name) plt.close()
def savePlotData(timeseries_title, timeseries_data, tweet_id, tweet_from): filename = None plt.figure() time_axis = [i + 1 for i in range(len(timeseries_data))] # custom x labels time_ticks_diff = int(len(timeseries_data) / 5) time_ticks = [ 1, time_ticks_diff, 2 * time_ticks_diff, 3 * time_ticks_diff, 4 * time_ticks_diff, len(timeseries_data) - 1, ] time_labels = [timeseries_data[a][0] for a in time_ticks] # change latest if timeseries_title[0] == "Time": time_labels[-1] = "Now" elif timeseries_title[0] == "Day": time_labels[-1] = "Today" else: time_labels[-1] = "This week" plt.xticks(time_ticks, time_labels) lines_styles = ["-", "--", "-.", ":", "-"] linestyle_cycler = cycle(lines_styles) lines_widths = [2, 2, 3, 3, 3] linewidth_cycler = cycle(lines_widths) for i in range(len(timeseries_title) - 1): y_axis = [(a[i + 1]) for a in timeseries_data] y_axis = ["0" if a == " " else a for a in y_axis] y_axis = [int(a) for a in y_axis] y_axis_smooth = sg_filter.savitzky_golay(y_axis, 11, 3) y_axis_smooth = [0 if a < 0 else a for a in y_axis_smooth] plt.plot( time_axis, y_axis_smooth, linewidth=next(linewidth_cycler), linestyle=next(linestyle_cycler), label=timeseries_title[i + 1], ) plt.xlabel(timeseries_title[0]) plot_title = [a[0].upper() + a[1:] for a in timeseries_title[1:]] plt.title(" vs. ".join(plot_title)) plt.annotate( "By @SnShines", xy=(0.9, 0.95), xycoords="figure fraction", xytext=(0.9, 0.95), textcoords="figure fraction" ) plt.legend() current_fig = plt.gcf() current_fig.set_size_inches(18.5, 10.5) current_fig.savefig("plots/%s_%s.png" % (tweet_id, tweet_from)) filename = "plots/%s_%s.png" % (tweet_id, tweet_from) return filename
import sg_filter trainingRewards = [] if path.isfile("rewards.csv"): rewards = csv.reader(open("rewards.csv")) for row in rewards: #print(row) if row: trainingRewards.append(float(row[0]) / 100) plt.figure(figsize=(5.5, 4)) #plt.plot(trainingRewards, alpha=.2, label = "orig") plt.plot(np.convolve(trainingRewards, np.ones((10, )) / 10, mode='valid'), alpha=.2, label="10") #plt.plot(np.convolve(trainingRewards, np.ones((100,))/100, mode='valid'), label = "100") #plt.plot(np.convolve(trainingRewards, np.ones((300,))/300, mode='valid'), label = "300") plt.plot(sg_filter.savitzky_golay(np.array(trainingRewards), 301, 3)) plt.xlabel('Episodes', fontsize=12) plt.ylabel('Accumulated reward', fontsize=12) plt.grid() plt.xticks(ticks=[0, 10000, 20000, 30000, 40000]) plt.figure(figsize=(5.5, 4)) rewards = np.loadtxt("QSimple/training/rewards.txt") print(rewards) plt.plot(rewards, alpha=.2) plt.plot(sg_filter.savitzky_golay(rewards, 31, 3)) plt.xlabel('Episodes', fontsize=12) plt.ylabel('Accumulated reward', fontsize=12) plt.grid() plt.show()
current_generation = 0 x = [] y = [] xmean = [] ymean = [] while current_generation < generation_max: winrates = np.loadtxt(pathprefix+"/generational_winrates/gen{}.txt".format(str(current_generation))) / 10 xmean.append(current_generation) ymean.append(np.mean(winrates)) for j in range(len(winrates)): x.append(current_generation) y.append(winrates[j]) if current_generation == 20: incrementer = 5 # analyze all up to gen 20 - hereafter only every fifth. current_generation += incrementer plt.figure(figsize=(5,4)) plt.scatter(x, y, alpha = 0.1, edgecolors='None') plt.plot(xmean,sg_filter.savitzky_golay(np.array(ymean), 15, 3), linewidth=3) plt.xlabel('Generation') plt.ylabel('Win rate [%]') plt.grid() plt.show() #scores = np.array(scores) #np.savetxt(pathprefix+"/gen_vs_random.txt", scores)
def flag(self, vis, vis_mask, li, gi, bl, ts, **kwargs): """Function that does the actual flag.""" freq_window = self.params['freq_window'] time_window = self.params['time_window'] freq_sigma = self.params['freq_sigma'] time_sigma = self.params['time_sigma'] plot_fit = self.params['plot_fit'] freq_fig_prefix = self.params['freq_fig_name'] time_fig_prefix = self.params['time_fig_name'] tag_output_iter = self.params['tag_output_iter'] iteration = self.iteration freq_flag = kwargs.get('freq_flag') time_flag = kwargs.get('time_flag') time = ts.time[:] nt = len(time) freq = ts.freq[:] nfreq = len(freq) if isinstance(ts, Timestream): # for Timestream pol = bl[0] bl = tuple(bl[1]) elif isinstance(ts, RawTimestream): # for RawTimestream pol = None bl = tuple(bl) else: raise ValueError('Need either a RawTimestream or Timestream') if freq_flag: # time integration tm_vis = np.ma.mean(np.ma.array(vis, mask=vis_mask), axis=0) abs_vis = np.abs(tm_vis) if np.ma.count_masked(tm_vis) > 0: # has masked value abs_vis_valid = abs_vis[~abs_vis.mask] inds_valid = np.arange(nfreq)[~abs_vis.mask] itp = InterpolatedUnivariateSpline(inds_valid, abs_vis_valid) abs_vis_itp = itp(np.arange(nfreq)) abs_vis1 = abs_vis_itp.copy() else: abs_vis1 = abs_vis.copy() for cnt in xrange(10): if cnt != 0: abs_vis1[inds] = smooth[inds] smooth = savitzky_golay(abs_vis1, freq_window, 3) # flage RFI diff = abs_vis1 - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where(np.abs(diff - mean) > freq_sigma*std)[0] if len(inds) == 0: break diff = abs_vis - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where(np.abs(diff - mean) > freq_sigma*std)[0] # masked inds vis_mask[:, inds] = True # set mask if plot_fit: plt.figure() plt.plot(freq, abs_vis, label='data') plt.plot(freq[inds], abs_vis[inds], 'ro', label='flag') plt.plot(freq, smooth, label='smooth') plt.xlabel(r'$\nu$ / MHz') plt.legend(loc='best') if pol is None: fig_name = '%s_%d_%d.png' % (freq_fig_prefix, bl[0], bl[1]) else: fig_name = '%s_%d_%d_%s.png' % (freq_fig_prefix, bl[0], bl[1], pol) if tag_output_iter: fig_name = output_path(fig_name, iteration=iteration) else: fig_name = output_path(fig_name) plt.savefig(fig_name) plt.close() if time_flag: # freq integration fm_vis = np.ma.mean(np.ma.array(vis, mask=vis_mask), axis=1) abs_vis = np.abs(fm_vis) if np.ma.count_masked(fm_vis) > 0: # has masked value abs_vis_valid = abs_vis[~abs_vis.mask] inds_valid = np.arange(nt)[~abs_vis.mask] itp = InterpolatedUnivariateSpline(inds_valid, abs_vis_valid) abs_vis_itp = itp(np.arange(nt)) abs_vis1 = abs_vis_itp.copy() else: abs_vis1 = abs_vis.copy() for cnt in xrange(10): if cnt != 0: abs_vis1[inds] = smooth[inds] smooth = savitzky_golay(abs_vis1, time_window, 3) # flage RFI diff = abs_vis1 - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where(np.abs(diff - mean) > time_sigma*std)[0] if len(inds) == 0: break diff = abs_vis - smooth mean = np.mean(diff) std = np.std(diff) inds = np.where(np.abs(diff - mean) > time_sigma*std)[0] # masked inds # Addtional threshold # inds1 = np.where(np.abs(diff[inds]) > 1.0e-2*np.abs(smooth[inds]))[0] # inds = inds[inds1] vis_mask[inds] = True # set mask if plot_fit: plt.figure() plt.plot(time, abs_vis, label='data') plt.plot(time[inds], abs_vis[inds], 'ro', label='flag') plt.plot(time, smooth, label='smooth') plt.xlabel(r'$t$ / Julian Date') plt.legend(loc='best') if pol is None: fig_name = '%s_%d_%d.png' % (time_fig_prefix, bl[0], bl[1]) else: fig_name = '%s_%d_%d_%s.png' % (time_fig_prefix, bl[0], bl[1], pol) if tag_output_iter: fig_name = output_path(fig_name, iteration=iteration) else: fig_name = output_path(fig_name) plt.savefig(fig_name) plt.close() return vis, vis_mask
parser.add_argument('--movetype', type=int, default=0) args = parser.parse_args() agent = QSimple(standardMoveType=args.movetype) evaluator = LudoPlayerRandom() players = [agent, LudoPlayerRandom(), LudoPlayerRandom(), LudoPlayerRandom()] for i, player in enumerate(players): player.id = i N = 500 rewards = [] wins = [0, 0, 0, 0] for i in tqdm(range(N)): agent.moves = 0 random.shuffle(players) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() wins[players[winner].id] += 1 rewards.append(agent.accumulatedReward) agent.accumulatedReward = 0 print(wins[0] / N) np.savetxt("QSimple/Q.txt", agent.Q) np.savetxt("QSimple/training/rewards.txt", rewards) plt.plot(rewards, alpha=.2) plt.plot(sg_filter.savitzky_golay(np.array(rewards), 31, 3)) plt.xlabel('Episodes', fontsize=16) plt.ylabel('Accumulated reward', fontsize=16) plt.grid() plt.show()