Exemplo n.º 1
0
    def test_build_lazy_sim_from_exp(self):
        lazy_sim = build_simulation_from_experiment(self.exp2,
                                                    lazy_loading=True)
        self.assertIsInstance(lazy_sim, LazyLoadingSimulation)
        self.assertValidSimFromExp(lazy_sim, self.exp2)

        sim = build_simulation_from_experiment(self.exp2, lazy_loading=False)
        assert_has_traits_almost_equal(sim, lazy_sim, check_type=False)
    def setUp(self):
        GuiTestAssistant.setUp(self)
        self.study = make_sample_study2()
        exp1 = self.study.search_experiment_by_name('Run_1')
        self.sim1 = build_simulation_from_experiment(exp1)
        self.study.simulations.append(self.sim1)

        exp2 = self.study.search_experiment_by_name('Run_2')
        self.sim2 = build_simulation_from_experiment(exp2)
        self.study.simulations.append(self.sim2)
Exemplo n.º 3
0
def make_sample_study2(source=DEFAULT_INPUT, add_sims=0,
                       add_transp_bind_models=False, with_ph_bind=False):
    """ Load an simulation from a standard source file.
    """
    from kromatography.io.study import load_study_from_excel
    from kromatography.utils.testing_utils import io_data_path
    from kromatography.model.factories.transport_model import \
        create_transport_model
    from kromatography.model.factories.binding_model import \
        create_binding_model
    from kromatography.model.binding_model import PH_STERIC_BINDING_MODEL, \
        STERIC_BINDING_MODEL
    from kromatography.model.factories.simulation import \
        build_simulation_from_experiment

    filepath = io_data_path(source)
    test_study = load_study_from_excel(filepath, allow_gui=False)

    if add_transp_bind_models:
        study_ds = test_study.study_datasource
        num_comp = len(test_study.product.product_components) + 1
        tr_model = create_transport_model(num_comp)
        study_ds.set_object_of_type("transport_models", tr_model)
        if with_ph_bind:
            bind_model = create_binding_model(
                num_comp, model_type=PH_STERIC_BINDING_MODEL
            )
        else:
            bind_model = create_binding_model(
                num_comp, model_type=STERIC_BINDING_MODEL
            )

        study_ds.set_object_of_type("binding_models", bind_model)
    else:
        tr_model = None
        bind_model = None

    if isinstance(add_sims, int) and add_sims > 0:
        for i in range(add_sims):
            exp = test_study.experiments[i]
            sim = build_simulation_from_experiment(
                exp, transport_model=tr_model, binding_model=bind_model
            )
            test_study.simulations.append(sim)

    elif isinstance(add_sims, basestring):
        exp = test_study.search_experiment_by_name(add_sims)
        sim = build_simulation_from_experiment(
            exp, transport_model=tr_model, binding_model=bind_model
        )
        test_study.simulations.append(sim)

    return test_study
Exemplo n.º 4
0
def make_sample_optimizer_builder(study, select_exp_names=None, **builder_kw):
    """ Build an OptimizerBuilder.
    """
    from kromatography.model.factories.simulation import \
        build_simulation_from_experiment

    if not study.simulations:
        sim = build_simulation_from_experiment(study.experiments[1])
        study.simulations.append(sim)

    first_sim_name = study.simulations[0].name
    exp_selector = ExperimentSelector(study=study)
    optim_builder = BruteForceOptimizerBuilder(
        experiment_selector=exp_selector,
        target_study=study, starting_point_simulation_name=first_sim_name,
        **builder_kw
    )

    if select_exp_names is None:
        select_exp_names = []
    elif isinstance(select_exp_names, basestring):
        select_exp_names = [select_exp_names]

    optim_builder.experiment_selector.experiment_selected = select_exp_names
    return optim_builder
Exemplo n.º 5
0
def make_sample_simulation2():
    """ Build a simulation from scratch (no output).
    """
    from kromatography.model.factories.simulation import \
        build_simulation_from_experiment
    exp = make_sample_experiment(name='Run 1')
    sim = build_simulation_from_experiment(exp, fstep='whatever name',
                                           lstep='Gradient Elution')
    return sim
