Ejemplo n.º 1
0
def biocLite(package=None, suppressUpdates=True, verbose=True):
    """Install a bioconductor package

    This function does not work like the R function. Only a few options are
    implemented so far. However, you can use rcode function directly if needed.

    :param str package: name of the bioconductor package to install. If None, no
        package is installed but installed packages are updated. If not provided, 
        biocLite itself may be updated if needed.
    :param bool suppressUpdates: updates the dependencies if needed (default is
        False)

    :return: True if update is required or the required package is installed and
        could be imported. False otherwise.

    ::

        >>> from biokit.viz.rtools import biocLite
        >>> biocLite("CellNOptR")

    """
    code = """source("http://bioconductor.org/biocLite.R")\n"""

    # without a package, biocLite performs an update of the installed packages
    if package is None:
        code += """biocLite(suppressUpdates=%s) """ % (
            bool2R(suppressUpdates))
    else:
        # if not found, no error is returned...
        code += """biocLite("%s", suppressUpdates=%s) """ % (
            package,
            bool2R(suppressUpdates))
    r = RSession(verbose=verbose)
    r.run(code)
Ejemplo n.º 2
0
    def simulate(self, bs=None, compression=True, expansion=True):
        """Return the score of the objective function

        :param list bs: a bitstring. Must be a list of zeros and ones
            with order identical to :meth:`reactions_r`, which 
            is populated once :meth:`optimise` is called.

        """
        if bs is None:
            bs = ",".join([str(x) for x in self.results.results.best_bitstring])
        else:
            bs = ",".join([str(x) for x in bs])

        script_template = """
        library(CellNOptR)
        pknmodel = readSIF("%(pkn)s")
        cnolist = CNOlist("%(midas)s")
        model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
            expansion=%(expansion)s, maxInputsPerGate=3)
        mse = computeScoreT1(cnolist, model, %(bs)s)
        """

        # FIXME: should re-use the user preprocess options
        script = script_template % {
                'pkn': self.pknmodel.filename,
                'midas': self.data.filename,
                'compression': bool2R(compression),
                'expansion': bool2R(expansion),
                'bs': "c(" + bs + ")"
                }

        self.session.run(script)
        return self.session.mse
Ejemplo n.º 3
0
def biocLite(package=None, suppressUpdates=True, verbose=True):
    """Install a bioconductor package

    This function does not work like the R function. Only a few options are
    implemented so far. However, you can use rcode function directly if needed.

    :param str package: name of the bioconductor package to install. If None, no
        package is installed but installed packages are updated. If not provided, 
        biocLite itself may be updated if needed.
    :param bool suppressUpdates: updates the dependencies if needed (default is
        False)

    :return: True if update is required or the required package is installed and
        could be imported. False otherwise.

    ::

        >>> from biokit.viz.rtools import biocLite
        >>> biocLite("CellNOptR")

    """
    code = """source("http://bioconductor.org/biocLite.R")\n"""

    # without a package, biocLite performs an update of the installed packages
    if package is None:
        code += """biocLite(suppressUpdates=%s) """ % (
            bool2R(suppressUpdates))
    else:
        # if not found, no error is returned...
        code += """biocLite("%s", suppressUpdates=%s) """ % (
            package,
            bool2R(suppressUpdates))
    r = RSession(verbose=verbose)
    r.run(code)
Ejemplo n.º 4
0
def install_package(query,
                    dependencies=False,
                    verbose=True,
                    repos="http://cran.univ-paris1.fr/"):
    """Install a R package

    :param str query: It can be a valid URL to a R package (tar ball), a CRAN
        package, a path to a R package (tar ball), or simply the directory
        containing a R package source.
    :param bool dependencies:
    :param repos: if provided, install_packages automatically select the
        provided repositories otherwise a popup window will ask you to select a repo

    ::

        >>> rtools.install_package("path_to_a_valid_Rpackage.tar.gz")
        >>> rtools.install_package("http://URL_to_a_valid_Rpackage.tar.gz")
        >>> rtools.install_package("hash") # a CRAN package
        >>> rtools.install_package("path to a valid R package directory")

    .. seealso:: :class:`biokit.rtools.RPackageManager`
    """
    session = RSession(verbose=verbose)

    # Is it a local file?
    if os.path.exists(query):
        repos = 'NULL'
    else:
        repos = '"{0}"'.format(
            repos)  # we want the " to be part of the string later on

    try:
        # PART for fetching a file on the web, download and install locally
        if verbose:
            print("Trying from the web ?")
        data = urlopen(query)
        fh = TempFile(suffix=".tar.gz")
        with open(fh.name, 'w') as fh:
            for x in data.readlines():
                fh.write(x)
        code = """install.packages("%s", dependencies=%s """ % \
            (fh.name, bool2R(dependencies))
        code += """ , repos=NULL) """
        session.run(code)

    except Exception as err:
        if verbose:
            print(err)
            print("trying local or from repos")
            print(
                "RTOOLS warning: URL provided does not seem to exist %s. Trying from CRAN"
                % query)
        code = """install.packages("%s", dependencies=%s """ % \
            (query, bool2R(dependencies))

        code += """ , repos=%s) """ % repos
        session.run(code)
        return
