def setUpClass(cls): cls.ref_task, _ = load_object( io_data_path("demo_with_optimizerv7.chrom")) cls.ref_task2, _ = load_object( io_data_path("demo_with_general_optimizerv9.chrom")) cls.ref_task3, _ = load_object( io_data_path("demo_with_general_optimizerv10.chrom"))
def test_load_same_file_twice(self): filename = "demo_with_optimizerv8.chrom" obj1, _ = load_object(io_data_path(filename)) obj2, _ = load_object(io_data_path(filename)) # Make sure they are identical without being the same object: assert_has_traits_almost_equal(obj1, obj2, eps=1e-15) self.assertIsNot(obj1, obj2) self.assertIsNot(obj1.project.study, obj2.project.study)
def test_2_task_creations_with_project_files(self): app = self.app filename = io_data_path('std_project_serialv4.chrom') filename2 = io_data_path('std_project_serialv5.chrom') task1 = app.open_project_from_file(filename) task2 = app.open_project_from_file(filename2) self.assertIsInstance(task1, KromatographyTask) self.assertIsInstance(task2, KromatographyTask) self.assertEqual(len(app.windows_created), 2) assert_app_valid(app) self.assertEqual(len(app.recent_files), 2) # Reverse order: self.assertEqual(app.recent_files, [filename2, filename])
def test_update_std_run_sim(self): sim = make_sample_simulation(name='Run_1') self.assertIsNone(sim.output) # Contains data from the CADET run: h5file = io_data_path("Sample_Sim_from_Run_1_cadet_simulation_run.h5") with self.assertTraitChanges(sim, "perf_param_data_event", 1): update_simulation_results(sim, h5file) self.assertIsInstance(sim.output, SimulationResults) perf_params = sim.output.performance_data self.assertIsInstance(perf_params, PerformanceData) self.assertIsInstance(perf_params.pool, SolutionWithProduct) num_comp = len(sim.product.product_components) self.assertEqual( len(perf_params.pool.product_component_concentrations), # noqa num_comp) assert_unit_scalar_almost_equal(perf_params.pool_volume, UnitScalar(1.727090, units="CV"), eps=1.e-6) assert_unit_scalar_almost_equal(perf_params.step_yield, UnitScalar(95.814, units='%'), eps=1.e-3) assert_unit_scalar_almost_equal(perf_params.start_collect_time, UnitScalar(200.946, units='minute'), eps=1.e-3) assert_unit_scalar_almost_equal(perf_params.stop_collect_time, UnitScalar(227.9, units='minute'), eps=1.e-3)
def test_build_study(self): filepath = io_data_path('ChromExampleDataV2.xlsx') exp_study = load_exp_study_from_excel(filepath, allow_gui=False) self.assertIsInstance(exp_study, ExperimentalStudy) study = build_study_from_experimental_study( experimental_study=exp_study) self.assertIsInstance(study, Study) attr_list = ["datasource", "product"] for attr in attr_list: self.assertIs(getattr(study, attr), getattr(exp_study, attr)) attr_list = ["experiments", "study_purpose", "study_type"] for attr in attr_list: self.assertEqual(getattr(study, attr), getattr(exp_study, attr)) self.assertEqual(study.simulations, []) self.assertEqual(len(study.experiments), 3) ds = study.study_datasource self.assertEqual(len(ds.methods), 3) self.assertEqual(len(ds.loads), 2) self.assertEqual(len(ds.buffers), 3) self.assertEqual(len(ds.columns), 1)
def test_study(self): input_file = io_data_path('ChromExampleDataV2.xlsx') obj = load_study_from_excel(input_file, allow_gui=False) expected = ['product', 'is_blank', 'name', 'study_type', 'editable', 'class_metadata', 'study_purpose', 'experiments', 'simulations', 'study_datasource', 'exp_study_filepath', 'analysis_tools'] assert_serial_data_contains(obj, expected)
def setUpClass(cls): # Ignore the names of objects and method's collection_step_number since # defaults have changed. cls.ignore = [ 'simulation_diffs', 'center_point_simulation', 'perf_params' ] cls.reference_filename = "demo_final_statev0.7.2.chrom" cls.ref_task, _ = load_object(io_data_path(cls.reference_filename))
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)
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)
def setUpClass(cls): folder = join(dirname(kromatography.__file__), "data", "tutorial_data") cls.pulse_akta = join(folder, "47cm_hr.asc") # Originally stored in PROD001_Example_Pulse_Injection_Study.xlsx fname = io_data_path("prod1_pulse_run2_experiment.chrom") cls.exp = load_object(fname)[0] # Multi-component product experiment cls.exp2 = make_sample_experiment2()
def test_task_creation_with_project(self): app = self.app filename = io_data_path('test.chrom') task = app.open_project_from_file(filename) self.assertEqual(len(app.windows_created), 1) self.assertIsInstance(task, KromatographyTask) assert_app_valid(app) self.assertEqual(len(app.recent_files), 1) self.assertEqual(app.recent_files[0], filename)
def assert_old_file_read(filename, reference_task, ignore=(), eps=1e-9): with reraise_traits_notification_exceptions(): obj, legacy = load_object(io_data_path(filename)) assert_has_traits_almost_equal(obj.project, reference_task.project, ignore=ignore, eps=eps) assert_true(legacy)
def test_task_creation_with_study(self): app = self.app filename = 'ChromExampleDataV2.xlsx' window = app.build_study_from_file(io_data_path(filename), allow_gui=False) task = window.active_task self.assertEqual(len(app.windows_created), 1) self.assertIsInstance(task, KromatographyTask) self.assertEqual(len(app.windows_created), 1) assert_app_valid(app)
def setUp(self): self.study = make_sample_study2(add_transp_bind_models=True, add_sims='Run_1') self.sim = self.study.simulations[0] self.sim_run = self.sim.copy() # Save time by loading results from an already generated file: self.result_filepath = io_data_path("std_example_xlsx_run1_cadet.h5") update_simulation_results(self.sim_run, self.result_filepath) self.sim_run.set_as_run()
def test_perf_data_name_update(self): # Since performance data pane shows sim's perf data, make sure the # change of name in sim is reflected in its performance data. sim = self.sim_from_scratch # Make it as if it is run: result_filepath = io_data_path("std_example_xlsx_run1_cadet.h5") update_simulation_results(sim, result_filepath) sim.set_as_run() sim.name = "foo" self.assertEqual(sim.output.performance_data.name, "foo")
def test_run_gui_with_project_file(self): """ test of high level app launcher with project input argument. """ filename = io_data_path('test.chrom') study_filename = 'Chrom Example Data.xlsx' self._start_and_stop_gui_app(init_files=[filename], num_windows=1) expected = '{} {}: {} ({})'.format(APP_FAMILY, APP_TITLE, os.path.abspath(filename), study_filename) # Window title added by _start_and_stop_gui_app: self.assertEqual(self.window_title, expected)
def test_update_results(self): # Since performance data pane shows sim's perf data, make sure the # change of name in sim is reflected in its performance data. sim = self.sim_from_scratch # Make it as if it is run: result_filepath = io_data_path("std_example_xlsx_run1_cadet.h5") update_simulation_results(sim, result_filepath) sim.set_as_run() self.assertTrue(sim.has_run) self.assertTrue(isfile(sim.cadet_filepath)) self.assertIsInstance(sim.output, SimulationResults)
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
def test_2_task_creations_with_same_project_file(self): app = self.app filename = io_data_path('std_project_serialv3.chrom') task1 = app.open_project_from_file(filename) task2 = app.open_project_from_file(filename) self.assertIsInstance(task1, KromatographyTask) # Second task is None since project already open: self.assertIsNone(task2, KromatographyTask) self.assertEqual(len(app.windows_created), 1) assert_app_valid(app) # Duplicates ignored: self.assertEqual(len(self.app.recent_files), 1) self.assertEqual(self.app.recent_files[0], filename)
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()
def make_sample_experiment2_with_strip(exp_name="Run_1", source=DEFAULT_INPUT_WITH_STRIP): from kromatography.utils.api import load_default_user_datasource from kromatography.io.api import load_study_from_excel from kromatography.utils.testing_utils import io_data_path from kromatography.model.factories.product import add_strip_to_product ds = load_default_user_datasource()[0] prod1 = ds.get_object_of_type("products", "Prod000Complex") new_prod, new_comp = add_strip_to_product(prod1, 18.8, 0.75) new_prod.name = "Prod001_with_strip" ds.set_object_of_type("products", new_prod) ds.set_object_of_type("product_components", new_comp) filepath = io_data_path(source) study = load_study_from_excel(filepath, datasource=ds, allow_gui=False) experim = study.search_experiment_by_name(exp_name) return experim
def test_experiment_import_settings(self): """ Test that experiments import settings get updated. """ filenames = [ "std_project_serialv5.chrom", "demo_final_statev5.chrom", "demo_final_statev6.chrom", "demo_with_optimizerv8.chrom", "demo_with_general_optimizerv9.chrom" ] for filename in filenames: task, _ = load_object(io_data_path(filename)) study = task.project.study for exp in study.experiments: expected = { "time_of_origin", "col_name_patterns", 'holdup_volume' } settings = exp.output.import_settings self.assertEqual(set(settings.keys()), expected) self.assertIsInstance(settings["time_of_origin"], UnitScalar) self.assertIsInstance(settings["holdup_volume"], UnitScalar)
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 test_binding_transport_model_target_prod_set(self): """ Make sure old files' binding and transport models are updated once loaded, such that they have target products. """ task, _ = load_object(io_data_path("demo_with_optimizerv8.chrom")) study = task.project.study prod_name = study.product.name # Find all binding and transport models to test for their # target_product: optimizers = [ optim for optim in study.analysis_tools.optimizations if isinstance(optim, BindModelOptim) ] optim_models = [] for optim in optimizers: optim_models += [model for model in optim.optimal_models] all_models = (study.study_datasource.binding_models + study.study_datasource.transport_models + optim_models) for model in all_models: self.assertEqual(model.target_product, prod_name)
def test_change_cadet_executable(self): cadet_file = io_data_path("Chrom_Example_Run_1_cadet_simulation.h5") copy(cadet_file, "backup.h5") try: run_cadet_simulator(cadet_file) modified_prefs = self.default_prefs # Make sure changing the executable breaks the ability to run cadet current_executable = \ modified_prefs.solver_preferences.solver_binary_path modified_prefs.solver_preferences.solver_binary_path = "BLAH" modified_prefs.to_preference_file() try: with self.assertRaises(InvalidExecutorError): run_cadet_simulator(cadet_file) finally: modified_prefs.solver_preferences.solver_binary_path = \ current_executable modified_prefs.to_preference_file() finally: copy("backup.h5", cadet_file)
def setUp(self): self.study = make_sample_study2(add_transp_bind_models=True, add_sims='Run_1') self.sim = self.study.simulations[0] # Save time by loading results from an already generated file: self.result_filepath = io_data_path("std_example_xlsx_run1_cadet.h5") update_simulation_results(self.sim, output_fname=self.result_filepath) self.sim.set_as_run() # Add a simulation grid grp = SimulationGroup(center_point_simulation=self.sim, name="new group") self.study.analysis_tools.simulation_grids.append(grp) # Add a Monte Carlo simulation group mc_grp = make_sample_mc_simulation_group() self.study.analysis_tools.monte_carlo_explorations.append(mc_grp) # Skip attributes not stored in a study/task: (user) datasource, # study_datasource.dirty, sim group's cp, self.ignore = {'center_point_simulation', 'datasource', 'perf_params', 'dirty'}
def test_store_akta_settings(self): # This file was created after akta settings were added to the models fname = 'std_input_with_akta_shift.chrom' file_with_stored_settings = io_data_path(fname) task, _ = load_object(file_with_stored_settings) exp0 = task.project.study.search_experiment_by_name('Run_1') self.assertIsInstance(exp0.output.import_settings, dict) custom_patterns = { 'uv': r'(UV.*280nm)', 'conductivity': r'(COND$)', 'concentration': r'(CONC$)', 'pH': r'(pH$)', 'flow': r'(FLOW$)', 'fraction': r'(FRACTIONS?$)', 'log_book': r'(Log(book)?$)', 'temperature': r'(TEMP$)', } expected = { "time_of_origin": UnitScalar(102.050, units="minute"), "col_name_patterns": custom_patterns, 'holdup_volume': UnitScalar(0.0, units='minute') } self.assertEqual(exp0.output.import_settings, expected)
from unittest import TestCase from app_common.apptools.testing_utils import temp_bringup_ui_for from kromatography.ui.simulation_selector import BaseSimulationSelector, \ SingleSimulationSelector, SingleSimulationGroupSelector from kromatography.utils.testing_utils import io_data_path from kromatography.io.api import load_study_from_project_file STUDY_FNAME = io_data_path("demo_final_statev0.7.2.chrom") STUDY = load_study_from_project_file(STUDY_FNAME) class TestBaseSimulationSelector(TestCase): def test_creation_base_selector(self): selector = BaseSimulationSelector(study=STUDY) self.assertEqual(selector.simulation_names_available, ["Sim: Run_1"]) self.assertEqual(selector.simulation_group_names_available, ["New SimulationGroup"]) def test_bringup(self): selector = BaseSimulationSelector(study=STUDY) with temp_bringup_ui_for(selector): pass class TestSimulationSelector(TestCase): def test_bringup(self): selector = SingleSimulationSelector(study=STUDY) with temp_bringup_ui_for(selector):
def test_run_gui_with_2_project_files(self): filename1 = io_data_path('std_project_serialv0.chrom') filename2 = io_data_path('std_project_serialv3.chrom') self._start_and_stop_gui_app(init_files=[filename1, filename2], num_windows=2)
def setUp(self): self.filepath = io_data_path("std_project_serialv5.chrom") self.filepath_temp = join(HERE, "temp.chrom") self.filepath_wrong_ext = join(HERE, "temp.temp")