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

Add isort Workflow #134

Open
wants to merge 8 commits into
base: master
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
5 changes: 2 additions & 3 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: psf/black@stable
with:
version: "22.1.0"
- name: Check code with black
run: touch .env && docker-compose run black
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this change with respect to the previous behavior. Could you explain what that is doing?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because you are setting the black version in the requirements-dev, so it's no longer needed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this will build the Docker image with the black version specified in dev-requirements.txt and then create a container from that image that then checks the code with black.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benefit is that most of the logic is moved from the workflow to Docker which you can also run locally.

12 changes: 12 additions & 0 deletions .github/workflows/isort.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Isort

on: [push, pull_request]

jobs:
isort:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: Check code with isort
run: touch .env && docker-compose run isort
5 changes: 5 additions & 0 deletions Dockerfile.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.9

COPY . /src/nnfabrik

RUN python -m pip install --requirement /src/nnfabrik/requirements-dev.txt
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# nnfabrik: a generalized model fitting pipeline

![Black](https://github.com/sinzlab/nnfabrik/workflows/Black/badge.svg)
![ISort](https://github.com/sinzlab/nnfabrik/workflows/ISort/badge.svg)
![GitHub Pages](https://github.com/sinzlab/nnfabrik/workflows/GitHub%20Pages/badge.svg?branch=master)

nnfabrik is a model fitting pipeline, mainly developed for neural networks, where training results (i.e. scores, and trained models) as well as any data related to models, trainers, and datasets used for training are stored in datajoint tables.
Expand Down Expand Up @@ -70,3 +71,31 @@ The documentation can be found [here](https://sinzlab.github.io/nnfabrik/). Plea
## :bug: Report bugs (or request features)

In case you find a bug or would like to see some new features added to nnfabrik, please create an issue or contact any of the contributors.

## :fire: How to contribute

Pull requests (and issues) are always welcome. This section describes some
preconditions that pull requests need to fulfill.

### black

This project uses the [black](https://github.com/psf/black) code formatter. You
can check whether your changes comply with its style by running the following
command:

```bash
docker-compose run black
```

Furthermore you can pass a path to the service to have black fix any errors in
the Python modules it finds in the given path.

### isort

[isort](https://github.com/PyCQA/isort) is used to sort Python imports. You can check the order of imports by running the following command:

```bash
docker-compose run isort
```

The imports can be sorted by passing a path to the service.
20 changes: 19 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ services:
build: .
volumes:
- ".:/src/nnfabrik"
entrypoint: /bin/bash -c "cd /src/nnfabrik/docs/; while :; do make html; sleep 30; done"
entrypoint: /bin/bash -c "cd /src/nnfabrik/docs/; while :; do make html; sleep 30; done"

black:
build:
context: .
dockerfile: Dockerfile.lint
volumes:
- .:/src/nnfabrik
entrypoint: ["black"]
command: ["--check", "--diff", "/src/nnfabrik"]

isort:
build:
context: .
dockerfile: Dockerfile.lint
volumes:
- .:/src/nnfabrik
entrypoint: ["isort"]
command: ["--check-only", "--diff", "/src/nnfabrik"]
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#
import os
import sys

import sphinx_rtd_theme

sys.path.insert(0, os.path.abspath("../"))
Expand Down
4 changes: 2 additions & 2 deletions nnfabrik/builder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from . import utility
from functools import partial

from .utility.nnf_helper import split_module_name, dynamic_import
from . import utility
from .utility.nn_helpers import load_state_dict
from .utility.nnf_helper import dynamic_import, split_module_name


def resolve_fn(fn_name, default_base):
Expand Down
1 change: 1 addition & 0 deletions nnfabrik/examples/mnist/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict

import numpy as np
import torch
import torch.nn as nn
Expand Down
7 changes: 3 additions & 4 deletions nnfabrik/examples/mnist/trainer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Dict, Tuple, Callable, List, Any
from typing import Any, Callable, Dict, List, Tuple

from tqdm import tqdm
import torch
from torch import nn
from torch import optim
from torch import nn, optim
from tqdm import tqdm


class MNISTTrainer:
Expand Down
4 changes: 2 additions & 2 deletions nnfabrik/examples/mnist_checkpoint/trainer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Dict, Tuple, Callable, Optional, List, Any
from typing import Any, Callable, Dict, List, Optional, Tuple

from tqdm import tqdm
import torch
import torch.nn as nn
from tqdm import tqdm

from nnfabrik.examples.mnist.trainer import MNISTTrainer

Expand Down
6 changes: 3 additions & 3 deletions nnfabrik/examples/mnist_transfer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
with our TransferredTrainedModel-table
"""

from typing import Dict, Tuple, Callable
from typing import Callable, Dict, Tuple

import numpy as np
from torch.utils.data import DataLoader
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from tqdm import tqdm

from nnfabrik.examples.mnist.trainer import MNISTTrainer

Expand Down
7 changes: 3 additions & 4 deletions nnfabrik/examples/nnfabrik.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# importing the tables here is a trick to get IntelliSense to work
from nnfabrik.main import Fabrikant, Trainer, Dataset, Model, Seed, my_nnfabrik
from nnfabrik.main import Dataset, Fabrikant, Model, Seed, Trainer, my_nnfabrik
from nnfabrik.templates.transfer.recipes import (
TrainedModelTransferRecipe,
DatasetTransferRecipe,
ModelTransferRecipe,
TrainerTransferRecipe,
TrainedModelTransferRecipe,
TrainerDatasetTransferRecipe,
TrainerTransferRecipe,
)


# define nnfabrik tables here
my_nnfabrik(
"nnfabrik_example",
Expand Down
12 changes: 6 additions & 6 deletions nnfabrik/main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import warnings
import types
from typing import Union, Optional, MutableMapping, Tuple
import warnings
from typing import MutableMapping, Optional, Tuple, Union

import datajoint as dj

from .builder import (
resolve_model,
resolve_data,
resolve_trainer,
get_data,
get_model,
get_trainer,
resolve_data,
resolve_model,
resolve_trainer,
)
from .utility.dj_helpers import make_hash, CustomSchema, Schema
from .utility.dj_helpers import CustomSchema, Schema, make_hash
from .utility.nnf_helper import cleanup_numpy_scalar

if "nnfabrik.schema_name" in dj.config:
Expand Down
2 changes: 1 addition & 1 deletion nnfabrik/templates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .trained_model import TrainedModelBase, DataInfoBase
from .scoring import ScoringBase
from .trained_model import DataInfoBase, TrainedModelBase
from .transfer.transferred_trained_model import TransferredTrainedModelBase
2 changes: 1 addition & 1 deletion nnfabrik/templates/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import torch

from nnfabrik.templates.trained_model import TrainedModelBase
from nnfabrik.utility.dj_helpers import make_hash, clone_conn, CustomSchema
from nnfabrik.utility.dj_helpers import CustomSchema, clone_conn, make_hash


def my_checkpoint(nnfabrik):
Expand Down
4 changes: 3 additions & 1 deletion nnfabrik/templates/scoring.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datajoint as dj
import numpy as np
from nnfabrik.main import Model, Dataset, Trainer, Seed, Fabrikant

from nnfabrik.main import Dataset, Fabrikant, Model, Seed, Trainer

from .trained_model import TrainedModelBase


Expand Down
6 changes: 4 additions & 2 deletions nnfabrik/templates/trained_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import tempfile
import torch
import os
import tempfile
import warnings

import datajoint as dj
import torch
from datajoint.fetch import DataJointError

from ..builder import get_all_parts, get_model, get_trainer, resolve_fn
from ..utility.dj_helpers import make_hash
from .utility import find_object
Expand Down
7 changes: 4 additions & 3 deletions nnfabrik/templates/transfer/transferred_trained_model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import tempfile
from collections import Mapping, Sequence

import torch
import os
import datajoint as dj
import numpy as np
import torch

from nnfabrik.templates.checkpoint import TrainedModelChkptBase
from nnfabrik.utility.dj_helpers import gitlog, make_hash
from nnfabrik.templates.trained_model import TrainedModelBase
from nnfabrik.utility.dj_helpers import gitlog, make_hash


class TransferredTrainedModelBase(TrainedModelBase):
Expand Down
3 changes: 2 additions & 1 deletion nnfabrik/templates/utility.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .. import main
from types import ModuleType
from typing import Union

from .. import main


def find_object(context: Union[ModuleType, dict], attribute: str, prop_name: str = None):
"""
Expand Down
4 changes: 1 addition & 3 deletions nnfabrik/utility/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from . import dj_helpers
from . import nnf_helper
from . import nn_helpers
from . import dj_helpers, nn_helpers, nnf_helper
12 changes: 6 additions & 6 deletions nnfabrik/utility/dj_helpers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# helper functions for use with DataJoint tables

import warnings
from datetime import datetime
import hashlib
import datajoint as dj
from git import Repo, cmd
import numpy as np
import inspect
import warnings
from collections import Iterable, Mapping, OrderedDict
from datetime import date, datetime

import datajoint as dj
import numpy as np
from datajoint.utils import to_camel_case
from collections import OrderedDict, Iterable, Mapping
from git import Repo, cmd

# try/except is necessary to support all versions of dj
try:
Expand Down
6 changes: 4 additions & 2 deletions nnfabrik/utility/hypersearch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import datajoint as dj
import numpy as np
from ax.service.managed_loop import optimize
from .nnf_helper import split_module_name, dynamic_import

from nnfabrik.main import *
import datajoint as dj

from .nnf_helper import dynamic_import, split_module_name


class Bayesian:
Expand Down
6 changes: 3 additions & 3 deletions nnfabrik/utility/nn_helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# helper functions concerning the ANN architecture

import torch
from torch import nn
import random

import numpy as np
import random
import torch
from torch import nn


def get_io_dims(data_loader):
Expand Down
4 changes: 3 additions & 1 deletion nnfabrik/utility/nnf_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from importlib import import_module

import numpy as np
from ..utility.dj_helpers import make_hash, cleanup_numpy_scalar

from ..utility.dj_helpers import cleanup_numpy_scalar, make_hash


def split_module_name(abs_class_name):
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[tool.black]
line-length = 120

[tool.isort]
profile = "black"
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
black==22.1.0
isort==5.10.1
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python
from setuptools import setup, find_packages
from os import path

from setuptools import find_packages, setup

here = path.abspath(path.dirname(__file__))

setup(
Expand Down