Ejemplo n.º 5
0
def install_package(query, dependencies=False, verbose=True,
    repos = "http://cran.univ-paris1.fr/"):
    """Install a R package

    :param str query: It can be a valid URL to a R package (tar ball), a CRAN
        package, a path to a R package (tar ball), or simply the directory
        containing a R package source.
    :param bool dependencies:
    :param repos: if provided, install_packages automatically select the
        provided repositories otherwise a popup window will ask you to select a repo

    ::

        >>> rtools.install_package("path_to_a_valid_Rpackage.tar.gz")
        >>> rtools.install_package("http://URL_to_a_valid_Rpackage.tar.gz")
        >>> rtools.install_package("hash") # a CRAN package
        >>> rtools.install_package("path to a valid R package directory")

    .. seealso:: :class:`biokit.rtools.RPackageManager`
    """
    session = RSession(verbose=verbose)

    # Is it a local file?
    if os.path.exists(query):
        repos = 'NULL'
    else:
        repos = '"{0}"'.format(repos) # we want the " to be part of the string later on
    
    try:
        # PART for fetching a file on the web, download and install locally
        if verbose:
            print("Trying from the web ?")
        data = urlopen(query)
        fh = TempFile(suffix=".tar.gz")
        with open(fh.name, 'w') as fh:
            for x in data.readlines():
                fh.write(x)
        code = """install.packages("%s", dependencies=%s """ % \
            (fh.name, bool2R(dependencies))
        code += """ , repos=NULL) """
        session.run(code)

    except Exception as err:
        if verbose:
            print(err)
            print("trying local or from repos")
            print("RTOOLS warning: URL provided does not seem to exist %s. Trying from CRAN" % query)
        code = """install.packages("%s", dependencies=%s """ % \
            (query, bool2R(dependencies))

        code += """ , repos=%s) """ % repos
        session.run(code)
        return
Ejemplo n.º 6
0
    def get_sim_data(self, bs=None):
        """

        input could be a bitstring with correct length and same order
        OR a model

        """
        if bs is None:
            bs = self.results.results.parValues
        else:
            # TODO check assert length bs is correct
            pass

        script_template = """
        library(%(library)s)
        pknmodel = readSIF("%(pknmodel)s")


        cnolist = CNOlist("%(midas)s")
        model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
                   expansion=%(expansion)s, cutNONC=%(cutnonc)s,
                   maxInputsPerGate=%(maxInputsPerGate)s)
        sim_data = plotLBodeFitness(cnolist,model, ode_parameters=ode_params)
        """

        params = {
                'library': self._library,
                'pknmodel': self.pknmodel.filename,
                'midas': self.data.filename,

            'compression': bool2R(self._compression),
            'expansion': bool2R(self._expansion),
            'cutnonc': bool2R(self._cutnonc),
             'maxInputsPerGate': self._max_inputs_per_gate,
                #'tag':tag
                }
        script = script_template % params
        self.session.run(script)
        # FIXME what about species/experiments

        sim_data = self.session.sim_data
        self.sim = pd.concat([pd.DataFrame(x, columns=self.species) 
            for x in sim_data])
Ejemplo n.º 7
0
    def simulate2(self, bs1=None, bs2=None, compression=True, expansion=True):
        """Return the score of the objective function using 2 time points

        :param list bs1: a bitstring. Must be a list of zeros and ones
            with order identical to :meth:`reactions_r`, which 
            is populated once :meth:`optimise` is called.
        :param list bs2: the second bitstring. Must be a list of zeros and ones
            with length equal to the number of zeros in the first btstring.

        """
        if bs1 is None:
            bs1 = ",".join([str(x) for x in self.results.results.best_bitstring])
        else:
            bs1 = ",".join([str(x) for x in bs1])

        if bs2 is None:
            bs2 = ",".join([str(x) for x in self.results2.results.best_bitstring])
        else:
            bs2 = ",".join([str(x) for x in bs2])


        script_template = """
        library(CellNOptR)
        pknmodel = readSIF("%(pkn)s")
        cnolist = CNOlist("%(midas)s")
        model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
            expansion=%(expansion)s, maxInputsPerGate=3)
        mse = computeScoreTN(cnolist, model, bStrings=list(%(bs1)s, %(bs2)s))
        """

        script = script_template % {
                'pkn': self.pknmodel.filename,
                'midas': self.data.filename,
                'compression': bool2R(compression),
                'expansion': bool2R(expansion),
                'bs1': "c(" + bs1 + ")",
                'bs2': "c(" + bs2 + ")"
                }

        self.session.run(script)
        return self.session.mse