Exemplo n.º 6
0
    def setUp(self):
        super(TestHighLevelFileStorage, self).setUp()
        input_file = io_data_path('ChromExampleDataV2.xlsx')
        self.study = load_study_from_excel(input_file, allow_gui=False)

        # Building and running simulation for only 1 experiment
        expt = self.study.experiments[0]
        sim = build_simulation_from_experiment(expt)
        self.study.simulations.append(sim)
Exemplo n.º 7
0
    def setUp(self):
        super(TestHighLevelFileStorageWithRunSims, self).setUp()
        input_file = io_data_path('test_roudoff_error.chrom')
        self.study = load_study_from_project_file(input_file)

        # Building and running simulation for only 1 experiment
        expt = self.study.experiments[0]
        self.sim = build_simulation_from_experiment(expt)
        self.study.simulations.append(self.sim)
Exemplo n.º 8
0
def make_sample_simulation(name='Run_3', result_file=""):
    """ Load a simulation from the default input file.
    """
    from kromatography.model.factories.simulation import \
        build_simulation_from_experiment
    from kromatography.io.simulation_updater import update_simulation_results

    exp = make_sample_experiment2(name=name)
    simu = build_simulation_from_experiment(exp)

    if result_file:
        update_simulation_results(simu, result_file)

    return simu
    def test_build_chromatogram_model_sim_no_data(self):
        from kromatography.model.factories.simulation import \
            build_simulation_from_experiment

        study = self.study
        # Make a second simulation from Run_1:
        exp = study.search_experiment_by_name("Run_1")
        new_sim = build_simulation_from_experiment(exp, name="Sim2: Run_1")
        study.simulations.append(new_sim)
        model_data = build_chromatogram_model(study)

        # New sim not there because no data:
        self.assertIsNone(new_sim.output)
        self.assertNotIn("Sim2: Run_1", model_data.log_collections.keys())
    def test_strip_fraction_changes_simulation_from_exp(self):
        experim = self.experim
        sim_init = build_simulation_from_experiment(experim)
        experim.strip_mass_fraction = UnitScalar(10, units="%")
        sim_end = build_simulation_from_experiment(experim)
        # Sims are not almost equal
        assert_has_traits_not_almost_equal(sim_init, sim_end)

        # More precisely, the product_component_concentrations of the load must
        # be different:
        init_load = sim_init.method.method_steps[0].solutions[0]
        comp_conc_init = init_load.product_component_concentrations
        # by default, the strip fraction is 0:
        expected = UnitArray([75.33, 696.681, 37.989, 0.], units='m**-3*g')
        assert_unit_array_almost_equal(comp_conc_init, expected)

        end_load = sim_end.method.method_steps[0].solutions[0]
        comp_conc_end = end_load.product_component_concentrations
        assert_unit_array_not_almost_equal(comp_conc_init, comp_conc_end)
        self.assertNotAlmostEqual(np.array(comp_conc_end)[-1], 0.)
        # The presence of a strip component should reduce the concentration of
        # all other components by 10%:
        expected = expected * 9 / 10.
        assert_unit_array_almost_equal(comp_conc_end[:-1], expected[:-1])
Exemplo n.º 11
0
    def _duplicate_starting_point_sims(self, sim0):
        """ Create a set of starting point simulation to build the optimizer
        steps around, one for each target experiment.

        This is done by building the simulation from the experiment, using the
        default starting point simulation for the additional parameters not
        contained in the target experiment: binding and transport models, and
        first and last simulated step.

        Parameters
        ----------
        sim0 : Simulation
            Default simulation to use to construct center point sims for
            parameters not available in the target experiment to model.
        """
        # Collect information about the default center point
        method0 = sim0.method
        fstep = method0.method_steps[0].name
        lstep = method0.method_steps[-1].name

        center_points = []
        target_experiments = [
            self.target_study.search_experiment_by_name(name)
            for name in self.experiment_selected
        ]
        for target_exp in target_experiments:
            sim = build_simulation_from_experiment(
                target_exp,
                sim0.binding_model,
                sim0.transport_model,
                initial_buffer=self.initial_buffer,
                fstep=fstep,
                lstep=lstep,
                # Make clones to avoid interactions between sims:
                discretization=sim0.discretization.clone_traits(),
                solver=sim0.solver.clone_traits(),
            )
            if OPTIMIZER_USE_LAZY_SIMS:
                sim = LazyLoadingSimulation.from_simulation(sim)

            center_points.append(sim)

        return center_points
