Ejemplo n.º 1
0
 def _criteria(self, model, criteria):
     # TODO. REST OF CRITERIA!
     P_I = []
     if criteria == '1/Mc':
         nMicroPerMacro = macroAccumulate(model, np.ones(model.micronum))
         P_I = 1 / macroAccumulate(model, model.data.N[model.cluster_ofmicro])
         P_I = P_I / nMicroPerMacro
     return P_I[model.macro_ofmicro]
Ejemplo n.º 2
0
 def _criteria(self, model, criteria):
     # TODO. REST OF CRITERIA!
     P_I = []
     if criteria == '1/Mc':
         nMicroPerMacro = macroAccumulate(model, np.ones(model.micronum))
         P_I = 1 / macroAccumulate(model,
                                   model.data.N[model.cluster_ofmicro])
         P_I = P_I / nMicroPerMacro
     return P_I[model.macro_ofmicro]
Ejemplo n.º 3
0
    def _algorithm(self):
        logger.info('Postprocessing new data')
        sims = simlist(glob(path.join(self.datapath, '*', '')),
                       glob(path.join(self.inputpath, '*', 'structure.pdb')),
                       glob(path.join(self.inputpath, '*', '')))
        if self.filter:
            sims = simfilter(sims, self.filteredpath, filtersel=self.filtersel)

        metr = Metric(sims, skip=self.skip)
        metr.set(self.projection)

        # if self.contactsym is not None:
        #    contactSymmetry(data, self.contactsym)

        if self.ticadim > 0:
            # tica = TICA(metr, int(max(2, np.ceil(self.ticalag))))  # gianni: without project it was tooooo slow
            tica = TICA(metr.project(), int(max(2, np.ceil(self.ticalag))))
            datadr = tica.project(self.ticadim)
        else:
            datadr = metr.project()

        datadr.dropTraj(
        )  # Preferably we should do this before any projections. Corrupted sims can affect TICA
        datadr.cluster(
            self.clustmethod(n_clusters=self._numClusters(datadr.numFrames)))
        model = Model(datadr)
        self._model = model
        self._model.markovModel(self.lag, self._numMacrostates(datadr))
        if self.save:
            self._model.save('adapt_model_e' + str(self._getEpoch()) + '.dat')

        # Undirected component
        uc = -model.data.N  # Lower counts should give higher score hence the -
        if self.statetype == 'micro':
            uc = uc[model.cluster_ofmicro]
        if self.statetype == 'macro':
            uc = macroAccumulate(model, uc[model.cluster_ofmicro])

        # Calculating the directed component
        dc = self._calculateDirectedComponent(sims, model.data.St,
                                              model.data.N)
        if self.statetype == 'micro':
            dc = dc[model.cluster_ofmicro]
        if self.statetype == 'macro':
            dc = macroAccumulate(model, dc[model.cluster_ofmicro])

        uc = self._featScale(uc)
        dc = self._featScale(dc)

        reward = dc + self.ucscale * uc

        relFrames = self._getSpawnFrames(reward, self._model, datadr)
        self._writeInputs(datadr.rel2sim(np.concatenate(relFrames)))
Ejemplo n.º 4
0
 def _criteria(self, model, criteria):
     if criteria == '1/Mc':
         nMicroPerMacro = macroAccumulate(model, np.ones(model.micronum))
         P_I = 1 / macroAccumulate(model, model.data.N[model.cluster_ofmicro])
         P_I = P_I / nMicroPerMacro
         ret = P_I[model.macro_ofmicro]
     elif criteria == 'pi/Mc':
         nMicroPerMacro = macroAccumulate(model, np.ones(model.micronum))
         P_I = 1 / macroAccumulate(model, model.data.N[model.cluster_ofmicro])
         P_I = P_I / nMicroPerMacro
         ret = P_I[model.macro_ofmicro]*model.msm.stationary_distribution
     return ret