Ejemplo n.º 8
0
 def _init(self):
     script_template = """
     library(%(library)s)
     pknmodel = readSIF("%(pknmodel)s")
     cnolist = CNOlist("%(midas)s")
     model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
                expansion=%(expansion)s, cutNONC=%(cutnonc)s,
                maxInputsPerGate=%(maxInputsPerGate)s)
     reactions = model$reacID
     species = colnames(cnolist@signals[[1]])"""
     params = {
             'library': self._library,
             'pknmodel': self.pknmodel.filename,
             'midas': self.data.filename,
         'compression': bool2R(self._compression),
         'expansion': bool2R(self._expansion),
         'cutnonc': bool2R(self._cutnonc),
          'maxInputsPerGate': self._max_inputs_per_gate,
             }
     self.session.run(script_template % params)
     self.species = self.session.species
Ejemplo n.º 9
0
    def run(self, model, data, k=2, compression=True,
            expansion=True, maxInputsPerGate=3, err1=0.1, err2=0,
            verbose=True):
        """

        :param model: filename to a SIF file
        :param data: filename to a MIDAS file
        :param int k: 2
        :param bool compression:
        :param bool expansion:
        :param int maxInputsPerGate:
        :param float err1:
        :param float err2:
        :param bool verbose:


        :return: nothing but populates the :attr:`newlinks` and :attr:`alllinks`
            attributes

        """

        # TODO could be a model/data instance (e.g. XMIDAS)
        self.model = model
        self.data = data
        cmd = self.Rscript % {'pkn': model,
                              'data': data,
                              'k': k,
                              'compression': rtools.bool2R(compression),
                              'expansion': rtools.bool2R(expansion),
                              'err1': err1,
                              'err2': err2,
                              'maxInputsPerGate': maxInputsPerGate}

        if verbose:
            print(cmd)
        self.rsession.run(cmd)
        self.alllinks = self.rsession.get('alllinks')
        self.newlinks = self.rsession.get('newlinks')
        self.newlinks = [x for x in self.newlinks if x is not None]
Ejemplo n.º 10
0
    def simulate(self, bs=None, compression=True, expansion=True):
        """

        input could be a bitstring with correct length and same order
        OR a model


        c.results.cnorbool.best_bitstring
        c.results.cnorbool.reactions
        array([1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0])
        c.results.cnorbool.reactions


        """
        if bs == None:
            bs = ",".join([str(x) for x in self.results.cnorbool.best_bitstring])
        else:
            bs = ",".join([str(x) for x in bs])


        script_template = """
        library(CellNOptR)
        pknmodel = readSIF("%(pkn)s")
        cnolist = CNOlist("%(midas)s")
        model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
            expansion=%(expansion)s, maxInputsPerGate=3)
        mse = computeScoreT1(cnolist, model, %(bs)s)
        """

        script = script_template % {
                'pkn': self.pknmodel.filename,
                'midas': self.data.filename,
                'compression': bool2R(compression),
                'expansion': bool2R(expansion),
                'bs': "c(" + bs + ")"
                }

        self.session.run(script)
        return self.session.mse
Ejemplo n.º 11
0
    def simulate(self, params, verboseR=False):
        # The first call is slow but then, it is faster but still
        # 10 times slower than the pure R version
        save_verboseR = self.verboseR
        self.verboseR = verboseR
        if self.session.get("simulator_initialised") is None:
            script = """
                library(%(library)s)
                pknmodel = readSIF("%(pknmodel)s")
                cnolist = CNOlist("%(midas)s")
                model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
                   expansion=%(expansion)s, cutNONC=%(cutnonc)s,
                   maxInputsPerGate=%(maxInputsPerGate)s)
                indices = indexFinder(cnolist, model,verbose=FALSE)
                ode_params = createLBodeContPars(model)
                objective_function = getLBodeContObjFunction(cnolist, model,
                    ode_params, indices)
                simulator_initialised = T
            """
            pars = {
                'library': self._library,
                'pknmodel': self.pknmodel.filename,
                'midas': self.data.filename,
            'compression': bool2R(self._compression),
            'expansion': bool2R(self._expansion),
            'cutnonc': bool2R(self._cutnonc),
             'maxInputsPerGate': self._max_inputs_per_gate,
            }
            self.session.run(script % pars)

        self.session['params'] = params
        script = """
            score = objective_function(params)
        """
        self.session.run(script)
        self.verboseR = save_verboseR
        return self.session.score
