Esempio n. 1
0
def executeFixPointSimulation(directory_for_network, inputsArray, masks,initializationDic=None, outputList=None,
                              sparse=False, modes=["verbose","time","outputEqui"],
                              initValue=10**(-13), rescaleFactor=None):
    """
        Execute the simulation of the system saved under the directory_for_network directory.
        InputsArray contain the values for the input species.
    :param directory_for_network: directory path, where the files equations.txt and constants.txt may be found.
    :param inputsArray: The test concentrations, a t * n array where t is the number of test and n the number of node in the first layer.
    :param initializationDic: can contain initialization values for some species. If none, or the species don't appear in its key, then its value is set at initValue (default to 10**(-13)).
    :param masks: network masks
    :param outputList: list or string, species we would like to see as outputs, if default (None), then will find the species of the last layer.
                                      if string and value is "nameDic" or "all", we will give all species taking part in the reaction (usefull for debug)
    :param sparse: if sparse, usefull for large system
    :param modes: modes for outputs, don't accept outputPlot as it only provides value at equilibrium now.
    :param initValue: initial concentration value to give to all species
    :param rescaleFactor: if None, then computed as the number of nodes, else: used to divide the value of the inputs
    :param masks:
    :return:
            A result tuple depending on the modes.
    """

    assert "outputPlot" not in modes

    parsedEquation,constants,nameDic=read_file(directory_for_network + "/equations.txt", directory_for_network + "/constants.txt")
    if sparse:
        KarrayA,stochio,maskA,maskComplementary = sparseParser(parsedEquation,constants)
    else:
        KarrayA,stochio,maskA,maskComplementary = parse(parsedEquation,constants)
    KarrayA,T0,C0,constants=setToUnits(constants,KarrayA,stochio)
    print("Initialisation constant: time:"+str(T0)+" concentration:"+str(C0))

    speciesArray = obtainSpeciesArray(inputsArray,nameDic,initValue,initializationDic,C0)
    speciesArray,rescaleFactor = rescaleInputConcentration(speciesArray,nameDic=nameDic,rescaleFactor=rescaleFactor)

    ##SAVE EXPERIMENT PARAMETERS:
    attributesDic = {}
    attributesDic["rescaleFactor"] = rescaleFactor
    attributesDic["T0"] = T0
    attributesDic["C0"] = C0
    for k in initializationDic.keys():
        attributesDic[k] = speciesArray[0,nameDic[k]]
    for idx,cste in enumerate(constants):
        attributesDic["k"+str(idx)] = cste
    attributesDic["Numbers_of_Constants"] = len(constants)
    experiment_path=saveAttribute(directory_for_network, attributesDic)

    shapeP=speciesArray.shape[0]

    #let us assign the right number of task in each process
    num_workers = multiprocessing.cpu_count()-1
    idxList = findRightNumberProcessus(shapeP,num_workers)

    #let us find the species of the last layer in case:
    if outputList is None:
        outputList = obtainOutputArray(nameDic)
    elif type(outputList)==str:
        if outputList=="nameDic" or outputList=="all":
            outputList=list(nameDic.keys())
        else:
            raise Exception("asked outputList is not taken into account.")

    nbrConstant = int(readAttribute(experiment_path,["Numbers_of_Constants"])["Numbers_of_Constants"])
    if nbrConstant == 12: #only one neuron, it is easy to extract cste values
        k1,k1n,k2,k3,k3n,k4,_,k5,k5n,k6,kd,_=[readAttribute(experiment_path,["k"+str(i)])["k"+str(i)] for i in range(0,nbrConstant)]
    else:
        k1,k1n,k2,k3,k3n,k4,_,k5,k5n,k6,kd,_= [0.9999999999999998,0.1764705882352941,1.0,0.9999999999999998,0.1764705882352941,1.0,
                                               0.018823529411764708,0.9999999999999998,0.1764705882352941,1.0,0.018823529411764708,0.018823529411764708]

    inhibTemplateNames = obtainTemplateArray(masks=masks,activ=False)
    activTemplateNames= obtainTemplateArray(masks=masks,activ=True)
    TA = initializationDic[activTemplateNames[0]]/C0
    TI = initializationDic[inhibTemplateNames[0]]/C0
    E0 = initializationDic["E"]/C0
    kdI = kd
    kdT = kd

    myconstants = [k1,k1n,k2,k3,k3n,k4,k5,k5n,k6,kdI,kdT,TA,TI,E0]

    t=tm()
    print("=======================Starting Fixed Point simulation===================")
    copyArgs = obtainCopyArgsFixedPoint(idxList,modes,speciesArray,nameDic,outputList,masks,myconstants,chemicalModel="templateModel")
    with multiprocessing.get_context("spawn").Pool(processes= len(idxList[:-1])) as pool:
        myoutputs = pool.map(fixPointSolverForMultiProcess, copyArgs)
    pool.close()
    pool.join()
    print("Finished computing, closing pool")
    timeResults={}
    timeResults[directory_for_network + "_wholeRun"]= tm() - t

    if("outputEqui" in modes):
        outputArray=np.zeros((len(outputList), shapeP))
    times = []
    for idx,m in enumerate(myoutputs):
        if("outputEqui" in modes):
            try:
                outputArray[:,idxList[idx]:idxList[idx+1]] = m[modes.index("outputEqui")]
            except:
                raise Exception("error")
        if("time" in modes):
            times += [m[modes.index("time")]]
    if("time" in modes):
        timeResults[directory_for_network + "_singleRunAvg"] = np.sum(times) / len(times)
    # Let us save our result:
    savedFiles = ["false_result.csv","output_equilibrium.csv","output_full.csv"]
    for k in nameDic.keys():
        savedFiles += [k+".csv"]
    for p in savedFiles:
        if(os._exists(os.path.join(experiment_path, p))):
            print("Allready exists: renaming older")
            os.rename(os.path.join(experiment_path,p),os.path.join(experiment_path,p.split(".")[0]+"Old."+p.split(".")[1]))
    if("outputEqui" in modes):
        df=pandas.DataFrame(outputArray)
        df.to_csv(os.path.join(experiment_path, "output_equilibrium.csv"))
    results=[0 for _ in range(len(modes))]
    if("outputEqui" in modes):
        results[modes.index("outputEqui")]= outputArray
    if "time" in modes:
        results[modes.index("time")]=timeResults
    return tuple(results)
Esempio n. 2
0
def simulModelIndicator(name,
                        nameFig,
                        enzymeInit=10**(-6),
                        activInit=10**(-8),
                        inhibInit=10**(-8),
                        x2val=None,
                        x1val=None,
                        indexSplit=10):
    """
        Generate an indicator using the indicator function after simulating the model over a range of inputs.
    :param name: directory where the network is located
    :param nameFig: directory to store figure
    :param enzymeInit: initial concentration of the enzyme
    :param activInit: initial concentration of the activators
    :param inhibInit: initial concentration of the inhibitors
    :param x2val: if not none, define fix initial concentration value for inhibitors
    :param x1val: if not none, define fix initial concentration value for activators
    :param indexSplit: the size of the test will be two time indexSplit.
    :return:
    """
    endTime = 10000
    timeStep = 0.1
    modes = ["verbose", "outputEqui"]

    leak = 10**(-10)

    layerInit = 10**(-13)  #initial concentation value for species in layers
    initValue = 10**(-13)  #initial concentration value for all species.

    endoInit = 10**(-5)  #only used if useEndo == True

    #generate the first layer concentration:

    if x1val is not None:
        assert x2val is None
        X1 = np.array([x1val])
        X2 = np.concatenate(
            (np.logspace(-8, -6, indexSplit), np.logspace(-6, -4, indexSplit)))
    else:
        assert x2val is not None
        X2 = np.array([x2val])
        X1 = np.concatenate(
            (np.logspace(-8, -6, indexSplit), np.logspace(-6, -4, indexSplit)))
    # generate concentration for all different experiments:
    x_test = []
    for x1 in X1:
        for x2 in X2:
            x_test += [[x1, x2]]
    x_test = np.array(x_test)

    initialization_dic = {}
    for layer in range(
            0, len(masks)):  ## the first layer need not to be initiliazed
        for node in range(masks[layer].shape[0]):
            initialization_dic["X_" + str(layer + 1) + "_" +
                               str(node)] = layerInit
    inhibTemplateNames = obtainTemplateArray(masks=masks, activ=False)
    for k in inhibTemplateNames:
        initialization_dic[k] = inhibInit
    activTemplateNames = obtainTemplateArray(masks=masks, activ=True)
    for k in activTemplateNames:
        initialization_dic[k] = activInit
    initialization_dic["E"] = enzymeInit
    if complexity != "simple":
        initialization_dic["E2"] = enzymeInit
    if complexity != None and useEndo:
        initialization_dic["Endo"] = endoInit

    if useDerivativeLeak:
        results = executeODESimulation(f,
                                       name,
                                       x_test,
                                       initialization_dic,
                                       outputList=outputList,
                                       leak=leak,
                                       endTime=endTime,
                                       sparse=False,
                                       modes=modes,
                                       timeStep=timeStep,
                                       initValue=initValue)
    else:
        results = executeODESimulation(f,
                                       name,
                                       x_test,
                                       initialization_dic,
                                       outputList=outputList,
                                       leak=0,
                                       endTime=endTime,
                                       sparse=False,
                                       modes=modes,
                                       timeStep=timeStep,
                                       initValue=initValue)

    if ("outputEqui" in modes):
        experiment_path = name
        C0 = readAttribute(experiment_path, ["C0"])["C0"]
        rescaleFactor = readAttribute(experiment_path,
                                      ["rescaleFactor"])["rescaleFactor"]
        output = results[modes.index("outputEqui")]
        output = np.reshape(output, (len(X1), len(X2)))

        X1 = X1 / (C0 * rescaleFactor)
        X2 = X2 / (C0 * rescaleFactor)

        X1log = np.log(X1)
        X2log = np.log(X2)

        experiment_path = nameFig
        colorDiagram(X1,
                     X2,
                     output,
                     "Initial concentration of X1",
                     "Initial concentration of X2",
                     "Equilibrium concentration of the output",
                     figname=os.path.join(experiment_path,
                                          "neuralDiagramm.png"),
                     equiPotential=False)
        neuronPlot(X1,
                   X2,
                   output,
                   figname=os.path.join(experiment_path, "activationX1.png"),
                   figname2=os.path.join(experiment_path, "activationX2.png"),
                   doShow=False)
        df = pandas.DataFrame(X1)
        df.to_csv(os.path.join(experiment_path, "inputX1.csv"))
        df2 = pandas.DataFrame(X2)
        df2.to_csv(os.path.join(experiment_path, "inputX2.csv"))

        if x1val is None:
            indicatorValue = indicator(X1log, X1, output[:, 0], indexSplit)
        else:
            indicatorValue = indicator(X2log,
                                       X2,
                                       output[0, :],
                                       indexSplit,
                                       sens=-1,
                                       Xactiv=x1val / (C0 * rescaleFactor))
    else:
        raise Exception("outputEqui should be in outputModes")
    return indicatorValue
    computedRescaleFactor = np.sum(
        [np.sum(m > 0) + np.sum(m < 0) for m in masks])
    print("Computed rescale factor is " + str(computedRescaleFactor))

    # Finally we observe that rescaling the number of enzyme up can also help to diminish the competition compare to the inhibition as the enzyme appear with a squared power in the last one
    enzymeInit = enzymeInit * (computedRescaleFactor**0.5)
    inhibInit = inhibInit
    activInit = activInit

    initialization_dic = {}
    for layer in range(
            0, len(masks)):  ## The first layer need not to be initiliazed
        for node in range(masks[layer].shape[0]):
            initialization_dic["X_" + str(layer + 1) + "_" +
                               str(node)] = layerInit
    inhibTemplateNames = obtainTemplateArray(masks=masks, activ=False)
    for k in inhibTemplateNames:
        initialization_dic[k] = inhibInit
    activTemplateNames = obtainTemplateArray(masks=masks, activ=True)
    for k in activTemplateNames:
        initialization_dic[k] = activInit
    initialization_dic["E"] = enzymeInit
    if complexity != "simple":
        initialization_dic["E2"] = enzymeInit
    if complexity != None and useEndo:
        initialization_dic["Endo"] = endoInit

    if useDerivativeLeak:
        results = executeODESimulation(fPythonSparse,
                                       name,
                                       x_test,
Esempio n. 4
0
def launch(inputsArray,
           y,
           resultArray,
           directory_name="weightDic",
           simulateMethod="fixPoint",
           layerInit=10**(-13),
           enzymeInit=10**(-6),
           inhibInit=10**(-4),
           activInit=10**(-4),
           endoInit=None,
           chemicalModel="templateModel"):
    """
        Load and then simulate:
            either with the fixed point strategy at equilibrium
            or throught the solving of ODE
    :param inputsArray:
    :param y :the result
    :param resultArray: the answer for the neural network to these test
    :param directory_name: directory where the weight are stored
    :param simulateMethod: string, either "ODE" or "fixPoint".

    :param layerInit: float, value for initial concentration of intermediate nodes
    :param enzymeInit: float, value for initial concentration of polymerase
    :param inhibInit: float, value for initial concentration of inhibition template
    :param activInit: float, value for initial concentration of activation template
    :param endoInit: float, if given we use the the complicated endo model.
    :return:
    """
    assert inputsArray.shape[0] == y.shape[0]
    if chemicalModel == "templateModel":
        complexity = "simple"
    elif chemicalModel == "normalTemplateModel":
        assert simulateMethod == "ODE"
        complexity = "normal"
    elif chemicalModel == "fullTemplateModel":
        assert simulateMethod == "ODE"
        complexity = "full"
    useEndo = False
    if endoInit is not None:
        assert simulateMethod == "ODE"
        useEndo = True
    useProtectionOnActivator = False
    useEndoOnInputs = False
    useEndoOnOutputs = True

    directory_for_network = os.path.join(directory_name, "Simul")
    _, masks = load(directory_name,
                    directory_for_network,
                    useEndo=useEndo,
                    complexity=complexity,
                    useProtectionOnActivator=useProtectionOnActivator,
                    useEndoOnOutputs=useEndoOnOutputs,
                    useEndoOnInputs=useEndoOnInputs)

    # We realised that the rescale factor should be proportionate to the number of edges in order to keep competition low compare to the inhibition.
    # Moreover we observe that rescaling the template rather than the activation is probably better.
    computedRescaleFactor = np.sum(
        [np.sum(m > 0) + np.sum(m < 0) for m in masks])
    print("Computed rescale factor is " + str(computedRescaleFactor))
    # Finally we observe that rescaling the number of enzyme up can also help to diminish the competition compare to the inhibition as the enzyme appear with a squared power in the last one
    enzymeInit = enzymeInit * (computedRescaleFactor**0.5)

    initialization_dic = {}
    for layer in range(
            0, len(masks)):  ## The first layer need not to be initiliazed
        for node in range(masks[layer].shape[0]):
            initialization_dic["X_" + str(layer + 1) + "_" +
                               str(node)] = layerInit
    inhibTemplateNames = obtainTemplateArray(masks=masks, activ=False)
    for k in inhibTemplateNames:
        initialization_dic[k] = inhibInit
    activTemplateNames = obtainTemplateArray(masks=masks, activ=True)
    for k in activTemplateNames:
        initialization_dic[k] = activInit
    initialization_dic["E"] = enzymeInit
    if complexity != "simple":
        initialization_dic["E2"] = enzymeInit
    if complexity != None and useEndo:
        initialization_dic["Endo"] = endoInit

    if simulateMethod == "ODE":
        modes = ["outputEqui", "verbose"]
        results = simulator.executeODESimulation(
            fPythonSparse,
            directory_for_network,
            inputsArray,
            initializationDic=initialization_dic,
            outputList=None,
            leak=10**(-13),
            endTime=1000,
            sparse=True,
            modes=modes,
            timeStep=0.1)
        outputArray = results[modes.index("outputEqui")]
    elif simulateMethod == "fixPoint":
        modes = ["outputEqui", "verbose", "time"]
        results = simulator.executeFixPointSimulation(
            directory_for_network,
            inputsArray,
            masks,
            initializationDic=initialization_dic,
            outputList=None,
            sparse=True,
            modes=modes,
            initValue=10**(-13),
            rescaleFactor=None)
        outputArray = results[modes.index("outputEqui")]
    ##We now compute the accuracy obtained:
    acc = 0
    distanceToNN = 0
    for test in range(inputsArray.shape[0]):
        answer = np.argmax(outputArray[:, test])
        if (answer == y[test]):
            acc += 1
        if (np.argmax(resultArray[test, :]) == answer):
            distanceToNN += 1
    acc = float(acc / inputsArray.shape[0])
    distanceToNN = float(distanceToNN) / inputsArray.shape[0]
    print("reached acc is: " + str(acc) +
          " ,the distance from the neural network is " + str(distanceToNN) +
          " in percentage of same answer")