Skip to content

Commit

Permalink
Fix laptime calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
larsll committed Mar 30, 2023
1 parent 748177f commit c19ecb2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
22 changes: 14 additions & 8 deletions deepracer/logs/log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ def convert_to_pandas(data, episodes_per_iteration=20, stream=None):

df_list = list()

# ignore the first two dummy values that coach throws at the start.
for d in data[2:]:
for d in data[:]:
parts = d.rstrip().split(",")
# TODO: this is a workaround and should be removed when logs are fixed
parts_workaround = 0
Expand Down Expand Up @@ -294,21 +293,28 @@ def simulation_agg(df, firstgroup='iteration', secondgroup='episode',
logging.warning('Multiple workers found, consider using'
'secondgroup="unique_episode"')

df.loc[:,'delta_time'] = df['tstamp'].astype(float)-df['tstamp'].shift(1).astype(float)
df.loc[df['episode_status'] == 'prepare', 'delta_time'] = 0.0
df.loc[:,'delta_dist']=(((df['x'].shift(1)-df['x']) ** 2 + (df['y'].shift(1)-df['y']) ** 2) ** 0.5)
df.loc[df['episode_status'] == 'prepare', 'delta_dist'] = 0.0

grouped = df.groupby([firstgroup, secondgroup])

by_steps = grouped['steps'].agg(np.ptp).reset_index()
by_dist = grouped.apply(
lambda x: (((x['x'].shift(1) - x['x']) ** 2 +
(x['y'].shift(1) - x['y']) ** 2) ** 0.5).sum()).reset_index() \
.rename(columns={0: "dist"})
by_dist = grouped['delta_dist'].agg(np.sum).reset_index() \
.rename(columns={'delta_dist': 'dist'})

by_start = grouped.first()['closest_waypoint'].reset_index() \
.rename(index=str, columns={"closest_waypoint": "start_at"})

by_progress = grouped['progress'].agg(np.max).reset_index()

by_speed = grouped['speed'].agg(np.mean).reset_index()
by_time = grouped['tstamp'].agg(np.ptp).reset_index() \
.rename(index=str, columns={"tstamp": "time"})

by_time = grouped['delta_time'].agg(np.sum).reset_index() \
.rename(index=str, columns={"delta_time": "time"})
by_time['time'] = by_time['time'].astype(float)

by_reset = grouped['episode_status'] \
.agg([('crashed', lambda x: (x == 'crashed').sum()),
('off_track', lambda x: (x == 'off_track').sum())]).reset_index()
Expand Down
52 changes: 40 additions & 12 deletions tests/deepracer/logs/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_dataframe_load(self):
drl.load(ignore_metadata=True)
df = drl.dataframe()

assert (44840, len(Constants.RAW_COLUMNS[:-2])) == df.shape
assert (44842, len(Constants.RAW_COLUMNS[:-2])) == df.shape

def test_episode_analysis(self):
drl = DeepRacerLog(model_folder='./deepracer/logs/sample-console-logs')
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_load_evaluation_logs(self):

bulk = SimulationLogsIO.load_a_list_of_logs(logs)

assert (1475, 20) == bulk.shape
assert (1479, 20) == bulk.shape
assert np.all(['index', 'iteration', 'episode', 'steps', 'x', 'y', 'yaw',
'steering_angle', 'speed', 'action', 'reward', 'done', 'on_track',
'progress', 'closest_waypoint', 'track_len', 'tstamp', 'episode_status',
Expand All @@ -217,8 +217,8 @@ def test_summarize_evaluation_logs(self):
assert (6, len(Constants.EVAL_COLUMNS)) == simulation_agg.shape
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert 0 == fastest.iloc[0, 1]
assert 238.0 == fastest.iloc[0, 2]
assert 15.800 == pytest.approx(fastest.iloc[0, 5])
assert 240.0 == fastest.iloc[0, 2]
assert 15.932 == pytest.approx(fastest.iloc[0, 5])

def test_evaluation_analysis(self):
drl = DeepRacerLog(model_folder='./deepracer/logs/sample-console-logs')
Expand All @@ -232,6 +232,34 @@ def test_evaluation_analysis(self):
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert 15.932 == pytest.approx(fastest.iloc[0, 5])

def test_evaluation_laptime_single_laps(self):
drl = DeepRacerLog(model_folder='./deepracer/logs/sample-console-logs')
drl.load_evaluation_trace(ignore_metadata=True)
df = drl.dataframe()
df = df[df['stream'] == '20220612082523']
simulation_agg = AnalysisUtils.simulation_agg(df, firstgroup='stream', is_eval=True)

total_time = simulation_agg.groupby('stream').agg({'time': ['sum']}).iloc[0,0]
start_to_finish_time = df['tstamp'].max() - df['tstamp'].min()

assert (3, len(Constants.EVAL_COLUMNS)) == simulation_agg.shape
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert start_to_finish_time > total_time

def test_evaluation_laptime_cont_laps(self):
drl = DeepRacerLog(model_folder='./deepracer/logs/sample-drfc-1-logs')
drl.load_evaluation_trace(ignore_metadata=True)
df = drl.dataframe()
df = df[df['stream'] == '20220709200242']
simulation_agg = AnalysisUtils.simulation_agg(df, firstgroup='stream', is_eval=True)

total_time = simulation_agg.groupby('stream').agg({'time': ['sum']}).iloc[0,0]
start_to_finish_time = df['tstamp'].max() - df['tstamp'].min()

assert (3, len(Constants.EVAL_COLUMNS)) == simulation_agg.shape
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert start_to_finish_time == total_time

@pytest.mark.skipif(os.environ.get("TOX_S3_BUCKET", None) is None, reason="Requires AWS access")
def test_evaluation_analysis_s3(self):
fh = S3FileHandler(bucket=os.environ.get("TOX_S3_BUCKET"),
Expand Down Expand Up @@ -267,7 +295,7 @@ def test_evaluation_analysis_drfc1_local(self):
assert LogType.EVALUATION == drl.active
assert (9, len(Constants.EVAL_COLUMNS)) == simulation_agg.shape
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert 13.325 == pytest.approx(fastest.iloc[0, 5])
assert 13.405 == pytest.approx(fastest.iloc[0, 5])

@pytest.mark.skipif(os.environ.get("TOX_S3_BUCKET", None) is None, reason="Requires AWS access")
def test_evaluation_analysis_drfc1_s3(self):
Expand All @@ -284,7 +312,7 @@ def test_evaluation_analysis_drfc1_s3(self):
assert LogType.EVALUATION == drl.active
assert (9, len(Constants.EVAL_COLUMNS)) == simulation_agg.shape
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert 13.325 == pytest.approx(fastest.iloc[0, 5])
assert 13.405 == pytest.approx(fastest.iloc[0, 5])

def test_evaluation_analysis_drfc3_local(self):
drl = DeepRacerLog(model_folder='./deepracer/logs/sample-drfc-3-logs')
Expand Down Expand Up @@ -334,8 +362,8 @@ def test_evaluation_analysis_robomaker_local(self):
assert (6, len(Constants.EVAL_COLUMNS)) == simulation_agg.shape
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert 0 == fastest.iloc[0, 1]
assert 238.0 == fastest.iloc[0, 2]
assert 15.800 == pytest.approx(fastest.iloc[0, 5])
assert 240.0 == fastest.iloc[0, 2]
assert 15.932 == pytest.approx(fastest.iloc[0, 5])

@pytest.mark.skipif(os.environ.get("TOX_S3_BUCKET", None) is None, reason="Requires AWS access")
def test_evaluation_analysis_robomaker_s3(self):
Expand All @@ -357,8 +385,8 @@ def test_evaluation_analysis_robomaker_s3(self):
assert (6, len(Constants.EVAL_COLUMNS)) == simulation_agg.shape
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert 0 == fastest.iloc[0, 1]
assert 238.0 == fastest.iloc[0, 2]
assert 15.800 == pytest.approx(fastest.iloc[0, 5])
assert 240.0 == fastest.iloc[0, 2]
assert 15.932 == pytest.approx(fastest.iloc[0, 5])


class TestLeadershipLogs:
Expand All @@ -381,7 +409,7 @@ def test_load_robomaker_logs(self):
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert 2 == fastest.iloc[0, 1]
assert 234.0 == fastest.iloc[0, 2]
assert 15.532 == pytest.approx(fastest.iloc[0, 5])
assert 15.598 == pytest.approx(fastest.iloc[0, 5])

@pytest.mark.skipif(os.environ.get("TOX_S3_BUCKET", None) is None, reason="Requires AWS access")
def test_evaluation_analysis_drfc3_s3(self):
Expand All @@ -400,4 +428,4 @@ def test_evaluation_analysis_drfc3_s3(self):
assert np.all(Constants.EVAL_COLUMNS == simulation_agg.columns)
assert 2 == fastest.iloc[0, 1]
assert 234.0 == fastest.iloc[0, 2]
assert 15.532 == pytest.approx(fastest.iloc[0, 5])
assert 15.598 == pytest.approx(fastest.iloc[0, 5])

0 comments on commit c19ecb2

Please sign in to comment.