# Path: humanrl/utils.py
# ACTION_MEANING = {
#     None: "NONE",
#     0: "NOOP",
#     1: "FIRE",
#     2: "UP",
#     3: "RIGHT",
#     4: "LEFT",
#     5: "DOWN",
#     6: "UPRIGHT",
#     7: "UPLEFT",
#     8: "DOWNRIGHT",
#     9: "DOWNLEFT",
#     10: "UPFIRE",
#     11: "RIGHTFIRE",
#     12: "LEFTFIRE",
#     13: "DOWNFIRE",
#     14: "UPRIGHTFIRE",
#     15: "UPLEFTFIRE",
#     16: "DOWNRIGHTFIRE",
#     17: "DOWNLEFTFIRE",
# }
# 
# ACTION_MEANING_TO_ACTION = {v: k for k, v in ACTION_MEANING.items()}
# 
# SAFE_ACTION_MAPPINGS = {"SpaceInvaders": {0: 0, 1: 0, 2: 2, 3: 3, 4: 2, 5: 3}}
# 
# def identity(*args):
#     if len(args) == 1:
#         return args[0]
#     return args
# 
# def shuffle(lst):
#     random.shuffle(lst)
#     return lst


import argparse
import datetime
import gzip
import itertools
import logging
import os
import os.path
import pickle
import random
import time
import numpy as np
import cv2
import humanrl.frame as frame_module
from multiprocessing.connection import Listener
from subprocess import call
from PIL import Image
from humanrl.utils import (ACTION_MEANING, ACTION_MEANING_TO_ACTION,
                           SAFE_ACTION_MAPPINGS, identity, shuffle)

    should_skip = False
    if output_dir:
        components = os.path.normpath(episode_path).split(os.path.sep)
        f = components[-1]
        print(components)
        if len(components) > 1 and components[-2][0] == 'w' and components[-2][1:].isnumeric():
            f = "{}_{}".format(components[-2], components[-1])
        output_filename = os.path.join(output_dir, f)
        if dry_run and os.path.exists(output_filename):
            should_skip = True
    else:
        output_filename = episode_path

    action_set = list(range(18))
    if "Pong" in episode_path or "SpaceInvaders" in episode_path:
        action_set = [0, 1, 3, 4, 11, 12]
    if "action_set" in episode.info:
        action_set = episode.info["action_set"]
    return output_filename, action_set, should_skip


class Episodes(object):
    """
    An iterator to read episodes from a directory.

    This assumes episodes are stored in the same format as `Frame.py` writes them.

    """


# Path: humanrl/utils.py
# ACTION_MEANING = {
#     None: "NONE",
#     0: "NOOP",
#     1: "FIRE",
#     2: "UP",
#     3: "RIGHT",
#     4: "LEFT",
#     5: "DOWN",
#     6: "UPRIGHT",
#     7: "UPLEFT",
#     8: "DOWNRIGHT",
#     9: "DOWNLEFT",
#     10: "UPFIRE",
#     11: "RIGHTFIRE",
#     12: "LEFTFIRE",
#     13: "DOWNFIRE",
#     14: "UPRIGHTFIRE",
#     15: "UPLEFTFIRE",
#     16: "DOWNRIGHTFIRE",
#     17: "DOWNLEFTFIRE",
# }
# 
# ACTION_MEANING_TO_ACTION = {v: k for k, v in ACTION_MEANING.items()}
# 
# SAFE_ACTION_MAPPINGS = {"SpaceInvaders": {0: 0, 1: 0, 2: 2, 3: 3, 4: 2, 5: 3}}
# 
# def identity(*args):
#     if len(args) == 1:
#         return args[0]
#     return args
# 
# def shuffle(lst):
#     random.shuffle(lst)
#     return lst


# Path: scripts/human_feedback.py
import argparse
import datetime
import gzip
import itertools
import logging
import os
import os.path
import pickle
import random
import time
import numpy as np
import cv2
import humanrl.frame as frame_module
from multiprocessing.connection import Listener
from subprocess import call
from PIL import Image
from humanrl.utils import (ACTION_MEANING, ACTION_MEANING_TO_ACTION,
                           SAFE_ACTION_MAPPINGS, identity, shuffle)


    should_skip = False
    if output_dir:
        components = os.path.normpath(episode_path).split(os.path.sep)
        f = components[-1]
        print(components)
        if len(components) > 1 and components[-2][0] == 'w' and components[-2][1:].isnumeric():
            f = "{}_{}".format(components[-2], components[-1])
        output_filename = os.path.join(output_dir, f)
        if dry_run and os.path.exists(output_filename):
            should_skip = True
    else:
        output_filename = episode_path

    action_set = list(range(18))
    if "Pong" in episode_path or "SpaceInvaders" in episode_path:
        action_set = [0, 1, 3, 4, 11, 12]
    if "action_set" in episode.info:
        action_set = episode.info["action_set"]
    return output_filename, action_set, should_skip


class Episodes(object):
    """
    An iterator to read episodes from a directory.

    This assumes episodes are stored in the same format as `Frame.py` writes them.

    """


    def __init__(self, episodes_dir, transform_func=identity):