def test_scaled_component_type_wrongly_modified(): diagnostic = ScalingWrapper( MockDiagnosticThatExpects(4.0), diagnostic_scale_factors={'expected_field': 0.5}) state = {'expected_field': 4.0} diagnostic._component_type = 'abcd' with pytest.raises(ValueError) as excinfo: diagnostics = diagnostic(state) assert 'bug in ScalingWrapper' in str(excinfo.value)
def test_tendency_no_scaling_when_input_scaled(self): self.input_properties = {'diag1': {'dims': ['dim1'], 'units': 'm'}} self.tendency_properties = { 'diag1': { 'dims': ['dim1'], 'units': 'm/s', } } self.tendency_output = {'diag1': np.ones([10])} base_component = self.get_component() component = ScalingWrapper( base_component, input_scale_factors={ 'diag1': 10., }, ) assert isinstance(component, self.component_type) state = { 'time': timedelta(0), 'diag1': DataArray(np.ones([10]), dims=['dim1'], attrs={'units': 'm'}) } tendencies = self.get_tendencies(self.call_component(component, state)) assert tendencies.keys() == self.tendency_output.keys() assert np.all(tendencies['diag1'] == 1.)
def test_inputs_one_scaling_with_unit_conversion(self): self.input_properties = { 'input1': { 'dims': ['dim1'], 'units': 'm', }, 'input2': { 'dims': ['dim1'], 'units': 'm', }, } state = { 'time': timedelta(0), 'input1': DataArray(np.ones([10]), dims=['dim1'], attrs={'units': 'km'}), 'input2': DataArray(np.ones([10]), dims=['dim1'], attrs={'units': 'm'}), } base_component = self.get_component() component = ScalingWrapper(base_component, input_scale_factors={'input1': 0.5}) assert isinstance(component, self.component_type) self.call_component(component, state) assert base_component.state_given.keys() == state.keys() assert np.all(base_component.state_given['input1'] == 500.) assert np.all(base_component.state_given['input2'] == 1.)
def scaled_version(self, input_scale_factors=None, diagnostic_scale_factors=None, output_scale_factors=None): """ Returns component whose input/outputs/tendencies/diagnostics are scaled by the given scale factors. Args: input_scale_factors (dict): a dictionary whose keys are the inputs that will be scaled and values are floating point scaling factors. output_scale_factors (dict): a dictionary whose keys are the tendencies that will be scaled and values are floating point scaling factors. diagnostic_scale_factors (dict): a dictionary whose keys are the diagnostics that will be scaled and values are floating point scaling factors. """ return ScalingWrapper(self, input_scale_factors=input_scale_factors, output_scale_factors=output_scale_factors, diagnostic_scale_factors=diagnostic_scale_factors)
def test_scaled_diagnostic_inputs(): diagnostic = ScalingWrapper(MockDiagnosticThatExpects(2.0), input_scale_factors={'expected_field': 0.5}) state = {'expected_field': 4.0} diagnostics = diagnostic(state) assert diagnostics['expected_field'] == 2.0
def test_scaled_component_wrong_type(): class WrongObject(object): def __init__(self): self.a = 1 wrong_component = WrongObject() with pytest.raises(TypeError): ScalingWrapper(wrong_component)
def test_scaled_prognostic_tendencies(): prognostic = ScalingWrapper(MockPrognosticThatExpects(4.0), tendency_scale_factors={'expected_field': 0.5}) state = {'expected_field': 4.0} tendencies, diagnostics = prognostic(state) assert tendencies['expected_field'] == 2.0 assert diagnostics['expected_field'] == 4.0
def test_scaled_implicit_diagnostics(): implicit = ScalingWrapper(MockImplicitThatExpects(4.0), diagnostic_scale_factors={'expected_field': 0.5}) state = {'expected_field': 4.0} diagnostics, new_state = implicit(state) assert diagnostics['expected_field'] == 2.0 assert new_state['expected_field'] == 4.0
def test_scaled_component_wrong_type(): class WrongType(object): def __init__(self): self.a = 1 wrong_component = WrongType() with pytest.raises(TypeError) as excinfo: component = ScalingWrapper(wrong_component) assert 'either of type Implicit' in str(excinfo.value)
def test_tendency_no_scaling(self): self.tendency_properties = { 'diag1': { 'dims': ['dim1'], 'units': 'm', } } self.tendency_output = {'diag1': np.ones([10])} base_component = self.get_component() component = ScalingWrapper( base_component, tendency_scale_factors={}, ) assert isinstance(component, self.component_type) state = {'time': timedelta(0)} tendencies = self.get_tendencies(self.call_component(component, state)) assert tendencies.keys() == self.tendency_output.keys() assert np.all(tendencies['diag1'] == 1.)
def test_output_one_scaling(self): self.output_properties = { 'diag1': { 'dims': ['dim1'], 'units': 'm', } } self.output_state = {'diag1': np.ones([10])} base_component = self.get_component() component = ScalingWrapper( base_component, output_scale_factors={ 'diag1': 10., }, ) assert isinstance(component, self.component_type) state = {'time': timedelta(0)} outputs = self.get_outputs(self.call_component(component, state)) assert outputs.keys() == self.output_state.keys() assert np.all(outputs['diag1'] == 10.)
def test_scaled_prognostic_with_wrong_tendency_field(): with pytest.raises(ValueError) as excinfo: prognostic = ScalingWrapper(MockPrognosticThatExpects(4.0), tendency_scale_factors={'abcd': 0.5}) assert 'not a valid tendency' in str(excinfo.value)
def test_scaled_implicit_created_with_wrong_diagnostic_field(): with pytest.raises(ValueError) as excinfo: implicit = ScalingWrapper(MockImplicitThatExpects(2.0), diagnostic_scale_factors={'abcd': 0.5}) assert 'not a valid diagnostic' in str(excinfo.value)