def test_compute_stop_collect_time_wrong_type(self):
     with self.assertRaises(TraitError):
         CollectionCriteria(
             name="Test collec crit",
             start_collection_target=0.5,
             start_collection_type="Percent Peak Maximum",
             stop_collection_target=60,
             stop_collection_type="BAD TYPE",
         )
 def test_compute_start_collect_time_fixed_abs_searching_too_far(self):
     coll_criteria = CollectionCriteria(
         name="Test collec crit",
         start_collection_target=0.5,
         start_collection_type="Fixed Absorbance",
         stop_collection_target=1.5,
         stop_collection_type="Fixed Absorbance",
     )
     # These times are larger than the end of the dataset
     step_start = UnitScalar(5 / 60., units="minute")
     step_stop = UnitScalar(6 / 60., units="minute")
     with self.assertRaises(ValueError):
         calculate_start_stop_collect(self.data, coll_criteria, step_start,
                                      step_stop)
    def test_compute_start_collect_time_fixed_abs_stop_before_start(self):
        coll_criteria = CollectionCriteria(
            name="Test collec crit",
            start_collection_target=0.5,
            start_collection_type="Fixed Absorbance",
            start_collection_while="Descending",
            stop_collection_target=1.5,
            stop_collection_type="Fixed Absorbance",
        )
        self.assertEqual(coll_criteria.stop_collection_while, "Descending")

        collections = calculate_start_stop_collect(self.data, coll_criteria,
                                                   self.step_start,
                                                   self.step_stop)
        start_time, start_idx, stop_time, stop_idx = collections
        self.assertGreater(start_time, stop_time)
 def test_compute_start_collect_time_ppm(self):
     coll_criteria = CollectionCriteria(
         name="Test collec crit",
         start_collection_target=40,
         start_collection_type="Percent Peak Maximum",
         stop_collection_target=60,
         stop_collection_type="Percent Peak Maximum",
     )
     collections = calculate_start_stop_collect(self.data, coll_criteria,
                                                self.step_start,
                                                self.step_stop)
     start_time, start_idx, stop_time, stop_idx = collections
     self.assertEqual(start_time, UnitScalar(1. / 60., units="minute"))
     self.assertEqual(start_idx, 1)
     self.assertEqual(stop_time, UnitScalar(3. / 60., units="minute"))
     self.assertEqual(stop_idx, 3)
 def test_compute_start_collect_time_fixed_abs(self):
     coll_criteria = CollectionCriteria(
         name="Test collec crit",
         start_collection_target=0.5,
         start_collection_type="Fixed Absorbance",
         stop_collection_target=1.5,
         stop_collection_type="Fixed Absorbance",
     )
     self.assertEqual(coll_criteria.start_collection_while, "Ascending")
     self.assertEqual(coll_criteria.stop_collection_while, "Descending")
     collections = calculate_start_stop_collect(self.data, coll_criteria,
                                                self.step_start,
                                                self.step_stop)
     start_time, start_idx, stop_time, stop_idx = collections
     self.assertEqual(start_time, UnitScalar(1. / 60., units="minute"))
     self.assertEqual(start_idx, 1)
     self.assertEqual(stop_time, UnitScalar(3. / 60., units="minute"))
     self.assertEqual(stop_idx, 3)
    def test_compute_start_collect_time_fixed_abs_double_peak(self):
        coll_criteria = CollectionCriteria(
            name="Test collec crit",
            start_collection_target=0.5,
            start_collection_type="Fixed Absorbance",
            stop_collection_target=1.5,
            stop_collection_type="Fixed Absorbance",
        )

        step_start = UnitScalar(5 / 60., units="minute")
        step_stop = UnitScalar(10 / 60., units="minute")
        collections = calculate_start_stop_collect(self.data_2peaks,
                                                   coll_criteria, step_start,
                                                   step_stop)
        start_time, start_idx, stop_time, stop_idx = collections
        self.assertEqual(start_time, UnitScalar(6. / 60., units="minute"))
        self.assertEqual(start_idx, 6)
        self.assertEqual(stop_time, UnitScalar(8. / 60., units="minute"))
        self.assertEqual(stop_idx, 8)
