Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed lists to tuples in ModelSpecProcessor #39

Merged
merged 3 commits into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion skillmodels/estimation/skill_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _restore_unestimated_quantities_args_dict(self, initial_quantities):
return r_args

def _update_args_dict(self, initial_quantities):
position_helper = self.update_info[self.factors].to_numpy().astype(bool)
position_helper = self.update_info[list(self.factors)].to_numpy().astype(bool)

u_args_list = []
k = 0
Expand Down
2 changes: 1 addition & 1 deletion skillmodels/pre_processing/data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def c_data(self):

for t in self.periods:
df = self.data[self.data["__period__"] == t]
arr = df[const_list + self.controls[t]].to_numpy()
arr = df[const_list + list(self.controls[t])].to_numpy()
c_data.append(arr)
return c_data

Expand Down
41 changes: 21 additions & 20 deletions skillmodels/pre_processing/model_spec_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
else:
self._timeinf = {}
self._facinf = model_dict["factor_specific"]
self.factors = sorted(list(self._facinf.keys()))
self.factors = tuple(sorted(list(self._facinf.keys())))
self.nfac = len(self.factors)
self.nsigma = 2 * self.nfac + 1

Expand Down Expand Up @@ -87,12 +87,12 @@ def _set_time_specific_attributes(self):
sm[-1] = sm[-2]
self.stagemap = sm

self.periods = list(range(self.nperiods))
self.stages = sorted(list(set(self.stagemap)))
self.periods = tuple(range(self.nperiods))
self.stages = tuple(sorted(set(self.stagemap)))
self.nstages = len(self.stages)
self.stage_length_list = [
self.stage_length_list = tuple(
list(self.stagemap[:-1]).count(s) for s in self.stages
]
)

assert len(self.stagemap) == self.nperiods, (
"You have to specify a list of length nperiods "
Expand Down Expand Up @@ -134,9 +134,9 @@ def _transition_equation_names(self):
The result is set as class attribute ``transition_names``.

"""
self.transition_names = [
self.transition_names = tuple(
self._facinf[f]["trans_eq"]["name"] for f in self.factors
]
)

def _transition_equation_included_factors(self):
"""Included factors and their position for each transition equation.
Expand All @@ -156,15 +156,15 @@ def _transition_equation_included_factors(self):
trans_inf = self._facinf[factor]["trans_eq"]
args_f = sorted(trans_inf["included_factors"])
pos_f = list(np.arange(self.nfac)[np.in1d(self.factors, args_f)])
included_factors.append(args_f)
included_positions.append(pos_f)
included_factors.append(tuple(args_f))
included_positions.append(tuple(pos_f))
assert len(included_factors) >= 1, (
"Each latent factor needs at least one included factor. This is "
"violated for {}".format(factor)
)

self.included_factors = included_factors
self.included_positions = included_positions
self.included_factors = tuple(included_factors)
self.included_positions = tuple(included_positions)

def _set_anchoring_attributes(self):
"""Set attributes related to anchoring and make some checks."""
Expand All @@ -177,16 +177,16 @@ def _set_anchoring_attributes(self):
"anchoring"
].items()
self.anchoring = True
self.anch_positions = [
self.anch_positions = tuple(
f for f in range(self.nfac) if self.factors[f] in self.anchored_factors
]
)
if self.anchoring_mode == "truly_anchor_latent_factors":
self.anchor_in_predict = True
else:
self.anchor_in_predict = False
else:
self.anchoring = False
self.anchored_factors = []
self.anchored_factors = ()
self.anchor_in_predict = False
self.anch_outcome = None

Expand Down Expand Up @@ -346,10 +346,10 @@ def _clean_controls_specification(self):
bad_missings = bad_missings | new_bad_missings

bad_missings_list.append(bad_missings)
controls.append(controls_t)
controls.append(tuple(controls_t))

self.bad_missings = bad_missings_list
self.controls = controls
self.bad_missings = tuple(bad_missings_list)
self.controls = tuple(controls)

def _check_anchoring_specification(self):
"""Consistency checks for the model specs related to anchoring."""
Expand Down Expand Up @@ -496,13 +496,14 @@ def _check_and_fill_normalization_specification(self):

def _nmeas_list(self):
info = self.update_info()
self.nmeas_list = []
nmeas_list = []
last_period = self.periods[-1]
for t in self.periods:
if t != last_period or self.anchoring is False:
self.nmeas_list.append(len(info.loc[t]))
nmeas_list.append(len(info.loc[t]))
else:
self.nmeas_list.append(len(info.loc[t]) - 1)
nmeas_list.append(len(info.loc[t]) - 1)
self.nmeas_list = tuple(nmeas_list)

