Skip to content

Commit

Permalink
Merge branch 'joss' into paper
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentAuriau authored Jun 13, 2024
2 parents 0ef1aed + 19c2a24 commit 31eaa77
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 204 deletions.
21 changes: 19 additions & 2 deletions .github/actions/publish/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,23 @@ inputs:
runs:
using: composite
steps:
- name: display inputs
run: |
echo ${{ inputs.BRANCH }}
shell: bash

- name: Checkout Repository
uses: actions/checkout@v3
with:
ref: ${{ inputs.BRANCH }}
token: ${{ inputs.ACCESS_TOKEN }}

- name: check install
run: |
git status
git branch
shell: bash

- name: Install poetry
run: pip install poetry${{ inputs.POETRY_VERSION != '' && format('=={0}', inputs.POETRY_VERSION) || '' }} poetry-core${{ inputs.POETRY_CORE_VERSION != '' && format('=={0}', inputs.POETRY_CORE_VERSION) || '' }}
shell: bash
Expand All @@ -84,8 +95,14 @@ runs:

- name: Set GitHub Tag as Package Version
run: |
sed -i -r 's/__version__ *= *".*"/__version__ = "${{ github.event.release.tag_name }}"/g' ${{ inputs.PACKAGE_DIRECTORY }}__init__.py
sed -i '0,/version =.*/s//version = "'"${{ github.event.release.tag_name }}"'"/' ./pyproject.toml
if ${{ inputs.UPDATE_CODE_VERSION }}
then
vname="${{ github.event.release.tag_name }}"
vname=${vname:1}
echo $vname
sed -i -r 's/__version__ *= *".*"/__version__ = "'"$vname"'" /g' ${{ inputs.PACKAGE_DIRECTORY }}__init__.py
sed -i '0,/version =.*/s//version = "'"$vname"'"/' ./pyproject.toml
fi
shell: bash

- name: Add and Commit Version
Expand Down
15 changes: 13 additions & 2 deletions .github/workflows/release_pypi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@ jobs:
python-version: '3.10'

- name: Install from TestPyPI & run tests with installed package
id: install
run: |
python -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ choice-learn
python tests/manual_run.py
cd ../
echo ${{ github.event.release.tag_name }}
python choice-learn/tests/manual_run.py
cd choice-learn
echo "b"
git fetch --all
BRANCH=$(git branch --list -r "origin/release_*" | tr '*' ' ')
echo $BRANCH
BRANCH="${BRANCH:9}"
echo $BRANCH
echo "BRANCH=$BRANCH" >> $GITHUB_OUTPUT
- name: publish to PyPI
uses: ./.github/actions/publish
Expand All @@ -32,5 +43,5 @@ jobs:
PUBLISH_REGISTRY_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
PUBLISH_REGISTRY_USER: ${{ secrets.PYPI_USERNAME }}
UPDATE_CODE_VERSION: false
BRANCH: release_${{ github.event.release.tag_name }}
BRANCH: ${{ steps.install.outputs.BRANCH }}

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div align="center">


<img src="docs/illustrations/logos/logo_choice_learn.png" width="300">
<img src="https://github.com/artefactory/choice-learn/blob/main/docs/illustrations/logos/logo_choice_learn.png" width="300">

*Large-scale choice modeling through the lens of machine learning*
*Large-scale choice modelling through the lens of machine learning*

