def test_manual_creation(self):
        with reraise_traits_notification_exceptions():
            group = self.sim_group_maker(size=3)

        self.assertIsInstance(group, SimulationGroup)
        self.assertEqual(group.size, 3)
        self.assertIsInstance(group.center_point_simulation, self.sim_class)

        self.assertFalse(group.has_run)
        self.assertEqual(group.run_status, MULTI_SIM_RUNNER_CREATED)

        # test data dataframe
        df = group.group_data
        self.assertIsInstance(df, pd.DataFrame)
        expected_columns = {
            'binding_model.sma_ka[1]', 'pool_volume (CV)',
            'pool_concentration (g/L)', 'step_yield (%)', SIM_COL_NAME
        }
        components = self.sim.product.product_component_names
        expected_purities = {
            "purity: {} (%)".format(comp)
            for comp in components
        }
        expected_columns = expected_columns.union(expected_purities)
        self.assertEqual(set(df.columns), expected_columns)
        expected_sims = {'Sim 2', 'Sim 1', 'Sim 0'}
        self.assertEqual(set(df[SIM_COL_NAME]), expected_sims)
        expected_index = range(len(expected_sims))
        self.assertEqual(list(df.index), expected_index)
        param_name = 'binding_model.sma_ka[1]'
        expected_bind_models = pd.Series([0.001, 0.01, 0.1], name=param_name)
        assert_series_equal(df[param_name], expected_bind_models)
        # No output set yet
        self.assertTrue(np.all(np.isnan(df.iloc[:, 2:])))
Пример #2
0
 def test_compute_summary_if_empty_filtered(self):
     analyzer = self.analyzer_klass(source_df=self.df)
     # Set filter so there is no filtered data:
     analyzer.filter_exp = "a > 100"
     with reraise_traits_notification_exceptions():
         summary = analyzer.compute_summary()
     assert_frame_equal(summary, pd.DataFrame([]))
    def test_update_starting_sim_from_name(self):
        """ Changing starting point sim should trigger a recomputation of cp
        """
        optim_builder = self.make_optimizer_builder()
        optim_builder.experiment_selected = ['Run_1']
        self.assertEqual(optim_builder.starting_point_simulation_name,
                         'Sim: Run_1')
        self.assertEqual(len(optim_builder.starting_point_simulations), 1)
        first_cp = optim_builder.starting_point_simulations[0]
        self.assertIsNot(first_cp, self.sim1)
        assert_has_traits_almost_equal(first_cp, self.sim1, check_type=False)

        with self.assertTraitChanges(optim_builder,
                                     "starting_point_simulations", 1):
            optim_builder.starting_point_simulation_name = 'Sim: Run_2'

        first_cp = optim_builder.starting_point_simulations[0]
        self.assertEqual(len(optim_builder.starting_point_simulations), 1)
        # The target experiment is still Run_1 so the resulting sim ~ sim1
        assert_has_traits_almost_equal(first_cp, self.sim1, check_type=False)
        self.assertIsInstance(first_cp, LazyLoadingSimulation)
        # But several quantities are pulled from the specified starting point
        self.assertEqual(first_cp.method.method_steps[0].name,
                         self.sim2.method.method_steps[0].name)
        self.assertEqual(first_cp.method.method_steps[-1].name,
                         self.sim2.method.method_steps[-1].name)

        # Choosing a different name
        with self.assertTraitDoesNotChange(optim_builder,
                                           "starting_point_simulations"):
            with self.assertRaises(KeyError):
                # Reraise the exception because it happens in listener
                with reraise_traits_notification_exceptions():
                    optim_builder.starting_point_simulation_name = \
                        "DOESN'T EXIST!"
    def test_change_cost_data(self):
        """ Changing cost data should trigger update of plot data and colorbar
        range. This mimics what happens when the weights of the cost function
        have changed.
        """
        initial_plot_data_dict = self.explorer.cost_plot_data.arrays.copy()
        colorbar_range = self.explorer.colorbar.index_mapper.range
        initial_low = colorbar_range.low
        initial_high = colorbar_range.high

        new_cost_data = self.optimizer.cost_data.copy()
        cols_to_change = [
            ALL_COST_COL_NAME, 'binding_model.sma_lambda',
            'column.resin.ligand_density'
        ]
        shift = 2
        new_cost_data[cols_to_change] += shift
        # Don't silence exception raised when listening to changes to cost_data
        with reraise_traits_notification_exceptions():
            self.optimizer.cost_data = new_cost_data

        new_plot_data = self.explorer.cost_plot_data.arrays.copy()
        for key, new_arr in new_plot_data.items():
            assert_array_almost_equal(new_arr,
                                      initial_plot_data_dict[key] + shift)

        colorbar_range = self.explorer.colorbar.index_mapper.range
        new_low = colorbar_range.low
        new_high = colorbar_range.high
        self.assertAlmostEqual(new_low, initial_low + shift)
        self.assertAlmostEqual(new_high, initial_high + shift)
