Skip to content

Commit

Permalink
⚫ 🔨 Automatically lint with black (pykeen#605)
Browse files Browse the repository at this point in the history
* Automatically lint with black

* Add isort

Trigger CI

* Change line length for isort

Trigger CI

* Reverse relative imports

Trigger CI
  • Loading branch information
cthoyt authored Oct 20, 2021
1 parent 3b6b11e commit 3616656
Show file tree
Hide file tree
Showing 178 changed files with 3,594 additions and 3,263 deletions.
4 changes: 2 additions & 2 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ values =
values = [0-9A-Za-z-]+

[bumpversion:file:src/pykeen/version.py]
search = VERSION = '{current_version}'
replace = VERSION = '{new_version}'
search = VERSION = "{current_version}"
replace = VERSION = "{new_version}"

[bumpversion:file:docs/source/conf.py]
search = release = '{current_version}'
Expand Down
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ignore =
D107
# FTP related stuff
S402
E203 # whitespace before ':'
exclude =
.tox,
.git,
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tool.black]
line-length = 120
target-version = ['py37', 'py38', 'py39']

[tool.isort]
profile = "black"
multi_line_output = 3
line_length = 120
include_trailing_comma = true
reverse_relative = true
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

import setuptools

if __name__ == '__main__':
if __name__ == "__main__":
setuptools.setup()
2 changes: 1 addition & 1 deletion src/pykeen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

from .cli import main

if __name__ == '__main__':
if __name__ == "__main__":
main()
15 changes: 9 additions & 6 deletions src/pykeen/ablation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
"""Ablation studies in PyKEEN."""

from .ablation import (
ablation_pipeline, ablation_pipeline_from_config, prepare_ablation, prepare_ablation_from_config,
ablation_pipeline,
ablation_pipeline_from_config,
prepare_ablation,
prepare_ablation_from_config,
prepare_ablation_from_path,
)

__all__ = [
'ablation_pipeline',
'ablation_pipeline_from_config',
'prepare_ablation_from_config',
'prepare_ablation_from_path',
'prepare_ablation',
"ablation_pipeline",
"ablation_pipeline_from_config",
"prepare_ablation_from_config",
"prepare_ablation_from_path",
"prepare_ablation",
]
132 changes: 66 additions & 66 deletions src/pykeen/ablation/ablation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from ..utils import normalize_string

__all__ = [
'ablation_pipeline',
'ablation_pipeline_from_config',
'prepare_ablation_from_config',
'prepare_ablation_from_path',
'prepare_ablation',
"ablation_pipeline",
"ablation_pipeline_from_config",
"prepare_ablation_from_config",
"prepare_ablation_from_path",
"prepare_ablation",
]

