Skip to content

Commit

Permalink
Merge pull request #7 from Flegyas/develop
Browse files Browse the repository at this point in the history
0.0.6
  • Loading branch information
Flegyas authored Oct 31, 2023
2 parents fda9167 + 46fc727 commit 09194e1
Show file tree
Hide file tree
Showing 24 changed files with 1,120 additions and 638 deletions.
95 changes: 36 additions & 59 deletions .github/workflows/test_suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,27 @@ on:
- synchronize

env:
CACHE_NUMBER: 1 # increase to reset cache manually
CONDA_ENV_FILE: './env.yaml'
CONDA_ENV_NAME: 'latentis'

CONDA_ENV_FILE: "./env.yaml"
CONDA_ENV_NAME: "latentis"

jobs:
build:

strategy:
fail-fast: false
matrix:
python-version: ['3.8', '3.9']
python-version: ["3.8", "3.9", "3.10"]
include:
- os: ubuntu-20.04
label: linux-64
prefix: /usr/share/miniconda3/envs/

# - os: macos-latest
# label: osx-64
# prefix: /Users/runner/miniconda3/envs/$CONDA_ENV_NAME
# - os: macos-latest
# label: osx-64
# prefix: /Users/runner/miniconda3/envs/$CONDA_ENV_NAME

# - os: windows-latest
# label: win-64
# prefix: C:\Miniconda3\envs\$CONDA_ENV_NAME
# - os: windows-latest
# label: win-64
# prefix: C:\Miniconda3\envs\$CONDA_ENV_NAME

name: ${{ matrix.label }}-py${{ matrix.python-version }}
runs-on: ${{ matrix.os }}
Expand All @@ -61,33 +58,22 @@ jobs:
sed -Ei '/^\s*-?\s*python\s*([#=].*)?$/d' ${{ env.CONDA_ENV_FILE }}
cat ${{ env.CONDA_ENV_FILE }}
# Remove pytorch-cuda line from the env.yml
- name: Remove explicit pytorch-cuda version from the environment
shell: bash -l {0}
run: |
sed -Ei '/^\s*-?\s*pytorch-cuda\s*([#=].*)?$/d' ${{ env.CONDA_ENV_FILE }}
cat ${{ env.CONDA_ENV_FILE }}
- name: Setup Mambaforge
uses: conda-incubator/setup-miniconda@v2
with:
miniforge-variant: Mambaforge
miniforge-version: latest
activate-environment: ${{ env.PY_CONDA_ENV_NAME }}
python-version: ${{ matrix.python-version }}
use-mamba: true

- uses: actions/cache@v2
name: Conda cache
with:
path: ${{ env.PY_PREFIX }}
key: ${{ matrix.label }}-conda-${{ matrix.python-version }}-${{ env.CACHE_NUMBER }}-${{ env.PY_CONDA_ENV_NAME }}-${{ hashFiles(env.CONDA_ENV_FILE) }}-${{hashFiles('./setup.cfg') }}
id: conda_cache

- uses: actions/cache@v2
name: Pip cache
with:
path: ~/.cache/pip
key: ${{ matrix.label }}-pip-${{ matrix.python-version }}-${{ env.CACHE_NUMBER }}-${{ env.PY_CONDA_ENV_NAME }}-${{ hashFiles(env.CONDA_ENV_FILE) }}-${{hashFiles('./setup.cfg') }}

- uses: actions/cache@v2
name: Pre-commit cache
with:
path: ~/.cache/pre-commit
key: ${{ matrix.label }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}-${{ matrix.python-version }}-${{ env.CACHE_NUMBER }}-${{ env.PY_CONDA_ENV_NAME }}-${{ hashFiles(env.CONDA_ENV_FILE) }}-${{hashFiles('./setup.cfg') }}
miniforge-variant: Mambaforge
miniforge-version: latest
environment-file: ${{ env.CONDA_ENV_FILE }}
activate-environment: ${{ env.PY_CONDA_ENV_NAME }}
python-version: ${{ matrix.python-version }}
use-mamba: true

# Ensure the hack for the python version worked
- name: Ensure we have the right Python
Expand All @@ -97,30 +83,18 @@ jobs:
echo "Expected: ${{ matrix.python-version }}"
python --version | grep "Python ${{ matrix.python-version }}"
# https://stackoverflow.com/questions/70520120/attributeerror-module-setuptools-distutils-has-no-attribute-version
# https://github.com/pytorch/pytorch/pull/69904
- name: Downgrade setuptools due to a but in PyTorch 1.10.1
shell: bash -l {0}
run: |
pip install setuptools==59.5.0 --upgrade
- name: Update conda environment
run: mamba env update -n ${{ env.PY_CONDA_ENV_NAME }} -f ${{ env.CONDA_ENV_FILE }}
if: steps.conda_cache.outputs.cache-hit != 'true'

# Update pip env whether or not there was a conda cache hit
- name: Update pip environment
shell: bash -l {0}
run: pip install -e ".[dev]"
if: steps.conda_cache.outputs.cache-hit == 'true'

