-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created class to assign reference population, refactored person matcher
- Loading branch information
Showing
7 changed files
with
269 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/org/matsim/prepare/choices/AssignReferencePopulation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.matsim.prepare.choices; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.matsim.api.core.v01.population.Population; | ||
import org.matsim.application.MATSimAppCommand; | ||
import org.matsim.application.options.ShpOptions; | ||
import org.matsim.core.population.PopulationUtils; | ||
import picocli.CommandLine; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
@CommandLine.Command( | ||
name = "assign-reference-population", | ||
description = "Assigns persons from reference data to a population." | ||
) | ||
public class AssignReferencePopulation implements MATSimAppCommand { | ||
|
||
private static final Logger log = LogManager.getLogger(AssignReferencePopulation.class); | ||
|
||
|
||
@CommandLine.Option(names = "--population", description = "Input population path.", required = true) | ||
private String populationPath; | ||
|
||
@CommandLine.Option(names = "--persons", description = "Input persons from survey data, in matsim-python-tools format.", required = true) | ||
private Path persons; | ||
|
||
@CommandLine.Option(names = "--trips", description = "Input trips from survey data, in matsim-python-tools format.", required = true) | ||
private Path trips; | ||
|
||
@CommandLine.Option(names = "--facilities", description = "Shp file with facilities", required = true) | ||
private Path facilities; | ||
|
||
@CommandLine.Option(names = "--output", description = "Output population path.", required = true) | ||
private Path output; | ||
|
||
@CommandLine.Mixin | ||
private ShpOptions shp; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
|
||
if (!shp.isDefined()) { | ||
log.error("No shapefile defined. Please specify a shapefile for the zones using the --shp option."); | ||
return 2; | ||
} | ||
|
||
if (!Files.exists(trips)) { | ||
log.error("Input trip file does not exist: {}", trips); | ||
return 2; | ||
} | ||
|
||
Population population = PopulationUtils.readPopulation(populationPath); | ||
|
||
PlanBuilder builder = new PlanBuilder(shp, new ShpOptions(facilities, null, null), population.getFactory()); | ||
|
||
builder.mergePlans(population, trips, persons); | ||
|
||
PopulationUtils.writePopulation(population, output.toString()); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/main/java/org/matsim/prepare/population/PersonMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package org.matsim.prepare.population; | ||
|
||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVParser; | ||
import org.apache.commons.csv.CSVRecord; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.matsim.api.core.v01.population.Person; | ||
import org.matsim.application.options.CsvOptions; | ||
import org.matsim.core.population.PersonUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Path; | ||
import java.util.*; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* This class is used to read and match persons from the reference data in csv format. | ||
*/ | ||
public class PersonMatcher { | ||
|
||
private static final Logger log = LogManager.getLogger(PersonMatcher.class); | ||
|
||
private final String idxColumn; | ||
|
||
private final CsvOptions csv = new CsvOptions(CSVFormat.Predefined.Default); | ||
private final Map<Key, List<String>> groups = new HashMap<>(); | ||
private final Map<String, CSVRecord> persons = new HashMap<>(); | ||
|
||
public PersonMatcher(String idxColumn, Path personsPath) { | ||
this.idxColumn = idxColumn; | ||
|
||
try (CSVParser parser = csv.createParser(personsPath)) { | ||
buildSubgroups(parser); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Match reference person to a person in the population. | ||
* @return person id | ||
*/ | ||
public String matchPerson(Person person, SplittableRandom rnd) { | ||
|
||
Key key = createKey(person); | ||
|
||
List<String> subgroup = groups.get(key); | ||
if (subgroup == null) { | ||
log.error("No subgroup found for key {}", key); | ||
throw new IllegalStateException("Invalid entry"); | ||
} | ||
|
||
if (subgroup.size() < 30) { | ||
log.warn("Group {} has low sample size: {}", key, subgroup.size()); | ||
} | ||
|
||
return subgroup.get(rnd.nextInt(subgroup.size())); | ||
} | ||
|
||
/** | ||
* Return reference person with given index. | ||
*/ | ||
public CSVRecord getPerson(String personId) { | ||
return persons.get(personId); | ||
} | ||
|
||
/** | ||
* Create subpopulations for sampling. | ||
*/ | ||
private void buildSubgroups(CSVParser csv) { | ||
|
||
int i = 0; | ||
|
||
for (CSVRecord r : csv) { | ||
|
||
String idx = r.get(idxColumn); | ||
int regionType = Integer.parseInt(r.get("region_type")); | ||
String gender = r.get("gender"); | ||
String employment = r.get("employment"); | ||
int age = Integer.parseInt(r.get("age")); | ||
|
||
Stream<Key> keys = createKey(gender, age, regionType, employment); | ||
keys.forEach(key -> groups.computeIfAbsent(key, (k) -> new ArrayList<>()).add(idx)); | ||
persons.put(idx, r); | ||
i++; | ||
} | ||
|
||
log.info("Read {} persons from csv.", i); | ||
} | ||
|
||
private Stream<Key> createKey(String gender, int age, int regionType, String employment) { | ||
if (age < 6) { | ||
return IntStream.rangeClosed(0, 5).mapToObj(i -> new Key(null, i, regionType, null)); | ||
} | ||
if (age <= 10) { | ||
return IntStream.rangeClosed(6, 10).mapToObj(i -> new Key(null, i, regionType, null)); | ||
} | ||
if (age < 18) { | ||
return IntStream.rangeClosed(11, 18).mapToObj(i -> new Key(gender, i, regionType, null)); | ||
} | ||
|
||
Boolean isEmployed = age > 65 ? null : !employment.equals("unemployed"); | ||
int min = Math.max(18, age - 6); | ||
int max = Math.min(65, age + 6); | ||
|
||
// larger groups for older people | ||
if (age > 65) { | ||
min = Math.max(66, age - 10); | ||
max = Math.min(99, age + 10); | ||
} | ||
|
||
return IntStream.rangeClosed(min, max).mapToObj(i -> new Key(gender, i, regionType, isEmployed)); | ||
} | ||
|
||
private Key createKey(Person person) { | ||
|
||
Integer age = PersonUtils.getAge(person); | ||
String gender = PersonUtils.getSex(person); | ||
if (age <= 10) | ||
gender = null; | ||
|
||
Boolean employed = PersonUtils.isEmployed(person); | ||
if (age < 18 || age > 65) | ||
employed = null; | ||
|
||
int regionType = (int) person.getAttributes().getAttribute(Attributes.RegioStaR7); | ||
|
||
// Region types have been reduced to 1 and 3 | ||
if (regionType != 1) | ||
regionType = 3; | ||
|
||
return new Key(gender, age, regionType, employed); | ||
} | ||
|
||
/** | ||
* Key used to match persons. | ||
*/ | ||
public record Key(String gender, int age, int regionType, Boolean employed) { | ||
} | ||
|
||
} |
Oops, something went wrong.