Skip to content

Commit

Permalink
Handle polynomial scaling with no coefficients (#183)
Browse files Browse the repository at this point in the history
For issue #182
  • Loading branch information
adamreeve committed Apr 10, 2020
1 parent 8bbaea7 commit b5eb1c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nptdms/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def from_properties(properties, scale_index):
return PolynomialScaling(coefficients, input_source)

def scale(self, data):
if len(self.coefficients) == 0:
return np.zeros(len(data), dtype=np.dtype('float64'))

# Ensure data is double type before scaling
data = data.astype(np.dtype('float64'), copy=False)
return np.polynomial.polynomial.polyval(data, self.coefficients)
Expand Down
17 changes: 17 additions & 0 deletions nptdms/test/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ def test_polynomial_scaling():
np.testing.assert_almost_equal(expected_scaled_data, scaled_data)


def test_polynomial_scaling_with_no_coefficients():
"""Test polynomial scaling when there are no coefficients, so data should be all zero
"""
data = StubTdmsData(np.array([1.0, 2.0, 3.0]))
expected_scaled_data = np.array([0.0, 0.0, 0.0])

properties = {
"NI_Number_Of_Scales": 1,
"NI_Scale[0]_Scale_Type": "Polynomial",
"NI_Scale[0]_Polynomial_Coefficients_Size": 0
}
scaling = get_scaling(properties, {}, {})
scaled_data = scaling.scale(data)

np.testing.assert_almost_equal(expected_scaled_data, scaled_data)


def test_polynomial_scaling_with_3_coefficients():
"""Test polynomial scaling"""

Expand Down

0 comments on commit b5eb1c6

Please sign in to comment.