Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_transform_feature():
name = 'dpsec'
data = np.array([1, 2, 3, 4])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_raises(ValueError, transform_feature, name, data, 'add_one_inverse')
assert_array_equal(transform_feature(name, data, 'inv'), 1/data)
assert_array_equal(transform_feature(name, data, 'raw'), data)
assert_array_equal(transform_feature(name, data, 'org'), data)
assert_array_equal(transform_feature(name, data, 'log'), np.log(data))
assert_array_equal(transform_feature(name, data, 'addOneInv'), 1/(data+1))
assert_array_equal(transform_feature(name, data, 'addOneLn'), np.log(data+1))
def test_transform_feature():
name = 'dpsec'
data = np.array([1, 2, 3, 4])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_raises(ValueError, transform_feature, name, data, 'add_one_inverse')
assert_array_equal(transform_feature(name, data, 'inv'), 1/data)
assert_array_equal(transform_feature(name, data, 'raw'), data)
assert_array_equal(transform_feature(name, data, 'org'), data)
assert_array_equal(transform_feature(name, data, 'log'), np.log(data))
assert_array_equal(transform_feature(name, data, 'addOneInv'), 1/(data+1))
assert_array_equal(transform_feature(name, data, 'addOneLn'), np.log(data+1))
def test_transform_feature_with_warning():
name = 'dpsec'
data = np.array([-1, 0, 2, 3])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_array_equal(transform_feature(name, data, 'sqrt', raise_error=False),
np.sqrt(data))
assert_array_equal(transform_feature(name, data, 'inv', raise_error=False),
1/data)
assert_array_equal(transform_feature(name, data, 'addOneInv', raise_error=False),
1/(data+1))
assert_array_equal(transform_feature(name, data, 'log', raise_error=False),
np.log(data))
assert_array_equal(transform_feature(name, data, 'addOneLn', raise_error=False),
np.log(data+1))
def test_transform_feature_with_warning():
name = 'dpsec'
data = np.array([-1, 0, 2, 3])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_array_equal(transform_feature(name, data, 'sqrt', raise_error=False),
np.sqrt(data))
assert_array_equal(transform_feature(name, data, 'inv', raise_error=False),
1/data)
assert_array_equal(transform_feature(name, data, 'addOneInv', raise_error=False),
1/(data+1))
assert_array_equal(transform_feature(name, data, 'log', raise_error=False),
np.log(data))
assert_array_equal(transform_feature(name, data, 'addOneLn', raise_error=False),
np.log(data+1))
def test_transform_feature():
name = 'dpsec'
data = np.array([1, 2, 3, 4])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_raises(ValueError, transform_feature, name, data, 'add_one_inverse')
assert_array_equal(transform_feature(name, data, 'inv'), 1/data)
assert_array_equal(transform_feature(name, data, 'raw'), data)
assert_array_equal(transform_feature(name, data, 'org'), data)
assert_array_equal(transform_feature(name, data, 'log'), np.log(data))
assert_array_equal(transform_feature(name, data, 'addOneInv'), 1/(data+1))
assert_array_equal(transform_feature(name, data, 'addOneLn'), np.log(data+1))
def test_transform_feature():
name = 'dpsec'
data = np.array([1, 2, 3, 4])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_raises(ValueError, transform_feature, name, data, 'add_one_inverse')
assert_array_equal(transform_feature(name, data, 'inv'), 1/data)
assert_array_equal(transform_feature(name, data, 'raw'), data)
assert_array_equal(transform_feature(name, data, 'org'), data)
assert_array_equal(transform_feature(name, data, 'log'), np.log(data))
assert_array_equal(transform_feature(name, data, 'addOneInv'), 1/(data+1))
assert_array_equal(transform_feature(name, data, 'addOneLn'), np.log(data+1))
def test_transform_feature_with_error():
name = 'dpsec'
data = np.array([-1, 0, 2, 3])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_raises(ValueError, transform_feature, name, data, 'sqrt')
assert_raises(ValueError, transform_feature, name, data, 'inv')
assert_raises(ValueError, transform_feature, name, data, 'addOneInv')
assert_raises(ValueError, transform_feature, name, data, 'log')
assert_raises(ValueError, transform_feature, name, data, 'addOneLn')
def test_transform_feature_with_warning():
name = 'dpsec'
data = np.array([-1, 0, 2, 3])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_array_equal(transform_feature(name, data, 'sqrt', raise_error=False),
np.sqrt(data))
assert_array_equal(transform_feature(name, data, 'inv', raise_error=False),
1/data)
assert_array_equal(transform_feature(name, data, 'addOneInv', raise_error=False),
1/(data+1))
assert_array_equal(transform_feature(name, data, 'log', raise_error=False),
np.log(data))
assert_array_equal(transform_feature(name, data, 'addOneLn', raise_error=False),
np.log(data+1))
def test_transform_feature():
name = 'dpsec'
data = np.array([1, 2, 3, 4])
# run the test but suppress the expected runtime warnings
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=RuntimeWarning)
assert_raises(ValueError, transform_feature, name, data, 'add_one_inverse')
assert_array_equal(transform_feature(name, data, 'inv'), 1/data)
assert_array_equal(transform_feature(name, data, 'raw'), data)
assert_array_equal(transform_feature(name, data, 'org'), data)
assert_array_equal(transform_feature(name, data, 'log'), np.log(data))
assert_array_equal(transform_feature(name, data, 'addOneInv'), 1/(data+1))
assert_array_equal(transform_feature(name, data, 'addOneLn'), np.log(data+1))
between the feature values and the human scores. See
:ref:`documentation ` for the
full list of transformations.
"""
# Do not use sqrt and ln for potential negative features.
# Do not use inv for positive features.
if any(feature_value < 0):
applicable_transformations = ['org', 'inv']
else:
applicable_transformations = ['org', 'sqrt', 'addOneInv', 'addOneLn']
correlations = []
for trans in applicable_transformations:
try:
transformed_value = transform_feature(feature_name, feature_value, trans)
correlations.append(abs(pearsonr(transformed_value, scores)[0]))
except ValueError:
# If the transformation returns an error, append 0.
correlations.append(0)
best = np.argmax(correlations)
best_transformation = applicable_transformations[best]
return best_transformation