METHOD_DATA = {
    'name':
    'method-1',
    'run_type':
    'Gradient Elution',
    'method_steps': [
        MethodStep(**PRE_EQUIL_STEP),
        MethodStep(**EQUIL_STEP),
        MethodStep(**LOAD_STEP),
        MethodStep(**GRADIENT_ELUTION_STEP)
    ],
    'collection_step_number':
    3,
    'collection_criteria':
    CollectionCriteria(**COLLECTION_CRITERIA_DATA),
}

SYSTEM_TYPE_DATA = {
    'name': 'AKTA_explorer',
    'manufacturer': 'GE',
    'manufacturer_name': 'AKTA Explorer 100',
    'flow_range': UnitArray([10, 100], units=ml_per_min),
    'num_inlets': 4,
    'num_channels': 2,
}

SYSTEM_DATA = {
    'name': 'Chrom Skid 1',
    'system_number': 'CS1234',
    'holdup_volume': UnitScalar(100, units=milliliter),
    def _build_experiments(self):
        """ Build experiments from all components, including continuous data.

        Note: To make the continuous data comparable to simulations, its time
        axis is shifted to remove all hold up volume, to make it as if the
        measurements (UV, conductivity, ...) are made right outside the column.
        """
        experiments = {}
        for name, experiment in self.excel_data['experiment_data'].items():
            msg = "Loading experiment {}".format(name)
            logger.debug(msg)

            method_data = experiment['method_data']

            if not method_data['collection_criteria']:
                collection_criteria = None
            else:
                collection_criteria = CollectionCriteria(
                    name='collection_{}'.format(name),
                    **method_data['collection_criteria'])

            collection_step_number = method_data['collection_step_number']
            method_steps = []
            for method_step in method_data['steps']:
                # The excel data has the names of the solutions used in
                # a method step. So lookup the names from the list of
                # solutions (both loads and buffers) in this study.
                solution_names = method_step.pop('solution_names')
                solutions = [
                    self.solutions[key] for key in solution_names
                    if key not in MISSING_VALUES
                ]

                # create unitted data for initializing model.
                unitted_step_data = {}
                for key, val in method_step.items():
                    # FIXME: special-casing can be removed once we parse units
                    # properly.
                    # NOTE: The volume for LOAD step is specified in terms
                    # of g/(L of resin). Must divide by product concentration
                    # in the load solution to get the volume in CV like the
                    # other steps:
                    if method_step['step_type'] == 'Load' and key == 'volume':
                        prod_conc = solutions[0].product_concentration
                        val /= prod_conc[()]

                    unitted_step_data[key] = self._get_unitted_value(
                        'method_' + key, val)
                # FIXME: We need a way to assign method_steps.step_type
                # initialize the MethodStep
                step = MethodStep(solutions=solutions, **unitted_step_data)
                method_steps.append(step)

            # Initialize the Method for the experiment
            run_type = method_data['run_type']
            method = Method(
                method_steps=method_steps,
                collection_step_number=collection_step_number,
                collection_criteria=collection_criteria,
                run_type=run_type,
                name=name,
            )
            # Build the experiment outputs (cont data from AKTA and fraction
            # and performance parameters from excel
            experiment = Experiment(name=name,
                                    system=self.system,
                                    column=self.column,
                                    method=method)

            try:
                output = self._build_experiment_output(name, experiment)
            except Exception as e:
                msg = ("Failed to load the experiment output for {}. Error was"
                       " {}. Traceback was\n{}".format(name, e, format_tb()))
                logger.error(msg)
                output = None

            # Finalize the experiment
            experiment.output = output
            experiments[name] = experiment
        return experiments
 def setUp(self):
     self.collection_criteria = CollectionCriteria(
         **COLLECTION_CRITERIA_DATA)