Skip to content

Commit 0a5eeb6

Browse files
committed
access to global constants and removing chooser col filtering
1 parent c57e032 commit 0a5eeb6

File tree

9 files changed

+72
-129
lines changed

9 files changed

+72
-129
lines changed

activitysim/abm/models/location_choice.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def _location_sample(
161161
"reindex": reindex,
162162
"land_use": state.get_dataframe("land_use"),
163163
}
164+
locals_d.update(state.get_global_constants())
164165
locals_d.update(model_settings.CONSTANTS or {})
165166

166167
# preprocess choosers table
@@ -232,9 +233,7 @@ def location_sample(
232233
chunk_tag,
233234
trace_label,
234235
):
235-
# FIXME - MEMORY HACK - only include columns actually used in spec
236-
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
237-
choosers = persons_merged[chooser_columns]
236+
choosers = persons_merged
238237

239238
# create wrapper with keys for this lookup - in this case there is a home_zone_id in the choosers
240239
# and a zone_id in the alternatives which get merged during interaction
@@ -385,12 +384,7 @@ def location_presample(
385384
HOME_TAZ in persons_merged
386385
) # 'TAZ' should already be in persons_merged from land_use
387386

388-
# FIXME - MEMORY HACK - only include columns actually used in spec
389-
# FIXME we don't actually require that land_use provide a TAZ crosswalk
390-
# FIXME maybe we should add it for multi-zone (from maz_taz) if missing?
391-
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
392-
chooser_columns = [HOME_TAZ if c == HOME_MAZ else c for c in chooser_columns]
393-
choosers = persons_merged[chooser_columns]
387+
choosers = persons_merged
394388

395389
# create wrapper with keys for this lookup - in this case there is a HOME_TAZ in the choosers
396390
# and a DEST_TAZ in the alternatives which get merged during interaction
@@ -555,11 +549,6 @@ def run_location_logsums(
555549
mandatory=False,
556550
)
557551

558-
# FIXME - MEMORY HACK - only include columns actually used in spec
559-
persons_merged_df = logsum.filter_chooser_columns(
560-
persons_merged_df, logsum_settings, model_settings
561-
)
562-
563552
logger.info(f"Running {trace_label} with {len(location_sample_df.index)} rows")
564553