Пример #5
0
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)
Пример #6
0
    def test_change_product_assays_list(self):
        view = self._get_model_view()
        original = view.product_assays
        try:
            self.assertEqual(len(view.product_assays), 3)
            self.assertEqual(len(self.model.product_component_assay_values), 3)
            with reraise_traits_notification_exceptions():
                view.product_assays = view.product_assays[1:]

            self.assertEqual(len(self.model.product_component_assay_values), 2)

            with reraise_traits_notification_exceptions():
                view.product_assays = []

            self.assertEqual(len(self.model.product_component_assay_values), 0)

        finally:
            # Reset to make ready for next steps
            view.product_assays = original
Пример #7
0
    def test_change_chemical_component_list(self):
        view = self._get_model_view()
        original = self.model.chemical_components
        try:
            self.assertEqual(len(view.chemical_components), 3)
            self.assertEqual(len(self.model.chemical_components), 3)
            with reraise_traits_notification_exceptions():
                view.chemical_components = view.chemical_components[1:]

            self.assertEqual(len(self.model.chemical_components), 2)

            with reraise_traits_notification_exceptions():
                view.chemical_components = []

            self.assertEqual(len(self.model.chemical_components), 0)

        finally:
            # Reset to initial value
            self.model.chemical_components = original
Пример #8
0
    def test_change_model_chem_components(self):
        view = self._get_model_view()
        self.assertEqual(len(self.model.chemical_components), 3)
        with reraise_traits_notification_exceptions():
            self.model.chemical_components = self.model.chemical_components[1:]

        # The view doesn't change from model changes until the
        # update_chemical_components is explicitely called:
        self.assertEqual(len(view.chemical_components), 3)
        view.update_chemical_components()
        self.assertEqual(len(view.chemical_components), 2)
Пример #9
0
    def test_filter_transformation(self):
        df = self.df
        with self.assertRaises(UndefinedVariableError):
            with reraise_traits_notification_exceptions():
                self.analyzer_klass(source_df=df, filter_exp="A > 2")

        analysis = self.analyzer_klass(
            source_df=df, filter_exp="A > 2",
            filter_transformation=lambda x: x.lower()
        )
        filtered = df.iloc[3:, :]
        assert_frame_equal(analysis.source_df, df)
        assert_frame_equal(analysis.filtered_df, filtered)
        assert_frame_equal(analysis.displayed_df, filtered)
Пример #10
0
    def test_bad_filter_with_raise(self):
        """ If filter set to a bad value, and raise behavior, error raised in
        listener.
        """
        df = self.df2
        analyzer = self.analyzer_klass(source_df=df,
                                       filter_error_handling="raise")
        self.assertEqual(analyzer.filter_exp, "")
        assert_frame_equal(analyzer.filtered_df, analyzer.source_df)
        # Request that involves an invalid expression raises a syntax error
        # inside the listener:
        with reraise_traits_notification_exceptions():
            with self.assertRaises(SyntaxError):
                analyzer.filter_exp = "a > "

            with self.assertRaises(UndefinedVariableError):
                analyzer.filter_exp = "c > 1"
Пример #11
0
    def test_change_model_assays(self):
        view = self._get_model_view()
        original = view.product_assays
        try:
            self.assertEqual(len(self.model.product_component_assay_values), 3)
            self.assertEqual(len(view.product_assays), 3)
            with reraise_traits_notification_exceptions():
                self.model.product_component_assay_values = \
                    self.model.product_component_assay_values[1:]

            # The view doesn't change from model changes until the
            # update_chemical_components is explicitely called:
            self.assertEqual(len(view.product_assays), 3)
            view.update_product_assays()
            self.assertEqual(len(view.product_assays), 2)
        finally:
            view.product_assays = original
    def test_change_cost_data(self):
        """ Changing cost data should trigger update of plot data.

        Mimics what happens when the weights of the cost function have changed.
        """
        explorer = self.explorer
        explorer.x_axis_param = "binding_model.sma_lambda"

        initial_plot_data_dict = self.explorer.cost_plot_data.arrays.copy()
        new_cost_data = self.optimizer.cost_data.copy()
        cols_to_change = [
            ALL_COST_COL_NAME, 'binding_model.sma_lambda',
            'column.resin.ligand_density'
        ]
        shift = 2.0
        new_cost_data[cols_to_change] += shift
        with reraise_traits_notification_exceptions():
            self.optimizer.cost_data = new_cost_data

        new_plot_data = self.explorer.cost_plot_data.arrays.copy()
        for key, new_arr in new_plot_data.items():
            assert_array_almost_equal(new_arr,
                                      initial_plot_data_dict[key] + shift)