Ejemplo n.º 5
0
 def _criteria(self, model, criteria):
     if criteria == '1/Mc':
         nMicroPerMacro = macroAccumulate(model, np.ones(model.micronum))
         P_I = 1 / macroAccumulate(model, model.data.N[model.cluster_ofmicro])
         P_I = P_I / nMicroPerMacro
         ret = P_I[model.macro_ofmicro]
     elif criteria == 'pi/Mc':
         nMicroPerMacro = macroAccumulate(model, np.ones(model.micronum))
         P_I = 1 / macroAccumulate(model, model.data.N[model.cluster_ofmicro])
         P_I = P_I / nMicroPerMacro
         ret = P_I[model.macro_ofmicro]*model.msm.stationary_distribution
     return ret
Ejemplo n.º 6
0
    def _algorithm(self):
        sims = self._getSimlist()

        if self.nosampledc:
            print('Spawning only from top DC conformations without sampling')
            goaldata = self._getGoalData(sims)
            if not self._checkNFrames(goaldata): return False
            datconcat = np.concatenate(goaldata.dat).flatten()
            sortedabs = np.argsort(datconcat)[::-1]
            if self._debug:
                np.save('debug.npy', sortedabs[:self.nmax - self._running])
                return True
            self._writeInputs(
                goaldata.abs2sim(sortedabs[:self.nmax - self._running]))
            return True

        data = self._getData(sims)
        if self.save:
            if not path.exists('saveddata'):
                os.makedirs('saveddata')
            np.savetxt(
                path.join('saveddata',
                          'e{}_report.npy'.format(self._getEpoch())),
                [self._getEpoch(), data.numFrames,
                 len(data.dat)])

        if not self._checkNFrames(data): return False
        goaldata = self._getGoalData(
            data.simlist
        )  # Using the simlist of data in case some trajectories were dropped
        if len(data.simlist) != len(goaldata.simlist):
            logger.warning(
                'The goal function was not able to project all trajectories that the MSM projection could.'
                'Check for possible errors in the goal function.')
        data.dropTraj(
            keepsims=goaldata.simlist
        )  # Ensuring that I use the intersection of projected simulations
        self._createMSM(data)

        model = self._model
        data = self._model.data

        # Undirected component
        uc = model.data.N  # Lower counts should give higher score
        if self.statetype == 'micro':
            uc = uc[model.cluster_ofmicro]
        if self.statetype == 'macro':
            uc = macroAccumulate(model, uc[model.cluster_ofmicro])
        uc = 1 / uc

        # Calculating the directed component
        dcmeans = dcstds = None
        if self.statetype == 'micro':
            dcmeans, dcstds = self._calculateDirectedComponent(
                goaldata, model.data.St, model.micro_ofcluster)
        elif self.statetype == 'macro':
            # TODO: Should we weigh by equilibrium population?
            dcmeans, dcstds = self._calculateDirectedComponent(
                goaldata, model.data.St, model.macro_ofcluster)

        ucunscaled = uc.copy()
        dcunscaled = dcmeans.copy()
        dc = dcmeans / np.sum(dcmeans)
        uc = uc / np.sum(uc)

        N = self.nmax - self._running
        Nuc, Ndc = np.random.multinomial(N, [self.epsilon, 1 - self.epsilon])
        rewardDC = self._truncate(uc * dc, Ndc)
        rewardUC = self._truncate(uc, Nuc)

        relFramesDC, spawncountsDC, truncprobDC = self._getSpawnFrames(
            rewardDC, self._model, data, Ndc)
        relFramesUC, spawncountsUC, truncprobUC = self._getSpawnFrames(
            rewardUC, self._model, data, Nuc)
        relFrames = np.concatenate(
            (np.concatenate(relFramesDC), np.concatenate(relFramesUC)))

        if self.save:
            if not path.exists('saveddata'):
                os.makedirs('saveddata')
            epoch = self._getEpoch()
            tosave = {
                'ucunscaled': -ucunscaled,
                'dcunscaled': dcunscaled,
                'uc': uc,
                'dc': dc,
                'epsilon': self.epsilon,
                'spawncountsDC': spawncountsDC,
                'spawncountsUC': spawncountsUC,
                'truncprobDC': truncprobDC,
                'truncprobUC': truncprobUC,
                'relFramesDC': relFramesDC,
                'relFramesUC': relFramesUC,
                'dcmeans': dcmeans,
                'dcstds': dcstds,
                'rewardDC': rewardDC,
                'rewardUC': rewardUC
            }
            np.save(path.join('saveddata', 'e{}_goalreport.npy'.format(epoch)),
                    tosave)
            np.save(
                path.join('saveddata', 'e{}_spawnframes.npy'.format(epoch)),
                relFrames)
            goaldata.save(
                path.join('saveddata', 'e{}_goaldata.dat'.format(epoch)))

        if self._debug:
            np.save('debug.npy', relFrames)
            return True

        self._writeInputs(data.rel2sim(relFrames))
        return True
