Ejemplo n.º 1
0
    def test_numpy_parameter(self):
        name = 'dummy'
        data = np.array([1.1, 1.2, 1.3, 1.4])
        alignment = np.array([0.01, 0.02, 0.03, 0.04])
        misalignment = np.array([0.01, 0.02, 0.03])

        with self.assertRaises(TypeError):
            pd = NumpyParameterData(1, data, alignment)
        with self.assertRaises(TypeError):
            pd = NumpyParameterData(name, 'hi', alignment)
        with self.assertRaises(TypeError):
            pd = NumpyParameterData(name, data, 'hi')
        with self.assertRaises(ValueError):
            pd = NumpyParameterData(name, data, misalignment)

        pd = NumpyParameterData(name, data, alignment)

        self.assertEqual(id(data), id(pd.get_data_as_numpy_array(pd.get_alignment())))

        self.assertEqual(0, pd.get_data_as_numpy_array(np.array([0.05, 0.015])).size)
        self.assertEqual(np.array([1.2, 1.4]).all(), pd.get_data_as_numpy_array(np.array([0.04, 0.02])).all())
    def write_parameters(self, write_id, values):
        if not isinstance(values, dict):
            raise TypeError("values must be dict type.  Found %s" % type(values))
        arr_len = -1
        all_values_constant_over_time = True
        write_time = get_current_ntp_time()
        bad_keys = []
        mutable_params = {}
        for k, v in values.iteritems():
            if isinstance(v, NumpyParameterData):
                arr_len = v.get_data().shape[0]
            elif isinstance(v, np.ndarray):
                arr_len = v.shape[0]
            elif isinstance(v, ConstantOverTime):
                continue
            break
        if arr_len > 0:
            values[Span.ingest_time_str] = NumpyParameterData(Span.ingest_time_str, self.master_manager.parameter_metadata[Span.ingest_time_str].parameter_context.param_type.create_filled_array(arr_len, write_time))

        for key, arr in values.iteritems():
            if key not in self.value_list:
                raise KeyError("Parameter, %s, has not been initialized" % (key))

            param_type = self.master_manager.parameter_metadata[key].parameter_context.param_type
            if isinstance(param_type, ParameterFunctionType):
                bad_keys.append(key)
                continue #TODO: throw error instead

            if isinstance(arr, np.ndarray):
                arr = NumpyParameterData(key, arr)
                values[key] = arr
            if not isinstance(arr, ParameterData):
                raise TypeError("Value for %s must implement <%s>, found <%s>" % (key, ParameterData.__name__, arr.__class__.__name__))

            if not isinstance(arr, ConstantOverTime):
                if self.master_manager.parameter_metadata[key].parameter_context.param_type.validate_value_set(arr.get_data()):
                    self.master_manager.parameter_metadata[key].read_only = False
                    self.master_manager.parameter_metadata[key].flush()
                    self.master_manager.parameter_metadata[key].read_only = True

                all_values_constant_over_time = False
            elif isinstance(arr, NumpyParameterData) and arr.get_data().shape[0] != arr_len:
                raise ValueError("Array size for %s is inconsistent.  Expected %s elements, found %s." % (key, str(arr_len), str(arr.get_data().size)))
            min_val, max_val = self.value_list[key].get_statistics(arr)
            self.update_parameter_bounds(key, (min_val, max_val))
            if param_type.is_mutable:
                mutable_params[key] = values[key]

        for key in bad_keys:
            values.pop(key)

        for key in mutable_params:
            values.pop(key)
        if len(mutable_params) > 0 and self.alignment_parameter in values:
            mutable_params[self.alignment_parameter] = values[self.alignment_parameter]
            mutable_params[Span.ingest_time_str] = values[Span.ingest_time_str]
        if not all_values_constant_over_time and self.alignment_parameter not in values:
            raise LookupError("Array must be supplied for parameter, %s, to ensure alignment" % self.alignment_parameter)

        write_span = False
        if len(values) > 1 and self.alignment_parameter in values:
            if len(values) == 2 and len(mutable_params) > 0:
                pass
            else:
                write_span = True
        elif all_values_constant_over_time:
            write_span = True
        if write_span:
            span = Span(write_id, self.master_manager.guid, values, compressors=self.value_list)
            span_table = SpanStorageFactory.get_span_storage_obj(self.storage_name)
            span_table.write_span(span)
        if len(mutable_params) > 2:
            self._write_mutable_params(mutable_params, write_id + '_mutable', self.master_manager.guid)