[![CI status](https://github.com/artefactory/choice-learn/actions/workflows/ci.yaml/badge.svg)](https://github.com/artefactory/choice-learn/actions/workflows/ci.yaml?query=branch%3Amain)
[![Linting , formatting, imports sorting: ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
Expand Down
2 changes: 1 addition & 1 deletion choice_learn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Choice-Learn library for choice modeling in Python."""

__version__ = "0.0.2"
__version__ = "0.0.4"
__author__ = "Vincent Auriau"
147 changes: 145 additions & 2 deletions choice_learn/data/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,67 @@ def __str__(self):
return f"FeatureStorage with name {self.name}"


class FeaturesStorage(Storage):
class FeaturesStorage(object):
"""Base FeaturesStorage class that redirects toward the right one."""

def __new__(
cls,
ids=None,
values=None,
values_names=None,
name=None,
as_one_hot=False,
):
"""Redirects toward the right object.
Parameters
----------
ids : Iterable, optional
IDs to references features with, by default None
values : Iterable, optional
Features to be stored, by default None
values_names : list, optional
List of names for the features to be stored, by default None
name : str, optional
Name of the FeaturesStorage, to be matched in ChoiceDataset, by default None
as_one_hot: bool
Whether features are OneHot representations or not.
Returns
-------
FeaturesStorage
One of ArrayStorage, DictStorage or OneHotStorage
"""
if as_one_hot:
return OneHotStorage(ids=ids, values=values, name=name)

if ids is None and (isinstance(values, np.ndarray) or isinstance(values, list)):
return ArrayStorage(values=values, values_names=values_names, name=name)

if ids is not None:
check_ids = np.unique(ids) == np.arange(len(ids))
if isinstance(check_ids, np.ndarray):
check_ids = check_ids.all()
if check_ids:
values = [values[np.where(np.array(ids) == i)[0][0]] for i in np.arange(len(ids))]
return ArrayStorage(values=values, values_names=values_names, name=name)

return DictStorage(
ids=ids, values=values, values_names=values_names, name=name, indexer=StorageIndexer
)


class DictStorage(Storage):
"""Function to store features with ids."""

def __init__(self, ids=None, values=None, values_names=None, name=None, indexer=StorageIndexer):
def __init__(
self,
ids=None,
values=None,
values_names=None,
name=None,
indexer=StorageIndexer,
):
"""Build the store.
Parameters
Expand All @@ -70,6 +127,7 @@ def __init__(self, ids=None, values=None, values_names=None, name=None, indexer=
name: string, optional
name of the features store
"""
print("DictStorage")
if isinstance(values, dict):
storage = values
lengths = []
Expand Down Expand Up @@ -169,6 +227,91 @@ def batch(self):
return self.indexer


class ArrayStorage(Storage):
"""Function to store features with ids as NumPy Array."""

def __init__(self, values=None, values_names=None, name=None):
"""Build the store.
Parameters
----------
values : array_like
list of values of features to store
values_names : array_like
Iterable of str indicating the name of the features. Must be same length as values.
name: string, optional
name of the features store
"""
if isinstance(values, list):
storage = np.array(values)
elif not isinstance(values, np.ndarray):
raise ValueError("ArrayStorage Values must be a list or a numpy array")

# self.storage = storage
self.values_names = values_names
self.name = name

self.shape = storage.shape
self.indexer = storage

def get_element_from_index(self, index):
"""Getter method over self.sequence.
Returns the features stored at index index. Compared to __getitem__, it does take
the index-th element of sequence but the index-th element of the store.
Parameters
----------
index : (int, list, slice)
index argument of the feature
Returns
-------
array_like
features corresponding to the index index in self.store
"""
return self.batch[index]

def __len__(self):
"""Return the length of the sequence of apparition of the features."""
return self.shape[0]

def __getitem__(self, id_keys):
"""Subset FeaturesStorage, keeping only features which id is in keys.
Parameters
----------
id_keys : Iterable
List of ids to keep.
Returns
-------
FeaturesStorage
Subset of the FeaturesStorage, with only the features whose id is in id_keys
"""
if not isinstance(id_keys, list):
id_keys = [id_keys]
return ArrayStorage(
values=self.batch[id_keys], values_names=self.values_names, name=self.name
)

def get_storage_type(self):
"""Functions to access stored elements dtypes.
Returns
-------
tuple
tuple of dtypes of the stored elements, as returned by np.dtype
"""
element = self.get_element_from_index(0)
return element.dtype

@property
def batch(self):
"""Indexing attribute."""
return self.indexer


class OneHotStorage(Storage):
"""Specific Storage for one hot features storage.
Expand Down
1 change: 0 additions & 1 deletion docs/paper/paper.bib
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ @Article{Harris:2020
volume = {585},
number = {7825},
pages = {357--362},
doi = {10.1038/s41586-020-2649-2},
publisher = {Springer Science and Business Media {LLC}},
}

Expand Down
Loading

0 comments on commit 31eaa77

Please sign in to comment.