Ejemplo n.º 12
0
    def optimise(self, NAFac=1, pmutation=0.5, selpress=1.2, popsize=50,
                 reltol=0.1, elitism=5, maxtime=60, sizefactor=0.0001,
                 time_index_1=1, maxgens=500, maxstallgens=100, bool_updates=10,
                 upper_bound=10, lower_bound=0.8, ga_verbose=True):
        """

        :param lowerB:
        :param upperB:
        :param boolUpdates:

        """

        if int(bool_updates) != len(self.midas.times):
            msg = "boolupdate must be set to number of time points. "
            msg += "Other cases not implemented so far"
            msg += "number time points s %s" % len(self.midas.times)
            sys.stdout.write('%s\n' % self.midas.filename)
            raise ValueError(msg)

        # TODO reuse the previous params
        self.logging.info("Running the optimisation. Can take a very long"
                          "time. To see the progression, set verboseR "
                          "attribute to True")
        # update config GA section with user parameters
        self._update_config('DiscreteTime', self.optimise.actual_kwargs)
        self._update_config('GA', self.optimise.actual_kwargs)

        # TODO: add bitstring as input ?

        script = """
        library(CNORdt)
        pknmodel = readSIF("%(pkn)s")
        cnolist = CNOlist("%(midas)s")
        model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
            expansion=%(expansion)s, maxInputsPerGate=%(maxInputsPerGate)s)

        optbs = NULL

        res = gaBinaryDT(CNOlist=cnolist, model=model,
          initBstring=optbs, boolUpdates=%(bool_updates)s,
            popSize=%(popsize)s, maxGens=%(maxgens)s,
            maxTime=%(maxtime)s, elitism=%(elitism)s, pMutation=%(pmutation)s,
            NAFac=%(NAFac)s,  selPress=%(selpress)s, relTol=%(reltol)s, sizeFac=%(sizefactor)s,
            stallGenMax=%(maxstallgens)s, lowerB=%(lower_bound)s,
                  upperB=%(upper_bound)s, verbose=%(ga_verbose)s)

        sim_results = cutAndPlotResultsDT(model=model,
            CNOlist=cnolist, bString=res$bString, boolUpdates=%(bool_updates)s,
            lowerB=%(lower_bound)s, upper=%(upper_bound)s)

        # TODO put this code inside CellNOptR
        signals = colnames(cnolist@signals$`0`)
        # TODO in Dt, MSE are not provided
        # colnames(sim_results$mse) = signals
        #for (i in seq_along(sim_results$simResults[[1]][1,1,])){
        #    colnames(sim_results$simResults[[1]][,,i]) = signals
        #}

        best_bitstring = res$bString
        best_score = res$bScore
        all_scores = res$stringsTolScores
        all_bitstrings = res$stringsTol
        reactions = model$reacID
        results = as.data.frame(res$results)
        stimuli = as.data.frame(cnolist@stimuli)
        inhibitors = as.data.frame(cnolist@inhibitors)
        species = colnames(cnolist@signals[[1]])
        """
        expansion = True
        compression = True

        self._results = {}

        params = {
            'pkn': self.pknmodel.filename,
            'midas': self.data.filename,
             'compression': bool2R(self._compression),
             'expansion': bool2R(self._expansion),
             'cutnonc': bool2R(self._cutnonc),
             'maxInputsPerGate': self._max_inputs_per_gate,

            }

        gad = self.config.GA.as_dict()
        params.update(gad)

        dt_params = self.config.DiscreteTime.as_dict()
        params.update(dt_params)

        params['ga_verbose'] = bool2R(params['ga_verbose'])

        self.session.run(script % params)

        results = self.session.results
        columns_int = ['Generation', 'Stall_Generation']
        columns_float = ['Best_score', 'Avg_Score_Gen', 'Best_score_Gen', 'Iter_time']
        results[columns_int] = results[columns_int].astype(int)
        results[columns_float] = results[columns_float].astype(float)

        reactions = self.session.reactions
        from cno.core.models import DTModels
        try:
            df = pd.DataFrame(self.session.all_bitstrings,
                              columns=reactions)
            self.df = df
        except:
            try:
                df = pd.DataFrame(self.session.all_bitstrings)
                df = df.transpose()
                df.columns = list(reactions)
                self.df = df
            except:
                self.df = pd.DataFrame()

        #df = pd.DataFrame(self.session.all_bitstrings,
        #                      columns=list(self.session.reactions))
        models = DTModels(df)
        models.scores = self.session.all_scores
        models.cnograph.midas = self.data.copy()

        self.best_bitstring = self.session.best_bitstring
        self.species = self.session.species

        results = {
                'best_score': self.session.best_score,
                'best_bitstring': self.session.best_bitstring,
                'all_scores': self.session.all_scores,
                'all_bitstrings': self.session.all_bitstrings,
                'reactions': self.session.reactions,
                'sim_results': self.session.sim_results,  # contains mse and sim at t0,t1,
                'results': results,
                'models': models,
                'stimuli': self.session.stimuli.copy(),
                'inhibitors': self.session.inhibitors.copy(),
                'species': self.session.species,
                #'tag': tag
        }
        results['pkn'] = self.pknmodel
        results['midas'] = self.data

        self.results.results = results
        self.results.models = models
