Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong lib name in ImportError mesage #577

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metrics/bleu/tokenizer_13a.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __call__(self, line):
:param line: a segment to tokenize
:return: the tokenized line
"""
for (_re, repl) in self._re:
for _re, repl in self._re:
line = _re.sub(repl, line)

# no leading or trailing spaces, single space within words
Expand Down
2 changes: 1 addition & 1 deletion metrics/google_bleu/tokenizer_13a.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __call__(self, line):
:param line: a segment to tokenize
:return: the tokenized line
"""
for (_re, repl) in self._re:
for _re, repl in self._re:
line = _re.sub(repl, line)

# no leading or trailing spaces, single space within words
Expand Down
16 changes: 10 additions & 6 deletions metrics/indic_glue/indic_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,16 @@ def _info(self):
inputs_description=_KWARGS_DESCRIPTION,
features=datasets.Features(
{
"predictions": datasets.Value("int64")
if self.config_name != "cvit-mkb-clsr"
else datasets.Sequence(datasets.Value("float32")),
"references": datasets.Value("int64")
if self.config_name != "cvit-mkb-clsr"
else datasets.Sequence(datasets.Value("float32")),
"predictions": (
datasets.Value("int64")
if self.config_name != "cvit-mkb-clsr"
else datasets.Sequence(datasets.Value("float32"))
),
"references": (
datasets.Value("int64")
if self.config_name != "cvit-mkb-clsr"
else datasets.Sequence(datasets.Value("float32"))
),
}
),
codebase_urls=[],
Expand Down
20 changes: 11 additions & 9 deletions metrics/roc_auc/roc_auc.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,17 @@ def _info(self):
"references": datasets.Value("int32"),
}
if self.config_name == "multiclass"
else {
"references": datasets.Sequence(datasets.Value("int32")),
"prediction_scores": datasets.Sequence(datasets.Value("float")),
}
if self.config_name == "multilabel"
else {
"references": datasets.Value("int32"),
"prediction_scores": datasets.Value("float"),
}
else (
{
"references": datasets.Sequence(datasets.Value("int32")),
"prediction_scores": datasets.Sequence(datasets.Value("float")),
}
if self.config_name == "multilabel"
else {
"references": datasets.Value("int32"),
"prediction_scores": datasets.Value("float"),
}
)
),
reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html"],
)
Expand Down
1 change: 1 addition & 0 deletions metrics/squad_v2/compute_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
This file is expected to map question ID's to the model's predicted probability
that a question is unanswerable.
"""

import argparse
import collections
import json
Expand Down
1 change: 0 additions & 1 deletion metrics/super_glue/record_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
(Some functions are adopted from the SQuAD evaluation script.)
"""


import argparse
import json
import re
Expand Down
1 change: 0 additions & 1 deletion src/evaluate/evaluator/audio_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def compute(
label_column: str = "label",
label_mapping: Optional[Dict[str, Number]] = None,
) -> Tuple[Dict[str, float], Any]:

"""
input_column (`str`, defaults to `"file"`):
The name of the column containing either the audio files or a raw waveform, represented as a numpy array, in the dataset specified by `data`.
Expand Down
1 change: 0 additions & 1 deletion src/evaluate/evaluator/image_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def compute(
label_column: str = "label",
label_mapping: Optional[Dict[str, Number]] = None,
) -> Tuple[Dict[str, float], Any]:

"""
input_column (`str`, defaults to `"image"`):
The name of the column containing the images as PIL ImageFile in the dataset specified by `data`.
Expand Down
64 changes: 51 additions & 13 deletions src/evaluate/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@