def update_info(self):
"""A DataFrame with all relevant information on Kalman updates.
Expand Down
2 changes: 1 addition & 1 deletion skillmodels/pre_processing/params_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _delta_index_tuples(controls, update_info):
"""
ind_tups = []
for period, meas in update_info.index:
for cont in ["constant"] + controls[period]:
for cont in ["constant"] + list(controls[period]):
ind_tups.append(("delta", period, meas, cont))
return ind_tups

Expand Down
24 changes: 12 additions & 12 deletions skillmodels/tests/pre_processing/model_spec_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class TestTransitionEquationNames:
def setup(self):
self.factors = ["f1", "f2", "f3"]
self.factors = ("f1", "f2", "f3")
names = ["linear", "ces", "ar1"]
self._facinf = {
factor: {"trans_eq": {"name": name}}
Expand All @@ -22,12 +22,12 @@ def setup(self):

def test_transition_equation_names(self):
ModelSpecProcessor._transition_equation_names(self)
assert_equal(self.transition_names, ["linear", "ces", "ar1"])
assert_equal(self.transition_names, ("linear", "ces", "ar1"))


class TestTransitionEquationIncludedFactors:
def setup(self):
self.factors = ["f1", "f2"]
self.factors = ("f1", "f2")
self._facinf = {
factor: {"trans_eq": {"included_factors": []}} for factor in self.factors
}
Expand All @@ -37,11 +37,11 @@ def setup(self):

def test_transition_equation_included_factors(self):
ModelSpecProcessor._transition_equation_included_factors(self)
assert_equal(self.included_factors, [["f1", "f2"], ["f2"]])
assert_equal(self.included_factors, (("f1", "f2"), ("f2",)))

def test_transition_equation_included_factor_positions(self):
ModelSpecProcessor._transition_equation_included_factors(self)
assert_equal(self.included_positions, [[0, 1], [1]])
assert_equal(self.included_positions, ((0, 1), (1,)))


class TestVariableCheckMethods:
Expand Down Expand Up @@ -79,13 +79,13 @@ def test_has_variance_where_false_missing(self):

class TestCleanMesaurementSpecifications:
def setup(self):
self.periods = [0, 1]
self.periods = (0, 1)
inf = {"f1": {}, "f2": {}}
inf["f1"]["measurements"] = [["m1", "m2", "m3", "m4"]] * 2
inf["f2"]["measurements"] = [["m5", "m6", "m7", "m8"]] * 2
self._facinf = inf
self.factors = sorted(list(self._facinf.keys()))
self.transition_names = ["log_ces", "blubb"]
self.factors = tuple(sorted(list(self._facinf.keys())))
self.transition_names = ("log_ces", "blubb")

def test_clean_measuremnt_specifications_nothing_to_clean(self):
self._present = Mock(return_value=True)
Expand Down Expand Up @@ -132,26 +132,26 @@ def setup(self):

def test_clean_control_specs_nothing_to_clean(self):
ModelSpecProcessor._clean_controls_specification(self)
res = [["c1", "c2"], ["c1", "c2"]]
res = (("c1", "c2"), ("c1", "c2"))
assert_equal(self.controls, res)

def test_clean_control_specs_missing_variable(self):
self._present = Mock(side_effect=[True, False, True, True])
ModelSpecProcessor._clean_controls_specification(self)
res = [["c1"], ["c1", "c2"]]
res = (("c1",), ("c1", "c2"))
assert_equal(self.controls, res)

def test_clean_control_specs_missing_observations_drop_variable(self):
self.data.loc[2, "c2"] = np.nan
ModelSpecProcessor._clean_controls_specification(self)
res = [["c1"], ["c1", "c2"]]
res = (("c1",), ("c1", "c2"))
assert_equal(self.controls, res)

def test_clean_control_specs_missing_observation_drop_observation(self):
self.data.loc[2, "c2"] = np.nan
self.controls_with_missings = "drop_observations"
ModelSpecProcessor._clean_controls_specification(self)
res = [["c1", "c2"], ["c1", "c2"]]
res = (("c1", "c2"), ("c1", "c2"))
assert_equal(self.controls, res)

def test_clean_control_specs_missing_observations_error(self):
Expand Down