# # https://stackoverflow.com/questions/70520120/attributeerror-module-setuptools-distutils-has-no-attribute-version
# # https://github.com/pytorch/pytorch/pull/69904
# - name: Downgrade setuptools due to a bug in PyTorch 1.10.1
# shell: bash -l {0}
# run: |
# pip install setuptools==59.5.0 --upgrade
- run: pip3 list
shell: bash -l {0}
- run: mamba info
- run: mamba list

# Ensure the hack for the python version worked
# Ensure the hack for the python version worked
- name: Ensure we have the right Python
shell: bash -l {0}
run: |
Expand All @@ -134,9 +108,12 @@ jobs:
pre-commit install
pre-commit run -v --all-files --show-diff-on-failure
- name: Test with pytest
- name: Run tests and collect coverage
shell: bash -l {0}
run: |
pytest -v
#
#
pytest --cov --cov-report xml:coverage.xml
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
105 changes: 55 additions & 50 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-added-large-files # prevents giant files from being committed.
args: ['--maxkb=4096']
- id: check-ast # simply checks whether the files parse as valid python.
# - id: check-byte-order-marker # forbids files which have a utf-8 byte-order marker.
# - id: check-builtin-literals # requires literal syntax when initializing empty or zero python builtin types.
# - id: check-case-conflict # checks for files that would conflict in case-insensitive filesystems.
- id: check-docstring-first # checks a common error of defining a docstring after code.
- id: check-executables-have-shebangs # ensures that (non-binary) executables have a shebang.
# - id: check-json # checks json files for parseable syntax.
# - id: check-shebang-scripts-are-executable # ensures that (non-binary) files with a shebang are executable.
# - id: pretty-format-json # sets a standard for formatting json files.
- id: check-merge-conflict # checks for files that contain merge conflict strings.
- id: check-symlinks # checks for symlinks which do not point to anything.
- id: check-toml # checks toml files for parseable syntax.
# - id: check-vcs-permalinks # ensures that links to vcs websites are permalinks.
# - id: check-xml # checks xml files for parseable syntax.
- id: check-yaml # checks yaml files for parseable syntax.
- id: debug-statements # checks for debugger imports and py37+ `breakpoint()` calls in python source.
- id: destroyed-symlinks # detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to.
# - id: detect-aws-credentials # detects *your* aws credentials from the aws cli credentials file.
- id: detect-private-key # detects the presence of private keys.
# - id: double-quote-string-fixer # replaces double quoted strings with single quoted strings.
- id: end-of-file-fixer # ensures that a file is either empty, or ends with one newline.
# - id: file-contents-sorter # sorts the lines in specified files (defaults to alphabetical). you must provide list of target files as input in your .pre-commit-config.yaml file.
# - id: fix-byte-order-marker # removes utf-8 byte order marker.
# - id: fix-encoding-pragma # adds # -*- coding: utf-8 -*- to the top of python files.
# - id: forbid-new-submodules # prevents addition of new git submodules.
- id: mixed-line-ending # replaces or checks mixed line ending.
args: ['--fix=no']
# - id: name-tests-test # this verifies that test files are named correctly.
# - id: no-commit-to-branch # don't commit to branch
# - id: requirements-txt-fixer # sorts entries in requirements.txt.
# - id: sort-simple-yaml # sorts simple yaml files which consist only of top-level keys, preserving comments and blocks.
- id: trailing-whitespace # trims trailing whitespace.
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-added-large-files # prevents giant files from being committed.
args: ["--maxkb=4096"]
- id: check-ast # simply checks whether the files parse as valid python.
# - id: check-byte-order-marker # forbids files which have a utf-8 byte-order marker.
# - id: check-builtin-literals # requires literal syntax when initializing empty or zero python builtin types.
# - id: check-case-conflict # checks for files that would conflict in case-insensitive filesystems.
- id: check-docstring-first # checks a common error of defining a docstring after code.
- id: check-executables-have-shebangs # ensures that (non-binary) executables have a shebang.
# - id: check-json # checks json files for parseable syntax.
# - id: check-shebang-scripts-are-executable # ensures that (non-binary) files with a shebang are executable.
# - id: pretty-format-json # sets a standard for formatting json files.
- id: check-merge-conflict # checks for files that contain merge conflict strings.
- id: check-symlinks # checks for symlinks which do not point to anything.
- id: check-toml # checks toml files for parseable syntax.
# - id: check-vcs-permalinks # ensures that links to vcs websites are permalinks.
# - id: check-xml # checks xml files for parseable syntax.
- id: check-yaml # checks yaml files for parseable syntax.
- id: debug-statements # checks for debugger imports and py37+ `breakpoint()` calls in python source.
- id: destroyed-symlinks # detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to.
# - id: detect-aws-credentials # detects *your* aws credentials from the aws cli credentials file.
- id: detect-private-key # detects the presence of private keys.
# - id: double-quote-string-fixer # replaces double quoted strings with single quoted strings.
- id: end-of-file-fixer # ensures that a file is either empty, or ends with one newline.
# - id: file-contents-sorter # sorts the lines in specified files (defaults to alphabetical). you must provide list of target files as input in your .pre-commit-config.yaml file.
# - id: fix-byte-order-marker # removes utf-8 byte order marker.
# - id: fix-encoding-pragma # adds # -*- coding: utf-8 -*- to the top of python files.
# - id: forbid-new-submodules # prevents addition of new git submodules.
- id: mixed-line-ending # replaces or checks mixed line ending.
args: ["--fix=no"]
# - id: name-tests-test # this verifies that test files are named correctly.
# - id: no-commit-to-branch # don't commit to branch
# - id: requirements-txt-fixer # sorts entries in requirements.txt.
# - id: sort-simple-yaml # sorts simple yaml files which consist only of top-level keys, preserving comments and blocks.
- id: trailing-whitespace # trims trailing whitespace.

