Skip to content

Commit

Permalink
Adding differential sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekyadav26 committed Aug 29, 2023
1 parent f0135f3 commit 250bc6a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
6 changes: 5 additions & 1 deletion abm/src/main/python/dataExporter/abmScenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ def properties(self) -> dict:
"rsmSamplingRate":{
"line": "rsm.default.sampling.rate=",
"type" : "float",
"value": None},
"useDifferentialSampling":{
"line": "use.differential.sampling=",
"type" : "int",
"value": None}
}

Expand Down Expand Up @@ -2777,7 +2781,7 @@ def ie(self) -> pd.DataFrame:

study_area_file = os.path.join(self.scenario_path, "input", "study_area.csv")

if os.path.exists(study_area_file):
if useDifferentialSampling & os.path.exists(study_area_file):
df = pd.read_csv(study_area_file)
study_area_taz = set(df['taz'])
rsm_zone = set(rsm_zones.loc[rsm_zones['taz'].isin(study_area_taz), 'cluster_id'])
Expand Down
1 change: 1 addition & 0 deletions abm/src/main/resources/sandag_abm.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,7 @@ rsm.zones = 2000
external.zones = 12
run.rsm.sampling = 0
rsm.default.sampling.rate = 0.25
use.differential.sampling = 1
rsm.min.sampling.rate = 0.25
run.rsm.assembler = 0
rsm.centroid.connector.start.id = 55000
Expand Down
6 changes: 4 additions & 2 deletions rsm/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def rsm_assemble(
taz_crosswalk=None,
sample_rate=0.25,
study_area_taz=None,
run_assembler=1
run_assembler=1,
differential_sampling=1

This comment has been minimized.

Copy link
@AshishKuls

AshishKuls Aug 30, 2023

Collaborator

@vivekyadav26 Can you add the differential_sampling to docsting?

):
"""
Assemble and evaluate RSM trip making.
Expand Down Expand Up @@ -70,6 +71,7 @@ def rsm_assemble(
study_area_rsm_zones : list
it is list of study area RSM zones
Returns
-------
#final_trips_rsm : pd.DataFrame
Expand Down Expand Up @@ -239,7 +241,7 @@ def _agg_by_hhid_and_tripmode(df, name):
final_jnt_trips = scaleup_to_rsm_samplingrate(jnt_trips_rsm,
households,
taz_crosswalk,
scale_factor,
scale_factor,
study_area_tazs=sa_rsm)

return final_ind_trips, final_jnt_trips
2 changes: 1 addition & 1 deletion rsm/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def scaleup_to_rsm_samplingrate(df,
hh["taz"] = hh["taz"].map(dict_clusters)
hh['scale_factor'] = scale_factor

if study_area_tazs:
if study_area_tazs:
hh.loc[hh['taz'].isin(study_area_tazs), 'scale_factor'] = 1

df = pd.merge(df, hh, left_on='hh_id', right_on='hhid', how='left')
Expand Down
21 changes: 11 additions & 10 deletions scripts/rsm_assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@
TAZ_CROSSWALK = os.path.join(rsm_dir, "input", "taz_crosswalk.csv")
STUDY_AREA = os.path.join(rsm_dir, "input", "study_area.csv")

if os.path.exists(STUDY_AREA):
#creating copy of individual and joint trips file
shutil.copy(RSM_INDIV_TRIPS, os.path.join(rsm_dir, "output", "indivTripData_abm_"+ str(iteration) + ".csv"))
shutil.copy(RSM_JOINT_TRIPS, os.path.join(rsm_dir, "output", "jointTripData_abm_"+ str(iteration) + ".csv"))

ABM_PROPERTIES_FOLDER = os.path.join(rsm_dir, "conf")
ABM_PROPERTIES = os.path.join(ABM_PROPERTIES_FOLDER, "sandag_abm.properties")
RUN_ASSEMBLER = int(get_property(ABM_PROPERTIES, "run.rsm.assembler"))
SAMPLE_RATE = float(get_property(ABM_PROPERTIES, "rsm.default.sampling.rate"))
USE_DIFFERENTIAL_SAMPLING = int(get_property(ABM_PROPERTIES, "use.differential.sampling"))

if USE_DIFFERENTIAL_SAMPLING & os.path.exists(STUDY_AREA):
logging.info(f"Study Area file: {STUDY_AREA}")
study_area_taz = find_rsm_zone_of_study_area(STUDY_AREA, TAZ_CROSSWALK)
if study_area_taz is not None:
Expand All @@ -53,15 +63,6 @@
else:
SA_TAZ = None

#creating copy of individual and joint trips file
shutil.copy(RSM_INDIV_TRIPS, os.path.join(rsm_dir, "output", "indivTripData_abm_"+ str(iteration) + ".csv"))
shutil.copy(RSM_JOINT_TRIPS, os.path.join(rsm_dir, "output", "jointTripData_abm_"+ str(iteration) + ".csv"))

ABM_PROPERTIES_FOLDER = os.path.join(rsm_dir, "conf")
ABM_PROPERTIES = os.path.join(ABM_PROPERTIES_FOLDER, "sandag_abm.properties")
RUN_ASSEMBLER = int(get_property(ABM_PROPERTIES, "run.rsm.assembler"))
SAMPLE_RATE = float(get_property(ABM_PROPERTIES, "rsm.default.sampling.rate"))

#RSM Assembler
final_ind_trips, final_jnt_trips = rsm_assemble(
ORG_INDIV_TRIPS,
Expand Down
4 changes: 3 additions & 1 deletion scripts/rsm_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
sampling_rate = float(get_property(ABM_PROPERTIES, "rsm.default.sampling.rate"))
min_sampling_rate = float(get_property(ABM_PROPERTIES, "rsm.min.sampling.rate"))
baseline_run_dir = get_property(ABM_PROPERTIES, "rsm.baseline.run.dir")
use_differential_sampling = int(get_property(ABM_PROPERTIES, "use.differential.sampling"))

if run_rsm_sampling == 1:
CURR_ITER_ACCESS = os.path.join(
Expand All @@ -70,7 +71,7 @@
logging.info(f"Current Iteration Accessibility File: {CURR_ITER_ACCESS}")
logging.info(f"Previous Iteration Accessibility File: {PREV_ITER_ACCESS}")

if os.path.exists(EXPILICT_AGG_TAZ):
if use_differential_sampling & os.path.exists(EXPILICT_AGG_TAZ):
logging.info(f"Study Area file: {EXPILICT_AGG_TAZ}")
study_area_taz = find_rsm_zone_of_study_area(EXPILICT_AGG_TAZ, OUTPUT_TAZ_CROSSWALK)
if study_area_taz is not None:
Expand All @@ -83,6 +84,7 @@
sa_taz = None

else:
logger.info("All RSM zones will be sampled at the deafult sampling rate")
sa_taz = None


Expand Down
2 changes: 2 additions & 0 deletions scripts/rsm_zone_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
logging.info("Check if the study area file exists in the RSM input folder")
if os.path.exists(EXPILICT_AGG_TAZ):
EXPLICIT_ZONE_AGG = create_list_study_area_taz(EXPILICT_AGG_TAZ)
logging.info("The input folder has a study_area file. The TAZs will be aggregated based on the study area file")
logging.info(EXPLICIT_ZONE_AGG)
else:
EXPLICIT_ZONE_AGG = []

Expand Down

0 comments on commit 250bc6a

Please sign in to comment.