Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
nmichlo committed Feb 5, 2021
2 parents 0ca93a9 + dd610a5 commit 4dd4b89
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 67 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will upload a Python Package
# using Twine when a release is created

name: publish

on:
release:
types: [created]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
python3 setup.py sdist bdist_wheel
python3 -m twine upload dist/*
44 changes: 44 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow will install Python dependencies,
# then run tests over a variety of Python versions.

name: test

on:
push:
branches: [ main, dev ]
tags: [ '*' ]
pull_request:
branches: [ main, dev ]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest] # [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.8]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements-test.txt
python3 -m pip install -r requirements.txt
- name: Test with pytest
run: |
python3 -m pytest --cov=disent tests/
- uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
# codecov automatically merges all generated files
# if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.9
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include requirements.txt
19 changes: 0 additions & 19 deletions disent/frameworks/_in_progress/__test__msp.py

This file was deleted.

26 changes: 26 additions & 0 deletions disent/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import logging
import os
import time
from dataclasses import dataclass

Expand All @@ -19,6 +20,31 @@
# ========================================================================= #


def is_test_run():
"""
This is used internally to test some scripts. There is no need
to use this function in your own scripts.
"""
return bool(os.environ.get('DISENT_TEST_RUN', False))


def test_run_int(integer: int, div=100):
"""
This is used to test some scripts, by dividing the input number.
There is no need to use this function in your own scripts.
"""
return ((integer + div - 1) // div) if is_test_run() else integer


def _set_test_run():
os.environ['DISENT_TEST_RUN'] = 'True'


# ========================================================================= #
# seeds #
# ========================================================================= #


def seed(long=777):
"""
https://pytorch.org/docs/stable/notes/randomness.html
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/overview_framework_adagvae.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from disent.frameworks.vae.weaklysupervised import AdaVae
from disent.model.ae import EncoderConv64, DecoderConv64, GaussianAutoEncoder
from disent.transform import ToStandardisedTensor
from disent.util import is_test_run

data: GroundTruthData = XYSquaresData()
dataset: Dataset = GroundTruthDatasetOrigWeakPairs(data, transform=ToStandardisedTensor())
Expand All @@ -20,5 +21,5 @@
cfg=AdaVae.cfg(beta=4, average_mode='gvae', symmetric_kl=False)
)

trainer = pl.Trainer(logger=False, checkpoint_callback=False)
trainer = pl.Trainer(logger=False, checkpoint_callback=False, fast_dev_run=is_test_run())
trainer.fit(module, dataloader)
3 changes: 2 additions & 1 deletion docs/examples/overview_framework_ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from disent.frameworks.ae.unsupervised import AE
from disent.model.ae import EncoderConv64, DecoderConv64, AutoEncoder
from disent.transform import ToStandardisedTensor
from disent.util import is_test_run

data: GroundTruthData = XYSquaresData()
dataset: Dataset = GroundTruthDataset(data, transform=ToStandardisedTensor())
Expand All @@ -20,5 +21,5 @@
cfg=AE.cfg()
)

trainer = pl.Trainer(logger=False, checkpoint_callback=False)
trainer = pl.Trainer(logger=False, checkpoint_callback=False, max_steps=1, fast_dev_run=is_test_run())
trainer.fit(module, dataloader)
3 changes: 2 additions & 1 deletion docs/examples/overview_framework_betavae.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from disent.frameworks.vae.unsupervised import BetaVae
from disent.model.ae import EncoderConv64, DecoderConv64, GaussianAutoEncoder
from disent.transform import ToStandardisedTensor
from disent.util import is_test_run

data: GroundTruthData = XYSquaresData()
dataset: Dataset = GroundTruthDataset(data, transform=ToStandardisedTensor())
Expand All @@ -20,5 +21,5 @@
cfg=BetaVae.cfg(beta=4)
)

trainer = pl.Trainer(logger=False, checkpoint_callback=False)
trainer = pl.Trainer(logger=False, checkpoint_callback=False, max_steps=1, fast_dev_run=is_test_run())
trainer.fit(module, dataloader)
7 changes: 4 additions & 3 deletions docs/examples/overview_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from disent.metrics import metric_dci, metric_mig
from disent.model.ae import EncoderConv64, DecoderConv64, GaussianAutoEncoder
from disent.transform import ToStandardisedTensor
from disent.util import is_test_run, test_run_int

data = XYObjectData()
dataset = GroundTruthDataset(data, transform=ToStandardisedTensor())
Expand All @@ -23,16 +24,16 @@ def make_vae(beta):
)

def train(module):
trainer = pl.Trainer(logger=False, checkpoint_callback=False, max_steps=256)
trainer = pl.Trainer(logger=False, checkpoint_callback=False, max_steps=256, fast_dev_run=is_test_run())
trainer.fit(module, dataloader)

# we cannot guarantee which device the representation is on
get_repr = lambda x: module.encode(x.to(module.device))

# evaluate
return {
**metric_dci(dataset, get_repr, num_train=1000, num_test=500, boost_mode='sklearn'),
**metric_mig(dataset, get_repr, num_train=2000),
**metric_dci(dataset, get_repr, num_train=10 if is_test_run() else 1000, num_test=5 if is_test_run() else 500, boost_mode='sklearn'),
**metric_mig(dataset, get_repr, num_train=20 if is_test_run() else 2000),
}

a_results = train(make_vae(beta=4))
Expand Down
4 changes: 0 additions & 4 deletions pytest.ini

This file was deleted.

3 changes: 3 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

pytest == 6.2.2
pytest-cov == 2.11.1
72 changes: 36 additions & 36 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
pip == 21.0
pip>=21.0

# DATA SCIENCE & ML
# =================
numpy == 1.19.5
# pandas == 1.2.1
numpy==1.19.5
# pandas==1.2.1

# opencv-python == 4.5.1.48
# opencv-python==4.5.1.48

torch == 1.7.1
torchvision == 0.8.2
torch-optimizer == 0.1.0
pytorch-lightning == 1.1.5
# pytorch-lightning-bolts == 0.3.0
torch==1.7.1
torchvision==0.8.2
torch-optimizer==0.1.0
pytorch-lightning==1.1.7
# pytorch-lightning-bolts==0.3.0

# tensorflow == 2.4.1
# tensorflow-gpu == 2.4.1
# tensorboard == 2.4.1
# tensorflow==2.4.1
# tensorflow-gpu==2.4.1
# tensorboard==2.4.1

scipy == 1.6.0
scikit-learn == 0.24.1
# xgboost == 1.3.3
# lightgbm == 3.1.1
scipy==1.6.0
scikit-learn==0.24.1
# xgboost==1.3.3
# lightgbm==3.1.1

# IMAGE AUGMENTATION
# ==================
kornia == 0.4.1 # $ pip install git+https://github.com/kornia/kornia
# imgaug == 0.4.0
# albumentations == 0.5.2
kornia==0.4.1 # $ pip install git+https://github.com/kornia/kornia
# imgaug==0.4.0
# albumentations==0.5.2

# INPUT / OUTPUT
# ================
# psutil == 5.8.0
# pillow == 8.1.0
# imageio == 2.9.0
# moviepy == 1.0.3
h5py >= 2.10.0 # as of tensorflow 2.4 it does not support h5py 3+
# psutil==5.8.0
# pillow==8.1.0
# imageio==2.9.0
# moviepy==1.0.3
h5py>=2.10.0 # as of tensorflow 2.4 it does not support h5py 3+

# GRAPHING & DISPLAY
# ==================
matplotlib == 3.3.3
# plotly == 4.14.3
# seaborn == 0.11.1
# tqdm == 4.56.0
matplotlib==3.3.4
# plotly==4.14.3
# seaborn==0.11.1
# tqdm==4.56.0

# streamlit == 0.75.0
wandb == 0.10.15
# streamlit==0.75.0
wandb==0.10.17

# UTILITY
# =======
# pytest == 6.2.1
# pytest==6.2.1

hydra-core == 1.0.5
hydra-colorlog == 1.0.0
# hydra-joblib-launcher == 1.1.1
hydra-submitit-launcher == 1.0.1
hydra-core==1.0.6
hydra-colorlog==1.0.0
# hydra-joblib-launcher==1.1.1
hydra-submitit-launcher==1.1.0

# submitit == 1.1.5
# submitit==1.1.5
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import setuptools


with open("README.md", "r", encoding="utf-8") as file:
long_description = file.read()

with open('requirements.txt', 'r') as f:
install_requires = (req[0] for req in map(lambda x: x.split('#'), f.readlines()))
install_requires = [req for req in map(str.strip, install_requires) if req]


setuptools.setup(
name="disent",
author="Nathan Juraj Michlo",
author_email="[email protected]",

version="0.0.1.dev1",
python_requires="==3.8",
packages=setuptools.find_packages(),

install_requires=install_requires,

url="https://github.com/nmichlo/eunomia",
description="Vae disentanglement framework built with pytorch lightning.",
long_description=long_description,
long_description_content_type="text/markdown",

classifiers=[
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.8",
"Intended Audience :: Science/Research",
],
)
30 changes: 30 additions & 0 deletions tests/test_docs_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys
from contextlib import contextmanager
import pytest
import os
from glob import glob
from disent.util import _set_test_run


@contextmanager
def no_stdout():
old_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
yield
sys.stdout = old_stdout


ROOT_DIR = os.path.abspath(__file__ + '/../..')


@pytest.mark.parametrize("module", [
os.path.relpath(path, ROOT_DIR).replace('/', '.')[:-3]
for path in glob(os.path.join(ROOT_DIR, 'docs/examples/**.py'))
])
def test_docs_examples(capsys, module):
# make sure everything doesnt take 5 years to run
_set_test_run()
# run all the files in the examples folder
import importlib
with no_stdout():
importlib.import_module(module)
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def test_new_functions():
s = StateSpace([2, 4, 6])
print(np.max([s.sample_factors((2, 2), factor_indices=[2, 1, 2, 2]) for i in range(100)], axis=0))
print(np.max([s.sample_missing_factors([[1, 1], [2, 2]], known_factor_indices=[0, 2]) for i in range(100)], axis=0))
print(np.min([s.resample_radius([[0, 1, 2], [0, 0, 0]], resample_radius=1, distinct=True) for i in range(1000)], axis=0).tolist())
print(np.max([s.resample_radius([[0, 1, 2], [0, 0, 0]], resample_radius=1, distinct=True) for i in range(1000)], axis=0).tolist())
# print(np.min([s.resample_radius([[0, 1, 2], [0, 0, 0]], resample_radius=1, distinct=True) for i in range(1000)], axis=0).tolist())
# print(np.max([s.resample_radius([[0, 1, 2], [0, 0, 0]], resample_radius=1, distinct=True) for i in range(1000)], axis=0).tolist())

0 comments on commit 4dd4b89

Please sign in to comment.