def _startingFrames( self, ntraj, startFrames=None, simlen=None, maintainlen=True, ): if startFrames is None: if self.is_adaptive: from htmd.adaptive.adaptive import epochSimIndexes idx = epochSimIndexes(self.fulldata.simlist) startFrames = [] # When starting, it picks random starting nmax trajs from epoch 1's trajectories. ntraj == nmax for i in np.random.choice(idx[1], ntraj, replace=False): startFrames.append([i, 0]) else: traj = np.random.randint(len(self.fulldata.trajectories)) nframes = self.fulldata.trajectories[traj].reference.shape[0] frame = np.random.randint(nframes) startFrames = [[traj, frame]] else: startFrames = self._convertRelFrames(startFrames) startFrames = self._pickFromCluster(startFrames, simlen, maintainlen=maintainlen) return startFrames
def __init__(self, input_folder, output_folder, analysis_function, sims=None, precalc_metric=None, epoch_analysis=None, sim_analysis=None, low_memory_usage=False, test=False): self.input_folder = input_folder self.output_folder = output_folder os.makedirs(self.output_folder, exist_ok=True) self.analyze_function = analysis_function self.precalc_metric = precalc_metric self.current_epoch = None self.current_analysis = None self.precalc_data = None self.precalculated_data = False self.analysis_type = "epoch" self.epoch_analysis = epoch_analysis self.sim_analysis = sim_analysis self.epoch_sim_indexes = None self.sims_number = 0 self.associated_metrics = {} self.associated_data = {} self.tmp_associated_data = {} self.low_memory_usage = low_memory_usage if isinstance(sims, np.ndarray): self._sims = sims else: self._sims = self._getsimlist(input_folder) self.epoch_sim_indexes = epochSimIndexes(self._sims) #self._detect_epochs() ## Sets both self.current epoch and self.current_analysis self.test = test
def _calculateScale(goaldata, epochdiff, multiplier=1, tolerance=0.2): from htmd.adaptive.adaptive import epochSimIndexes # Calculate the max goal of each epoch and the total min of the goal to normalize the goals to a [0, 1] epochs = epochSimIndexes(goaldata.simlist) g = np.zeros(len(epochs)) totalmin = None dat = goaldata.dat for i, e in enumerate(sorted(epochs.keys())): idx = epochs[e] epochgoals = np.concatenate(dat[idx]) g[i] = epochgoals.max() if totalmin is None or epochgoals.min() < totalmin: totalmin = epochgoals.min() rangeG = g.max() - totalmin # Calculate the dG g = np.hstack( ([g[0]] * epochdiff, g) ) # Prepending the first element epochdiff-times to calculate the dG dG = np.abs(g[epochdiff:] - g[:-epochdiff]) / rangeG # Tolerance is the range of what we consider a significant change on a scale of [0, 1] # Multiplier is a scaling factor in case we want to react stronger to grad = -multiplier * (dG - tolerance) # Euler integration tstep = 1 y = [ 0, ] #print(dG, g, grad) for t in range(1, len(dG), tstep): y.append(max(min(y[-1] + tstep * grad[t], 1), 0)) #print('time: {} a: {} gradient: {} rangemax: {} rangemin: {}'.format(t, y[-1], grad[t], g.max(), totalmin)) #print("END") #return y, grad, dG, g return y[-1]
def _calculateScale(goaldata, epochdiff, multiplier=1, tolerance=0.2): from htmd.adaptive.adaptive import epochSimIndexes # Calculate the max goal of each epoch and the total min of the goal to normalize the goals to a [0, 1] epochs = epochSimIndexes(goaldata.simlist) g = np.zeros(len(epochs)) totalmin = None dat = goaldata.dat for i, e in enumerate(sorted(epochs.keys())): idx = epochs[e] epochgoals = np.concatenate(dat[idx]) g[i] = epochgoals.max() if totalmin is None or epochgoals.min() < totalmin: totalmin = epochgoals.min() rangeG = g.max() - totalmin # Calculate the dG g = np.hstack(([g[0]] * epochdiff, g)) # Prepending the first element epochdiff-times to calculate the dG dG = np.abs(g[epochdiff:] - g[:-epochdiff]) / rangeG # Tolerance is the range of what we consider a significant change on a scale of [0, 1] # Multiplier is a scaling factor in case we want to react stronger to grad = -multiplier * (dG - tolerance) # Euler integration tstep = 1 y = [0, ] #print(dG, g, grad) for t in range(1, len(dG), tstep): y.append(max(min(y[-1] + tstep * grad[t], 1), 0)) #print('time: {} a: {} gradient: {} rangemax: {} rangemin: {}'.format(t, y[-1], grad[t], g.max(), totalmin)) #print("END") #return y, grad, dG, g return y[-1]
def fitBaselineSpam(model, data=None, basedata=None, ticalag=25, ticadim=4, ticaunits='ns', factor=1): import pandas as pd from htmd.metricdata import MetricData import numpy as np all_data = pd.DataFrame(columns=["epoch", "sim", "x", "y"]) if basedata: spam_model = spamming(model) for idx, data in enumerate(spam_model): if idx == 0: projectdata, basetica = fitBaseline(data, basedata, ticalag=ticalag, ticadim=ticadim, ticaunits=ticaunits, tica=True) else: projectdata = basetica.tic.transform(data) for i in projectdata: all_data = all_data.append({ "epoch": idx, "x": i[0], "y": i[1] }, ignore_index=True) else: from htmd.adaptive.adaptive import epochSimIndexes epoch_idx = epochSimIndexes(model.data.simlist) if isinstance(data, np.ndarray): simids = np.array([i.simid for i in model.data.simlist]) first_frames = np.array(np.ceil(model.data.trajLengths / factor), dtype=int) spam_data = {i: [data[np.sum(first_frames[0:i])]] for i in simids} #List within list for compatibility choosen_frames = [np.sum(first_frames[0:i]) for i in simids] elif isinstance(data, MetricData): spam_data = data.dat else: spam_data = model.data.dat for key, val in epoch_idx.items(): for sim in val: x, y = spam_data[sim][0][0:2] all_data = all_data.append( { "epoch": key, "sim": sim, "x": x, "y": y }, ignore_index=True) return all_data