logger = logging.getLogger(__name__)
Expand All @@ -40,7 +40,7 @@ def ablation_pipeline(
regularizers: Union[None, str, List[str]] = None,
negative_sampler: Union[str, None] = None,
evaluator: Optional[str] = None,
stopper: Optional[str] = 'NopStopper',
stopper: Optional[str] = "NopStopper",
model_to_model_kwargs: Optional[Mapping2D] = None,
model_to_model_kwargs_ranges: Optional[Mapping2D] = None,
model_to_loss_to_loss_kwargs: Optional[Mapping3D] = None,
Expand All @@ -59,10 +59,10 @@ def ablation_pipeline(
stopper_kwargs: Optional[Mapping[str, Any]] = None,
n_trials: Optional[int] = 5,
timeout: Optional[int] = 3600,
metric: Optional[str] = 'hits@10',
direction: Optional[str] = 'maximize',
sampler: Optional[str] = 'random',
pruner: Optional[str] = 'nop',
metric: Optional[str] = "hits@10",
direction: Optional[str] = "maximize",
sampler: Optional[str] = "random",
pruner: Optional[str] = "nop",
metadata: Optional[Mapping] = None,
save_artifacts: bool = True,
move_to_cpu: bool = True,
Expand Down Expand Up @@ -215,9 +215,9 @@ def _run_ablation_experiments(
if not best_replicates:
continue

best_pipeline_dir = output_directory.joinpath('best_pipeline')
best_pipeline_dir = output_directory.joinpath("best_pipeline")
best_pipeline_dir.mkdir(exist_ok=True, parents=True)
logger.info('Re-training best pipeline and saving artifacts in %s', best_pipeline_dir)
logger.info("Re-training best pipeline and saving artifacts in %s", best_pipeline_dir)
hpo_pipeline_result.replicate_best_pipeline(
replicates=best_replicates,
move_to_cpu=move_to_cpu,
Expand All @@ -228,8 +228,8 @@ def _run_ablation_experiments(

def _create_path_with_id(directory: pathlib.Path) -> pathlib.Path:
"""Add unique id to path."""
datetime = time.strftime('%Y-%m-%d-%H-%M')
return directory.joinpath(f'{datetime}_{uuid4()}')
datetime = time.strftime("%Y-%m-%d-%H-%M")
return directory.joinpath(f"{datetime}_{uuid4()}")


def ablation_pipeline_from_config(
Expand Down Expand Up @@ -306,9 +306,9 @@ def prepare_ablation_from_config(
:return: pairs of output directories and HPO config paths inside those directories
"""
metadata = config['metadata']
optuna_config = config['optuna']
ablation_config = config['ablation']
metadata = config["metadata"]
optuna_config = config["optuna"]
ablation_config = config["ablation"]

return prepare_ablation(
**ablation_config,
Expand Down Expand Up @@ -347,13 +347,13 @@ def prepare_ablation( # noqa:C901
model_to_regularizer_to_regularizer_kwargs_ranges: Optional[Mapping3D] = None,
n_trials: Optional[int] = 5,
timeout: Optional[int] = 3600,
metric: Optional[str] = 'hits@10',
direction: Optional[str] = 'maximize',
sampler: Optional[str] = 'random',
pruner: Optional[str] = 'nop',
metric: Optional[str] = "hits@10",
direction: Optional[str] = "maximize",
sampler: Optional[str] = "random",
pruner: Optional[str] = "nop",
evaluator_kwargs: Optional[Mapping[str, Any]] = None,
evaluation_kwargs: Optional[Mapping[str, Any]] = None,
stopper: Optional[str] = 'NopStopper',
stopper: Optional[str] = "NopStopper",
stopper_kwargs: Optional[Mapping[str, Any]] = None,
metadata: Optional[Mapping] = None,
save_artifacts: bool = True,
Expand Down Expand Up @@ -468,31 +468,31 @@ def prepare_ablation( # noqa:C901
optimizer,
training_loop,
) in enumerate(it):
dataset_name = normalize_string(dataset) if isinstance(dataset, str) else 'user_data'
experiment_name = f'{counter:04d}_{dataset_name}_{normalize_string(model)}'
dataset_name = normalize_string(dataset) if isinstance(dataset, str) else "user_data"
experiment_name = f"{counter:04d}_{dataset_name}_{normalize_string(model)}"
output_directory = directory.joinpath(experiment_name)
output_directory.mkdir(exist_ok=True, parents=True)
# TODO what happens if already exists?

_experiment_optuna_config = {
'n_trials': n_trials,
'timeout': timeout,
'metric': metric,
'direction': direction,
'sampler': sampler,
'pruner': pruner,
"n_trials": n_trials,
"timeout": timeout,
"metric": metric,
"direction": direction,
"sampler": sampler,
"pruner": pruner,
}
_experiment_optuna_config['storage'] = f'sqlite:///{output_directory.as_posix()}/optuna_results.db'
_experiment_optuna_config["storage"] = f"sqlite:///{output_directory.as_posix()}/optuna_results.db"
if save_artifacts:
save_model_directory = output_directory.joinpath('artifacts')
save_model_directory = output_directory.joinpath("artifacts")
save_model_directory.mkdir(exist_ok=True, parents=True)
_experiment_optuna_config['save_model_directory'] = save_model_directory.as_posix()
_experiment_optuna_config["save_model_directory"] = save_model_directory.as_posix()

hpo_config: Dict[str, Any] = dict()
hpo_config['stopper'] = stopper
hpo_config["stopper"] = stopper

if stopper_kwargs is not None:
hpo_config['stopper_kwargs'] = stopper_kwargs
hpo_config["stopper_kwargs"] = stopper_kwargs

# TODO incorporate setting of random seed
# pipeline_kwargs=dict(
Expand All @@ -508,105 +508,105 @@ def _set_arguments(config: Optional[Mapping3D], key: str, value: str) -> None:

# Add dataset to current_pipeline
if isinstance(dataset, str):
hpo_config['dataset'] = dataset
hpo_config["dataset"] = dataset
elif isinstance(dataset, dict):
# Training, test, and validation paths are provided
hpo_config['training'] = dataset['training']
hpo_config['testing'] = dataset['testing']
hpo_config['validation'] = dataset['validation']
hpo_config["training"] = dataset["training"]
hpo_config["testing"] = dataset["testing"]
hpo_config["validation"] = dataset["validation"]
else:
raise ValueError(
"Dataset must be either the dataset name, i.e., of type str, or a dictionary containing\n"
"the paths to the training, testing, and validation data.",
)
logger.info(f"Dataset: {dataset}")
hpo_config['dataset_kwargs'] = dict(create_inverse_triples=create_inverse_triples)
hpo_config["dataset_kwargs"] = dict(create_inverse_triples=create_inverse_triples)
logger.info(f"Add inverse triples: {create_inverse_triples}")

hpo_config['model'] = model
hpo_config['model_kwargs'] = model_to_model_kwargs.get(model, {})
hpo_config['model_kwargs_ranges'] = model_to_model_kwargs_ranges.get(model, {})
hpo_config["model"] = model
hpo_config["model_kwargs"] = model_to_model_kwargs.get(model, {})
hpo_config["model_kwargs_ranges"] = model_to_model_kwargs_ranges.get(model, {})
logger.info(f"Model: {model}")

# Add loss function to current_pipeline
hpo_config['loss'] = loss
_set_arguments(config=model_to_loss_to_loss_kwargs, key='loss_kwargs', value=loss)
_set_arguments(config=model_to_loss_to_loss_kwargs_ranges, key='loss_kwargs_ranges', value=loss)
hpo_config["loss"] = loss
_set_arguments(config=model_to_loss_to_loss_kwargs, key="loss_kwargs", value=loss)
_set_arguments(config=model_to_loss_to_loss_kwargs_ranges, key="loss_kwargs_ranges", value=loss)
logger.info(f"Loss functions: {loss}")

# Add regularizer to current_pipeline
if regularizer is not None:
hpo_config['regularizer'] = regularizer
hpo_config["regularizer"] = regularizer
_set_arguments(
config=model_to_regularizer_to_regularizer_kwargs,
key='regularizer_kwargs',
key="regularizer_kwargs",
value=regularizer,
)
_set_arguments(
config=model_to_regularizer_to_regularizer_kwargs_ranges,
key='regularizer_kwargs_ranges',
key="regularizer_kwargs_ranges",
value=regularizer,
)
logger.info(f"Regularizer: {regularizer}")

# Add optimizer to current_pipeline
hpo_config['optimizer'] = optimizer
_set_arguments(config=model_to_optimizer_to_optimizer_kwargs, key='optimizer_kwargs', value=optimizer)
hpo_config["optimizer"] = optimizer
_set_arguments(config=model_to_optimizer_to_optimizer_kwargs, key="optimizer_kwargs", value=optimizer)
_set_arguments(
config=model_to_optimizer_to_optimizer_kwargs_ranges,
key='optimizer_kwargs_ranges',
key="optimizer_kwargs_ranges",
value=optimizer,
)
logger.info(f"Optimizer: {optimizer}")

# Add training approach to current_pipeline
hpo_config['training_loop'] = training_loop
hpo_config["training_loop"] = training_loop
_set_arguments(
config=model_to_training_loop_to_training_loop_kwargs,
key='training_loop_kwargs',
key="training_loop_kwargs",
value=training_loop,
)
_set_arguments(config=model_to_training_loop_to_training_kwargs, key='training_kwargs', value=training_loop)
_set_arguments(config=model_to_training_loop_to_training_kwargs, key="training_kwargs", value=training_loop)
_set_arguments(
config=model_to_training_loop_to_training_kwargs_ranges,
key='training_kwargs_ranges',
key="training_kwargs_ranges",
value=training_loop,
)
logger.info(f"Training loop: {training_loop}")

if issubclass(training_loop_resolver.lookup(training_loop), SLCWATrainingLoop):
negative_sampler = negative_sampler or 'basic' # default to basic
negative_sampler = negative_sampler or "basic" # default to basic
_set_arguments(
config=model_to_neg_sampler_to_neg_sampler_kwargs,
key='negative_sampler_kwargs',
key="negative_sampler_kwargs",
value=negative_sampler,
)
_set_arguments(
config=model_to_neg_sampler_to_neg_sampler_kwargs_ranges,
key='negative_sampler_kwargs_ranges',
key="negative_sampler_kwargs_ranges",
value=negative_sampler,
)
logger.info(f"Negative sampler: {negative_sampler}")

# Add evaluation
hpo_config['evaluator'] = evaluator
hpo_config["evaluator"] = evaluator
if evaluator_kwargs:
hpo_config['evaluator_kwargs'] = evaluator_kwargs
hpo_config['evaluation_kwargs'] = evaluation_kwargs or {}
hpo_config["evaluator_kwargs"] = evaluator_kwargs
hpo_config["evaluation_kwargs"] = evaluation_kwargs or {}
logger.info(f"Evaluator: {evaluator}")

if epochs is not None:
hpo_config.setdefault('training_kwargs', {}).setdefault('num_epochs', epochs)
hpo_config.setdefault("training_kwargs", {}).setdefault("num_epochs", epochs)

rv_config = dict(
type='hpo',
type="hpo",
metadata=metadata or {},
pipeline=hpo_config,
optuna=_experiment_optuna_config,
)

rv_config_path = output_directory.joinpath('hpo_config.json')
with rv_config_path.open('w') as file:
rv_config_path = output_directory.joinpath("hpo_config.json")
with rv_config_path.open("w") as file:
json.dump(rv_config, file, indent=2, ensure_ascii=True)

directories.append((output_directory, rv_config_path))
Expand Down
Loading

0 comments on commit 3616656

Please sign in to comment.