maro/tests/test_binary_convert_read.py

167 строки
5.2 KiB
Python
Исходник Обычный вид История

2020-09-18 06:02:37 +03:00
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT licence
import os
import tempfile
import unittest
V0.1 improve code quality with isort (#186) * refine readme * feat: refine data push/pull (#138) * feat: refine data push/pull * test: add cli provision testing * fix: style fix * fix: add necessary comments * fix: from code review * add fall back function in weather download (#112) * fix deployment issue in multi envs * fix typo * fix ~/.maro not exist issue in build * skip deploy when build * update for comments * temporarily disable weather info * replace ecr with cim in setup.py * replace ecr in manifest * remove weather check when read data * fix station id issue * fix format * add TODO in comments * add noaa weather source * fix weather reset and weather comment * add comment for weather data url * some format update * add fall back function in weather download * update comment * update for comments * update comment * add period * fix for pylint * update for pylint check * added example docs (#136) * added example docs * added citibike greedy example doc * modified citibike doc * fixed PR comments * fixed more PR comments * fixed small formatting issue Co-authored-by: ysqyang <v-yangqi@microsoft.com> * switch the key and value of handler_dict in decorator (#144) * switch the key and value of handler_dict in decorator * add dist decorator UT and fixed multithreading conflict in maro test suite * pr comments update. * resolved comments about decorator UT * rename handler_fun in dist decorator * change self.attr into class_name.attr * update UT tests comments * V0.1 annotation (#147) * refine the annotation of simulator core * remove reward from env(be) * format refined * white spaces test * left-padding spaces refined * format modifed * update the left-padding spaces of docstrings * code format updated * update according to comments * update according to PR comments Co-authored-by: Jinyu Wang <Wang.Jinyu@microsoft.com> * V0.1 issues in CLI (#157) * fix: add docs for AzCopy * style: refine code style * fix: change stdout format to previous version * test: add dqn tests in grass/k8s mode && add "maro k8s job list" * docs: add scripts for installing AzCopy * v0.1 update citi bike doc and data version (#160) * fix deployment issue in multi envs * fix typo * fix ~/.maro not exist issue in build * skip deploy when build * update for comments * temporarily disable weather info * replace ecr with cim in setup.py * replace ecr in manifest * remove weather check when read data * fix station id issue * fix format * add TODO in comments * add noaa weather source * fix weather reset and weather comment * add comment for weather data url * some format update * add fall back function in weather download * update comment * update for comments * update comment * add period * fix for pylint * update for pylint check * update data pipeline docs and data version * update for comments * update for comments * update for lint * update for lint * update for lint * V0.1revert data version to 0.1 (#164) * fix deployment issue in multi envs * fix typo * fix ~/.maro not exist issue in build * skip deploy when build * update for comments * temporarily disable weather info * replace ecr with cim in setup.py * replace ecr in manifest * remove weather check when read data * fix station id issue * fix format * add TODO in comments * add noaa weather source * fix weather reset and weather comment * add comment for weather data url * some format update * add fall back function in weather download * update comment * update for comments * update comment * add period * fix for pylint * update for pylint check * update data pipeline docs and data version * update for comments * update for comments * update for lint * update for lint * update for lint * revert data version to 0.1 * initalize gnn network for CIM problem (#60) * initalize gnn netowrk for ECR problem * fix import path error and remove useless function * add the annotation for gnn and state shaper * rename to cim * modify topology 22p * polish coding style in cim.gnn * run pass after polishing * change the name of simplegat to simple_transformer * fix typo and suggestions * fix format * fix code bugs due to refactoring mistake * refine the code format * refine again * refine the format * refine again * refine again * fix syntax bug * refine again * remove useless comments * remove unnecessary blank line * rename pending_event to decision_event * remove some code faced to the future. * change topology * fix bugs in learner: no training logic executed * revert the topology changes Co-authored-by: wenshi-113 <wenshi@microsoft.com> * V0.1 store unpicklable bug fix (#166) * fixed unpicklable store bug * fixed a bug * fixed a bug * fixed a bug * fixed lint formatting * fixed a lint formatting issue * fixed a bug * renamed dump_experience_store to dump_experience_pool * fixed a PR comment Co-authored-by: ysqyang <v-yangqi@microsoft.com> * V0.1 confusing component naming fix (#167) * confusing component naming fix * fixed a bug * fixed a bug * fixed lint formatting issues * fixed a PR comment * replaced 'actor_worker' with 'actor' * fixed a minor formatting issue * fixed a minor formatting issue * fixed a minor formatting issue Co-authored-by: ysqyang <v-yangqi@microsoft.com> * version updated to 0.1.2a0 * modified according to flake8 * update according to lint checker * update import format according to lint checker * update according to lint checker * isort config added * add isort config file path in lint * update import according to lint isort * update import according to lint isort * skip file added for isort linter * filter files option added for isort * V0.1 reformat imports with isort (#184) * init auto-isort * re-format maro init * redis added to isort known third party * update according to lint isort * update according to lint isort * update according to lint * update import path for communication module * update import path for cli scripts module * update according to lint * filter file option added for isort in CONTRIBUTING * update import path for cli utils and communication * update import path for tests * remove filter file option in CONTRIBUTING Co-authored-by: Jinyu Wang <Wang.Jinyu@microsoft.com> * update according to isort Co-authored-by: Arthur Jiang <sjian@microsoft.com> Co-authored-by: Romic Huang <romic.kid@gmail.com> Co-authored-by: zhanyu wang <pocket_2001@163.com> Co-authored-by: ysqyang <ysqyang@gmail.com> Co-authored-by: ysqyang <v-yangqi@microsoft.com> Co-authored-by: kaiqli <59279714+kaiqli@users.noreply.github.com> Co-authored-by: Jinyu-W <53509467+Jinyu-W@users.noreply.github.com> Co-authored-by: Jinyu Wang <Wang.Jinyu@microsoft.com> Co-authored-by: Jinyu Wang <jinywan@microsoft.com> Co-authored-by: WesleyStone <4548088+wesley-stone@users.noreply.github.com> Co-authored-by: wenshi-113 <wenshi@microsoft.com>
2020-11-05 12:53:04 +03:00
from maro.data_lib import BinaryConverter, BinaryReader
2020-09-18 06:02:37 +03:00
from maro.data_lib.item_meta import BinaryMeta
class TestBinaryConverter(unittest.TestCase):
def test_convert_with_events(self):
out_dir = tempfile.mkdtemp()
out_bin = os.path.join(out_dir, "trips.bin")
meta_file = os.path.join("tests", "data", "data_lib", "case_1", "meta.yml")
csv_file = os.path.join("tests", "data", "data_lib", "trips.csv")
bct = BinaryConverter(out_bin, meta_file)
# add and convert 1st csv file
bct.add_csv(csv_file)
# add again will append to the end ignore the order
bct.add_csv(csv_file)
# flush will close the file, cannot add again
bct.flush()
# check if output exist
self.assertTrue(os.path.exists(out_bin))
# check content
reader = BinaryReader(out_bin)
# start tick should be smallest one
start_date = reader.start_datetime
self.assertEqual(start_date.year, 2019)
self.assertEqual(start_date.month, 1)
self.assertEqual(start_date.day, 1)
self.assertEqual(start_date.hour, 0)
self.assertEqual(start_date.minute, 0)
self.assertEqual(start_date.second, 0)
end_date = reader.end_datetime
self.assertEqual(end_date.year, 2019)
self.assertEqual(end_date.month, 1)
self.assertEqual(end_date.day, 1)
self.assertEqual(end_date.hour, 0)
self.assertEqual(end_date.minute, 5)
self.assertEqual(end_date.second, 0)
# there should be double items as trips.csv
self.assertEqual(4*2, reader.header.item_count)
# 20 byte
self.assertEqual(20, reader.header.item_size)
start_station_index = [0, 0, 1, 0]
idx = 0
# check iterating interface
for item in reader.items():
# check if fields same as meta
self.assertTupleEqual(('timestamp', 'durations', 'src_station', 'dest_station'), item._fields)
# check item start station index
self.assertEqual(start_station_index[idx % len(start_station_index)], item.src_station)
idx += 1
# check if filter works as expected
l = len([item for item in reader.items(end_time_offset=0, time_unit="m")])
# although there are 2 items that match the condition, but they not sorted, reader will not try to read to the end, but
# to the first item which not match the condition
self.assertEqual(1, l)
l = len([item for item in reader.items(start_time_offset=1, time_unit='m')])
# reader will try to read 1st one that > end tick, so there should be 6 items
self.assertEqual(6, l)
def test_convert_without_events(self):
out_dir = tempfile.mkdtemp()
out_bin = os.path.join(out_dir, "trips.bin")
meta_file = os.path.join("tests", "data", "data_lib", "case_2", "meta.yml")
csv_file = os.path.join("tests", "data", "data_lib", "trips.csv")
bct = BinaryConverter(out_bin, meta_file)
bct.add_csv(csv_file)
# flush will close the file, cannot add again
bct.flush()
reader = BinaryReader(out_bin)
meta: BinaryMeta = reader.meta
self.assertIsNotNone(meta)
# check events
self.assertListEqual(["require_bike", "return_bike", "rebalance_bike", "deliver_bike"], [event.display_name for event in meta.events])
self.assertListEqual(["RequireBike", "ReturnBike", "RebalanceBike", "DeliverBike"], [event.type_name for event in meta.events])
self.assertEqual("RequireBike", meta.default_event_name)
self.assertIsNone(meta.event_attr_name)
def test_convert_with_starttimestamp(self):
out_dir = tempfile.mkdtemp()
out_bin = os.path.join(out_dir, "trips.bin")
meta_file = os.path.join("tests", "data", "data_lib", "case_2", "meta.yml")
csv_file = os.path.join("tests", "data", "data_lib", "trips.csv")
#12/31/2018 @ 11:59pm (UTC)
bct = BinaryConverter(out_bin, meta_file, utc_start_timestamp=1546300740)
bct.add_csv(csv_file)
# flush will close the file, cannot add again
bct.flush()
reader = BinaryReader(out_bin)
# check header
self.assertEqual(1546300740, reader.header.starttime)
# then tick 0 will not be 2019/01/01 00:00:00
l = len([item for item in reader.items(end_time_offset=0, time_unit='m')])
self.assertEqual(0, l)
# it should be tick 1 for now
l = len([item for item in reader.items(end_time_offset=1, time_unit='m')])
self.assertEqual(1, l)
def test_convert_without_meta_timestamp(self):
out_dir = tempfile.mkdtemp()
out_bin = os.path.join(out_dir, "trips.bin")
meta_file = os.path.join("tests", "data", "data_lib", "case_3", "meta.yml")
csv_file = os.path.join("tests", "data", "data_lib", "trips.csv")
#12/31/2018 @ 11:59pm (UTC)
with self.assertRaises(Exception) as ctx:
bct = BinaryConverter(out_bin, meta_file)
if __name__ == "__main__":
V0.1 improve code quality with isort (#186) * refine readme * feat: refine data push/pull (#138) * feat: refine data push/pull * test: add cli provision testing * fix: style fix * fix: add necessary comments * fix: from code review * add fall back function in weather download (#112) * fix deployment issue in multi envs * fix typo * fix ~/.maro not exist issue in build * skip deploy when build * update for comments * temporarily disable weather info * replace ecr with cim in setup.py * replace ecr in manifest * remove weather check when read data * fix station id issue * fix format * add TODO in comments * add noaa weather source * fix weather reset and weather comment * add comment for weather data url * some format update * add fall back function in weather download * update comment * update for comments * update comment * add period * fix for pylint * update for pylint check * added example docs (#136) * added example docs * added citibike greedy example doc * modified citibike doc * fixed PR comments * fixed more PR comments * fixed small formatting issue Co-authored-by: ysqyang <v-yangqi@microsoft.com> * switch the key and value of handler_dict in decorator (#144) * switch the key and value of handler_dict in decorator * add dist decorator UT and fixed multithreading conflict in maro test suite * pr comments update. * resolved comments about decorator UT * rename handler_fun in dist decorator * change self.attr into class_name.attr * update UT tests comments * V0.1 annotation (#147) * refine the annotation of simulator core * remove reward from env(be) * format refined * white spaces test * left-padding spaces refined * format modifed * update the left-padding spaces of docstrings * code format updated * update according to comments * update according to PR comments Co-authored-by: Jinyu Wang <Wang.Jinyu@microsoft.com> * V0.1 issues in CLI (#157) * fix: add docs for AzCopy * style: refine code style * fix: change stdout format to previous version * test: add dqn tests in grass/k8s mode && add "maro k8s job list" * docs: add scripts for installing AzCopy * v0.1 update citi bike doc and data version (#160) * fix deployment issue in multi envs * fix typo * fix ~/.maro not exist issue in build * skip deploy when build * update for comments * temporarily disable weather info * replace ecr with cim in setup.py * replace ecr in manifest * remove weather check when read data * fix station id issue * fix format * add TODO in comments * add noaa weather source * fix weather reset and weather comment * add comment for weather data url * some format update * add fall back function in weather download * update comment * update for comments * update comment * add period * fix for pylint * update for pylint check * update data pipeline docs and data version * update for comments * update for comments * update for lint * update for lint * update for lint * V0.1revert data version to 0.1 (#164) * fix deployment issue in multi envs * fix typo * fix ~/.maro not exist issue in build * skip deploy when build * update for comments * temporarily disable weather info * replace ecr with cim in setup.py * replace ecr in manifest * remove weather check when read data * fix station id issue * fix format * add TODO in comments * add noaa weather source * fix weather reset and weather comment * add comment for weather data url * some format update * add fall back function in weather download * update comment * update for comments * update comment * add period * fix for pylint * update for pylint check * update data pipeline docs and data version * update for comments * update for comments * update for lint * update for lint * update for lint * revert data version to 0.1 * initalize gnn network for CIM problem (#60) * initalize gnn netowrk for ECR problem * fix import path error and remove useless function * add the annotation for gnn and state shaper * rename to cim * modify topology 22p * polish coding style in cim.gnn * run pass after polishing * change the name of simplegat to simple_transformer * fix typo and suggestions * fix format * fix code bugs due to refactoring mistake * refine the code format * refine again * refine the format * refine again * refine again * fix syntax bug * refine again * remove useless comments * remove unnecessary blank line * rename pending_event to decision_event * remove some code faced to the future. * change topology * fix bugs in learner: no training logic executed * revert the topology changes Co-authored-by: wenshi-113 <wenshi@microsoft.com> * V0.1 store unpicklable bug fix (#166) * fixed unpicklable store bug * fixed a bug * fixed a bug * fixed a bug * fixed lint formatting * fixed a lint formatting issue * fixed a bug * renamed dump_experience_store to dump_experience_pool * fixed a PR comment Co-authored-by: ysqyang <v-yangqi@microsoft.com> * V0.1 confusing component naming fix (#167) * confusing component naming fix * fixed a bug * fixed a bug * fixed lint formatting issues * fixed a PR comment * replaced 'actor_worker' with 'actor' * fixed a minor formatting issue * fixed a minor formatting issue * fixed a minor formatting issue Co-authored-by: ysqyang <v-yangqi@microsoft.com> * version updated to 0.1.2a0 * modified according to flake8 * update according to lint checker * update import format according to lint checker * update according to lint checker * isort config added * add isort config file path in lint * update import according to lint isort * update import according to lint isort * skip file added for isort linter * filter files option added for isort * V0.1 reformat imports with isort (#184) * init auto-isort * re-format maro init * redis added to isort known third party * update according to lint isort * update according to lint isort * update according to lint * update import path for communication module * update import path for cli scripts module * update according to lint * filter file option added for isort in CONTRIBUTING * update import path for cli utils and communication * update import path for tests * remove filter file option in CONTRIBUTING Co-authored-by: Jinyu Wang <Wang.Jinyu@microsoft.com> * update according to isort Co-authored-by: Arthur Jiang <sjian@microsoft.com> Co-authored-by: Romic Huang <romic.kid@gmail.com> Co-authored-by: zhanyu wang <pocket_2001@163.com> Co-authored-by: ysqyang <ysqyang@gmail.com> Co-authored-by: ysqyang <v-yangqi@microsoft.com> Co-authored-by: kaiqli <59279714+kaiqli@users.noreply.github.com> Co-authored-by: Jinyu-W <53509467+Jinyu-W@users.noreply.github.com> Co-authored-by: Jinyu Wang <Wang.Jinyu@microsoft.com> Co-authored-by: Jinyu Wang <jinywan@microsoft.com> Co-authored-by: WesleyStone <4548088+wesley-stone@users.noreply.github.com> Co-authored-by: wenshi-113 <wenshi@microsoft.com>
2020-11-05 12:53:04 +03:00
unittest.main()