Ejemplo n.º 7
0
    def _algorithm(self):
        sims = self._getSimlist()

        if self.nosampledc:
            print('Spawning only from top DC conformations without sampling')
            goaldata = self._getGoalData(sims)
            if not self._checkNFrames(goaldata): return False
            datconcat = np.concatenate(goaldata.dat).flatten()
            sortedabs = np.argsort(datconcat)[::-1]
            if self._debug: np.save('debug.npy', sortedabs[:self.nmax - self._running]); return True
            self._writeInputs(goaldata.abs2sim(sortedabs[:self.nmax - self._running]))
            return True

        data = self._getData(sims)
        if self.save:
            if not path.exists('saveddata'):
                os.makedirs('saveddata')
            np.savetxt(path.join('saveddata', 'e{}_report.npy'.format(self._getEpoch())), [self._getEpoch(), data.numFrames, len(data.dat)])

        if not self._checkNFrames(data): return False
        goaldata = self._getGoalData(data.simlist)  # Using the simlist of data in case some trajectories were dropped
        if len(data.simlist) != len(goaldata.simlist):
            logger.warning('The goal function was not able to project all trajectories that the MSM projection could.'
                           'Check for possible errors in the goal function.')
        data.dropTraj(keepsims=goaldata.simlist)  # Ensuring that I use the intersection of projected simulations
        self._createMSM(data)

        model = self._model
        data = self._model.data

        # Undirected component
        uc = -model.data.N  # Lower counts should give higher score hence the -
        if self.statetype == 'micro':
            uc = uc[model.cluster_ofmicro]
        if self.statetype == 'macro':
            uc = macroAccumulate(model, uc[model.cluster_ofmicro])

        # Calculating the directed component
        dcmeans = dcstds = None
        if self.statetype == 'micro':
            dcmeans, dcstds = self._calculateDirectedComponent(goaldata, model.data.St, model.micro_ofcluster)
        elif self.statetype == 'macro':
            # TODO: Should we weigh by equilibrium population?
            dcmeans, dcstds = self._calculateDirectedComponent(goaldata, model.data.St, model.macro_ofcluster)

        ucunscaled = uc
        dcunscaled = dcmeans
        uc = self._featScale(uc)
        dc = self._featScale(dcmeans)

        scale = self.ucscale
        if self.autoscale:
            scale = AdaptiveGoal._calculateScale(goaldata, self.autoscalediff, self.autoscalemult, self.autoscaletol)
        reward = scale * uc + (1 - scale) * dc

        N = self.nmax - self._running
        reward = self._truncate(reward, N)
        relFrames, spawncounts, truncprob = self._getSpawnFrames(reward, self._model, data, N)

        if self.save:
            if not path.exists('saveddata'):
                os.makedirs('saveddata')
            epoch = self._getEpoch()
            tosave = {'ucunscaled': -ucunscaled, 'dcunscaled': dcunscaled, 'uc': uc, 'dc': dc, 'ucscale': scale,
                      'spawncounts': spawncounts, 'truncprob': truncprob, 'relFrames': relFrames, 'dcmeans': dcmeans,
                      'dcstds': dcstds, 'reward': reward}
            np.save(path.join('saveddata', 'e{}_goalreport.npy'.format(epoch)), tosave)
            np.save(path.join('saveddata', 'e{}_spawnframes.npy'.format(epoch)), relFrames)
            goaldata.save(path.join('saveddata', 'e{}_goaldata.dat'.format(epoch)))

        if self._debug: np.save('debug.npy', relFrames); return True
        self._writeInputs(data.rel2sim(np.concatenate(relFrames)))
        return True
