Skip to content

Commit

Permalink
Reformat (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaazzini authored Jun 4, 2020
1 parent 6556369 commit 83d865e
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 73 deletions.
5 changes: 1 addition & 4 deletions darwin/cli_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ def authenticate(api_key: str, default_team: Optional[bool] = None, datasets_dir
config_path.parent.mkdir(exist_ok=True)

if default_team is None:
default_team = input(f"Make {client.default_team} the default team? [y/N] ") in [
"Y",
"y",
]
default_team = input(f"Make {client.default_team} the default team? [y/N] ") in ["Y", "y"]
if datasets_dir is None:
datasets_dir = prompt("Datasets directory", "~/.darwin/datasets")

Expand Down
20 changes: 4 additions & 16 deletions darwin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@
from darwin.config import Config
from darwin.dataset import RemoteDataset
from darwin.dataset.identifier import DatasetIdentifier
from darwin.exceptions import (
InsufficientStorage,
InvalidLogin,
MissingConfig,
NotFound,
Unauthorized,
)
from darwin.utils import is_project_dir, urljoin, is_deprecated_project_dir
from darwin.exceptions import InsufficientStorage, InvalidLogin, MissingConfig, NotFound, Unauthorized
from darwin.utils import is_deprecated_project_dir, is_project_dir, urljoin
from darwin.validators import name_taken, validation_error


Expand Down Expand Up @@ -76,9 +70,7 @@ def get(
else:
return self._decode_response(response, debug)

def put(
self, endpoint: str, payload: Dict, team: Optional[str] = None, retry: bool = False, debug: bool = False
):
def put(self, endpoint: str, payload: Dict, team: Optional[str] = None, retry: bool = False, debug: bool = False):
"""Put something on the server trough HTTP
Parameters
Expand Down Expand Up @@ -437,11 +429,7 @@ def _decode_response(response, debug: bool = False):
if debug:
print(f"[ERROR {response.status_code}] {response.text}")
response.close()
return {
"error": "Response is not JSON encoded",
"status_code": response.status_code,
"text": response.text,
}
return {"error": "Response is not JSON encoded", "status_code": response.status_code, "text": response.text}

def __str__(self):
return f"Client(default_team={self.default_team})"
2 changes: 1 addition & 1 deletion darwin/dataset/local_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(

# Get the list of classes
self.classes = get_classes(
self.dataset_path, release_name, annotation_type=self.annotation_type, remove_background=True,
self.dataset_path, release_name, annotation_type=self.annotation_type, remove_background=True
)

# Get the list of stems
Expand Down
4 changes: 1 addition & 3 deletions darwin/dataset/remote_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,7 @@ def split(
make_default_split=make_default_split,
)

def classes(
self, annotation_type: str, release_name: Optional[str] = None
):
def classes(self, annotation_type: str, release_name: Optional[str] = None):
"""
Returns the list of `class_type` classes
Expand Down
4 changes: 2 additions & 2 deletions darwin/dataset/upload_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def upload_annotations(
client=client,
team=team,
annotation_type_ids=["3"], # TODO maybe in the future allow to use polygons and BB as well
cropped_image={"image_id": image_dataset_id, "scale": 0.01, "x": "0", "y": "0",},
cropped_image={"image_id": image_dataset_id, "scale": 0.01, "x": "0", "y": "0"},
dataset_id=dataset_id,
description="",
expected_occurrences=[0, 1],
Expand Down Expand Up @@ -387,7 +387,7 @@ def upload_annotations(


def _upload_annotation(
class_mapping: Dict, client: "Client", team: str, data: Dict, image_dataset_id: int, output_file_path: Path,
class_mapping: Dict, client: "Client", team: str, data: Dict, image_dataset_id: int, output_file_path: Path
):
"""
Expand Down
25 changes: 7 additions & 18 deletions darwin/dataset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,7 @@ def _stratify_samples(idx_to_classes, split_seed, test_percentage, val_percentag
# Remove duplicates within the same set
# NOTE: doing that earlier (e.g. in remove_cross_contamination()) would produce mathematical
# mistakes in the class balancing between validation and test sets.
return (
list(set(X_train.astype(np.int))),
list(set(X_val.astype(np.int))),
list(set(X_test.astype(np.int))),
)
return (list(set(X_train.astype(np.int))), list(set(X_val.astype(np.int))), list(set(X_test.astype(np.int))))


def split_dataset(
Expand Down Expand Up @@ -365,10 +361,7 @@ def split_dataset(

# Prepare the return value with the paths of the splits
splits = {}
splits["random"] = {
"train": Path(split_path / "random_train.txt"),
"val": Path(split_path / "random_val.txt"),
}
splits["random"] = {"train": Path(split_path / "random_train.txt"), "val": Path(split_path / "random_val.txt")}
splits["stratified_tag"] = {
"train": Path(split_path / "stratified_tag_train.txt"),
"val": Path(split_path / "stratified_tag_val.txt"),
Expand All @@ -394,15 +387,15 @@ def split_dataset(
# RANDOM SPLIT
# Compute split sizes
dataset_size = sum(1 for _ in annotation_files)
val_size = int(dataset_size * (val_percentage / 100.))
test_size = int(dataset_size * (test_percentage / 100.))
val_size = int(dataset_size * (val_percentage / 100.0))
test_size = int(dataset_size * (test_percentage / 100.0))
train_size = dataset_size - val_size - test_size
# Slice a permuted array as big as the dataset
np.random.seed(split_seed)
indices = np.random.permutation(dataset_size)
train_indices = list(indices[:train_size])
val_indices = list(indices[train_size: train_size + val_size])
test_indices = list(indices[train_size + val_size:])
val_indices = list(indices[train_size : train_size + val_size])
test_indices = list(indices[train_size + val_size :])
# Write files
_write_to_file(annotation_files, splits["random"]["train"], train_indices)
_write_to_file(annotation_files, splits["random"]["val"], val_indices)
Expand Down Expand Up @@ -539,11 +532,7 @@ def get_coco_format_record(
category = classes.index(obj["name"])
else:
category = obj["name"]
new_obj = {
"bbox_mode": box_mode,
"category_id": category,
"iscrowd": 0,
}
new_obj = {"bbox_mode": box_mode, "category_id": category, "iscrowd": 0}

if annotation_type == "polygon":
for point in obj["polygon"]["path"]:
Expand Down
2 changes: 1 addition & 1 deletion darwin/exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _parse_darwin_annotation(annotation):
elif "bounding_box" in annotation:
bounding_box = annotation["bounding_box"]
main_annotation = dt.make_bounding_box(
name, bounding_box["x"], bounding_box["y"], bounding_box["w"], bounding_box["h"],
name, bounding_box["x"], bounding_box["y"], bounding_box["w"], bounding_box["h"]
)
elif "tag" in annotation:
main_annotation = dt.make_tag(name)
Expand Down
2 changes: 1 addition & 1 deletion darwin/exporter/formats/cvat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import xml.etree.ElementTree as ET
import datetime
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import Generator

Expand Down
2 changes: 1 addition & 1 deletion darwin/importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _import_annotations(client: "Client", id: int, remote_classes, annotations,
annotation_class = annotation.annotation_class
annotation_class_id = remote_classes[annotation_class.annotation_type][annotation_class.name]
serialized_annotations.append(
{"annotation_class_id": annotation_class_id, "data": {annotation_class.annotation_type: annotation.data},}
{"annotation_class_id": annotation_class_id, "data": {annotation_class.annotation_type: annotation.data}}
)

if client.feature_enabled("WORKFLOW", dataset.team):
Expand Down
10 changes: 5 additions & 5 deletions darwin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self):
parser_create = subparsers.add_parser("team", help="List or pick teams. ")
parser_create.add_argument("team_name", nargs="?", type=str, help="Team name to use. ")
parser_create.add_argument(
"-c", "--current", action="store_true", required=False, help="Shows only the current team. ",
"-c", "--current", action="store_true", required=False, help="Shows only the current team. "
)

parser_convert = subparsers.add_parser("convert", help="Converts darwin json to other annotation formats.")
Expand All @@ -33,7 +33,7 @@ def __init__(self):

# DATASET
dataset = subparsers.add_parser(
"dataset", help="Dataset related functions", description="Arguments to interact with datasets",
"dataset", help="Dataset related functions", description="Arguments to interact with datasets"
)
dataset_action = dataset.add_subparsers(dest="action")

Expand Down Expand Up @@ -76,7 +76,7 @@ def __init__(self):
help="Excludes the files with the specified extension/s if a data folder is provided as data path. ",
)
parser_push.add_argument(
"-f", "--fps", type=int, default="1", help="Frames per second for video split (recommended: 1).",
"-f", "--fps", type=int, default="1", help="Frames per second for video split (recommended: 1)."
)

# Remove
Expand All @@ -87,7 +87,7 @@ def __init__(self):
parser_report = dataset_action.add_parser("report", help="Report about the annotators ")
parser_report.add_argument("dataset", type=str, help="Remote dataset name to report on.")
parser_report.add_argument(
"-g", "--granularity", choices=["day", "week", "month", "total"], help="Granularity of the report",
"-g", "--granularity", choices=["day", "week", "month", "total"], help="Granularity of the report"
)

# Export
Expand Down Expand Up @@ -137,7 +137,7 @@ def __init__(self):
parser_split.add_argument("dataset", type=str, help="Local dataset name to split.")
parser_split.add_argument("-v", "--val-percentage", type=float, required=True, help="Validation percentage.")
parser_split.add_argument(
"-t", "--test-percentage", type=float, required=False, default=0, help="Test percentage.",
"-t", "--test-percentage", type=float, required=False, default=0, help="Test percentage."
)
parser_split.add_argument("-s", "--seed", type=int, required=False, default=0, help="Split seed.")

Expand Down
16 changes: 4 additions & 12 deletions darwin/torch/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ def _return_std(image_path, mean):


class ClassificationDataset(DarwinDataset):
def __init__(
self, transform: Optional[List] = None, **kwargs,
):
def __init__(self, transform: Optional[List] = None, **kwargs):
"""See class `LocalDataset` for documentation"""
super().__init__(annotation_type="tag", **kwargs)

Expand Down Expand Up @@ -212,9 +210,7 @@ def measure_weights(self, **kwargs) -> np.ndarray:


class InstanceSegmentationDataset(DarwinDataset):
def __init__(
self, transform: Optional[List] = None, convert_polygons_to_masks: Optional[bool] = True, **kwargs,
):
def __init__(self, transform: Optional[List] = None, convert_polygons_to_masks: Optional[bool] = True, **kwargs):
"""See `LocalDataset` class for documentation"""
super().__init__(annotation_type="polygon", **kwargs)

Expand Down Expand Up @@ -309,9 +305,7 @@ def measure_weights(self, **kwargs):


class SemanticSegmentationDataset(DarwinDataset):
def __init__(
self, transform: Optional[List] = None, convert_polygons_to_masks: Optional[bool] = True, **kwargs,
):
def __init__(self, transform: Optional[List] = None, convert_polygons_to_masks: Optional[bool] = True, **kwargs):
"""See `LocalDataset` class for documentation"""
super().__init__(annotation_type="polygon", **kwargs)

Expand Down Expand Up @@ -346,9 +340,7 @@ def __getitem__(self, index: int):
sequences[:] = [s for s in sequences if len(s) >= 6]
if not sequences:
continue
annotations.append(
{"category_id": self.classes.index(obj["name"]), "segmentation": np.array(sequences),}
)
annotations.append({"category_id": self.classes.index(obj["name"]), "segmentation": np.array(sequences)})
target["annotations"] = annotations

if self.convert_polygons is not None:
Expand Down
6 changes: 2 additions & 4 deletions darwin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def prompt(msg: str, default: Optional[str] = None) -> str:


def find_files(
files: List[Union[str, Path]] = [], recursive: bool = True, files_to_exclude: List[Union[str, Path]] = [],
files: List[Union[str, Path]] = [], recursive: bool = True, files_to_exclude: List[Union[str, Path]] = []
) -> List[Path]:
"""Retrieve a list of all files belonging to supported extensions. The exploration can be made
recursive and a list of files can be excluded if desired.
Expand Down Expand Up @@ -150,9 +150,7 @@ def persist_client_configuration(

team_config = client.config.get_default_team()
config = Config(config_path)
config.set_team(
team=team_config["slug"], api_key=team_config["api_key"], datasets_dir=team_config["datasets_dir"],
)
config.set_team(team=team_config["slug"], api_key=team_config["api_key"], datasets_dir=team_config["datasets_dir"])
config.set_global(api_endpoint=client.url, base_url=client.base_url, default_team=default_team)

return config
6 changes: 2 additions & 4 deletions darwin_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ def run_demo(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="This script can be used to download a dataset from Darwin",
)
parser.add_argument(
"--datasets-dir", help="Path to where the dataset will be downloaded", default=None, type=Path,
)
parser.add_argument("--datasets-dir", help="Path to where the dataset will be downloaded", default=None, type=Path)
parser.add_argument("--dataset-slug", help="Dataset slug (see Darwin documentation)", default=None, type=str)
parser.add_argument("--team-slug", help="Team slug (see Darwin documentation)", default=None, type=str)
parser.add_argument("--api-key", help="API key to authenticate the client", default=None, type=str)
parser.add_argument(
"--config-path", help="Path to the configuration file to authenticate the client", default=None, type=Path,
"--config-path", help="Path to the configuration file to authenticate the client", default=None, type=Path
)
args = parser.parse_args()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
],
packages=setuptools.find_packages(),
entry_points={"console_scripts": ["darwin=darwin.cli:main"]},
classifiers=["Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License",],
classifiers=["Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License"],
python_requires=">=3.6",
)

0 comments on commit 83d865e

Please sign in to comment.