Exemplo n.º 12
0
def make_sample_binding_model_optimizer(num_values=5, num_params=2, exp=None,
                                        exp_source=DEFAULT_INPUT,
                                        with_data=False, **traits):
    from kromatography.compute.brute_force_binding_model_optimizer import \
        BruteForce2StepBindingModelOptimizer
    from kromatography.model.factories.simulation import \
        build_simulation_from_experiment
    from kromatography.model.api import LazyLoadingSimulation

    if exp is None:
        exp = make_sample_experiment2(source=exp_source)

    cp_sim = build_simulation_from_experiment(exp)
    # Center point sims coming from optimizer builder are LazyLoading
    cp_sim = LazyLoadingSimulation.from_simulation(cp_sim)
    starting_point_simulations = [cp_sim]
    param_list = make_sample_param_scan_list(num_values, num_params=num_params)
    optimizer_params = dict(
            target_experiments=[exp],
            starting_point_simulations=starting_point_simulations,
            constant_step_parameter_list=param_list,
            refining_step_num_values=num_values
        )
    optimizer_params.update(traits)

    optimizer = BruteForce2StepBindingModelOptimizer(**optimizer_params)

    if with_data:
        data = {}
        size = num_values**num_params
        data[ALL_COST_COL_NAME] = range(size)
        param_products = product(*[i+np.arange(num_values)
                                   for i in range(num_params)])
        cols = zip(*param_products)
        for param, col in zip(param_list, cols):
            data[param.name] = col

        cost_data = pd.DataFrame(data)
        ordered_cols = [p.name for p in param_list] + [ALL_COST_COL_NAME]
        optimizer.cost_data = cost_data[ordered_cols]

    return optimizer
Exemplo n.º 13
0
    def setUpClass(cls):
        input_file = io_data_path('ChromExampleDataV2.xlsx')
        cls.study = load_study_from_excel(input_file, allow_gui=False)

        # Building and running simulation for only 1 experiment
        expt = cls.study.experiments[0]
        output_file = "{}_{}_{}.h5".format(cls.study.name, expt.name,
                                           'cadet_simulation')
        output_file = string2filename(output_file)
        sim = build_simulation_from_experiment(expt)
        cls.study.simulations.append(sim)

        # create the CADET inputs
        cadet_input = build_cadet_input(sim)

        # write the CADET inputs
        write_to_h5(output_file, cadet_input, root='/input', overwrite=True)
        # run simulation to generate outputs
        run_cadet_simulator(output_file)
        update_simulation_results(sim, output_file)
def load_default_experiment_simulation(expt_id='Run_1'):
    """ Load a default experiment and already run simulation.

    DEPRECATED: use
    kromatography.model.tests.sample_data_factories.make_sample_experiment2
    instead.
    """
    # Load the experiment
    input_file = io_data_path('ChromExampleDataV2.xlsx')
    study = load_exp_study_from_excel(input_file,
                                      datasource=None,
                                      allow_gui=False)
    expt = study.search_experiment_by_name(expt_id)

    # Build and load the simulation
    sim = build_simulation_from_experiment(expt)
    build_cadet_input(sim)
    output_file = io_data_path('Chrom_Example_Run_1_cadet_simulation.h5')
    update_simulation_results(sim, output_file)
    return expt, sim
Exemplo n.º 15
0
    def setUpClass(cls):
        cls.tmp_dir = tempfile.mkdtemp()

        # Run the analysis
        input_file = io_data_path('ChromExampleDataV2.xlsx')
        outfile = join(cls.tmp_dir, 'cadet_data.h5')
        cls.exp_id = 'Run_1'
        binding_model = make_sample_binding_model()
        transport_model = make_sample_transport_model()

        cls.study = run_chromatography_simulation(
            input_file,
            output_file=outfile,
            expt_id=cls.exp_id,
            skip_plot=True,
            skip_animation=True,
            skip_cadet=False,
            binding_model=binding_model,
            transport_model=transport_model,
            allow_gui=False)

        sim_name = 'Sim: {}'.format(cls.exp_id)
        cls.output_sim = cls.study.search_simulation_by_name(sim_name)

        initial_experiment = cls.study.search_experiment_by_name(cls.exp_id)
        cls.expected_sim = build_simulation_from_experiment(
            initial_experiment,
            binding_model=binding_model,
            transport_model=transport_model)
        out_file = io_data_path('Chrom_Example_Run_1_cadet_simulation.h5')
        update_simulation_results(cls.expected_sim, out_file)

        cls.prod_comps = cls.expected_sim.product.product_component_names
        cls.expected_components = cls.expected_sim.output.continuous_data.keys(
        )  # noqa
        cls.found_components = cls.output_sim.output.continuous_data.keys()
Exemplo n.º 16
0
# expression
assays = ["CEX_comp1", "CEX_comp2"]
product_component_concentration_exps = [
    "product_concentration * CEX_comp1 / 100",
    "product_concentration * CEX_comp2 / 100"
]
new_prod = Product(
    name="PROD003",
    product_type="Mab",
    product_components=[comp1, comp2],
    product_component_concentration_exps=product_component_concentration_exps,
    product_component_assays=assays)
user_datasource.set_object_of_type("products", new_prod)
user_datasource.set_object_of_type("product_components", comp1)
user_datasource.set_object_of_type("product_components", comp2)

print("Current Study contains {} experiments".format(len(study.experiments)))

# Create a new simulation from an existing experiment:
exp = study.experiments[0]
# Let's give it a pH dependent binding model, leaving the rest as defaults.
# Note: +1 because binding models need 1 component for the cation component
ph_binding = PhDependentStericMassAction(
    num_prod_comp=exp.product.num_components, name="New binding")
new_sim = build_simulation_from_experiment(exp, binding_model=ph_binding)
study.add_simulations(new_sim)

# Open the new product in the central pane:
task.edit_object_in_central_pane(new_prod)
task.edit_object_in_central_pane(new_sim)
def run_chromatography_simulation(input_file,
                                  output_file=None,
                                  expt_id="Run_1",
                                  skip_cadet=False,
                                  skip_plot=False,
                                  skip_animation=False,
                                  binding_model=None,
                                  transport_model=None,
                                  allow_gui=True):
    """ Run chromatography simulation for the inputs given in `input_file`
    and plot the results.

    Parameters
    ----------
    input_file : str (file path)
        The file path for the excel file containing the experiment/simulation
        input data.

    output_file : str (file path)
        The file path for the CADET output h5 file. If None, then a default
        name is chosen based on the study and experiment ids.

    expt_id : str
        The name of the exeperiment to use for intializing the CADET
        simulation. If None, the first experiment in the study is chosen.

    skip_cadet : bool
        If True, the CADET simulation is not run but instead the output
        file is directly loaded (assuming one already exists). The main usecase
        here is for testing.

    skip_plot : bool
        If True, the chromatogram plot is not generated. This is useful for
        testing.

    Returns
    -------
    study : Study
        The Study instance containing the experiment and the simulation data.
    """
    study = load_study_from_excel(input_file, allow_gui=allow_gui)

    # lets just pick a single experiment
    if expt_id is None:
        expt = study.experiments[0]
    else:
        expt = study.search_experiment_by_name(expt_id)

    logger.info('Running simulation for experiment : {!r}'.format(expt_id))

    if output_file is None:
        output_file = ("{}_{}_{}.h5".format(study.name, expt.name,
                                            'cadet_simulation'))
        output_file = string2filename(output_file)

    # create a simulation object from an experiment
    # FIXME: how to identify related expt and simulations !
    sim = build_simulation_from_experiment(expt,
                                           binding_model=binding_model,
                                           transport_model=transport_model)

    study.simulations.append(sim)

    # create the CADET inputs
    cadet_input = build_cadet_input(sim)

    # NOTE: This is primarily used for testing/debugging workflow when the
    # CADET simulation doesn't need to be run everytime.
    if not skip_cadet:
        # write the CADET inputs
        write_to_h5(output_file, cadet_input, root='/input', overwrite=True)
        logger.info("Output file {} was generated".format(output_file))

        # run simulation to generate outputs
        run_cadet_simulator(output_file)

    update_simulation_results(sim, output_file)

    if not skip_plot:
        plot_chromatogram(expt, sim)

    if not skip_animation:
        column_animation(sim)

    return study