Ejemplo n.º 13
0
    def optimise(self, N=2,
        NAFac=1, pmutation=0.5, selpress=1.2, popsize=50,
        reltol=0.1, elitism=5, maxtime=60, sizefactor=0.0001,
        time_index_1=1, maxgens=500, maxstallgens=100, ga_verbose=True, **kargs):

        self.logging.info("Running the optimisation. Can take a very long"
                          "time. To see the progression, set verboseR "
                          "attribute to True")
        # update config GA section with user parameters
        self._update_config('GA', self.optimise.actual_kwargs)
        self._update_config('Fuzzy', self.optimise.actual_kwargs)

         # keep track of the GA parameters, which may have been update above
        gad = dict([(k, self.config.GA[k].value)
            for k in self.config.GA._get_names()])

        fuzzyd = dict([(k, self.config.Fuzzy[k].value)
            for k in self.config.Fuzzy._get_names()])

        print(N)
        print(fuzzyd)
        script = """
        library(CNORfuzzy)
        cnolist = CNOlist("%(midas)s")
        pknmodel = readSIF("%(pkn)s")

        # pknmodel is processed internally. Need to change the R API
        #model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
        #    expansion=%(expansion)s, maxInputsPerGate=3)

        # which one to use ?? pknmodel or model
        # looks like CNORwrap overwrite paramsList$model
        # with the processed one
        # $model not used in the function so one can provide anything
        paramsList = defaultParametersFuzzy(cnolist, pknmodel)
        paramsList$popSize = %(popsize)s
        paramsList$maxTime = %(maxtime)s
        paramsList$maxGens = %(maxgens)s
        paramsList$elitism = %(elitism)s
        paramsList$stallGenMax = %(maxstallgens)s
        paramsList$optimisation$maxtime = %(optimisation_max_time)s

        N = %(N)s
        allRes = list()
        paramsList$verbose=TRUE
        for (i in 1:N){
            Res = CNORwrapFuzzy(cnolist, pknmodel, paramsList=paramsList)
            allRes[[i]] = Res
        }
        summary = compileMultiRes(allRes,show=FALSE)
        summary = compileMultiRes(allRes,show=T)
        # signals order is not sorted in CellNOptR
        signals = colnames(cnolist@signals[[1]])
        #sim = plotMeanFuzzyFit(0.01, summary$allFinalMSEs, allRes)

        res1 = allRes[[1]]
        best_score = res1['currBestDiscrete']
        best_bitstring = res1['intString']
        """

        expansion = True
        compression = True

        params = {
            'pkn': self.pknmodel.filename,
            'midas': self.data.filename,
            'compression': bool2R(compression),
            'expansion': bool2R(expansion)
            }
        params.update(gad)
        params.update(fuzzyd)

        print(params)
        self.session.run(script % params)
        allRes = self.session.allRes

        # The contents of allRes is a list of N Res structures
        # Each Res structure is itself made of 9 structures with those keys:
        # 't1opt': ['stringsTol', 'bScore', 'results', 'currBest', 'bString', 'stringsTolScores']
        # 'paramsList': parameters used in particular GA params, optim params, inputs
        # 'unRef':
        # 'processedModel':
        # 'currBestDiscrete': best score
        # 'bit'
        # 'intString'
        # 'redRef'
        # 'cutBit'

        res1 = allRes[0] # same for all indices
        #reactions = res1['paramsList']['model']['reacID']
        species = res1['paramsList']['model']['namesSpecies']

        reactions = res1['processedModel']['reacID']

        # TODO: find best MSE/bitstring amongst the N runs
        # redRef contains a MSE for each threshold
        res1['redRef'][8]['MSE']
        # !! several runs; should be gathered together

        strings = self.session.allRes[0]['t1opt']['stringsTol']
        scores = self.session.allRes[0]['t1opt']['stringsTolScores']

        # ! reactions here is different. it should include the
        # AND edges as well
        if strings.ndim == 1:
            # BUGGY CNORfuzzy looks like bitstrings do not have correct length
            # if not enough strings are found.
            bstring = self.session.allRes[0]['t1opt']['bString']
            reactions = self.session.allRes[0]['processedModel']['reacID']
            N = len(bstring)
            M = len(reactions)
            fuzreactions = ['a=' + str(i) for i in range(0, N)]
            for i, reac in enumerate(reactions):
                fuzreactions[i] = reac
            df = pd.DataFrame([[0]*N], columns=fuzreactions)
        else:
            # FIXME what is it ? why adding a= ? reactions
            fuzreactions = ['a=' + str(i) for i in range(0, len(strings[0]))]
            for i, reac in enumerate(reactions):
                fuzreactions[i] = reac
            df = pd.DataFrame(strings, columns=fuzreactions)

        models = FuzzyModels(df)
        models.scores = scores
        models.cnograph.midas = self.data.copy()

        self.species = species
        self.reactions = reactions
        self.signals = self.session.signals

        # transforms the results into dataframes
        for i, res in enumerate(allRes):
            df = pd.DataFrame(res['t1opt']['results'],
                columns=("Generation","Best_score","Best_bitString","Stall_Generation",
                "Avg_Score_Gen","Best_score_Gen","Best_bit_Gen","Iter_time"))
            allRes[i]['t1opt']['results'] = df

        results = {
            'best_score': self.session.best_score,
            'best_bitstring': self.session.best_bitstring,
            'species': species,
        }

        self.results = FuzzyResults()
        self.results.results = results
        self.results.models = models
        self.results.allRes = allRes
