Skip to content

Commit

Permalink
Mod: Update SPICE installation to avoid using bash shell scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Labbeti committed Sep 25, 2023
1 parent 534ab30 commit c0efbbf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 52 deletions.
99 changes: 67 additions & 32 deletions src/aac_metrics/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import os
import os.path as osp
import platform
import shutil
import subprocess
import sys

from argparse import ArgumentParser, Namespace
from subprocess import CalledProcessError
from typing import Optional
from zipfile import ZipFile

from torch.hub import download_url_to_file

Expand Down Expand Up @@ -62,17 +64,19 @@
"url": "https://github.com/tylin/coco-caption/raw/master/pycocoevalcap/spice/spice-1.0.jar",
"fname": "spice-1.0.jar",
},
"spice_zip": {
"url": "https://panderson.me/images/SPICE-1.0.zip",
"fname": "SPICE-1.0.zip",
},
"stanford_nlp": {
"url": "https://github.com/tylin/coco-caption/raw/master/pycocoevalcap/tokenizer/stanford-corenlp-3.4.1.jar",
"fname": "stanford-corenlp-3.4.1.jar",
},
"core_nlp": {
"spice_zip": {
"url": "https://panderson.me/images/SPICE-1.0.zip",
"fname": "SPICE-1.0.zip",
"extract_to": ".",
},
"spice_corenlp_zip": {
"url": "http://nlp.stanford.edu/software/stanford-corenlp-full-2015-12-09.zip",
"fname": osp.join("SPICE-1.0", "stanford-corenlp-full-2015-12-09.zip"),
"extract_to": "lib",
},
}
_TRUE_VALUES = ("true", "1", "t")
Expand Down Expand Up @@ -206,7 +210,7 @@ def _download_spice(
os.makedirs(spice_jar_dpath, exist_ok=True)
os.makedirs(spice_local_cache_path, exist_ok=True)

for name in ("spice_zip", "core_nlp"):
for name in ("spice_zip", "spice_corenlp_zip"):
url = DATA_URLS[name]["url"]
fname = DATA_URLS[name]["fname"]
fpath = osp.join(spice_cache_dpath, fname)
Expand All @@ -222,32 +226,63 @@ def _download_spice(
os.makedirs(dpath, exist_ok=True)
download_url_to_file(url, fpath, progress=verbose > 0)

script_fname = "install_spice.sh"
script_fpath = osp.join(osp.dirname(__file__), script_fname)
if not osp.isfile(script_fpath):
raise FileNotFoundError(f"Cannot find script '{osp.basename(script_fpath)}'.")

if verbose >= 1:
pylog.info(
f"Downloading JAR sources for SPICE metric into '{spice_jar_dpath}'..."
)

if use_shell is None:
use_shell = platform.system() == "Windows"

command = ["bash", script_fpath, spice_jar_dpath]
try:
subprocess.check_call(
command,
stdout=None if verbose >= 1 else subprocess.DEVNULL,
stderr=None if verbose >= 1 else subprocess.DEVNULL,
shell=use_shell,
)
except (CalledProcessError, PermissionError) as err:
pylog.error(
f"Cannot install SPICE java source code from '{script_fname}' script."
)
raise err
if fname.endswith(".zip"):
target_dpath = osp.join(spice_cache_dpath, DATA_URLS[name]["extract_to"])
os.makedirs(target_dpath, exist_ok=True)
with ZipFile(fpath, "r") as file:
file.extractall(target_dpath)

# TODO: rm
# mv $SPICELIB/$CORENLP/stanford-corenlp-3.6.0.jar $SPICELIB/
# mv $SPICELIB/$CORENLP/stanford-corenlp-3.6.0-models.jar $SPICELIB/
# dpath_unzip="$dpath_spice/SPICE-1.0"
# mv "$dpath_unzip/lib" "$dpath_spice"
# mv "$dpath_unzip/spice-1.0.jar" "$dpath_spice"

spice_lib_dpath = osp.join(spice_cache_dpath, "lib")
spice_unzip_dpath = osp.join(spice_cache_dpath, "SPICE-1.0")
corenlp_dname = "stanford-corenlp-full-2015-12-09"

to_move = {
osp.join(
spice_lib_dpath, corenlp_dname, "stanford-corenlp-3.6.0.jar"
): spice_lib_dpath,
osp.join(
spice_lib_dpath, corenlp_dname, "stanford-corenlp-3.6.0-models.jar"
): spice_lib_dpath,
osp.join(spice_unzip_dpath, "lib"): spice_cache_dpath,
osp.join(spice_unzip_dpath, "spice-1.0.jar"): spice_cache_dpath,
}
for source, target in to_move.items():
shutil.move(source, target)

# TODO: rm
# script_fname = "install_spice.sh"
# script_fpath = osp.join(osp.dirname(__file__), script_fname)
# if not osp.isfile(script_fpath):
# raise FileNotFoundError(f"Cannot find script '{osp.basename(script_fpath)}'.")

# if verbose >= 1:
# pylog.info(
# f"Downloading JAR sources for SPICE metric into '{spice_jar_dpath}'..."
# )

# if use_shell is None:
# use_shell = platform.system() == "Windows"

# command = ["bash", script_fpath, spice_jar_dpath]
# try:
# subprocess.check_call(
# command,
# stdout=None if verbose >= 1 else subprocess.DEVNULL,
# stderr=None if verbose >= 1 else subprocess.DEVNULL,
# shell=use_shell,
# )
# except (CalledProcessError, PermissionError) as err:
# pylog.error(
# f"Cannot install SPICE java source code from '{script_fname}' script."
# )
# raise err


def _download_fense(
Expand Down
File renamed without changes.
13 changes: 4 additions & 9 deletions tests/test_compare_cet.py → tests/ign_test_compare_cet.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ class TestCompareCaptionEvaluationTools(TestCase):
# Set Up methods
@classmethod
def setUpClass(cls) -> None:
if platform.system() == "Windows":
tmp_path = osp.join(".", "tmp")
os.makedirs(tmp_path, exist_ok=True)
set_default_tmp_path(tmp_path)

cls.evaluate_metrics_from_lists = cls._import_cet_eval_func()

@classmethod
Expand Down Expand Up @@ -110,6 +105,10 @@ def _get_example_0(self) -> tuple[list[str], list[list[str]]]:
return cands, mrefs

def _test_with_example(self, cands: list[str], mrefs: list[list[str]]) -> None:
if platform.system() == "Windows":
# Skip this setup on windows
return None

corpus_scores, _ = evaluate(cands, mrefs, metrics="dcase2020")

self.assertIsInstance(corpus_scores, dict)
Expand All @@ -118,10 +117,6 @@ def _test_with_example(self, cands: list[str], mrefs: list[list[str]]) -> None:
self.assertIsInstance(score, Tensor, f"Invalid score type for {name=}")
self.assertEqual(score.ndim, 0, f"Invalid score ndim for {name=}")

if platform.system() == "Windows":
# Skip this setup on windows
return None

cet_outs = self.__class__.evaluate_metrics_from_lists(cands, mrefs)
cet_global_scores, _cet_sents_scores = cet_outs

Expand Down
File renamed without changes.
20 changes: 9 additions & 11 deletions tests/test_doc_examples.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import os.path as osp
import platform
import unittest

Expand All @@ -16,19 +14,15 @@
preprocess_mono_sents,
preprocess_mult_sents,
)
from aac_metrics.utils.paths import set_default_tmp_path


class TestReadmeExamples(TestCase):
# Set Up methods
@classmethod
def setUpClass(cls) -> None:
if platform.system() == "Windows":
tmp_path = osp.join(".", "tmp")
os.makedirs(tmp_path, exist_ok=True)
set_default_tmp_path(tmp_path)

def test_example_1(self) -> None:
# TODO : rm or redo ?
# if platform.system() == "Windows":
# # Skip this setup on windows
# return None

candidates: list[str] = ["a man is speaking", "rain falls"]
mult_references: list[list[str]] = [
[
Expand Down Expand Up @@ -66,6 +60,10 @@ def test_example_1(self) -> None:
)

def test_example_2(self) -> None:
if platform.system() == "Windows":
# Skip this setup on windows
return None

candidates: list[str] = ["a man is speaking", "rain falls"]
mult_references: list[list[str]] = [
[
Expand Down

0 comments on commit c0efbbf

Please sign in to comment.