mirror of
https://github.com/Ahp06/SUMO_Emissions.git
synced 2024-11-22 03:26:30 +00:00
Fix CSV export code
This commit is contained in:
parent
229ab72279
commit
2a4ce7f935
@ -1,5 +1,6 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import csv
|
import csv
|
||||||
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from traci import trafficlight
|
from traci import trafficlight
|
||||||
@ -15,6 +16,9 @@ import actions
|
|||||||
from config import Config
|
from config import Config
|
||||||
from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic, Emission
|
from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic, Emission
|
||||||
|
|
||||||
|
# Absolute path of the directory the script is in
|
||||||
|
SCRIPTDIR = os.path.dirname(__file__)
|
||||||
|
|
||||||
|
|
||||||
def init_grid(simulation_bounds, areas_number,window_size):
|
def init_grid(simulation_bounds, areas_number,window_size):
|
||||||
grid = list()
|
grid = list()
|
||||||
@ -125,29 +129,22 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi
|
|||||||
def get_reduction_percentage(ref,total):
|
def get_reduction_percentage(ref,total):
|
||||||
return (ref - total) / ref * 100
|
return (ref - total) / ref * 100
|
||||||
|
|
||||||
|
|
||||||
def export_data_to_csv(config, grid):
|
def export_data_to_csv(config, grid):
|
||||||
|
csv_dir = os.path.join(SCRIPTDIR, 'csv')
|
||||||
|
if not os.path.exists(csv_dir):
|
||||||
|
os.mkdir(csv_dir)
|
||||||
|
now = datetime.datetime.utcnow().isoformat()
|
||||||
|
|
||||||
now = datetime.datetime.now()
|
with open(os.path.join(csv_dir, f'{now}.csv'), 'w') as f:
|
||||||
current_date = now.strftime("%Y_%m_%d_%H_%M_%S")
|
writer = csv.writer(f)
|
||||||
|
# Write CSV headers
|
||||||
'''if not os.path.exists(f'csv/{current_date}'):
|
writer.writerow(itertools.chain(('Step',), (a.name for a in grid)))
|
||||||
os.makedirs(f'csv/{current_date}')'''
|
emissions = (a.emissions_by_step for a in grid)
|
||||||
|
step = 0
|
||||||
with open(f'test.csv', mode='w', newline = '') as emission_file:
|
for em in emissions:
|
||||||
try:
|
writer.writerow(itertools.chain((step,), (e.value() for e in em)))
|
||||||
csv_writer = csv.writer(emission_file, delimiter = ' ', quoting=csv.QUOTE_MINIMAL)
|
step += 1
|
||||||
csv_writer.writerow(['steps'])
|
|
||||||
a = list(range(config.n_steps))
|
|
||||||
for item in a:
|
|
||||||
csv_writer.writerow([item])
|
|
||||||
|
|
||||||
for area in grid:
|
|
||||||
csv_writer.writerow([f'{area.name}'])
|
|
||||||
for emission in area.emissions_by_step:
|
|
||||||
csv_writer.writerow([emission.value()])
|
|
||||||
|
|
||||||
finally:
|
|
||||||
emission_file.close()
|
|
||||||
|
|
||||||
|
|
||||||
def run(config, logger):
|
def run(config, logger):
|
||||||
|
Loading…
Reference in New Issue
Block a user