Ejemplo n.º 14
0
    def optimise(self,  NAFac=1, pmutation=0.5, selpress=1.2, popsize=50,
                 reltol=0.1, elitism=5, maxtime=60, sizefactor=0.0001,
                 time_index_1=1, maxgens=500, maxstallgens=100, ga_verbose=True):
        """Perform the optimisation and save results


        * Results are stored in :attr:`results`
        * Models with the tolerance are stored in :attr:`results.models`

        Parameters are those of a Genetic Algorithm used to perform
        the analysis.

        If you run again, it uses the previous best bitstirng.
        Set self.session.best_bitstring = None to start from the
        full network.
        """
        self.logging.info("Running the optimisation. Can take a very long"
                          "time. To see the progression, set verboseR "
                          "attribute to True")
        # update config GA section with user parameters
        self._update_config('GA', self.optimise.actual_kwargs)

        # keep track of the GA parameters, which may have been update above
        gad = self.config.GA.as_dict()

        bs = self.session.get('best_bitstring')
        if bs is not None:
            bs = "c(" + ",".join([str(x) for x in list(bs)]) + ")"
        else:
            bs = 'NULL'

        # do we want to pre process the data ?

        script_template = """
        library(CellNOptR)
        pknmodel = readSIF("%(pkn)s")
        cnolist = CNOlist("%(midas)s")
        model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
                expansion=%(expansion)s, cutNONC=%(cutnonc)s,
                maxInputsPerGate=%(maxInputsPerGate)s)

        res = gaBinaryT1(cnolist, model, popSize=%(popsize)s, maxGens=%(maxgens)s,
            maxTime=%(maxtime)s, elitism=%(elitism)s, pMutation=%(pmutation)s,
            NAFac=%(NAFac)s,  selPress=%(selpress)s, relTol=%(reltol)s, sizeFac=%(sizefactor)s,
            stallGenMax=%(maxstallgens)s, initBstring=%(bs)s)

        sim_results = cutAndPlot(cnolist, model, list(res$bString),
                                 plotParams=list(maxrow = 80, cex=0.5),
                                 plotPDF=F)
        sim_results2 = NULL

        # output are not the same... as in T1
        signals = colnames(cnolist@signals$`0`)
        colnames(sim_results$mse) = signals
        for (i in seq_along(sim_results$simResults[[1]])){
            colnames(sim_results$simResults[[1]][[i]]) = signals
        }

        # to be retrieved inside Python code
        best_bitstring = res$bString
        best_score = res$bScore
        all_scores = res$stringsTolScores
        all_bitstrings = res$stringsTol
        reactions = model$reacID
        results = as.data.frame(res$results)
        stimuli = as.data.frame(cnolist@stimuli)
        inhibitors = as.data.frame(cnolist@inhibitors)
        species = colnames(cnolist@signals[[1]])
        optim1 = T
        """

        params = {
            'pkn': self.pknmodel.filename,
            'midas': self.data.filename,
            'compression': bool2R(self._compression),
            'expansion': bool2R(self._expansion),
            'cutnonc': bool2R(self._cutnonc),
            'maxInputsPerGate': self._max_inputs_per_gate,
            'bs':bs
            }
        params.update(gad)

        script = script_template % params
        self.session.run(script)
        self.reactions_r = self.session.reactions
        # need to change type of some columns, which are all string
        results = self.session.results
        results.columns = [x.strip() for x in results.columns]

        columns_int = ['Generation', 'Stall_Generation']
        columns_float = ['Best_score', 'Avg_Score_Gen', 'Best_score_Gen', 'Iter_time']
        results[columns_int] = results[columns_int].astype(int)
        results[columns_float] = results[columns_float].astype(float)

        # cnograph created automatically from the reactions
        try:
            N = len(self.session.best_bitstring)
            all_bs = self.session.all_bitstrings
            df = pd.DataFrame(all_bs, columns=self.reactions_r)
            models = BooleanModels(df)
            # flatten to handle exhaustive
            import numpy as np
            models.scores = np.array(list(pylab.flatten(self.session.all_scores)))
            try:
                models.cnograph.midas = self.data.copy()
            except Exception as err:
                # does not work with ExtLiverPCB
                # CNOError: 'The cues IFNg was found in the MIDAS file but is not present in the model. Change your model or MIDAS file.'
                print("something wrong in the copying of the midas into cnograph(models)")
                print(err.message)
        except:
            N = len(self.session.best_bitstring)
            all_bs = self.session.all_bitstrings
            if N == len(self.reactions_r) and len(self.session.all_bitstrings)>0:
                df = pd.DataFrame([self.session.all_bitstrings],
                              columns=self.reactions_r)
                models = BooleanModels(df)
                models.scores = easydev.to_list(self.session.all_scores)
                self._models = models
            else:
                df = pd.DataFrame(columns=self.reactions_r)
                models = BooleanModels(df)
                models.scores = easydev.to_list(self.session.all_scores)
                self._models = models

            models.cnograph.midas = self.data.copy()

        results = {
                'best_score': self.session.best_score,
                'best_bitstring': self.session.best_bitstring,
                'all_scores': self.session.all_scores,
                'all_bitstrings': self.session.all_bitstrings,
                'reactions': self._reac_cnor2cno(self.reactions_r),
                'sim_results': self.session.sim_results,  # contains mse and sim at t0,t1,
                'results': results,
                'models': models,
                'stimuli': self.session.stimuli.copy(),
                'inhibitors': self.session.inhibitors.copy(),
                'species': self.session.species,
        }

        results['pkn'] = self.pknmodel
        results['midas'] = self.data

        self.results.results = results
        self.results.models = models

        self._called.append('optimise')
