Commit 6c746bbc authored by tchervec's avatar tchervec
Browse files

in context.progress, replace desc with label

parent e847e9fe
......@@ -20,7 +20,7 @@ def impute(context, df_ovgk, df, on):
indices = np.array_split(np.arange(len(df)), 100)
df_join = []
for chunk in context.progress(indices, desc="Imputing ÖV Güteklasse"):
for chunk in context.progress(indices, label="Imputing ÖV Güteklasse"):
df_join.append(gpd.sjoin(df.iloc[chunk], df_ovgk, op="within")[on + ["ovgk"]])
df_join = pd.concat(df_join)
......
......@@ -23,7 +23,7 @@ def to_gpd(context, df, x="x", y="y", crs={"init": "EPSG:2056"}):
df["geometry"] = [
geo.Point(*coord) for coord in context.progress(
zip(df[x], df[y]), total=len(df),
desc="Converting coordinates"
label="Converting coordinates"
)]
df = gpd.GeoDataFrame(df)
df.crs = crs
......
......@@ -128,7 +128,7 @@ def parallel_fit(context, args):
df = fitting_problem.df
group_controls, individual_controls = compute_filters(fitting_problem)
with context.progress(total=maxiter, position=index, desc="progress #%s" % str(index)) as progress:
with context.progress(total=maxiter, position=index, label="progress #%s" % str(index)) as progress:
for i in range(maxiter):
df["r_factor"] = 1.0
df = group_fit(df, group_controls, fitting_problem.group_id)
......
......@@ -150,7 +150,7 @@ def execute(context):
writer = matsim.writers.PopulationWriter(raw_writer)
writer.start_population()
with context.progress(total=len(df_persons), desc="Writing persons ...") as progress:
with context.progress(total=len(df_persons), label="Writing persons ...") as progress:
try:
while True:
person = next(person_iterator)
......@@ -181,7 +181,7 @@ def execute(context):
freight_iterator = iter(df_freight.itertuples())
number_of_written_freight = 0
with context.progress(total=len(df_freight), desc="Writing freight agents ...") as progress:
with context.progress(total=len(df_freight), label="Writing freight agents ...") as progress:
try:
while True:
freight = next(freight_iterator)
......
......@@ -44,7 +44,7 @@ class HotDeckMatcher:
column_index = 0
with self.context.progress(total=columns,
desc="Reading categories (%s) ..." % ("source" if source else "target"),
label="Reading categories (%s) ..." % ("source" if source else "target"),
position=chunk_index) as progress:
for field_name in self.all_fields:
for field_value in self.values[field_name]:
......@@ -64,12 +64,12 @@ class HotDeckMatcher:
# for each person which is later on used for the sampling.
random = np.array([
np.random.random() for _ in
self.context.progress(range(len(df_target)), desc="Generating random numbers", position=chunk_index)
self.context.progress(range(len(df_target)), label="Generating random numbers", position=chunk_index)
])
with self.context.progress(total=len(self.field_masks),
position=chunk_index,
desc="Hot Deck Matching") as progress:
label="Hot Deck Matching") as progress:
for field_mask in self.field_masks:
field_mask = np.array(functools.reduce(lambda x, y: x + y, field_mask), dtype=np.bool)
source_mask = np.all(self.source_matrix[:, field_mask], axis=1)
......
......@@ -11,6 +11,7 @@ def configure(context):
context.stage("population.sociodemographics")
context.stage("data.spatial.zones")
# TODO: We only assign work here through OD matrices. However, we *can* generate
# OD matrices for education as well (the STATPOP information is available). What
# would need to be done is to adjust data.od.matrix to produce two kinds of
......@@ -26,7 +27,7 @@ def execute(context):
# Load commute information for work
df_commute = pd.DataFrame(context.stage("data.microcensus.commute")[[
"person_id", "commute_mode", "commute_home_distance", "commute_purpose"
]], copy = True)
]], copy=True)
df_commute = df_commute[df_commute["commute_purpose"] == "work"]
df_commute["mz_person_id"] = df_commute["person_id"]
del df_commute["person_id"]
......@@ -38,10 +39,10 @@ def execute(context):
# Merge commute information into the persons
df = pd.merge(
df_persons, df_commute, on = "mz_person_id"
df_persons, df_commute, on="mz_person_id"
)
df_demand = df.groupby(["commute_mode", "home_zone_id"]).size().reset_index(name = "count")
df_demand = df.groupby(["commute_mode", "home_zone_id"]).size().reset_index(name="count")
pdf_matrices, cdf_matrices = context.stage("data.od.matrix")
commute_counts = {}
......@@ -51,31 +52,31 @@ def execute(context):
origin_counts = np.array([
np.sum(df_demand.loc[
(df_demand["commute_mode"] == mode) & (df_demand["home_zone_id"] == origin_zone), "count"
]) for origin_zone in context.progress(df_zones["zone_id"], desc = mode)
(df_demand["commute_mode"] == mode) & (df_demand["home_zone_id"] == origin_zone), "count"
]) for origin_zone in context.progress(df_zones["zone_id"], label=mode)
])[:, np.newaxis]
counts = np.zeros(pdf_matrices[source_mode].shape, dtype = np.int)
counts = np.zeros(pdf_matrices[source_mode].shape, dtype=np.int)
for i in range(len(df_zones)):
if origin_counts[i] > 0:
assert(~np.any(np.isnan(pdf_matrices[source_mode][i])))
counts[i,:] = np.random.multinomial(origin_counts[i], pdf_matrices[source_mode][i,:])
assert (~np.any(np.isnan(pdf_matrices[source_mode][i])))
counts[i, :] = np.random.multinomial(origin_counts[i], pdf_matrices[source_mode][i, :])
commute_counts[mode] = counts
assert(len(counts) == len(df_zones))
assert (len(counts) == len(df_zones))
distances = context.stage("data.od.distances")
work_zones = np.zeros((len(df),), dtype = np.int)
work_zones = np.zeros((len(df),), dtype=np.int)
zone_ids = list(df_zones["zone_id"])
with context.progress(desc = "Assigning work zones", total = 5 * len(df_zones)) as progress:
with context.progress(label="Assigning work zones", total=5 * len(df_zones)) as progress:
for mode in ["car", "pt", "bike", "walk", "car_passenger"]:
mode_f = df["commute_mode"] == mode
for origin_index, origin_zone in enumerate(zone_ids):
destination_counts = commute_counts[mode][origin_index,:]
destination_order = np.argsort(distances[origin_index,:])
destination_counts = commute_counts[mode][origin_index, :]
destination_order = np.argsort(distances[origin_index, :])
destinations = [[zone_ids[i]] * destination_counts[i] for i in destination_order]
destinations = functools.reduce(lambda x, y: x + y, destinations)
......@@ -89,6 +90,6 @@ def execute(context):
df.loc[:, "work_zone_id"] = work_zones
df = df[["person_id", "work_zone_id", "commute_mode"]]
assert(len(df) == len(df.dropna()))
assert (len(df) == len(df.dropna()))
return df
......@@ -10,6 +10,7 @@ def configure(context):
context.stage("data.spatial.zones")
context.stage("data.spatial.zone_shapes")
def execute(context):
df = context.stage("population.spatial.by_person.primary_zones")
df_statent = context.stage("data.statent.statent")
......@@ -17,14 +18,15 @@ def execute(context):
df_zones = context.stage("data.spatial.zones")
df_zones["work_zone_id"] = df_zones["zone_id"]
df_demand = df.groupby("work_zone_id").size().reset_index(name = "count")
df_demand = df.groupby("work_zone_id").size().reset_index(name="count")
df_demand = pd.merge(df_demand, df_zones[["work_zone_id", "zone_level"]])
# First handle the national commuters
df_national = df_demand[df_demand["zone_level"].isin(("municipality", "quarter"))]
empty_zones = []
for zone_id, count in context.progress(zip(df_national["work_zone_id"], df_national["count"]), desc = "Assigning national locations ...", total = len(df_demand)):
for zone_id, count in context.progress(zip(df_national["work_zone_id"], df_national["count"]),
label="Assigning national locations ...", total=len(df_demand)):
indices = np.where(df_statent["zone_id"] == zone_id)[0]
weights = df_statent.iloc[indices]["number_employees"]
weights /= np.sum(weights)
......@@ -46,12 +48,12 @@ def execute(context):
df_shapes = context.stage("data.spatial.zone_shapes")
for zone_id in context.progress(empty_zones, desc = "Assigning national locations for empty zones ..."):
for zone_id in context.progress(empty_zones, label="Assigning national locations for empty zones ..."):
count = df_national[df_national["work_zone_id"] == zone_id]["count"].iloc[0]
row = df_shapes[df_shapes["zone_id"] == zone_id].iloc[0]
coordinates = data.spatial.zone_shapes.sample_coordinates(row, count)
df.loc[df["work_zone_id"] == zone_id, "work_x"] = coordinates[:,0]
df.loc[df["work_zone_id"] == zone_id, "work_y"] = coordinates[:,1]
df.loc[df["work_zone_id"] == zone_id, "work_x"] = coordinates[:, 0]
df.loc[df["work_zone_id"] == zone_id, "work_y"] = coordinates[:, 1]
# Second, handle the international commuters
print("TODO: We do not handle commuter traffic at the moment.")
......@@ -59,8 +61,8 @@ def execute(context):
# For now, make sure that we do not have any international traffic
df_international = df_demand[df_demand["zone_level"] == "country"]
assert(len(df_international) == 0)
assert(len(df) == len(df.dropna()))
assert (len(df_international) == 0)
assert (len(df) == len(df.dropna()))
df = df[[
"person_id", "work_x", "work_y", "work_location_id"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment