Florian il y a 6 ans
Parent
commit
4e26f84ce7
5 fichiers modifiés avec 82 ajouts et 33 suppressions
  1. 1 0
      .gitignore
  2. 0 0
      plan_gen/__init__.py
  3. 35 33
      plan_gen.py
  4. 32 0
      plan_gen/plan_gen_cli.py
  5. 14 0
      setup.py

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+__pycache__

+ 0 - 0
plan_gen/__init__.py


+ 35 - 33
plan_gen.py

@@ -1,28 +1,49 @@
 #!/usr/bin/env python3
-''' Python generator for MATSim plans. '''
+''' plan_gen main functions '''
 
-import sys
 import time
 import numpy as np
 import lxml.etree as etree
 
 # constants
+# ------------------
 MIN_DEPARTURE_TIME = '08:00:00'
 MAX_DEPARTURE_TIME = '09:00:00'
 WORK_DURATION = '04:00:00'
 
-def read_nodes(input_network):
-    ''' returns all network nodes as a list '''
-    if not input_network:
-        return None
-    tree = etree.parse(input_network)
-    return [node for node in tree.xpath("/network/nodes/node")]
+# utils
+# ------------------
+
+def parse_value(string):
+    ''' convert string to int, float or string '''
+    try:
+        return int(string)
+    except ValueError:
+        try:
+            return float(string)
+        except ValueError:
+            return string
+
+def parse_params(param_str):
+    ''' parse a param string to a dict '''
+    dict_params = {}
+    if param_str:
+        for key_value_str in param_str.split(','):
+            key, value = key_value_str.split('=')
+            if key in ['xmin', 'xmax', 'c', 'w', 'alpha']:
+                dict_params[key] = np.fromstring(str(value), dtype=float, sep=':')
+            else:
+                dict_params[key] = parse_value(value)
+    return dict_params
 
 def get_seconds(time_str):
     ''' returns seconds from a time string '''
     h, m, s = time_str.split(':')
     return int(h) * 3600 + int(m) * 60 + int(s)
 
+# main functions
+# ------------------
+
 def rand_time(low, high):
     ''' returns a random time between low and high bounds '''
     low_s = get_seconds(low)
@@ -69,28 +90,9 @@ def make_plans(persons):
         create_child(plan, 'act', {'type': 'h', 'x': p['home'][0], 'y': p['home'][1]})
     return plans
 
-if __name__ == '__main__':
-
-    # command line arguments
-    if len(sys.argv) != 5:
-        print('usage:', sys.argv[0], '<nb_persons> <input_network> <input_home_clusters> <input_work_clusters>')
-        print('type "" if you don\'t want to use specific clusters')
-        sys.exit(-1)
-    NB_PERSONS = int(sys.argv[1])
-    INPUT_NETWORK = sys.argv[2]
-    INPUT_HOME_CLUSTERS = sys.argv[3].split(',')
-    INPUT_WORK_CLUSTERS = sys.argv[4].split(',')
-
-    # get data
-    NODES = read_nodes(INPUT_NETWORK)
-    HOME_CLUSTERS = [read_nodes(cluster) for cluster in INPUT_HOME_CLUSTERS]
-    WORK_CLUSTERS = [read_nodes(cluster) for cluster in INPUT_WORK_CLUSTERS]
-
-    # make xml
-    PERSONS = [rand_person(NODES, HOME_CLUSTERS, WORK_CLUSTERS) for _ in range(NB_PERSONS)]
-    PLANS = make_plans(PERSONS)
-
-    # print XML
-    print('<?xml version="1.0" ?>')
-    print('<!DOCTYPE plans SYSTEM "http://www.matsim.org/files/dtd/plans_v4.dtd">')
-    print(etree.tostring(PLANS, pretty_print=True).decode('utf-8'))
+def read_nodes(input_network):
+    ''' returns all network nodes as a list '''
+    if not input_network:
+        return None
+    tree = etree.parse(input_network)
+    return [node for node in tree.xpath("/network/nodes/node")]

+ 32 - 0
plan_gen/plan_gen_cli.py

@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+''' Python generator for MATSim plans. '''
+
+import sys
+import lxml.etree as etree
+import plan_gen.plan_gen as pg
+
+if __name__ == '__main__':
+
+    # command line arguments
+    if len(sys.argv) != 5:
+        print('usage:', sys.argv[0], '<nb_persons> <input_network> <input_home_clusters> <input_work_clusters>')
+        print('type "" if you don\'t want to use specific clusters')
+        sys.exit(-1)
+    NB_PERSONS = int(sys.argv[1])
+    INPUT_NETWORK = sys.argv[2]
+    INPUT_HOME_CLUSTERS = sys.argv[3].split(',')
+    INPUT_WORK_CLUSTERS = sys.argv[4].split(',')
+
+    # get data
+    NODES = pg.read_nodes(INPUT_NETWORK)
+    HOME_CLUSTERS = [pg.read_nodes(cluster) for cluster in INPUT_HOME_CLUSTERS]
+    WORK_CLUSTERS = [pg.read_nodes(cluster) for cluster in INPUT_WORK_CLUSTERS]
+
+    # make xml
+    PERSONS = [pg.rand_person(NODES, HOME_CLUSTERS, WORK_CLUSTERS) for _ in range(NB_PERSONS)]
+    PLANS = pg.make_plans(PERSONS)
+
+    # print XML
+    print('<?xml version="1.0" ?>')
+    print('<!DOCTYPE plans SYSTEM "http://www.matsim.org/files/dtd/plans_v4.dtd">')
+    print(etree.tostring(PLANS, pretty_print=True).decode('utf-8'))

+ 14 - 0
setup.py

@@ -0,0 +1,14 @@
+import pkgconfig
+from setuptools import setup
+
+setup(
+    name = 'plan_gen',
+    version = '0.1',
+    scripts = ['plan_gen/plan_gen_cli.py'],
+    packages = ['plan_gen'],
+    install_requires = [
+        'numpy', 
+        'lxml'
+    ],
+)
+