Example #1
0
    def read(self, simufname="results.zip", verbose=False):
        """
        Creates a simul_spindle.Metaphase from a results.zip file.

        :param simufname: The .zip file where results from the existing
                          simulation are (zip which contains xml and npy)
        :type simufname: string, optional

        :param verbose: Set Metaphase verbose
        :type verbose: bool

        :return: a Metaphase instance
        """

        # Unzip results.xml and data.npy and put it in
        # tempfile
        try:
            zipf = zipfile.ZipFile(simufname, "r")
        except:
            logger.info("%s does not appear to be a Zipfile" % simufname)
            return False

        # Test if simu results file are in the archive
        if "results.xml" not in zipf.namelist() or "data.npy" not in zipf.namelist():
            logger.info("%s does not contain results.xml and data.npy" % simufname)
            return False

        xmltemp = StringIO.StringIO(zipf.read("results.xml"))
        datatemp = StringIO.StringIO(zipf.read("data.npy"))

        restree = ResultTree(xmltemp, datatemp)

        param_root = restree.root.find('parameters')
        paramtree = ParamTree(root=param_root)
        params = paramtree.relative_dic
        measure_root = restree.root.find('measures')
        measuretree = ParamTree(root=measure_root,
                                adimentionalized=False)

        metaphase = Metaphase(paramtree=paramtree,
                              measuretree=measuretree,
                              verbose=verbose)

        traj_matrix = restree.get_all_trajs()
        correct_matrix = restree.get_all_correct()
        erroneous_matrix = restree.get_all_erroneous()
        state_hist_matrix = restree.get_all_plug_state()
        KD = KinetoDynamics(params)
        KD.spbR.traj = traj_matrix[:, 0]
        KD.spbL.traj = traj_matrix[:, 1]
        col_num = 2
        state_num = 0
        for n, ch in enumerate(KD.chromosomes):
            ch.cen_A.traj = traj_matrix[:, col_num]
            ch.cen_A.pos = ch.cen_A.pos
            col_num += 1
            ch.cen_B.traj = traj_matrix[:, col_num]
            ch.cen_B.pos = ch.cen_B.pos
            col_num += 1
            ch.erroneous_history = (erroneous_matrix[:, n * 2: n * 2 + 2])
            ch.correct_history = (correct_matrix[:, n * 2: n * 2 + 2])
            for plugsite in ch.cen_A.plugsites:
                plugsite.traj = traj_matrix[:, col_num]
                col_num += 1
                plugsite.state_hist = state_hist_matrix[:, state_num]
                plugsite.plug_state = plugsite.state_hist[-1]
                state_num += 1
            for plugsite in ch.cen_B.plugsites:
                plugsite.traj = traj_matrix[:, col_num]
                col_num += 1
                plugsite.state_hist = state_hist_matrix[:, state_num]
                plugsite.plug_state = plugsite.state_hist[-1]
                state_num += 1

        metaphase.KD = KD

        metaphase.KD.simulation_done = True

        return metaphase
Example #2
0
    def read(self, simufname, paramtree=None, measuretree=None, verbose=False):
        """
        Creates a simul_spindle.Metaphase from a results.zip file.

        Parameters
        ----------
        simufname : string, optional
            The .zip file where results from the existing simulation are
            (zip which contains xml and npy)

        verbose : bool
            Set Metaphase verbose

        Returns
        -------
        :class:`~kt_simul.core.simul_spindle.Metaphase`

        """

        store = pd.HDFStore(simufname)

        if not paramtree:
            param_root = build_tree(store['params'])
            paramtree = ParamTree(root=param_root)
        params = paramtree.relative_dic

        if not measuretree:
            measure_root = build_tree(store['measures'])
            measuretree = ParamTree(root=measure_root,
                                    adimentionalized=False)

        meta = Metaphase(paramtree=paramtree, measuretree=measuretree, verbose=False)
        KD = KinetoDynamics(params)

        spbs = store['spbs']

        KD.spbL.traj = spbs.xs('A', level='side').values.T[0]
        KD.spbR.traj = spbs.xs('B', level='side').values.T[0]

        kts = store['kts']
        plug_sites = store['plug_sites']
        store.close()

        for ch, (ch_id, ch_df) in zip(KD.chromosomes, kts.groupby(level="id")):
            ch.cen_A.traj = ch_df.xs('A', level='side')['x'].values.T
            ch.cen_B.traj = ch_df.xs('B', level='side')['x'].values.T
            ch.cen_A.tag = 'A'
            ch.cen_B.tag = 'B'

            for i, plugsite in enumerate(ch.cen_A.plugsites):
                ps = plug_sites.xs((ch_id, 'A', i), level=['id', 'side', 'plug_id'])
                plugsite.traj = ps['x'].values
                plugsite.state_hist = ps['state_hist'].values
                plugsite.plug_state = plugsite.state_hist[-1]
                plugsite.tag = 'A'

            for i, plugsite in enumerate(ch.cen_B.plugsites):
                ps = plug_sites.xs((ch_id, 'B', i), level=['id', 'side', 'plug_id'])
                plugsite.traj = ps['x'].values
                plugsite.state_hist = ps['state_hist'].values
                plugsite.plug_state = plugsite.state_hist[-1]
                plugsite.tag = 'B'

            ch.cen_A.calc_plug_vector()
            ch.cen_B.calc_plug_vector()

            ch.calc_erroneous_history()
            ch.calc_correct_history()

        meta.KD = KD
        meta.KD.simulation_done = True

        return meta