Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
env2 = EvalEnvironment([{"a": 11}], flags=test_flag)
assert env2.eval("a <> 0") == True
assert_raises(SyntaxError, env2.eval, "a != 0")
assert env2.subset(["a"]).flags == test_flag
assert env2.with_outer_namespace({"b": 10}).flags == test_flag
else:
test_flag = __future__.division.compiler_flag
assert test_flag & _ALL_FUTURE_FLAGS
env = EvalEnvironment([{"a": 11}], flags=0)
assert env.eval("a / 2") == 11 // 2 == 5
assert env.subset(["a"]).flags == 0
assert env.with_outer_namespace({"b": 10}).flags == 0
env2 = EvalEnvironment([{"a": 11}], flags=test_flag)
assert env2.eval("a / 2") == 11 * 1. / 2 != 5
env2.subset(["a"]).flags == test_flag
assert env2.with_outer_namespace({"b": 10}).flags == test_flag
])
assert col_indices.size > 0, "Could not find any matching columns!"
if coef_to_test is not None:
if len(factor_loc_totest) > 1:
raise ValueError("do not set coef_to_test if more than one factor_loc_totest is given")
samples = sample_description[factor_loc_totest].astype(type(coef_to_test)) == coef_to_test
one_cols = np.where(design_loc[samples][:, col_indices][0] == 1)
if one_cols.size == 0:
# there is no such column; modify design matrix to create one
design_loc[:, col_indices] = np.where(samples, 1, 0)
elif coef_to_test is not None:
# Directly select coefficients to test from design matrix (xarray):
# Check that coefficients to test are not dependent parameters if constraints are given:
# TODO: design_loc is sometimes xarray and sometimes patsy when it arrives here,
# should it not always be xarray?
if isinstance(design_loc, patsy.design_info.DesignMatrix):
col_indices = np.asarray([
design_loc.design_info.column_names.index(x)
for x in coef_to_test
])
else:
col_indices = np.asarray([
list(np.asarray(design_loc.coords['design_params'])).index(x)
for x in coef_to_test
])
if constraints_loc is not None:
dep_coef_indices = np.where(np.any(constraints_loc == -1, axis=0) == True)[0]
assert np.all([x not in dep_coef_indices for x in col_indices]), "cannot test dependent coefficient"
indep_coef_indices = np.where(np.any(constraints_loc == -1, axis=0) == False)[0]
## Fit GLM:
model = _fit(
def test_harmonic_transform():
x = np.arange(735688, 735688 + 100, 1)
design = patsy.dmatrix('0 + harm(x, 1)')
truth = np.vstack((np.cos(2 * np.pi / 365.25 * x),
np.sin(2 * np.pi / 365.25 * x))).T
np.testing.assert_equal(np.asarray(design), truth)
def raise_patsy_error(x):
raise PatsyError("WHEEEEEE")
formula = "raise_patsy_error(X) + Y"
def t_invalid(formula_like, data, depth, exc=PatsyError): # pragma: no cover
if isinstance(depth, int):
depth += 1
fs = [dmatrix, dmatrices]
if have_pandas:
fs += [dmatrix_pandas, dmatrices_pandas]
for f in fs:
try:
f(formula_like, data, depth)
except exc:
pass
else:
raise AssertionError
True,
[[1, 1], [1, 2], [1, 3]], ["Intercept", "x_in_env"])
t("~ x_in_env", {"x_in_env": [10, 20, 30]}, 0,
True,
[[1, 10], [1, 20], [1, 30]], ["Intercept", "x_in_env"])
# Trying to pull x_in_env out of our *caller* shouldn't work.
t_invalid("~ x_in_env", {}, 1, exc=(NameError, PatsyError))
# But then again it should, if called from one down on the stack:
def check_nested_call():
x_in_env = "asdf"
t("~ x_in_env", {}, 1,
True,
[[1, 1], [1, 2], [1, 3]], ["Intercept", "x_in_env"])
check_nested_call()
# passing in an explicit EvalEnvironment also works:
e = EvalEnvironment.capture(1)
t_invalid("~ x_in_env", {}, e, exc=(NameError, PatsyError))
e = EvalEnvironment.capture(0)
def check_nested_call_2():
x_in_env = "asdf"
t("~ x_in_env", {}, e,
True,
[[1, 1], [1, 2], [1, 3]], ["Intercept", "x_in_env"])
check_nested_call_2()
def make_termlist(*entries):
terms = []
for entry in entries:
terms.append(Term([LookupFactor(name) for name in entry]))
return terms
return_type="patsy"
)
design_scale, design_scale_names, constraints_scale, term_names_scale = constraint_system_from_star(
dmat=dmat_scale,
sample_description=sample_description,
formula=formula_scale,
as_numeric=as_numeric,
constraints=constraints_scale,
return_type="patsy"
)
# Define indices of coefficients to test:
constraints_loc_temp = constraints_loc if constraints_loc is not None else np.eye(design_loc.shape[-1])
# Check that design_loc is patsy, otherwise use term_names for slicing.
if factor_loc_totest is not None:
if not isinstance(design_loc, patsy.design_info.DesignMatrix):
col_indices = np.where([
x in factor_loc_totest
for x in term_names_loc
])[0]
else:
# Select coefficients to test via formula model:
col_indices = np.concatenate([
np.arange(design_loc.shape[-1])[design_loc.design_info.slice(x)]
for x in factor_loc_totest
])
assert len(col_indices) > 0, "Could not find any matching columns!"
if coef_to_test is not None:
if len(factor_loc_totest) > 1:
raise ValueError("do not set coef_to_test if more than one factor_loc_totest is given")
samples = sample_description[factor_loc_totest].astype(type(coef_to_test)) == coef_to_test
one_cols = np.where(design_loc[samples][:, col_indices][0] == 1)
def test_formula_likes():
# Plain array-like, rhs only
t([[1, 2, 3], [4, 5, 6]], {}, 0,
False,
[[1, 2, 3], [4, 5, 6]], ["x0", "x1", "x2"])
t((None, [[1, 2, 3], [4, 5, 6]]), {}, 0,
False,
[[1, 2, 3], [4, 5, 6]], ["x0", "x1", "x2"])
t(np.asarray([[1, 2, 3], [4, 5, 6]]), {}, 0,
False,
[[1, 2, 3], [4, 5, 6]], ["x0", "x1", "x2"])
t((None, np.asarray([[1, 2, 3], [4, 5, 6]])), {}, 0,
False,
[[1, 2, 3], [4, 5, 6]], ["x0", "x1", "x2"])
dm = DesignMatrix([[1, 2, 3], [4, 5, 6]], default_column_prefix="foo")
t(dm, {}, 0,
False,
[[1, 2, 3], [4, 5, 6]], ["foo0", "foo1", "foo2"])
t((None, dm), {}, 0,
False,
[[1, 2, 3], [4, 5, 6]], ["foo0", "foo1", "foo2"])
# Plain array-likes, lhs and rhs
t(([1, 2], [[1, 2, 3], [4, 5, 6]]), {}, 0,
False,
[[1, 2, 3], [4, 5, 6]], ["x0", "x1", "x2"],
[[1], [2]], ["y0"])
t(([[1], [2]], [[1, 2, 3], [4, 5, 6]]), {}, 0,
False,
[[1, 2, 3], [4, 5, 6]], ["x0", "x1", "x2"],
[[1], [2]], ["y0"])
def t(formula_like, data, depth,
expect_full_designs,
expected_rhs_values, expected_rhs_names,
expected_lhs_values=None, expected_lhs_names=None): # pragma: no cover
if isinstance(depth, int):
depth += 1
def data_iter_maker():
return iter([data])
if (isinstance(formula_like, six.string_types + (ModelDesc, DesignInfo))
or (isinstance(formula_like, tuple)
and isinstance(formula_like[0], DesignInfo))
or hasattr(formula_like, "__patsy_get_model_desc__")):
if expected_lhs_values is None:
builder = incr_dbuilder(formula_like, data_iter_maker, depth)
lhs = None
(rhs,) = build_design_matrices([builder], data)
else:
builders = incr_dbuilders(formula_like, data_iter_maker, depth)
lhs, rhs = build_design_matrices(builders, data)
check_result(expect_full_designs, lhs, rhs, data,
expected_rhs_values, expected_rhs_names,
expected_lhs_values, expected_lhs_names)
else:
assert_raises(PatsyError, incr_dbuilders,
formula_like, data_iter_maker)
assert_raises(PatsyError, incr_dbuilder,
formula_like, data_iter_maker)