Ejemplo n.º 15
0
    def optimise(self,  n_diverse=10, dim_ref_set=10, maxtime=60,
                 verbose=False, reltol=1e-4, atol=1e-3, maxeval='Inf',
                 transfer_function=3, maxstepsize='Inf', reuse_ode_params=False,
                 local_solver=None):
        """Optimise the ODE parameters using SSM algorithm 

        :param int maxtime: (default 10)
        :param int ndiverse: (default 10)
        :param int dim_refset: (default 10)
        :param bool: ode_params if True, load the ode_params.RDAta file saved in a previous run
            mus be compatible with the model.
        
        verbose should be False all the time internally to the R code. Here, verbose
        meana we want to see the status of the optimisation (not all warnings and R
        errors).
        """
        self.logging.info("Running the optimisation. Can take a very long"
                          "time. To see the progression, set verboseR "
                          "attribute to True")
        # update config GA section with user parameters
        self._update_config('SSM', self.optimise.actual_kwargs)
        ssmd = self.config.SSM.as_dict()

        if self.session.get('ode_params') is None:
            self.session.run('ode_params=NULL')
        if reuse_ode_params is False:
            self.session.run('ode_params=NULL')

        # todo: ode_params to be provided as input
        script = """
        library(%(library)s)
        pknmodel = readSIF("%(pknmodel)s")
        cnolist = CNOlist("%(midas)s")


        model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
                   expansion=%(expansion)s, cutNONC=%(cutnonc)s,
                   maxInputsPerGate=%(maxInputsPerGate)s)


        reactions = model$reacID
        species = colnames(cnolist@signals[[1]])


        if (is.null(ode_params) == TRUE){
         ode_params = createLBodeContPars(model)
        }
        ode_params = parEstimationLBodeSSm(cnolist, model, 
            maxtime=%(maxtime)s, maxStepSize=%(maxstepsize)s, dim_refset=%(dim_ref_set)s, maxeval=%(maxeval)s,
            verbose=F, ndiverse=%(n_diverse)s, ode_parameters=ode_params,
            local_solver=%(local_solver)s)
        """

        if local_solver is None:
            local_solver = 'NULL'
        params = {
                'library': self._library,
            'pknmodel': self.pknmodel.filename,
            'midas': self.data.filename,
            'compression': bool2R(self._compression),
            'expansion': bool2R(self._expansion),
            'cutnonc': bool2R(self._cutnonc),
             'maxInputsPerGate': self._max_inputs_per_gate,
             'local_solver': local_solver
            }

        params.update(ssmd)

        self.session.run(script % params)

        ssm_results = self.session.ode_params['ssm_results'].copy()
        self.ssm_results = ssm_results
        results = {
                'best_score': ssm_results['fbest'],
                'all_scores': ssm_results['f'],
                'reactions': self.session.reactions[:],
                'best_params': ssm_results['xbest']
        }
        for k,v in self.session.ode_params.items():
            results[k] = v.copy()

        self.results = ODEResults()
        self.results.results = results
        self.species = self.session.species
