SearchLibrium 0.0.271__tar.gz → 0.0.272__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {searchlibrium-0.0.271/src/SearchLibrium.egg-info → searchlibrium-0.0.272}/PKG-INFO +1 -1
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/pyproject.toml +1 -1
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/__init__.py +2 -2
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/larch_compat.py +144 -1
- searchlibrium-0.0.272/src/SearchLibrium/version.txt +1 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272/src/SearchLibrium.egg-info}/PKG-INFO +1 -1
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium.egg-info/SOURCES.txt +1 -0
- searchlibrium-0.0.272/tests/test_larch_compat.py +155 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/tests/test_larch_integration.py +2 -1
- searchlibrium-0.0.271/src/SearchLibrium/version.txt +0 -1
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/LICENSE +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/MANIFEST.in +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/README.md +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/setup.cfg +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/BEHier.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/Halton.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/MixedLogit.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/MixedLogitGSE.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/Mode_Activity_Nested.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/RandomP.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/SEARCH_SM_MARIO.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/Two_Level_Nest.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/__main__.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/_choice_model.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/_device.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/abm.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/accessibility.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/banditsa.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/boxcox_functions.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/call_meta.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/constraints_builder.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/coupled_solver.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/data/Swissmetro_final.csv +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/data/TravelMode.csv +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/data/electricity.csv +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/destination_distances.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/harmony.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/hspbil.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/jax_utils.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/larch_models.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/latent_class.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/latent_spec.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/lc_rail_guard.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/logistic_regression.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/main.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/main_debug.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/mdcev.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/mh_choice_estimator.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/misc.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/mixed_logit.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/mixed_nested.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/mixedrrm.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/multinomial_logit.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/multinomial_nested.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/multinomial_probit.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/numba_engine.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/numba_mxl.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/numba_rrm.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/ordered_logit.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/ordered_logit_mixed.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/predict.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/regularization.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/rrm.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/sample_data.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/sapbil.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/search.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/selection_models.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/setup.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/siman.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/skims.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/sparseea_agds.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/test_lc_de.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/test_mario_searches.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/test_sapbil_vs_banditsa.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/threshold.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/zero_inflated_ordered_probit.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium.egg-info/dependency_links.txt +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium.egg-info/entry_points.txt +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium.egg-info/requires.txt +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium.egg-info/top_level.txt +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/tests/test_destination_distances.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/tests/test_jax_mask_equivalence.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/tests/test_lc_smart_init.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/tests/test_mh_choice_estimator.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/tests/test_mxl_random_starts.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/tests/test_per_alternative_random.py +0 -0
- {searchlibrium-0.0.271 → searchlibrium-0.0.272}/tests/test_zero_inflated_ordered_probit.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: SearchLibrium
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.272
|
|
4
4
|
Summary: Automated specification search for discrete choice models (logit, mixed logit, nested logit, RRM and more) using metaheuristic optimisation
|
|
5
5
|
Author: Zeke Ahern, Mario Fernando Taco Morales, Alexander Paz, Prithvi Beeramole, Robert Burdett
|
|
6
6
|
Maintainer-email: Zeke Ahern <z.ahern@qut.edu.au>
|
|
@@ -132,7 +132,7 @@ Documentation = "https://pypi.org/project/SearchLibrium/"
|
|
|
132
132
|
realpython = "SearchLibrium.__main__:main"
|
|
133
133
|
|
|
134
134
|
[tool.bumpver]
|
|
135
|
-
current_version = "0.0.
|
|
135
|
+
current_version = "0.0.272"
|
|
136
136
|
version_pattern = "MAJOR.MINOR.PATCH"
|
|
137
137
|
commit_message = "[skip ci] Bump version {old_version} -> {new_version}"
|
|
138
138
|
commit = true
|
|
@@ -195,7 +195,7 @@ _IMPORT_GROUPS = [
|
|
|
195
195
|
'estimate_row_guard', 'check_gradient_health']),
|
|
196
196
|
('larch_compat', ['apply_larch_patches']),
|
|
197
197
|
('larch_models', ['LarchMNL', 'LarchNestedLogit', 'LarchMixedLogit',
|
|
198
|
-
'build_latent_class']),
|
|
198
|
+
'LarchLatentClass', 'build_latent_class']),
|
|
199
199
|
('skims', ['omx_to_dataframe', 'read_omx_skims', 'prepare_skims_from_omx',
|
|
200
200
|
'merge_skim_component']),
|
|
201
201
|
('destination_distances', ['haversine_km', 'pick_skim_matrices',
|
|
@@ -254,7 +254,7 @@ __all__ = [
|
|
|
254
254
|
"elasticnet_objective", "elasticnet_penalty_and_grad", "nl_identification_priors",
|
|
255
255
|
"ensure_jax_environment", "is_large_model", "estimate_row_guard", "check_gradient_health",
|
|
256
256
|
"apply_larch_patches", "LarchMNL", "LarchNestedLogit", "LarchMixedLogit",
|
|
257
|
-
"build_latent_class",
|
|
257
|
+
"LarchLatentClass", "build_latent_class",
|
|
258
258
|
"omx_to_dataframe", "read_omx_skims", "prepare_skims_from_omx", "merge_skim_component",
|
|
259
259
|
"haversine_km", "pick_skim_matrices", "load_skim_matrices",
|
|
260
260
|
"build_case_alt_distances", "summarize_distance_sources",
|
|
@@ -25,9 +25,30 @@ What it fixes (see original module docstring for the full story):
|
|
|
25
25
|
4. **``pf`` inference sync** — writes ridge-aware ``std_err`` / ``t_stat``
|
|
26
26
|
(``value / se``, not the inverted historic form) / ``p_value`` into the
|
|
27
27
|
parameter frame, served through a ``pf`` property shim.
|
|
28
|
+
5. **Numba engine graph-None rebuild guards** (promoted 2026-11-11;
|
|
29
|
+
HPC-validated in probe jobs ``lc_graphfix_probe`` / ``lc_classdt_probe``).
|
|
30
|
+
Larch 6.0.x rebuilds fixed/work arrays from graph slots on every
|
|
31
|
+
unmangle/reflow; a graph-less (datatreeless) sub-model — e.g. the class
|
|
32
|
+
membership model whenever upstream wiring skips it — died with
|
|
33
|
+
``AttributeError "'NoneType' object has no attribute
|
|
34
|
+
'node_slot_arrays'"`` the moment anyone reassigns or reads its dataset.
|
|
35
|
+
Both rebuilds now no-op when ``graph is None``.
|
|
36
|
+
6. **LatentClass membership datatree wiring for tree inputs** (promoted
|
|
37
|
+
2026-11-11; same probe lineage). Stock larch auto-wires the class
|
|
38
|
+
membership model's datatree/graph ONLY when ``datatree`` is a Dataset;
|
|
39
|
+
every tree-shaped input (exactly what the production Stage-5 pipeline
|
|
40
|
+
passes) took no branch, so the membership model stayed datatreeless and
|
|
41
|
+
the first ``loglike()`` died inside ``LatentClass.jax_probability``.
|
|
42
|
+
The patch mirrors the Dataset-branch wiring for non-Dataset inputs by
|
|
43
|
+
handing the membership model a minimal flat class dataset (case × K
|
|
44
|
+
altids). Canary-gated: deactivates itself once upstream larch wires
|
|
45
|
+
membership for DataTree inputs natively.
|
|
28
46
|
|
|
29
47
|
Idempotent and a safe no-op when larch is not installed or its internals
|
|
30
|
-
differ (every patch site is guarded by ``hasattr``).
|
|
48
|
+
differ (every patch site is guarded by ``hasattr``). Patches 5 and 6 share
|
|
49
|
+
the marker attributes ``_graph_none_guard`` / ``_qut_membership_wiring``
|
|
50
|
+
with the tour pipeline's own ``larch_patches.py``, so applying either copy
|
|
51
|
+
first makes the other a no-op (no double wrapping).
|
|
31
52
|
"""
|
|
32
53
|
|
|
33
54
|
from __future__ import annotations
|
|
@@ -272,6 +293,128 @@ def apply_larch_patches() -> bool:
|
|
|
272
293
|
if _cls is not None:
|
|
273
294
|
_install_pf_shim(_cls)
|
|
274
295
|
|
|
296
|
+
# ── numba engine: graph-None rebuild guards ──────────────────────
|
|
297
|
+
# Larch 6.0.x rebuilds fixed/work arrays from graph slots on every
|
|
298
|
+
# unmangle/reflow; a graph-less (datatreeless) sub-model therefore dies
|
|
299
|
+
# with AttributeError "'NoneType' object has no attribute
|
|
300
|
+
# 'node_slot_arrays'" the moment anyone reassigns or reads its dataset
|
|
301
|
+
# (first surfaced as the stage-5 LCCM arm's anonymous -1e9 GA sentinel).
|
|
302
|
+
# Guard both rebuilds to a no-op when graph is None: a graphless model has
|
|
303
|
+
# no slots to rebuild FROM, and its evaluation path (flow folds) does not
|
|
304
|
+
# take them from there. Idempotent via the _graph_none_guard marker,
|
|
305
|
+
# shared with the tour pipeline's larch_patches.py.
|
|
306
|
+
try:
|
|
307
|
+
from larch.model import numbamodel as _nbm_g
|
|
308
|
+
except Exception as _e_g: # noqa: BLE001
|
|
309
|
+
_nbm_g = None
|
|
310
|
+
logger.debug("larch_compat: numbamodel unavailable (%s)", _e_g)
|
|
311
|
+
if _nbm_g is not None:
|
|
312
|
+
for _gname in ("_rebuild_fixed_arrays", "_rebuild_work_arrays"):
|
|
313
|
+
_gorig = getattr(_nbm_g.NumbaModel, _gname, None)
|
|
314
|
+
if _gorig is None or getattr(_gorig, "_graph_none_guard", False):
|
|
315
|
+
continue
|
|
316
|
+
|
|
317
|
+
def _graph_guarded(orig, _nm):
|
|
318
|
+
def _impl(self):
|
|
319
|
+
if getattr(self, "graph", None) is None:
|
|
320
|
+
logger.debug(
|
|
321
|
+
"larch_compat: %s skipped for graphless %s",
|
|
322
|
+
_nm, type(self).__name__)
|
|
323
|
+
return
|
|
324
|
+
return orig(self)
|
|
325
|
+
_impl._graph_none_guard = True
|
|
326
|
+
return _impl
|
|
327
|
+
|
|
328
|
+
setattr(_nbm_g.NumbaModel, _gname, _graph_guarded(_gorig, _gname))
|
|
329
|
+
logger.info("larch_compat: NumbaModel.%s → graph-None guard", _gname)
|
|
330
|
+
|
|
331
|
+
# ── LatentClass: membership-model datatree wiring for tree inputs ──
|
|
332
|
+
# Larch 6.0.x LatentClass.__init__ auto-wires the class-membership
|
|
333
|
+
# model's datatree/graph ONLY when ``datatree`` is a Dataset (the
|
|
334
|
+
# no-groupid ``set_altids`` branch). Every tree-shaped input — exactly
|
|
335
|
+
# what the production Stage-5 pipeline passes — takes NO branch, so the
|
|
336
|
+
# membership model stays datatreeless: graph None, _data_arrays None,
|
|
337
|
+
# and the first loglike() dies inside LatentClass.jax_probability with
|
|
338
|
+
# "'NoneType' object has no attribute 'av'" (pre-guard form:
|
|
339
|
+
# 'node_slot_arrays'; validated 2026-11-10 via HPC probes
|
|
340
|
+
# lc_graphfix_probe / lc_classdt_probe). Reference arms converge only
|
|
341
|
+
# because they pass a flat Dataset.
|
|
342
|
+
#
|
|
343
|
+
# The patch mirrors the no-groupid wiring for non-Dataset inputs by
|
|
344
|
+
# handing the membership model a minimal flat class dataset
|
|
345
|
+
# (case × K altids) — the intercept-only membership used by the
|
|
346
|
+
# pipeline needs nothing more, larch builds the membership graph from
|
|
347
|
+
# these altids, and reflow overwrites classmodel.dataset with the
|
|
348
|
+
# derived class data anyway. Covariate plumbing stays the caller's
|
|
349
|
+
# business, as in stock larch. Canary-gated: deactivates itself once
|
|
350
|
+
# upstream larch wires membership for DataTree inputs natively.
|
|
351
|
+
def _lc_membership_needs_wiring():
|
|
352
|
+
import pandas as _pd_lc
|
|
353
|
+
from larch.dataset import DataTree as _DT, Dataset as _DS
|
|
354
|
+
try:
|
|
355
|
+
_d = _pd_lc.DataFrame(
|
|
356
|
+
{"case": ["a", "a", "b", "b"], "alt": [1, 2, 1, 2],
|
|
357
|
+
"ch": [1.0, 0.0, 0.0, 1.0]}
|
|
358
|
+
).set_index(["case", "alt"])
|
|
359
|
+
dto = _DS.construct.from_idca(_d, fill_missing=0.0)
|
|
360
|
+
_cm = larch.Model() # membership model
|
|
361
|
+
_ch = larch.Model() # single class
|
|
362
|
+
_ch.utility_co[2] = larch.P("g") * 1.0
|
|
363
|
+
_lc = larch.LatentClass(_cm, {1: _ch}, datatree=_DT(main=dto))
|
|
364
|
+
return getattr(_lc.class_membership_model, "datatree",
|
|
365
|
+
None) is None
|
|
366
|
+
except Exception as _e_c: # noqa: BLE001
|
|
367
|
+
logger.debug("larch_compat: LC membership canary failed (%r) "
|
|
368
|
+
"— assuming wiring needed", _e_c)
|
|
369
|
+
return True
|
|
370
|
+
|
|
371
|
+
if _lc_membership_needs_wiring():
|
|
372
|
+
from larch.dataset import DataTree as _DT_w, Dataset as _DS_w
|
|
373
|
+
_LC_cls = larch.LatentClass
|
|
374
|
+
_lc_orig_init = _LC_cls.__init__
|
|
375
|
+
if not getattr(_lc_orig_init, "_qut_membership_wiring", False):
|
|
376
|
+
|
|
377
|
+
def _lc_wired_init(self, classmodel, choicemodels,
|
|
378
|
+
datatree=None, **kw):
|
|
379
|
+
try:
|
|
380
|
+
if (classmodel.datatree is None and datatree is not None
|
|
381
|
+
and not isinstance(datatree, _DS_w)):
|
|
382
|
+
_keys = sorted(choicemodels.keys())
|
|
383
|
+
_rid = datatree.root_dataset
|
|
384
|
+
_dims = list(_rid.sizes)
|
|
385
|
+
_cdim = next(
|
|
386
|
+
(d for d in _dims
|
|
387
|
+
if "caseid" in str(d).lower()
|
|
388
|
+
or str(d).startswith("_case")),
|
|
389
|
+
None)
|
|
390
|
+
if _cdim is None:
|
|
391
|
+
_cdim = max(_dims,
|
|
392
|
+
key=lambda d: int(_rid.sizes[d]))
|
|
393
|
+
_nc = int(_rid.sizes[_cdim])
|
|
394
|
+
import pandas as _pd_lc2
|
|
395
|
+
_ca = _pd_lc2.DataFrame({
|
|
396
|
+
"case_id": np.repeat(np.arange(_nc), len(_keys)),
|
|
397
|
+
"_altid_": np.tile(
|
|
398
|
+
np.asarray(_keys, dtype=np.int64), _nc),
|
|
399
|
+
}).set_index(["case_id", "_altid_"])
|
|
400
|
+
classmodel.datatree = _DS_w.construct.from_idca(
|
|
401
|
+
_ca, fill_missing=0.0)
|
|
402
|
+
logger.info(
|
|
403
|
+
"larch_compat: LatentClass membership datatree "
|
|
404
|
+
"wired for %s input (%d classes)",
|
|
405
|
+
type(datatree).__name__, len(_keys))
|
|
406
|
+
except Exception as _e_mw: # noqa: BLE001
|
|
407
|
+
logger.debug(
|
|
408
|
+
"larch_compat: membership wiring skipped (%r)",
|
|
409
|
+
_e_mw)
|
|
410
|
+
return _lc_orig_init(self, classmodel, choicemodels,
|
|
411
|
+
datatree=datatree, **kw)
|
|
412
|
+
|
|
413
|
+
_lc_wired_init._qut_membership_wiring = True
|
|
414
|
+
_LC_cls.__init__ = _lc_wired_init
|
|
415
|
+
logger.info("larch_compat: LatentClass.__init__ → membership "
|
|
416
|
+
"datatree wiring for tree inputs")
|
|
417
|
+
|
|
275
418
|
logger.info("larch_compat: applied")
|
|
276
419
|
return True
|
|
277
420
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.0.272
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: SearchLibrium
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.272
|
|
4
4
|
Summary: Automated specification search for discrete choice models (logit, mixed logit, nested logit, RRM and more) using metaheuristic optimisation
|
|
5
5
|
Author: Zeke Ahern, Mario Fernando Taco Morales, Alexander Paz, Prithvi Beeramole, Robert Burdett
|
|
6
6
|
Maintainer-email: Zeke Ahern <z.ahern@qut.edu.au>
|
|
@@ -75,6 +75,7 @@ src/SearchLibrium/data/TravelMode.csv
|
|
|
75
75
|
src/SearchLibrium/data/electricity.csv
|
|
76
76
|
tests/test_destination_distances.py
|
|
77
77
|
tests/test_jax_mask_equivalence.py
|
|
78
|
+
tests/test_larch_compat.py
|
|
78
79
|
tests/test_larch_integration.py
|
|
79
80
|
tests/test_lc_smart_init.py
|
|
80
81
|
tests/test_mh_choice_estimator.py
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Regression tests for the promoted larch compatibility patches.
|
|
2
|
+
|
|
3
|
+
Covers the two LCCM fixes tracked in GitHub issue #1:
|
|
4
|
+
|
|
5
|
+
1. graph-None guards on ``NumbaModel._rebuild_fixed_arrays`` /
|
|
6
|
+
``_rebuild_work_arrays``;
|
|
7
|
+
2. ``LatentClass.__init__`` membership datatree wiring for ``DataTree``
|
|
8
|
+
inputs, including the cross-copy marker contract with the tour
|
|
9
|
+
pipeline's ``larch_patches.py`` (stand-in: a second copy of this module).
|
|
10
|
+
"""
|
|
11
|
+
import importlib.util
|
|
12
|
+
import os
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
import textwrap
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
import pandas as pd
|
|
20
|
+
import pytest
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _larch_importable():
|
|
24
|
+
return importlib.util.find_spec("larch") is not None
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
pytestmark = pytest.mark.skipif(
|
|
28
|
+
not _larch_importable(), reason="larch not installed")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _apply():
|
|
32
|
+
from SearchLibrium.larch_compat import apply_larch_patches
|
|
33
|
+
assert apply_larch_patches() is True
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _toy_datatree(n=80, alts=(1, 2), seed=0):
|
|
37
|
+
"""Return an idca Dataset wrapped in a single-node DataTree."""
|
|
38
|
+
import larch as lx
|
|
39
|
+
|
|
40
|
+
rng = np.random.default_rng(seed)
|
|
41
|
+
rows = []
|
|
42
|
+
for i in range(n):
|
|
43
|
+
x = rng.normal(size=len(alts))
|
|
44
|
+
chosen = int(np.argmax(x + rng.gumbel(size=len(alts))))
|
|
45
|
+
for j, alt in enumerate(alts):
|
|
46
|
+
rows.append((i, alt, x[j], 1 if j == chosen else 0))
|
|
47
|
+
frame = pd.DataFrame(
|
|
48
|
+
rows, columns=["case", "alt", "x", "ch"]
|
|
49
|
+
).set_index(["case", "alt"])
|
|
50
|
+
ds = lx.Dataset.construct.from_idca(frame)
|
|
51
|
+
return lx.DataTree(main=ds), ds
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_larch_latent_class_is_public():
|
|
55
|
+
"""The first-class LCCM wrapper is exported at package level."""
|
|
56
|
+
from SearchLibrium import LarchLatentClass
|
|
57
|
+
from SearchLibrium.larch_models import LarchLatentClass as Direct
|
|
58
|
+
|
|
59
|
+
assert LarchLatentClass is Direct
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_graph_none_rebuild_guards_installed():
|
|
63
|
+
_apply()
|
|
64
|
+
from larch.model.numbamodel import NumbaModel
|
|
65
|
+
|
|
66
|
+
for name in ("_rebuild_fixed_arrays", "_rebuild_work_arrays"):
|
|
67
|
+
assert getattr(getattr(NumbaModel, name), "_graph_none_guard", False)
|
|
68
|
+
# A graphless model must survive a rebuild call instead of raising
|
|
69
|
+
# "'NoneType' object has no attribute 'node_slot_arrays'".
|
|
70
|
+
import larch as lx
|
|
71
|
+
|
|
72
|
+
model = lx.Model()
|
|
73
|
+
assert getattr(model, "graph", None) is None
|
|
74
|
+
model._rebuild_fixed_arrays()
|
|
75
|
+
model._rebuild_work_arrays()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_latent_class_membership_wired_for_datatree_input():
|
|
79
|
+
"""Issue #1 acceptance test (a) + (b)."""
|
|
80
|
+
_apply()
|
|
81
|
+
import larch as lx
|
|
82
|
+
from larch import P
|
|
83
|
+
|
|
84
|
+
tree, ds = _toy_datatree()
|
|
85
|
+
class_models = {}
|
|
86
|
+
for k, name in ((1, "c1"), (2, "c2")):
|
|
87
|
+
cm = lx.Model(ds)
|
|
88
|
+
cm.choice_ca_var = "ch"
|
|
89
|
+
cm.utility_co[2] = P(name)
|
|
90
|
+
class_models[k] = cm
|
|
91
|
+
membership = lx.Model()
|
|
92
|
+
membership.utility_co[2] = P("g")
|
|
93
|
+
|
|
94
|
+
latent = lx.LatentClass(
|
|
95
|
+
membership, class_models, datatree=tree)
|
|
96
|
+
|
|
97
|
+
assert latent.class_membership_model.datatree is not None
|
|
98
|
+
ll0 = float(latent.loglike())
|
|
99
|
+
assert np.isfinite(ll0)
|
|
100
|
+
|
|
101
|
+
# Parameters must actually move the likelihood (no frozen/datatreeless
|
|
102
|
+
# membership path).
|
|
103
|
+
latent.pvals = np.asarray(latent.pvals, dtype=float) + 0.3
|
|
104
|
+
ll1 = float(latent.loglike())
|
|
105
|
+
assert np.isfinite(ll1)
|
|
106
|
+
assert abs(ll1 - ll0) > 1e-6
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_apply_is_idempotent_for_latent_class_init():
|
|
110
|
+
_apply()
|
|
111
|
+
import larch as lx
|
|
112
|
+
|
|
113
|
+
init_before = lx.LatentClass.__init__
|
|
114
|
+
_apply()
|
|
115
|
+
assert lx.LatentClass.__init__ is init_before
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_marker_contract_with_second_patch_copy():
|
|
119
|
+
"""Issue #1 acceptance test (c): a second copy must not double-wrap.
|
|
120
|
+
|
|
121
|
+
The tour pipeline's ``larch_patches.py`` shares the
|
|
122
|
+
``_graph_none_guard`` / ``_qut_membership_wiring`` markers. A second
|
|
123
|
+
copy of this module is a faithful stand-in: load it under the
|
|
124
|
+
SearchLibrium package so its relative imports resolve, apply it, then
|
|
125
|
+
apply the package copy in the same fresh interpreter.
|
|
126
|
+
"""
|
|
127
|
+
repo = Path(__file__).resolve().parents[1]
|
|
128
|
+
src = str(repo / "src")
|
|
129
|
+
code = textwrap.dedent(
|
|
130
|
+
"""
|
|
131
|
+
import importlib.util
|
|
132
|
+
import larch as lx
|
|
133
|
+
from SearchLibrium import larch_compat as slc
|
|
134
|
+
|
|
135
|
+
spec = importlib.util.spec_from_file_location(
|
|
136
|
+
"SearchLibrium._larch_compat_second_copy", slc.__file__)
|
|
137
|
+
second = importlib.util.module_from_spec(spec)
|
|
138
|
+
spec.loader.exec_module(second)
|
|
139
|
+
assert second.apply_larch_patches() is True
|
|
140
|
+
|
|
141
|
+
init_before = lx.LatentClass.__init__
|
|
142
|
+
assert slc.apply_larch_patches() is True
|
|
143
|
+
print("REVERSE_OK" if lx.LatentClass.__init__ is init_before
|
|
144
|
+
else "REVERSE_BAD")
|
|
145
|
+
"""
|
|
146
|
+
)
|
|
147
|
+
env = os.environ.copy()
|
|
148
|
+
env["JAX_PLATFORMS"] = "cpu"
|
|
149
|
+
env["PYTHONPATH"] = os.pathsep.join(
|
|
150
|
+
[src] + ([env["PYTHONPATH"]] if env.get("PYTHONPATH") else []))
|
|
151
|
+
proc = subprocess.run(
|
|
152
|
+
[sys.executable, "-c", code], capture_output=True, text=True,
|
|
153
|
+
env=env, timeout=300)
|
|
154
|
+
assert "REVERSE_OK" in proc.stdout, (
|
|
155
|
+
f"stdout={proc.stdout!r} stderr={proc.stderr[-2000:]!r}")
|
|
@@ -218,7 +218,8 @@ def test_stage_configs():
|
|
|
218
218
|
def test_registry_lists_larch_models():
|
|
219
219
|
from SearchLibrium.search import ModelRegistry
|
|
220
220
|
models = ModelRegistry().get_models()
|
|
221
|
-
assert {"larch_mnl", "larch_nested", "larch_mixed"
|
|
221
|
+
assert {"larch_mnl", "larch_nested", "larch_mixed",
|
|
222
|
+
"larch_latent_class"} <= set(models)
|
|
222
223
|
|
|
223
224
|
|
|
224
225
|
def test_parameters_accepts_larch_opts_quietly(capsys):
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
0.0.271
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/test_sapbil_vs_banditsa.py
RENAMED
|
File without changes
|
|
File without changes
|
{searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium/zero_inflated_ordered_probit.py
RENAMED
|
File without changes
|
{searchlibrium-0.0.271 → searchlibrium-0.0.272}/src/SearchLibrium.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|