def init_dynamic_modules(
name: str = config.MODULE_NAME_FOR_DYNAMIC_MODULES, hf_modules_cache: Optional[Union[Path, str]] = None
name: str = config.MODULE_NAME_FOR_DYNAMIC_MODULES,
hf_modules_cache: Optional[Union[Path, str]] = None,
):
"""
Create a module with name `name` in which you can add dynamic modules
Expand All @@ -71,7 +72,9 @@ def init_dynamic_modules(
return dynamic_modules_path


def import_main_class(module_path) -> Optional[Union[Type[DatasetBuilder], Type[EvaluationModule]]]:
def import_main_class(
module_path,
) -> Optional[Union[Type[DatasetBuilder], Type[EvaluationModule]]]:
"""Import a module at module_path and return its main class, a Metric by default"""
module = importlib.import_module(module_path)
main_cls_type = EvaluationModule
Expand Down Expand Up @@ -176,7 +179,11 @@ def get_imports(file_path: str) -> Tuple[str, str, str, str]:
# not be added as required dependencies
continue

match = re.match(r"^import\s+(\.?)([^\s\.]+)[^#\r\n]*(?:#\s+From:\s+)?([^\r\n]*)", line, flags=re.MULTILINE)
match = re.match(
r"^import\s+(\.?)([^\s\.]+)[^#\r\n]*(?:#\s+From:\s+)?([^\r\n]*)",
line,
flags=re.MULTILINE,
)
if match is None:
match = re.match(
r"^from\s+(\.?)([^\s\.]+)(?:[^\s]*)\s+import\s+[^#\r\n]*(?:#\s+From:\s+)?([^\r\n]*)",
Expand Down Expand Up @@ -210,7 +217,10 @@ def get_imports(file_path: str) -> Tuple[str, str, str, str]:


def _download_additional_modules(
name: str, base_path: str, imports: Tuple[str, str, str, str], download_config: Optional[DownloadConfig]
name: str,
base_path: str,
imports: Tuple[str, str, str, str],
download_config: Optional[DownloadConfig],
) -> List[Tuple[str, str]]:
"""
Download additional module for a module <name>.py at URL (or local path) <base_path>/<name>.py
Expand Down Expand Up @@ -260,6 +270,7 @@ def _download_additional_modules(
lib = importlib.import_module(library_import_name) # noqa F841
except ImportError:
library_import_name = "scikit-learn" if library_import_name == "sklearn" else library_import_name
library_import_name = "absl-py" if library_import_name == "absl" else library_import_name
needs_to_be_installed.add((library_import_name, library_import_path))
if needs_to_be_installed:
raise ImportError(
Expand Down Expand Up @@ -329,7 +340,10 @@ def _copy_script_and_other_resources_in_importable_dir(
# Record metadata associating original dataset path with local unique folder
meta_path = importable_local_file.split(".py")[0] + ".json"
if not os.path.exists(meta_path):
meta = {"original file path": original_local_path, "local file path": importable_local_file}
meta = {
"original file path": original_local_path,
"local file path": importable_local_file,
}
# the filename is *.py in our case, so better rename to filenam.json instead of filename.py.json
with open(meta_path, "w", encoding="utf-8") as meta_file:
json.dump(meta, meta_file)
Expand Down Expand Up @@ -381,7 +395,13 @@ def _create_importable_file(
)
logger.debug(f"Created importable dataset file at {importable_local_file}")
module_path = ".".join(
[os.path.basename(dynamic_modules_path), module_namespace, name.replace("/", "--"), hash, name.split("/")[-1]]
[
os.path.basename(dynamic_modules_path),
module_namespace,
name.replace("/", "--"),
hash,
name.split("/")[-1],
]
)
return module_path, hash

Expand Down Expand Up @@ -550,7 +570,13 @@ def _get_modification_time(module_hash):
)
# make the new module to be noticed by the import system
module_path = ".".join(
[os.path.basename(dynamic_modules_path), self.module_type, self.name, hash, self.name.split("--")[-1]]
[
os.path.basename(dynamic_modules_path),
self.module_type,
self.name,
hash,
self.name.split("--")[-1],
]
)
importlib.invalidate_caches()
return ImportableModule(module_path, hash)
Expand Down Expand Up @@ -614,13 +640,17 @@ def evaluation_module_factory(
if path.endswith(filename):
if os.path.isfile(path):
return LocalEvaluationModuleFactory(
path, download_mode=download_mode, dynamic_modules_path=dynamic_modules_path
path,
download_mode=download_mode,
dynamic_modules_path=dynamic_modules_path,
).get_module()
else:
raise FileNotFoundError(f"Couldn't find a metric script at {relative_to_absolute_path(path)}")
elif os.path.isfile(combined_path):
return LocalEvaluationModuleFactory(
combined_path, download_mode=download_mode, dynamic_modules_path=dynamic_modules_path
combined_path,
download_mode=download_mode,
dynamic_modules_path=dynamic_modules_path,
).get_module()
elif is_relative_path(path) and path.count("/") <= 1 and not force_local_path:
try:
Expand Down Expand Up @@ -658,21 +688,25 @@ def evaluation_module_factory(
download_mode=download_mode,
dynamic_modules_path=dynamic_modules_path,
).get_module()
except Exception as e1: # noqa: all the attempts failed, before raising the error we should check if the module is already cached.
except (
Exception
) as e1: # noqa: all the attempts failed, before raising the error we should check if the module is already cached.
# if it's a canonical module we need to check if it's any of the types
if path.count("/") == 0:
for current_type in ["metric", "comparison", "measurement"]:
try:
return CachedEvaluationModuleFactory(
f"evaluate-{current_type}--{path}", dynamic_modules_path=dynamic_modules_path
f"evaluate-{current_type}--{path}",
dynamic_modules_path=dynamic_modules_path,
).get_module()
except Exception as e2: # noqa: if it's not in the cache, then it doesn't exist.
pass
# if it's a community module we just need to check on path
elif path.count("/") == 1:
try:
return CachedEvaluationModuleFactory(
path.replace("/", "--"), dynamic_modules_path=dynamic_modules_path
path.replace("/", "--"),
dynamic_modules_path=dynamic_modules_path,
).get_module()
except Exception as e2: # noqa: if it's not in the cache, then it doesn't exist.
pass
Expand Down Expand Up @@ -746,7 +780,11 @@ def load(
"""
download_mode = DownloadMode(download_mode or DownloadMode.REUSE_DATASET_IF_EXISTS)
evaluation_module = evaluation_module_factory(
path, module_type=module_type, revision=revision, download_config=download_config, download_mode=download_mode
path,
module_type=module_type,
revision=revision,
download_config=download_config,
download_mode=download_mode,
)
evaluation_cls = import_main_class(evaluation_module.module_path)
evaluation_instance = evaluation_cls(
Expand Down
8 changes: 5 additions & 3 deletions tests/test_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ def __init__(self, v2: bool):
def __call__(self, question, context, **kwargs):
if self.v2:
return [
{"score": 0.95, "start": 31, "end": 39, "answer": "Felix"}
if i % 2 == 0
else {"score": 0.95, "start": 0, "end": 0, "answer": ""}
(
{"score": 0.95, "start": 31, "end": 39, "answer": "Felix"}
if i % 2 == 0
else {"score": 0.95, "start": 0, "end": 0, "answer": ""}
)
for i in range(len(question))
]
else:
Expand Down