Ejemplo n.º 16
0
    def optimise(self, tag="cnorbool", reltol=0.1,
            expansion=True, maxgens=150, stallgenmax=100, compression=True):

        script_template = """
        library(CellNOptR)
        pknmodel = readSIF("%(pkn)s")
        cnolist = CNOlist("%(midas)s")
        model = preprocessing(cnolist, pknmodel, compression=%(compression)s,
            expansion=%(expansion)s, maxInputsPerGate=3)

        res = gaBinaryT1(cnolist, model, relTol=%(reltol)s,
            stallGenMax=%(stallgenmax)s, maxGens=%(maxgens)s)
        sim_results = cutAndPlot(cnolist, model, list(res$bString),
                                 plotParams=list(maxrow = 80, cex=0.5),
                                 plotPDF=T, tag="%(tag)s")

        signals = colnames(cnolist@signals$`0`)
        colnames(sim_results$mse) = signals
        for (i in seq_along(sim_results$simResults[[1]])){
            colnames(sim_results$simResults[[1]][[i]]) = signals
        }

        #plotModel(model, cnolist, bString=res$bString,
        #          filename="Model-CNO-%(tag)s", output="PDF")

        # to be retrieved inside Python code
        best_bitstring = res$bString
        best_score = res$bScore
        all_scores = res$stringsTolScores
        all_bitstrings = res$stringsTol
        reactions = model$reacID
        results = as.data.frame(res$results)
        stimuli = as.data.frame(cnolist@stimuli)
        inhibitors = as.data.frame(cnolist@inhibitors)
        species = colnames(cnolist@signals[[1]])
        """
        self.results = {}
        self.results['cnorbool'] = {}

        script = script_template % {
                'pkn': self.pknmodel.filename,
                'midas': self.data.filename,
                'tag':tag,
                'maxgens':maxgens,
                'stallgenmax':stallgenmax,
                'reltol':reltol,
                'compression': bool2R(compression),
                'expansion': bool2R(expansion)
                }
        self.session.run(script)

        # need to change type of some columns, which are all string
        results = self.session.results

        columns_int = ['Generation', 'Stall_Generation']
        columns_float = ['Best_score', 'Avg_Score_Gen', 'Best_score_Gen', 'Iter_time']
        results[columns_int] = results[columns_int].astype(int)
        results[columns_float] = results[columns_float].astype(float)

        from cno.misc.models import Models
        df = pd.DataFrame(self.session.all_bitstrings,
                              columns=self.session.reactions)

        # cnograph created automatically from the reactions
        models = Models(df)
        models.cnograph.midas = self.data.copy()
        models.scores = self.session.all_scores

        from cno.misc.results import BooleanResults
        results = BooleanResults()


        self.results['cnorbool'] = {
                'best_score': self.session.best_score,
                'best_bitstring': self.session.best_bitstring,
                'all_scores': self.session.all_scores,
                'all_bitstrings': self.session.all_bitstrings,
                'reactions': self.session.reactions,
                'sim_results': self.session.sim_results,  # contains mse and sim at t0,t1,
                'reactions': self.session.reactions,
                'results': results,
                'models':models,
                'stimuli':self.session.stimuli.copy(),
                'inhibitors':self.session.inhibitors.copy(),
                'species':self.session.species,
                'tag': tag
        }
        self.results['pkn'] = self.pknmodel
        self.results['midas'] = self.data
        self.results = AttrDict(**self.results)
        self.results['cnorbool'] = AttrDict(**self.results['cnorbool'])