- repo: https://github.com/PyCQA/isort.git
rev: '5.12.0'
rev: "5.12.0"
hooks:
- id: isort

- repo: https://github.com/psf/black.git
rev: '22.3.0'
hooks:
- id: black
- id: black-jupyter
- repo: https://github.com/psf/black.git
rev: "22.3.0"
hooks:
- id: black
- id: black-jupyter

- repo: https://github.com/asottile/blacken-docs.git
rev: 'v1.12.1'
rev: "v1.12.1"
hooks:
- id: blacken-docs
- id: blacken-docs

- repo: https://github.com/PyCQA/flake8.git
rev: '4.0.1'
rev: "4.0.1"
hooks:
- id: flake8
additional_dependencies:
- flake8-docstrings==1.6.0

- repo: https://github.com/pycqa/pydocstyle.git
rev: '6.1.1'
rev: "6.1.1"
hooks:
- id: pydocstyle
- id: pydocstyle
additional_dependencies:
- toml

- repo: https://github.com/kynan/nbstripout.git
rev: '0.5.0'
rev: "0.5.0"
hooks:
- id: nbstripout
- id: nbstripout

# - repo: https://github.com/PyCQA/bandit
# rev: '1.7.4'
Expand All @@ -81,3 +81,8 @@ repos:
# args: ['-c', 'pyproject.toml', '--recursive', 'src']
# additional_dependencies:
# - toml
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.1
hooks:
- id: autoflake
args: [--remove-all-unused-imports, --in-place]
67 changes: 23 additions & 44 deletions notebooks/demo_relative.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
},
"outputs": [],
"source": [
"from latentis.relative.projection import Projections\n",
"from latentis.relative.projection import RelativeProjector\n",
"from latentis.transforms import Transforms\n",
"from latentis.relative import RelativeProjector\n",
"from latentis import relative\n",
"from latentis.space import LatentSpace\n",
"from latentis.sampling import Uniform\n",
"\n",
"import torch"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "QETgBS_lah0I"
},
"metadata": {},
"outputs": [],
"source": [
"x = torch.randn(5, 10)\n",
"anchors = x[:3]"
"space = LatentSpace(name=\"x\", vectors=torch.randn(5, 10))\n",
"anchors = space.sample(sampler=Uniform(), n=3)\n",
"space.shape, anchors.shape"
]
},
{
Expand All @@ -34,62 +34,41 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tB96Jvutanb8",
"outputId": "50eb1648-7444-4486-e42a-ee8a8ba4b2d7"
"id": "h6ZyakpmcKCa",
"outputId": "b0e32f91-97a9-4451-b25c-2445339a2787"
},
"outputs": [],
"source": [
"RelativeProjector(\n",
" projection=Projections.COSINE, anchors=anchors, abs_transforms=Transforms.StandardScaling(), rel_transforms=None\n",
")(x=x)"
"projector = RelativeProjector(\n",
" projection_fn=relative.cosine_proj,\n",
")\n",
"\n",
"projector(x=space, anchors=anchors).vectors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "keHv66YPZtY6",
"outputId": "add9119a-20d3-4b5a-81bf-51d4224d2bbd"
"id": "2NZKpscRcZRi"
},
"outputs": [],
"source": [
"RelativeProjector(projection=Projections.COSINE, abs_transforms=Transforms.StandardScaling(), rel_transforms=None)(\n",
" x=x, anchors=anchors\n",
")"
"rel_space = space.to_relative(anchors=anchors, projector=projector)\n",
"rel_space, rel_space.vectors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "h6ZyakpmcKCa",
"outputId": "b0e32f91-97a9-4451-b25c-2445339a2787"
},
"metadata": {},
"outputs": [],
"source": [
"projector = RelativeProjector(\n",
" anchors=anchors,\n",
" projection=Projections.COSINE,\n",
" abs_transforms=Transforms.StandardScaling(),\n",
" rel_transforms=Transforms.L2(),\n",
")\n",
"projector(x=x)"
"relative.cosine_proj(\n",
" x=space.vectors,\n",
" anchors=rel_space.anchors.vectors,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2NZKpscRcZRi"
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
4 changes: 4 additions & 0 deletions src/latentis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
file=sys.stderr,
)
__version__ = "unknown"

from .space import LatentSpace

__all__ = ["LatentSpace", "__version__"]
Loading

0 comments on commit 09194e1

Please sign in to comment.