Exemplo n.º 18
0
def make_sample_brute_force_optimizer(num_values=5, num_params=2, exp=None,
                                      exp_source=DEFAULT_INPUT,
                                      target_sim=None, with_data=False,
                                      parallel_params=False, **traits):
    """ Create a sample Brute force optimizer.
    """
    from kromatography.compute.brute_force_optimizer import BruteForceOptimizer
    from kromatography.model.factories.simulation import \
        build_simulation_from_experiment
    from kromatography.model.lazy_simulation import is_lazy, \
        LazyLoadingSimulation

    if exp is None:
        exp = make_sample_experiment2(source=exp_source)

    if target_sim is None:
        target_sim = build_simulation_from_experiment(exp)

    if isinstance(exp, Experiment):
        target_exp = [exp]
    else:
        target_exp = exp

    if isinstance(target_sim, Simulation):
        target_sims = [target_sim]
    else:
        target_sims = target_sim

    for i, sim in enumerate(target_sims):
        if not is_lazy(sim):
            target_sims[i] = LazyLoadingSimulation.from_simulation(sim)

    param_list = make_sample_general_param_scan_list(num_values,
                                                     num_params=num_params,
                                                     target_sim=target_sims[0])

    if parallel_params:
        for i in range(num_params-1):
            param_list[0].parallel_parameters.append(param_list.pop(1))

    optimizer_params = dict(
            target_experiments=target_exp,
            starting_point_simulations=target_sims,
            parameter_list=param_list
    )
    optimizer_params.update(traits)
    optimizer = BruteForceOptimizer(**optimizer_params)

    if with_data:
        data = {}
        size = num_values**num_params
        data[ALL_COST_COL_NAME] = range(size)
        param_products = product(*[i+np.arange(num_values)
                                   for i in range(num_params)])
        cols = zip(*param_products)
        for param, col in zip(param_list, cols):
            data[param.name] = col

        cost_data = pd.DataFrame(data)
        ordered_cols = [p.name for p in param_list] + [ALL_COST_COL_NAME]
        optimizer.cost_data = cost_data[ordered_cols]

    return optimizer
 def setUpClass(cls):
     cls.real_study = make_sample_study2(add_transp_bind_models=True)
     cls.real_exp = cls.real_study.search_experiment_by_name('Run_1')
     cls.real_sim = build_simulation_from_experiment(cls.real_exp)
     cls.job_manager = create_start_job_manager(max_workers=1)
def build_sim_from_study(study, exp_idx=0):
    sim = build_simulation_from_experiment(study.experiments[exp_idx],
                                           fstep='whatever name',
                                           lstep='Gradient Elution')
    return sim
Exemplo n.º 21
0
 def test_build_with_custom_solver(self):
     solv = Solver(number_user_solution_points=500)
     sim = build_simulation_from_experiment(self.exp2, solver=solv)
     self.assertEqual(sim.solver.number_user_solution_points, 500)
Exemplo n.º 22
0
 def test_build_with_custom_discretization(self):
     disc = Discretization(ncol=30)
     sim = build_simulation_from_experiment(self.exp2, discretization=disc)
     self.assertEqual(sim.discretization.ncol, 30)
Exemplo n.º 23
0
 def test_build_sim_from_exp(self):
     sim = build_simulation_from_experiment(self.exp, fstep='Equilibration',
                                            lstep='Gradient Elution')
     self.assertValidSimFromExp(sim, self.exp,
                                first_step_name='Equilibration',
                                last_step_name='Gradient Elution')
Exemplo n.º 24
0
 def test_build_sim_from_exp_no_collection_criteria(self):
     self.exp2.method.collection_criteria = None
     sim = build_simulation_from_experiment(self.exp2)
     self.assertValidSimFromExp(sim, self.exp2)
Exemplo n.º 25
0
 def test_build_sim_from_real_exp(self):
     sim = build_simulation_from_experiment(self.exp2)
     self.assertValidSimFromExp(sim, self.exp2)