Skip to content

Commit

Permalink
Merge 7f1978b into 3f037dd
Browse files Browse the repository at this point in the history
  • Loading branch information
phausamann authored Aug 7, 2018
2 parents 3f037dd + 7f1978b commit 8b113c2
Show file tree
Hide file tree
Showing 17 changed files with 582 additions and 679 deletions.
14 changes: 12 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,32 @@ env:
- DISTRIB="conda" PYTHON_VERSION="3.5" COVERAGE="true"

install: source build_tools/travis/install.sh
script: bash build_tools/travis/test.sh
script:
- bash build_tools/travis/test.sh
- bash build_tools/travis/make_docs.sh

after_success:
- source build_tools/travis/success.sh
# - chmod +x build_tools/travis/deploy_anaconda.sh

deploy:

- provider: pages
local-dir: doc/_build/html
skip-cleanup: true
github-token: $GITHUB_TOKEN # Set in the settings page of your repository, as a secure variable
keep-history: true
on:
branch: master

- provider: pypi
skip_cleanup: true
user: phausamann
password:
secure: cRtfmsupJcyrZ1EU+NJ1eng0Abn9LeDVHLf1xZ1/1sg3qq6PwJwxNFJHTMin0sIJXERFAGG3btRnFqiwYsrxF7OdWObDYZN3G9riKKhS2Z5bSanWyrQk4XF/s9haONHKv2falsZ6nnux9GDMod+ojPzedNGagISLsLixHMRZmYFnUAJtdzDOm6PoNTui0+0C3bHoAIPu+FZJ1rPV1xmGM+4YGLg/j3yFt6SIY0XYY9d2torXSwD1E0+8V/kPxTcyNCQVE9LlFP3v9xLt2wYq7ehjGbetehSZyJxjchjtgABBMBkGTKqBwb3pgagaRmC9KVatpRVVVSLJRZAbFOmfK3QkZzrVzDVwOEloVhhUxUAm3rZDbZHmvHO0maS5VkpDAb3lE1edLziLiD0qqLBSuy5Tru+uELa6IO6gO8r/dA8usnKAcNWHIjrpLd3W7P+btjrmrSx8ReYs9PitKFiCLgleoAJGZFoSN0sOIAimCzvIsCvJyjlbHOvDyb+ziqvxu66yz/hBmupGibIT2529pyVW713gBOyrIvsLqzX3uDw6aYMTSi4aYp5+sfkCA5RE8Fc6PEPnqj6LbWjBF6bgelj3wUc9J4ZniuSWFMDKmBhk/p/j9CRg7RYQ5g+lK5E0oJma0vThqx8MKDivVk4oOMD8txcA0g1DJn8oE6i4ptc=
on:
branch: master
tags: true
skip_cleanup: true

# - provider: script
# script: build_tools/travis/deploy_anaconda.sh
Expand Down
5 changes: 1 addition & 4 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.. -*- mode: rst -*-
|Travis|_ |AppVeyor|_ |CircleCI|_ |Coverage|_ |PyPI|_
|Travis|_ |AppVeyor|_ |Coverage|_ |PyPI|_

.. |Travis| image:: https://travis-ci.org/phausamann/sklearn-xarray.svg?branch=master
.. _Travis: https://travis-ci.org/phausamann/sklearn-xarray
Expand All @@ -11,9 +11,6 @@
.. |Coverage| image:: https://coveralls.io/repos/github/phausamann/sklearn-xarray/badge.svg?branch=master
.. _Coverage: https://coveralls.io/github/phausamann/sklearn-xarray?branch=master

.. |CircleCI| image:: https://circleci.com/gh/phausamann/sklearn-xarray.svg?style=shield&circle-token=:circle-token
.. _CircleCI: https://circleci.com/gh/phausamann/sklearn-xarray

.. |PyPI| image:: https://badge.fury.io/py/sklearn-xarray.svg
.. _PyPI: https://badge.fury.io/py/sklearn-xarray

Expand Down
53 changes: 0 additions & 53 deletions build_tools/circleci/push_doc.sh

This file was deleted.

7 changes: 3 additions & 4 deletions build_tools/travis/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ popd

# Configure the conda environment and put it in the path using the
# provided versions
conda create -n testenv --yes python=$PYTHON_VERSION pip nose \
--file requirements.txt

# numpy=$NUMPY_VERSION scipy=$SCIPY_VERSION cython=$CYTHON_VERSION
conda create -n testenv -y -c conda-forge python=$PYTHON_VERSION \
nose matplotlib sphinx pillow sphinx-gallery sphinx_rtd_theme numpydoc \
--file requirements.txt

source activate testenv

Expand Down
10 changes: 10 additions & 0 deletions build_tools/travis/make_docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mkdir -p doc/modules/generated

cd doc
set -o pipefail && make html 2>&1 | tee ~/log.txt
cd ..

cat ~/log.txt && if grep -q "Traceback (most recent call last):" ~/log.txt; \
then false; else true; fi

cp .nojekyll doc/_build/html/.nojekyll
3 changes: 3 additions & 0 deletions build_tools/travis/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ set -e
mkdir -p $TEST_DIR
cp .coveragerc $TEST_DIR/.coveragerc

wd=$(pwd)
cd $TEST_DIR

if [[ "$COVERAGE" == "true" ]]; then
nosetests -s --with-coverage --cover-package=$MODULE $MODULE
else
nosetests -s $MODULE
fi

cd $wd
57 changes: 0 additions & 57 deletions circle.yml

This file was deleted.

2 changes: 1 addition & 1 deletion doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXOPTS = -v
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build
Expand Down
9 changes: 9 additions & 0 deletions doc/content/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ What's New
==========