565554
choosers = location_sample_df.join(persons_merged_df, how="left")
@@ -618,9 +607,7 @@ def run_location_simulate(
618607
"""
619608
assert not persons_merged.empty
620609

621-
# FIXME - MEMORY HACK - only include columns actually used in spec
622-
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
623-
choosers = persons_merged[chooser_columns]
610+
choosers = persons_merged
624611

625612
alt_dest_col_name = model_settings.ALT_DEST_COL_NAME
626613

@@ -651,6 +638,7 @@ def run_location_simulate(
651638
"reindex": reindex,
652639
"land_use": state.get_dataframe("land_use"),
653640
}
641+
locals_d.update(state.get_global_constants())
654642
locals_d.update(model_settings.CONSTANTS or {})
655643

656644
# preprocess choosers table

activitysim/abm/models/school_escorting.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6+
import warnings
67
from typing import Any, Literal
78

89
import numpy as np
@@ -335,8 +336,6 @@ class SchoolEscortSettings(BaseLogitComponentSettings, extra="forbid"):
335336
GENDER_WEIGHT: float = 10.0
336337
AGE_WEIGHT: float = 1.0
337338

338-
SIMULATE_CHOOSER_COLUMNS: list[str] | None = None
339-
340339
SPEC: None = None
341340
"""The school escort model does not use this setting."""
342341

@@ -369,6 +368,21 @@ class SchoolEscortSettings(BaseLogitComponentSettings, extra="forbid"):
369368
Multinomial logit model.
370369
"""
371370

371+
@property
372+
def SIMULATE_CHOOSER_COLUMNS(self) -> None:
373+
"""Was used to help reduce the memory needed for the model.
374+
Setting is now obsolete and doesn't do anything.
375+
Functionality was replaced by util.drop_unused_columns
376+
377+
.. deprecated:: 1.4
378+
"""
379+
warnings.warn(
380+
"SIMULATE_CHOOSER_COLUMNS is deprecated and replaced by util.drop_unused_columns",
381+
DeprecationWarning,
382+
stacklevel=2,
383+
)
384+
return None
385+
372386

373387
@workflow.step
374388
def school_escorting(
@@ -469,12 +483,6 @@ def school_escorting(
469483
# else:
470484
# locals_dict.pop("_sharrow_skip", None)
471485

472-
# reduce memory by limiting columns if selected columns are supplied
473-
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
474-
if chooser_columns is not None:
475-
chooser_columns = chooser_columns + participant_columns
476-
choosers = choosers[chooser_columns]
477-
478486
# add previous data to stage
479487
if stage_num >= 1:
480488
choosers = add_prev_choices_to_choosers(

activitysim/abm/models/util/logsums.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,6 @@
1616
logger = logging.getLogger(__name__)
1717

1818

19-
def filter_chooser_columns(
20-
choosers, logsum_settings: dict | PydanticBase, model_settings: dict | PydanticBase
21-
):
22-
try:
23-
chooser_columns = logsum_settings.LOGSUM_CHOOSER_COLUMNS
24-
except AttributeError:
25-
chooser_columns = logsum_settings.get("LOGSUM_CHOOSER_COLUMNS", [])
26-
27-
if (
28-
isinstance(model_settings, dict)
29-
and "CHOOSER_ORIG_COL_NAME" in model_settings
30-
and model_settings["CHOOSER_ORIG_COL_NAME"] not in chooser_columns
31-
):
32-
chooser_columns.append(model_settings["CHOOSER_ORIG_COL_NAME"])
33-
if (
34-
isinstance(model_settings, PydanticBase)
35-
and hasattr(model_settings, "CHOOSER_ORIG_COL_NAME")
36-
and model_settings.CHOOSER_ORIG_COL_NAME
37-
and model_settings.CHOOSER_ORIG_COL_NAME not in chooser_columns
38-
):
39-
chooser_columns.append(model_settings.CHOOSER_ORIG_COL_NAME)
40-
41-
missing_columns = [c for c in chooser_columns if c not in choosers]
42-
if missing_columns:
43-
logger.debug(
44-
"logsum.filter_chooser_columns missing_columns %s" % missing_columns
45-
)
46-
47-
# ignore any columns not appearing in choosers df
48-
chooser_columns = [c for c in chooser_columns if c in choosers]
49-
50-
choosers = choosers[chooser_columns]
51-
return choosers
52-
53-
5419
def compute_location_choice_logsums(
5520
state: workflow.State,
5621
choosers: pd.DataFrame,

activitysim/abm/models/util/tour_destination.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def _destination_sample(
113113
"dest_col_name": skims.dest_key, # added for sharrow flows
114114
"timeframe": "timeless",
115115
}
116+
locals_d.update(state.get_global_constants())
116117
constants = model_settings.CONSTANTS
117118
if constants is not None:
118119
locals_d.update(constants)
@@ -619,18 +620,9 @@ def run_destination_sample(
619620
chunk_size,
620621
trace_label,
621622
):
622-
# FIXME - MEMORY HACK - only include columns actually used in spec (omit them pre-merge)
623-
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
624623

625624
# if special person id is passed
626625
chooser_id_column = model_settings.CHOOSER_ID_COLUMN
627-
628-
persons_merged = persons_merged[
629-
[c for c in persons_merged.columns if c in chooser_columns]
630-
]
631-
tours = tours[
632-
[c for c in tours.columns if c in chooser_columns or c == chooser_id_column]
633-
]
634626
choosers = pd.merge(
635627
tours, persons_merged, left_on=chooser_id_column, right_index=True, how="left"
636628
)
@@ -726,11 +718,6 @@ def run_destination_logsums(
726718

727719
chunk_tag = "tour_destination.logsums"
728720

729-
# FIXME - MEMORY HACK - only include columns actually used in spec
730-
persons_merged = logsum.filter_chooser_columns(
731-
persons_merged, logsum_settings, model_settings
732-
)
733-
734721
# merge persons into tours
735722
choosers = pd.merge(
736723
destination_sample,
@@ -793,18 +780,8 @@ def run_destination_simulate(
793780
coefficients_file_name=model_settings.COEFFICIENTS,
794781
)
795782

796-
# FIXME - MEMORY HACK - only include columns actually used in spec (omit them pre-merge)
797-
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
798-
799783
# if special person id is passed
800784
chooser_id_column = model_settings.CHOOSER_ID_COLUMN
801-
802-
persons_merged = persons_merged[
803-
[c for c in persons_merged.columns if c in chooser_columns]
804-
]
805-
tours = tours[
806-
[c for c in tours.columns if c in chooser_columns or c == chooser_id_column]
807-
]
808785
choosers = pd.merge(
809786
tours, persons_merged, left_on=chooser_id_column, right_index=True, how="left"
810787
)
@@ -846,6 +823,7 @@ def run_destination_simulate(
846823
"dest_col_name": skims.dest_key, # added for sharrow flows
847824
"timeframe": "timeless",
848825
}
826+
locals_d.update(state.get_global_constants())
849827
if constants is not None:
850828
locals_d.update(constants)
851829

activitysim/abm/models/util/tour_od.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def _od_sample(
178178
"orig_col_name": ORIG_TAZ,
179179
"dest_col_name": DEST_TAZ,
180180
}
181+
locals_d.update(state.get_global_constants())
181182
constants = model_settings.CONSTANTS
182183
if constants is not None:
183184
locals_d.update(constants)
@@ -690,9 +691,6 @@ def run_od_sample(
690691
)
691692

692693
choosers = tours
693-
# FIXME - MEMORY HACK - only include columns actually used in spec
694-
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
695-
choosers = choosers[chooser_columns]
696694

697695
# interaction_sample requires that choosers.index.is_monotonic_increasing
698696
if not choosers.index.is_monotonic_increasing:
@@ -769,11 +767,6 @@ def run_od_logsums(
769767
dest_id_col = model_settings.DEST_COL_NAME
770768
tour_od_id_col = get_od_id_col(origin_id_col, dest_id_col)
771769

772-
# FIXME - MEMORY HACK - only include columns actually used in spec
773-
tours_merged_df = logsum.filter_chooser_columns(
774-
tours_merged_df, logsum_settings, model_settings
775-
)
776-
777770
# merge ods into choosers table
778771
choosers = od_sample.join(tours_merged_df, how="left")
779772
choosers[tour_od_id_col] = (
@@ -949,10 +942,6 @@ def run_od_simulate(
949942
# merge persons into tours
950943
choosers = tours
951944

952-
# FIXME - MEMORY HACK - only include columns actually used in spec
953-
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
954-
choosers = choosers[chooser_columns]
955-
956945
# interaction_sample requires that choosers.index.is_monotonic_increasing
957946
if not choosers.index.is_monotonic_increasing:
958947
logger.debug(
@@ -1003,6 +992,7 @@ def run_od_simulate(
1003992
"orig_col_name": origin_col_name,
1004993
"dest_col_name": dest_col_name,
1005994
}
995+
locals_d.update(state.get_global_constants())
1006996
if constants is not None:
1007997
locals_d.update(constants)
1008998

activitysim/abm/models/util/tour_scheduling.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,6 @@ def run_tour_scheduling(
2424
trace_label: str,
2525
):
2626

27-
if model_settings.LOGSUM_SETTINGS:
28-
logsum_settings = TourModeComponentSettings.read_settings_file(
29-
state.filesystem,
30-
str(model_settings.LOGSUM_SETTINGS),
31-
mandatory=False,
32-
)
33-
logsum_columns = logsum_settings.LOGSUM_CHOOSER_COLUMNS
34-
else:
35-
logsum_columns = []
36-
37-
# - filter chooser columns for both logsums and simulate
38-
model_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
39-
chooser_columns = logsum_columns + [
40-
c for c in model_columns if c not in logsum_columns
41-
]
42-
43-
persons_merged = expressions.filter_chooser_columns(persons_merged, chooser_columns)
44-
4527
timetable = state.get_injectable("timetable")
4628

4729
# - run preprocessor to annotate choosers

activitysim/abm/models/util/vectorize_tour_scheduling.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6+
import warnings
67
from collections import OrderedDict
78
from pathlib import Path
89
from typing import Any
@@ -42,7 +43,6 @@ class TourSchedulingSettings(LogitComponentSettings, extra="forbid"):
4243
it is assumed to be an unsegmented preprocessor. Otherwise, the dict keys
4344
give the segements.
4445
"""
45-
SIMULATE_CHOOSER_COLUMNS: list[str] | None = None
4646

4747
SPEC_SEGMENTS: dict[str, LogitComponentSettings] = {}
4848

@@ -63,6 +63,21 @@ class TourSchedulingSettings(LogitComponentSettings, extra="forbid"):
6363
If less than 1, use this fraction of the total number of rows.
6464
"""
6565

66+
@property
67+
def SIMULATE_CHOOSER_COLUMNS(self) -> None:
68+
"""Was used to help reduce the memory needed for the model.
69+
Setting is now obsolete and doesn't do anything.
70+
Functionality was replaced by util.drop_unused_columns
71+
72+
.. deprecated:: 1.4
73+
"""
74+
warnings.warn(
75+
"SIMULATE_CHOOSER_COLUMNS is deprecated and replaced by util.drop_unused_columns",
76+
DeprecationWarning,
77+
stacklevel=2,
78+
)
79+
return None
80+
6681

6782
def skims_for_logsums(
6883
state: workflow.State,
@@ -822,6 +837,7 @@ def _schedule_tours(
822837

823838
# - make choices
824839
locals_d = {"tt": timetable.attach_state(state)}
840+
locals_d.update(state.get_global_constants())
825841
constants = config.get_model_constants(model_settings)
826842
if constants is not None:
827843
locals_d.update(constants)

activitysim/core/configuration/logit.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ class TourLocationComponentSettings(LocationComponentSettings, extra="forbid"):
261261
SEGMENT_IDS: dict[str, int] | dict[str, str] | dict[str, bool] | None = None
262262
SHADOW_PRICE_TABLE: str | None = None
263263
MODELED_SIZE_TABLE: str | None = None
264-
SIMULATE_CHOOSER_COLUMNS: list[str] | None = None
265264
ALT_DEST_COL_NAME: str
266265
LOGSUM_TOUR_PURPOSE: str | dict[str, str] | None = None
267266
MODEL_SELECTOR: str | None = None
@@ -271,6 +270,21 @@ class TourLocationComponentSettings(LocationComponentSettings, extra="forbid"):
271270
ORIG_ZONE_ID: str | None = None
272271
"""This setting appears to do nothing..."""
273272

273+
@property
274+
def SIMULATE_CHOOSER_COLUMNS(self) -> None:
275+
"""Was used to help reduce the memory needed for the model.
276+
Setting is now obsolete and doesn't do anything.
277+
Functionality was replaced by util.drop_unused_columns
278+
279+
.. deprecated:: 1.4
280+
"""
281+
warnings.warn(
282+
"SIMULATE_CHOOSER_COLUMNS is deprecated and replaced by util.drop_unused_columns",
283+
DeprecationWarning,
284+
stacklevel=2,
285+
)
286+
return None
287+
274288

275289
class TourModeComponentSettings(TemplatedLogitComponentSettings, extra="forbid"):
276290
MODE_CHOICE_LOGSUM_COLUMN_NAME: str | None = None
@@ -281,4 +295,18 @@ class TourModeComponentSettings(TemplatedLogitComponentSettings, extra="forbid")
281295
nontour_preprocessor: PreprocessorSettings | list[
282296
PreprocessorSettings
283297
] | None = None
284-
LOGSUM_CHOOSER_COLUMNS: list[str] = []
298+
299+
@property
300+
def LOGSUM_CHOOSER_COLUMNS(self) -> None:
301+
"""Was used to help reduce the memory needed for the model.
302+
Setting is now obsolete and doesn't do anything.
303+
Functionality was replaced by util.drop_unused_columns
304+
305+
.. deprecated:: 1.4
306+
"""
307+
warnings.warn(
308+
"LOGSUM_CHOOSER_COLUMNS is deprecated and replaced by util.drop_unused_columns",
309+
DeprecationWarning,
310+
stacklevel=2,
311+
)
312+
return None

0 commit comments

Comments
 (0)