Ejemplo n.º 8
0
    def _algorithm(self):
        sims = self._getSimlist()

        if self.nosampledc:
            print('Spawning only from top DC conformations without sampling')
            goaldata = self._getGoalData(sims)
            if not self._checkNFrames(goaldata): return False
            datconcat = np.concatenate(goaldata.dat).flatten()
            sortedabs = np.argsort(datconcat)[::-1]
            if self._debug: np.save('debug.npy', sortedabs[:self.nmax - self._running]); return True
            self._writeInputs(goaldata.abs2sim(sortedabs[:self.nmax - self._running]))
            return True

        data = self._getData(sims)
        if self.save:
            if not path.exists('saveddata'):
                os.makedirs('saveddata')
            np.savetxt(path.join('saveddata', 'e{}_report.npy'.format(self._getEpoch())), [self._getEpoch(), data.numFrames, len(data.dat)])

        if not self._checkNFrames(data): return False
        goaldata = self._getGoalData(data.simlist)  # Using the simlist of data in case some trajectories were dropped
        if len(data.simlist) != len(goaldata.simlist):
            logger.warning('The goal function was not able to project all trajectories that the MSM projection could.'
                           'Check for possible errors in the goal function.')
        data.dropTraj(keepsims=goaldata.simlist)  # Ensuring that I use the intersection of projected simulations
        self._createMSM(data)

        model = self._model
        data = self._model.data

        # Undirected component
        uc = -model.data.N  # Lower counts should give higher score hence the -
        if self.statetype == 'micro':
            uc = uc[model.cluster_ofmicro]
        if self.statetype == 'macro':
            uc = macroAccumulate(model, uc[model.cluster_ofmicro])

        # Calculating the directed component
        dcmeans = dcstds = None
        if self.statetype == 'micro':
            dcmeans, dcstds = self._calculateDirectedComponent(goaldata, model.data.St, model.micro_ofcluster)
        elif self.statetype == 'macro':
            # TODO: Should we weigh by equilibrium population?
            dcmeans, dcstds = self._calculateDirectedComponent(goaldata, model.data.St, model.macro_ofcluster)

        ucunscaled = uc
        dcunscaled = dcmeans
        uc = self._featScale(uc)
        dc = self._featScale(dcmeans)

        scale = self.ucscale
        if self.autoscale:
            scale = AdaptiveGoal._calculateScale(goaldata, self.autoscalediff, self.autoscalemult, self.autoscaletol)
        reward = scale * uc + (1 - scale) * dc

        N = self.nmax - self._running
        reward = self._truncate(reward, N)
        relFrames, spawncounts, truncprob = self._getSpawnFrames(reward, self._model, data, N)

        if self.save:
            if not path.exists('saveddata'):
                os.makedirs('saveddata')
            epoch = self._getEpoch()
            tosave = {'ucunscaled': -ucunscaled, 'dcunscaled': dcunscaled, 'uc': uc, 'dc': dc, 'ucscale': scale,
                      'spawncounts': spawncounts, 'truncprob': truncprob, 'relFrames': relFrames, 'dcmeans': dcmeans,
                      'dcstds': dcstds, 'reward': reward}
            np.save(path.join('saveddata', 'e{}_goalreport.npy'.format(epoch)), tosave)
            np.save(path.join('saveddata', 'e{}_spawnframes.npy'.format(epoch)), relFrames)
            goaldata.save(path.join('saveddata', 'e{}_goaldata.dat'.format(epoch)))

        if self._debug: np.save('debug.npy', relFrames); return True
        self._writeInputs(data.rel2sim(np.concatenate(relFrames)))
        return True