From ccea89112b3d282b6784fd26ace98f9787b81ee2 Mon Sep 17 00:00:00 2001 From: David Kuhta Date: Wed, 27 Dec 2017 16:25:40 -0500 Subject: [PATCH] 1. Modified imports to use six.moves where applicable for Python2/3 compatibility 2. Moved referenced files to 'data' folder 3. Modified MANIFEST.in for recursive expansion and incorporation of data files.4. Updated revision --- MANIFEST.in | 2 +- dasc2/bin/build_orders.py | 6 +++--- dasc2/bin/download_replays.py | 8 ++------ dasc2/bin/extract_replays.py | 17 +++++++++++------ dasc2/bin/generate_states.py | 2 -- dasc2/{ref => data}/abilities.json | 0 dasc2/{ref => data}/build_versions.json | 0 dasc2/{ref/units_full.json => data/units.json} | 0 .../{ref/units.json => data/units_simple.json} | 0 dasc2/lib/process_helpers.py | 18 +++++++++++++++--- dasc2/lib/replay_helpers.py | 2 +- dasc2/lib/replay_processor.py | 4 ++-- dasc2/ref/__init__.py | 13 ------------- setup.py | 3 +-- 14 files changed, 36 insertions(+), 39 deletions(-) rename dasc2/{ref => data}/abilities.json (100%) rename dasc2/{ref => data}/build_versions.json (100%) rename dasc2/{ref/units_full.json => data/units.json} (100%) rename dasc2/{ref/units.json => data/units_simple.json} (100%) delete mode 100644 dasc2/ref/__init__.py diff --git a/MANIFEST.in b/MANIFEST.in index 06c970e..b700091 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1 @@ -include ref/build_versions.json \ No newline at end of file +recursive-include dasc2/data * \ No newline at end of file diff --git a/dasc2/bin/build_orders.py b/dasc2/bin/build_orders.py index 76f8112..85254c6 100644 --- a/dasc2/bin/build_orders.py +++ b/dasc2/bin/build_orders.py @@ -30,7 +30,7 @@ def __create_unit_dict(): Dictionary: { unit_id : unit_name } : """ - with open(resource_filename(__name__, '../ref/units.json')) as units_data: + with open(resource_filename(__name__, '../data/units_simple.json')) as units_data: units = json.load(units_data) unit_ids = { int(unit_id) : name for unit_id, name in units.items()} return unit_ids @@ -90,8 +90,8 @@ def build_order(states, unit_id_dict): labeled_commanding_b_o = __label_units(commanding_b_o, unit_id_dict) labeled_opposing_b_o = __label_units(opposing_b_o, unit_id_dict) - build_data = { "Commanding " : labeled_commanding_b_o.keys(), - "Opposing" : labeled_opposing_b_o.keys() } + build_data = { "Commanding " : list(labeled_commanding_b_o.keys()), + "Opposing" : list(labeled_opposing_b_o.keys()) } return build_data diff --git a/dasc2/bin/download_replays.py b/dasc2/bin/download_replays.py index d2e0b82..5c9afb2 100644 --- a/dasc2/bin/download_replays.py +++ b/dasc2/bin/download_replays.py @@ -31,12 +31,8 @@ import argparse import shutil -# support both python 2 & 3 -try: - import urlparse as urlparser -except ImportError: - import urllib.parse as urlparser - +import six +from six.moves.urllib import parse as urlparser from dasc2.lib.replay_helpers import check_build_version diff --git a/dasc2/bin/extract_replays.py b/dasc2/bin/extract_replays.py index 8939292..aeab9e9 100644 --- a/dasc2/bin/extract_replays.py +++ b/dasc2/bin/extract_replays.py @@ -23,6 +23,9 @@ import sys sys.path.insert(0, '../') +import six +from six.moves import input + def extract_replays(archives_dir, replays_dir, password, remove=False): """Function which extracts replays Args: @@ -67,7 +70,7 @@ def __prompt_for_licensing_agreement(license_agreement): valid_option = {"iagreetotheeula": "iagreetotheeula", "no": False, "n": False} # Create user prompt - prompt = " [denoted password or 'No'] \n" + prompt = " [denoted password or 'No'] \n\n" # Intialize counter count = 0 @@ -76,10 +79,12 @@ def __prompt_for_licensing_agreement(license_agreement): # Output the license_agreement and prompt sys.stdout.write(license_agreement + prompt) # Assign user input to choice - try: - choice = raw_input().lower() - except NameError: - choice = input().lower() + #REMOVING + # try: + # choice = raw_input().lower() + # except NameError: + # choice = input().lower() + choice = input().lower() # If user entires a valid option return it's corresponding value if choice in valid_option: @@ -87,7 +92,7 @@ def __prompt_for_licensing_agreement(license_agreement): # Else provide the user with three additional attempts elif count < 3: count += 1 - sys.stdout.write("Please respond with the denoted password or 'no'.\n") + sys.stdout.write("Please respond with the denoted password or 'no'.\n\n") else: return False diff --git a/dasc2/bin/generate_states.py b/dasc2/bin/generate_states.py index 846b49c..4372659 100644 --- a/dasc2/bin/generate_states.py +++ b/dasc2/bin/generate_states.py @@ -33,8 +33,6 @@ from future.builtins import range from websocket import _exceptions -import six -from six.moves import queue from pysc2 import run_configs diff --git a/dasc2/ref/abilities.json b/dasc2/data/abilities.json similarity index 100% rename from dasc2/ref/abilities.json rename to dasc2/data/abilities.json diff --git a/dasc2/ref/build_versions.json b/dasc2/data/build_versions.json similarity index 100% rename from dasc2/ref/build_versions.json rename to dasc2/data/build_versions.json diff --git a/dasc2/ref/units_full.json b/dasc2/data/units.json similarity index 100% rename from dasc2/ref/units_full.json rename to dasc2/data/units.json diff --git a/dasc2/ref/units.json b/dasc2/data/units_simple.json similarity index 100% rename from dasc2/ref/units.json rename to dasc2/data/units_simple.json diff --git a/dasc2/lib/process_helpers.py b/dasc2/lib/process_helpers.py index cea0608..402df99 100644 --- a/dasc2/lib/process_helpers.py +++ b/dasc2/lib/process_helpers.py @@ -17,6 +17,7 @@ """A module containing helper functions for processing SC2 minimaps""" import numpy as np +import json from collections import defaultdict from dasc2.lib.minimap_processor import SC2MinimapProcessor, sf @@ -34,7 +35,7 @@ def process_minimap(minimap, screen): "Opposing" alignments """ mp = SC2MinimapProcessor(minimap, screen) - + minimap_data = { "Camera" : minimap[3,:,:].tolist(), "PlayerID" : minimap[4,:,:].tolist(), @@ -54,7 +55,7 @@ def process_minimap(minimap, screen): "HPShields" : mp.hp_and_shields(4) } } - + return minimap_data def army_count(screen, alignment): @@ -82,4 +83,15 @@ def army_count(screen, alignment): for unit in gen: army_dict[str(unit[0])] += unit[1] - return army_dict \ No newline at end of file + return army_dict + +class NumpyEncoder(json.JSONEncoder): + """Modifies JSON encoder to correctly process JSON holding int64""" + def default(self, obj): + if isinstance(obj, np.integer): + return int(obj) + elif isinstance(obj, np.floating): + return float(obj) + elif isinstance(obj, np.ndarray): + return obj.tolist() + return json.JSONEncoder.default(self, obj) \ No newline at end of file diff --git a/dasc2/lib/replay_helpers.py b/dasc2/lib/replay_helpers.py index c8b45eb..353d109 100644 --- a/dasc2/lib/replay_helpers.py +++ b/dasc2/lib/replay_helpers.py @@ -84,7 +84,7 @@ def check_build_version(build_version, return_label): Raises error in the event of an unsupported build type """ # Process the build_versions JSON file - version_file = open(resource_filename(__name__, '../ref/build_versions.json')).read() + version_file = open(resource_filename(__name__, '../data/build_versions.json')).read() versions = json.loads(version_file) # Cast build_version to int if it's an int diff --git a/dasc2/lib/replay_processor.py b/dasc2/lib/replay_processor.py index 3544207..6ab75b0 100644 --- a/dasc2/lib/replay_processor.py +++ b/dasc2/lib/replay_processor.py @@ -46,7 +46,7 @@ from s2clientprotocol import sc2api_pb2 as sc_pb from dasc2.lib.stats import ProcessStats -from dasc2.lib.process_helpers import army_count, process_minimap +from dasc2.lib.process_helpers import army_count, process_minimap, NumpyEncoder import numpy as np import json @@ -287,7 +287,7 @@ def process_replay(self, controller, replay_data, map_data, "PlayerID" : player_id, "Won": won, "Race" : race, "EnemyRace" : enemy_race, "States" : state_list } with open(states_file, 'a') as outfile: - json.dump(final_state, outfile) + json.dump(final_state, outfile, cls=NumpyEncoder) break diff --git a/dasc2/ref/__init__.py b/dasc2/ref/__init__.py deleted file mode 100644 index c3dc956..0000000 --- a/dasc2/ref/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright 2017 David Kuhta & Anshul Sacheti. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS-IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. \ No newline at end of file diff --git a/setup.py b/setup.py index f87f774..604eacf 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ setup( name='daSC2', - version='0.2.1-dev3', + version='0.2.2-dev3', description='Data Analytics Library for StarCraft II', long_description=description, author='David Kuhta', @@ -44,7 +44,6 @@ 'dasc2', 'dasc2.bin', 'dasc2.lib', - 'dasc2.ref', 'dasc2.agent' ], install_requires=[