def rri_test_recurrent(filelist=None): global shape_tmp for i in range(len(filelist)): # for i in range(10): with open(filelist[i], 'rb') as f: plk_tmp = pkl.load(f) ecg_re = ecg.ecg(signal=plk_tmp, sampling_rate=Fs, show=False) rpeaks_tmp = ecg_re['rpeaks'].tolist() nni = tools.nn_intervals(rpeaks=rpeaks_tmp) nni_tmp = nni.reshape((-1, int(nni.shape[0]))) # for 2d data type rp = RecurrencePlot(threshold='point', percentage=20) X_rp = rp.fit_transform(nni_tmp) dst = cv2.resize(X_rp[0], dsize=(135, 135), interpolation=cv2.INTER_AREA) shape_tmp.append(X_rp.shape) recurrence_tmp.append(X_rp) recur_resize.append(dst) # for pandas # shape_tmp = shape_tmp.append(pd.DataFrame(X_rp.shape)) # plot check plt.imshow(X_rp[0], cmap='binary', origin='lower') plt.plot(nni) plt.title('Recurrence Plot', fontsize=16) plt.tight_layout() plt.show() # np_tmp = np.column_stack([np_tmp, X_rp]) if i == 0: pass return shape_tmp, recurrence_tmp, np.asarray(recur_resize)
class TSToRP(Transform): r"""Transforms a time series batch to a 4d TSImage (bs, n_vars, size, size) by applying Recurrence Plot. It requires input to be previously normalized between -1 and 1""" order = 98 def __init__(self, size=224, cmap=None, **kwargs): self.size, self.cmap = size, cmap self.encoder = RecurrencePlot(**kwargs) def encodes(self, o: TSTensor): bs, *_, seq_len = o.shape size = ifnone(self.size, seq_len) if size != seq_len: o = F.interpolate(o.reshape(-1, 1, seq_len), size=size, mode='linear', align_corners=False)[:, 0] else: o = o.reshape(-1, seq_len) output = self.encoder.fit_transform(o.cpu().numpy()) / 2 output = output.reshape(bs, -1, size, size) if self.cmap and output.shape[1] == 1: output = TSImage(plt.get_cmap( self.cmap)(output)[..., :3]).squeeze(1).permute(0, 3, 1, 2) else: output = TSImage(output) return output.to(device=o.device)
def create_rp(segment, dimension=2, time_delay=1, percentage=1, use_clip=False, knn=None, imsize=None, images_dir='', base_name='Sample', suffix='jpg', # suffix='png' show_image=False, cmap=None, ##cmap='gray', cmap='binary' ): """Generate recurrence plot for specified signal segment and save to disk""" if base_name is None: base_name = 'sample' fname = '{}_d{}_t{}_p{}{}.{}'.format(base_name, dimension, time_delay, percentage, '_clipped' if use_clip else '', suffix) segment = np.expand_dims(segment, 0) if knn is not None: rp = RecurrencePlot(dimension=dimension, time_delay=time_delay) X_dist = rp.fit_transform(segment)[0] X_rp = mask_knn(X_dist, k=knn, policy='cols') elif use_clip: rp = RecurrencePlot(dimension=dimension, time_delay=time_delay) X_dist = rp.fit_transform(segment) X_rp = rp_norm(X_dist, threshold='percentage_clipped', percentage=percentage)[0] else: rp = RecurrencePlot(dimension=dimension, time_delay=time_delay, #threshold='percentage_points', percentage=percentage) threshold='point', percentage=percentage) X_rp = rp.fit_transform(segment)[0] if imsize is not None: X_rp = resize_rp(X_rp, new_shape=imsize, use_max=True) imageio.imwrite(os.path.join(images_dir, fname), np_to_uint8(X_rp)) if show_image: plt.figure(figsize=(3, 3)) plt.imshow(X_rp, cmap=cmap, origin='lower') plt.title('Recurrence Plot for {}'.format(fname), fontsize=14) plt.show() return fname
def save_recurrencePlots(net, save_recurrencePlots_file): global save_recurrence_plots if save_recurrence_plots: for name, parameters in net.named_parameters(): if "fc" in name and parameters.cpu().detach().numpy().ndim == 2: hiddenState = parameters.cpu().detach().numpy() rp = RecurrencePlot() X_rp = rp.fit_transform(hiddenState) plt.figure(figsize=(6, 6)) plt.imshow(X_rp[0], cmap='binary', origin='lower') plt.savefig(save_recurrencePlots_file, dpi=600) else: continue else: pass
def toRPdata(tsdatas, dimension=1, time_delay=1, threshold=None, percentage=10, flatten=False): X = [] rp = RecurrencePlot(dimension=dimension, time_delay=time_delay, threshold=threshold, percentage=percentage, flatten=flatten) for data in tsdatas: data_rp = rp.fit_transform(data) X.append(data_rp[0]) return np.array(X)
def RP_encoder(ts, size=None, dimension=1, time_delay=1, threshold=None, percentage=10, norm_output=True, **kwargs): ts = To2dArray(ts) assert ts.ndim == 2, 'ts ndim must be 2!' if size is None: size = ts.shape[-1] else: size = min(size, ts.shape[-1]) ts = PAA(window_size=None, output_size=size).fit_transform(ts) encoder = RP(dimension=dimension, time_delay=time_delay, threshold=threshold, percentage=percentage) output = np.squeeze(encoder.fit_transform(ts), 0) if norm_output: return norm(output) else: return output
""" # noqa:E501 # Author: Johann Faouzi <*****@*****.**> # License: BSD-3-Clause import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import ImageGrid from pyts.image import RecurrencePlot from pyts.datasets import load_gunpoint # Load the GunPoint dataset X, _, _, _ = load_gunpoint(return_X_y=True) # Get the recurrence plots for all the time series rp = RecurrencePlot(threshold='point', percentage=20) X_rp = rp.fit_transform(X) # Plot the 50 recurrence plots fig = plt.figure(figsize=(10, 5)) grid = ImageGrid(fig, 111, nrows_ncols=(5, 10), axes_pad=0.1, share_all=True) for i, ax in enumerate(grid): ax.imshow(X_rp[i], cmap='binary', origin='lower') grid[0].get_yaxis().set_ticks([]) grid[0].get_xaxis().set_ticks([]) fig.suptitle( "Recurrence plots for the 50 time series in the 'GunPoint' dataset", y=0.92) plt.show()
def PSOGSA_ResNet(dataset, max_iters, num_particles, Epochs, NumSave, lr, resume, savepath): np.seterr(divide='ignore', invalid='ignore') # %config InlineBackend.figure_format = 'retina' c1 = 2 c2 = 2 g0 = 1 dim = 2 w1 = 2 wMax = 0.9 wMin = 0.5 current_fitness = np.zeros((num_particles, 1)) gbest = np.zeros((1, dim)) gbest_score = float('inf') OldBest = float('inf') convergence = np.zeros(max_iters) alpha = 20 epsilon = 1 class Particle: pass #all particle initialized particles = [] Max = 1024 for i in range(num_particles): p = Particle() p.params = [ np.random.randint(Max * 0.5, Max), np.random.uniform(0.2, 0.9) ] p.fitness = rnd.rand() p.velocity = 0.3 * rnd.randn(dim) p.res_force = rnd.rand() p.acceleration = rnd.randn(dim) p.force = np.zeros(dim) p.id = i particles.append(p) #training print('training begain:', dataset) for i in range(max_iters): if i % 10 == 0: print('iteration number:', i) # gravitational constant g = g0 * np.exp((-alpha * i) / max_iters) # calculate mse cf = 0 for p in particles: fitness = 0 y_train = 0 if p.params[0] < Max * 0.5 or p.params[0] > Max: p.params[0] = np.random.randint(Max * 0.5, Max) if p.params[1] < 0.2 or p.params[1] > 0.9: p.params[1] = np.random.uniform(0.2, 0.9) print('hidden size, and contraction coefficients are:', p.params[0], p.params[1]) [fitness, hidden0] = ResNetBasics.ResNet(dataset, p.params, Epochs, 1, lr, resume, savepath) hiddensize = int(p.params[0]) # fitness = fitness/X.shape[0] OldFitness = fitness current_fitness[cf] = fitness cf += 1 if gbest_score > fitness and OldBest > fitness: hiddenState = hidden0.cpu().detach().numpy() rp = RecurrencePlot() X_rp = rp.fit_transform(hiddenState) plt.figure(figsize=(6, 6)) plt.imshow(X_rp[0], cmap='binary', origin='lower') # plt.title('Recurrence Plot', fontsize=14) plt.savefig(savepath + 'RecurrencePlots/' + 'RecurrencePlots_' + dataset + str(round(fitness, NumSave)) + '_' + str(hiddensize) + '_' + '.png', dpi=600) plt.show() """weightsName='reservoir.weight_hh' for name, param in named_parameters: # print(name,param) if name.startswith(weightsName): # set_trace() torch.save(param,savepath+'weights'+str(round(fitness,6))+'.pt')""" OldBest = gbest_score gbest_score = fitness gbest = p.params best_fit = min(current_fitness) worst_fit = max(current_fitness) for p in particles: p.mass = (current_fitness[particles.index(p)] - 0.99 * worst_fit) / (best_fit - worst_fit) for p in particles: p.mass = p.mass * 5 / sum([p.mass for p in particles]) # gravitational force for p in particles: for x in particles[particles.index(p) + 1:]: p.force = (g * (x.mass * p.mass) * (np.array(p.params) - np.array(x.params)).tolist() ) / (euclid_dist(p.params, x.params)) # resultant force for p in particles: p.res_force = p.res_force + rnd.rand() * p.force # acceleration for p in particles: p.acc = p.res_force / p.mass w1 = wMin - (i * (wMax - wMin) / max_iters) # velocity for p in particles: p.velocity = w1 * p.velocity + rnd.rand() * p.acceleration + ( rnd.rand() * np.array(gbest) - np.array(p.params)).tolist() # position for p in particles: p.params = p.params + p.velocity convergence[i] = gbest_score plt.figure(figsize=(6, 6)) plt.plot(convergence) plt.xlabel('Convergence') plt.ylabel('Error') plt.draw() plt.savefig(savepath + dataset + '_ConvergenceChanges.png', dpi=600) sys.stdout.write('\rMPSOGSA is training ResnNet (Iteration = ' + str(i + 1) + ', MSE = ' + str(gbest_score) + ')') sys.stdout.flush() # save results FileName = dataset + '_BestParameters.csv' newdata = [max_iters, num_particles, p.params, convergence] PathFileName = os.path.join(savepath, FileName) SV.SaveDataCsv(PathFileName, newdata)
def test_actual_results_recurrence_plot(params, arr_desired): """Test that the actual results are the expected ones.""" recurrence = RecurrencePlot(**params) arr_actual = recurrence.fit_transform(X) ** 2 np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)
def recurrence_plot(data, t_hold='point', percent=50, w_size=24): rp = RecurrencePlot(threshold=t_hold, percentage=percent) X = data.reshape(-1, w_size) X_rp = rp.fit_transform(X) return X_rp
def _method(self, X, **kwargs): rp = RecurrencePlot(**kwargs) return rp.fit_transform(X)
def rp_encode_2_to_3(arr_2d, percentage=60): recplot = RecurrencePlot(threshold='point', percentage=percentage) recplot_sff_3d = (recplot.fit_transform(array) for array in arr_2d) return recplot_sff_3d
def main(): # sine sin = np.vectorize(sin_f) series = sin(samples) # cos #cos = np.vectorize(cos_f) #series = cos(samples) # tan #tan = np.vectorize(tan_f) #series = tan(samples) # normal #series = np.random.normal(loc=6, scale=0.1, size=(N,1)) # binomial #series = np.random.binomial(n=10, p=0.7, size=(N,1)) # Poisson #series = np.random.poisson(lam=2, size=(N,1)) # Uniform #series = np.random.uniform(size=(N, 1)) # Logistic #series = np.random.logistic(loc=1, scale=2, size=(N, 1)) # Multi #series = np.random.multinomial(n=6, pvals=[1/6, 1/6, 1/6, 1/6, 1/6, 1/6]) # Exponential #series = np.random.exponential(scale=2, size=(N, 1)) # Chi #series = np.random.chisquare(df=2, size=(N, 1)) # Rayleigh #series = np.random.rayleigh(scale=2, size=(N, 1)) # Pareto #series = np.random.pareto(a=2, size=(N, 1)) # Zipf #series = np.random.zipf(a=2, size=(N, 1)) # rotate data and aggregate X = list() s_list = list(series) d_list = deque(s_list) i = 0 for _ in range(len(series)): d_list.rotate(i) X.append(list(d_list)) i = i + 1 # Recurrence plot transformation X_num = np.squeeze(np.array(X)) rp = RecurrencePlot(threshold='point', percentage=20) X_rp = rp.fit_transform(X_num) plt.figure(1) plt.plot(series) plt.show() plt.figure(2) for idx, _ in enumerate(series): plt.clf() plt.subplot(211) plt.imshow(X_rp[idx], cmap='binary', origin='lower') plt.title('Recurrence Plot ' + str(idx), fontsize=16) plt.tight_layout() plt.subplot(212) plt.plot(series) plt.scatter(idx, series[idx]) plt.tight_layout() plt.show() time.sleep(0.1) plt.figure(3) plt.hist(series) plt.show()
def rps(self, thres_type=None, thres_percentage=50, flatten=False, visualize=True): ''' generate RPs from sequences :param thres_type: ‘point’, ‘distance’ or None (default = None) :param thres_percentage: :param flatten: :param visualize: visualize generated RPs (first training sample) :return: PRs (samples for NNs and their label) ''' # Recurrence plot transformation for training samples rp_train = RecurrencePlot(threshold=thres_type, percentage=thres_percentage, flatten=flatten) rp_list = [] for idx in range(self.seq_array_train.shape[0]): temp_mts = self.seq_array_train[idx] # print (temp_mts.shape) X_rp_temp = rp_train.fit_transform(temp_mts) # print (X_rp_temp.shape) rp_list.append(X_rp_temp) rp_train_samples = np.stack(rp_list, axis=0) # Recurrence plot transformation for test samples rp_test = RecurrencePlot(threshold=thres_type, percentage=thres_percentage, flatten=flatten) rp_list = [] for idx in range(self.seq_array_test_last.shape[0]): temp_mts = self.seq_array_test_last[idx] # print (temp_mts.shape) X_rp_temp = rp_test.fit_transform(temp_mts) # print (X_rp_temp.shape) rp_list.append(X_rp_temp) rp_test_samples = np.stack(rp_list, axis=0) label_array_train = self.label_array_train label_array_test = self.label_array_test # Visualize RPs of the last sequences sliced from each TS (of the first engine) if visualize == True: X_rp = rp_train_samples[-1] plt.figure(figsize=(15, 15)) for s in range(len(X_rp)): # plt.subplot(last_seq_engine_1_array.shape[1],(s//4) + 1, (s%4)+1) plt.subplot(4, 4, s + 1) if flatten == True: img = np.atleast_2d(X_rp[s]) plt.imshow(img, extent=(0, img.shape[1], 0, round(img.shape[1] / 9))) else: plt.imshow(X_rp[s], origin='lower') # plt.legend() plt.show() return rp_train_samples, label_array_train, rp_test_samples, label_array_test
def sig2img(signals): rp = RecurrencePlot(dimension=1, time_delay=1, threshold=None) signals.reshape(1,-1) img = rp.fit_transform(signals) return img
engine="python") li.append(df) frame = pd.concat(li, axis=0, ignore_index=True) frame = frame.rename(columns={0: dir_name}) for i, f in zip(range(0, 409100, 600), range(700, 409700, 600)): data_rp = [] data_rp.append(frame[dir_name][i:f].values.reshape(1, -1)) data_rp.append(frame[dir_name][i:f].values.reshape(1, -1)) data_rp = np.asarray(data_rp) # Recurrence plot transformation rp = RecurrencePlot(threshold=None) X_rp = rp.fit_transform(data_rp[0])[0] # Show the results for the first time series plt.figure(figsize=(5, 5)) plt.imshow(X_rp, cmap='binary', origin='lower') plt.title('Recurrence Plot', fontsize=16) plt.tight_layout() plt.savefig("/gdrive/My Drive/EEG/RP_None/" + dir_name[-1] + "/" + str(i) + "-" + str(f) + ".png") plt.close() print(dir_name + " done!") # ### Recurrence Plots for A, C & E at Point Threshold - 5%, 25%, 75% # In[ ]:
def convertToTensor(dataset): rp = RecurrencePlot(dimension=1, percentage=10) rpArray = rp.fit_transform(dataset) rpTensor = [torch.tensor(s).float() for s in rpArray] return rpTensor