@@ -41,13 +41,18 @@ class FixedConstraintWithValue(om.FixedConstraint):
4141 """Value to enforce on the parameter."""
4242
4343 def __post_init__ (self ) -> None :
44- """Validate that `loc` and `value` are not None."""
44+ """Validate that `loc` and `value` are not None and derive `selector` ."""
4545 if self .loc is None :
4646 msg = "loc must not be None"
4747 raise TypeError (msg )
4848 if self .value is None :
4949 msg = "value must not be None"
5050 raise TypeError (msg )
51+ object .__setattr__ (
52+ self ,
53+ "selector" ,
54+ functools .partial (select_by_loc , loc = self .loc ),
55+ )
5156
5257
5358def get_constraints (
@@ -178,22 +183,10 @@ def _get_normalization_constraints(
178183 for period in periods :
179184 for meas , normval in normalizations [factor ].loadings [period ].items ():
180185 loc = ("loadings" , period , meas , factor )
181- constraints .append (
182- FixedConstraintWithValue (
183- selector = functools .partial (select_by_loc , loc = loc ),
184- loc = loc ,
185- value = normval ,
186- )
187- )
186+ constraints .append (FixedConstraintWithValue (loc = loc , value = normval ))
188187 for meas , normval in normalizations [factor ].intercepts [period ].items ():
189188 loc = ("controls" , period , meas , "constant" )
190- constraints .append (
191- FixedConstraintWithValue (
192- selector = functools .partial (select_by_loc , loc = loc ),
193- loc = loc ,
194- value = normval ,
195- )
196- )
189+ constraints .append (FixedConstraintWithValue (loc = loc , value = normval ))
197190
198191 return constraints
199192
@@ -205,11 +198,7 @@ def _get_mixture_weights_constraints(
205198 loc = "mixture_weights"
206199 if n_mixtures == 1 :
207200 return [
208- FixedConstraintWithValue (
209- selector = functools .partial (select_by_loc , loc = loc ),
210- loc = loc ,
211- value = 1.0 ,
212- ),
201+ FixedConstraintWithValue (loc = loc , value = 1.0 ),
213202 ]
214203 return [
215204 om .ProbabilityConstraint (selector = functools .partial (select_by_loc , loc = loc ))
@@ -277,11 +266,7 @@ def _get_constant_factors_constraints(
277266 for aug_period in labels .aug_periods [:- 1 ]:
278267 loc = ("shock_sds" , aug_period , factor , "-" )
279268 constraints .append (
280- FixedConstraintWithValue (
281- selector = functools .partial (select_by_loc , loc = loc ),
282- loc = loc ,
283- value = 0.0 ,
284- ),
269+ FixedConstraintWithValue (loc = loc , value = 0.0 ),
285270 )
286271 return constraints
287272
@@ -371,11 +356,7 @@ def _get_anchoring_constraints( # noqa: C901
371356 if locs :
372357 loc = tuple (locs )
373358 constraints .append (
374- FixedConstraintWithValue (
375- selector = functools .partial (select_by_loc , loc = loc ),
376- loc = loc ,
377- value = 0 ,
378- ),
359+ FixedConstraintWithValue (loc = loc , value = 0 ),
379360 )
380361
381362 if not anchoring_info .free_controls :
@@ -386,11 +367,7 @@ def _get_anchoring_constraints( # noqa: C901
386367 if ind_tups :
387368 loc = tuple (ind_tups )
388369 constraints .append (
389- FixedConstraintWithValue (
390- selector = functools .partial (select_by_loc , loc = loc ),
391- loc = loc ,
392- value = 0 ,
393- ),
370+ FixedConstraintWithValue (loc = loc , value = 0 ),
394371 )
395372
396373 if not anchoring_info .free_loadings :
@@ -404,11 +381,7 @@ def _get_anchoring_constraints( # noqa: C901
404381 if ind_tups :
405382 loc = tuple (ind_tups )
406383 constraints .append (
407- FixedConstraintWithValue (
408- selector = functools .partial (select_by_loc , loc = loc ),
409- loc = loc ,
410- value = 1 ,
411- ),
384+ FixedConstraintWithValue (loc = loc , value = 1 ),
412385 )
413386
414387 return constraints
@@ -467,7 +440,6 @@ def _get_constraints_for_augmented_periods(
467440 loc = ("shock_sds" , aug_period , factor , "-" )
468441 constraints .append (
469442 FixedConstraintWithValue (
470- selector = functools .partial (select_by_loc , loc = loc ),
471443 loc = loc ,
472444 value = endogenous_factors_info .bounds_distance ,
473445 )
0 commit comments