Github master branch
--------------------

Enhancements
~~~~~~~~~~~~

- Added ``inverse_transform`` to ``preprocessing.Concatenator``.


v0.2.0 (April 9, 2018)
----------------------

Expand Down
21 changes: 10 additions & 11 deletions examples/plot_activity_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import GroupShuffleSplit, GridSearchCV
from sklearn.pipeline import Pipeline

Expand Down Expand Up @@ -74,16 +74,16 @@
# three-dimensional but the standardizer and classifier expect a one-dimensional
# feature vector, we have to vectorize the samples.
#
# Finally, we use PCA and logistic regression to perform the classification.
# Finally, we use PCA and a naive Bayes classifier for classification.

pl = Pipeline([
('splitter', Splitter(
groupby=['subject', 'activity'], new_dim='timepoint')),
groupby=['subject', 'activity'], new_dim='timepoint', new_len=30)),
('sanitizer', Sanitizer()),
('featurizer', Featurizer()),
('scaler', wrap(StandardScaler)),
('pca', wrap(PCA, reshapes='feature')),
('lr', wrap(LogisticRegression, reshapes='feature'))
('cls', wrap(GaussianNB, reshapes='feature'))
])

##############################################################################
Expand All @@ -94,20 +94,19 @@
# training and validation set.

cv = CrossValidatorWrapper(
GroupShuffleSplit(n_splits=3, test_size=0.3), groupby=['subject'])
GroupShuffleSplit(n_splits=2, test_size=0.5), groupby=['subject'])

##############################################################################
# The grid search will try different combinations of segment length and
# number of PCA components to find the best parameters for this task.
# The grid search will try different numbers of PCA components to find the best
# parameters for this task.
#
# .. tip::
#
# To use multi-processing, set ``n_jobs=-1``.

gs = GridSearchCV(
pl, cv=cv, n_jobs=1, verbose=1, param_grid={
'splitter__new_len': [30, 60],
'pca__n_components': [20, 40]
'pca__n_components': [10, 20]
})

##############################################################################
Expand All @@ -129,5 +128,5 @@
##############################################################################
# .. note::
#
# The performance of this classifier is obviously pretty terrible, it's only
# for demonstration purposes.
# The performance of this classifier is obviously pretty bad,
# it was chosen for execution speed, not accuracy.
74 changes: 62 additions & 12 deletions sklearn_xarray/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def _update_dims(self, X_in, X_out):
raise ValueError(
'Inconsistent dimensions returned by estimator')

# get new dims
for new_dim, old_dims in self.reshapes.items():
for d in old_dims:
dims_new.remove(d)
Expand All @@ -73,23 +72,71 @@ def _update_dims(self, X_in, X_out):

return dims_new

def _restore_dims(self, X_in, X_out):
""" Restore the dimensions of a reshaped DataArray. """

# dict syntax
if hasattr(self.reshapes, 'items'):

# check if new dims are dropped by estimator
all_old_dims = []
for _, old_dims in self.reshapes.items():
all_old_dims += old_dims

if X_in.ndim == X_out.ndim - len(all_old_dims) + len(self.reshapes):
drop_new_dims = False
elif X_in.ndim == X_out.ndim - len(all_old_dims):
drop_new_dims = True
else:
raise ValueError(
'Inconsistent dimensions returned by estimator')

# get new dims
dims_new = list(X_in.dims)
dims_old = []
for d in dims_new:
if d in self.reshapes:
dims_old += self.reshapes[d]
else:
dims_old.append(d)

if drop_new_dims:
# TODO: figure out where to insert the dropped dims
for d in all_old_dims:
if d not in dims_old:
dims_old.append(d)

# string syntax
else:
dims_old = list(X_in.dims)
# check if dim is dropped by estimator
if X_out.ndim < X_in.ndim:
# TODO: figure out where to insert the dropped dim
dims_old.append(self.reshapes)

return dims_old

def _update_coords(self, X):
""" Update the coordinates of a reshaped DataArray. """

coords_new = dict()

# dict syntax
if hasattr(self.reshapes, 'items'):
# drop all coords along the reshaped dimensions

all_old_dims = []
for _, old_dims in self.reshapes.items():
for c in X.coords:
old_dims_in_c = [x for x in X[c].dims if x in old_dims]
if any(old_dims_in_c) and c not in old_dims:
c_t = X[c].isel(**{d: 0 for d in old_dims_in_c})
new_dims = [d for d in X[c].dims if d not in old_dims]
coords_new[c] = (new_dims, c_t.drop(old_dims_in_c))
elif c not in old_dims:
coords_new[c] = X[c]
all_old_dims += old_dims

# drop all coords along the reshaped dimensions
for c in X.coords:
old_dims_in_c = [x for x in X[c].dims if x in all_old_dims]
if any(old_dims_in_c) and c not in all_old_dims:
c_t = X[c].isel(**{d: 0 for d in old_dims_in_c})
new_dims = [d for d in X[c].dims if d not in all_old_dims]
coords_new[c] = (new_dims, c_t.drop(old_dims_in_c))
elif c not in all_old_dims:
coords_new[c] = X[c]

# string syntax
else:
Expand Down Expand Up @@ -117,8 +164,11 @@ def _call_array_method(self, estimator, method, X):
else:
y = getattr(estimator, method)(X.data)

# update coords and dims
dims_new = self._update_dims(X, y)
# update dims
if method == 'inverse_transform':
dims_new = self._restore_dims(X, y)
else:
dims_new = self._update_dims(X, y)

return y, dims_new

Expand Down
Loading

0 comments on commit 8b113c2

Please sign in to comment.