From 0b5e8d895e8234a801bbd43cd90d94373f918564 Mon Sep 17 00:00:00 2001 From: Siivola Eero Date: Mon, 3 Sep 2018 18:38:21 +0300 Subject: [PATCH] Changed the place of index_to_slices to more logical place --- GPy/kern/src/ODE_UY.py | 2 +- GPy/kern/src/ODE_UYC.py | 2 +- GPy/kern/src/ODE_st.py | 2 +- GPy/kern/src/ODE_t.py | 2 +- GPy/kern/src/independent_outputs.py | 28 +---------------------- GPy/kern/src/multioutput_kern.py | 2 +- GPy/kern/src/splitKern.py | 2 +- GPy/likelihoods/multioutput_likelihood.py | 2 +- GPy/util/multioutput.py | 27 ++++++++++++++++++++++ 9 files changed, 35 insertions(+), 34 deletions(-) diff --git a/GPy/kern/src/ODE_UY.py b/GPy/kern/src/ODE_UY.py index 19fb1e94..2604773c 100644 --- a/GPy/kern/src/ODE_UY.py +++ b/GPy/kern/src/ODE_UY.py @@ -2,7 +2,7 @@ # Licensed under the BSD 3-clause license (see LICENSE.txt) from .kern import Kern -from .independent_outputs import index_to_slices +from GPy.util.multioutput import index_to_slices from ...core.parameterization import Param from paramz.transformations import Logexp import numpy as np diff --git a/GPy/kern/src/ODE_UYC.py b/GPy/kern/src/ODE_UYC.py index 57c41767..413c874a 100644 --- a/GPy/kern/src/ODE_UYC.py +++ b/GPy/kern/src/ODE_UYC.py @@ -5,7 +5,7 @@ from .kern import Kern from ...core.parameterization import Param from paramz.transformations import Logexp import numpy as np -from .independent_outputs import index_to_slices +from GPy.util.multioutput import index_to_slices class ODE_UYC(Kern): def __init__(self, input_dim, variance_U=3., variance_Y=1., lengthscale_U=1., lengthscale_Y=1., ubias =1. ,active_dims=None, name='ode_uyc'): diff --git a/GPy/kern/src/ODE_st.py b/GPy/kern/src/ODE_st.py index 0b4fecae..b7f0358a 100644 --- a/GPy/kern/src/ODE_st.py +++ b/GPy/kern/src/ODE_st.py @@ -4,7 +4,7 @@ from .kern import Kern from ...core.parameterization import Param from paramz.transformations import Logexp import numpy as np -from .independent_outputs import index_to_slices +from GPy.util.multioutput import index_to_slices class ODE_st(Kern): diff --git a/GPy/kern/src/ODE_t.py b/GPy/kern/src/ODE_t.py index f09ab77d..51a17aeb 100644 --- a/GPy/kern/src/ODE_t.py +++ b/GPy/kern/src/ODE_t.py @@ -2,7 +2,7 @@ from .kern import Kern from ...core.parameterization import Param from paramz.transformations import Logexp import numpy as np -from .independent_outputs import index_to_slices +from GPy.util.multioutput import index_to_slices class ODE_t(Kern): diff --git a/GPy/kern/src/independent_outputs.py b/GPy/kern/src/independent_outputs.py index db6f9d37..c93d689f 100644 --- a/GPy/kern/src/independent_outputs.py +++ b/GPy/kern/src/independent_outputs.py @@ -5,34 +5,8 @@ from .kern import CombinationKernel import numpy as np import itertools +from GPy.util.multioutput import index_to_slices -def index_to_slices(index): - """ - take a numpy array of integers (index) and return a nested list of slices such that the slices describe the start, stop points for each integer in the index. - - e.g. - >>> index = np.asarray([0,0,0,1,1,1,2,2,2]) - returns - >>> [[slice(0,3,None)],[slice(3,6,None)],[slice(6,9,None)]] - - or, a more complicated example - >>> index = np.asarray([0,0,1,1,0,2,2,2,1,1]) - returns - >>> [[slice(0,2,None),slice(4,5,None)],[slice(2,4,None),slice(8,10,None)],[slice(5,8,None)]] - """ - if len(index)==0: - return[] - - #contruct the return structure - ind = np.asarray(index,dtype=np.int) - ret = [[] for i in range(ind.max()+1)] - - #find the switchpoints - ind_ = np.hstack((ind,ind[0]+ind[-1]+1)) - switchpoints = np.nonzero(ind_ - np.roll(ind_,+1))[0] - - [ret[ind_i].append(slice(*indexes_i)) for ind_i,indexes_i in zip(ind[switchpoints[:-1]],zip(switchpoints,switchpoints[1:]))] - return ret class IndependentOutputs(CombinationKernel): """ diff --git a/GPy/kern/src/multioutput_kern.py b/GPy/kern/src/multioutput_kern.py index b7feaadb..2c26d544 100644 --- a/GPy/kern/src/multioutput_kern.py +++ b/GPy/kern/src/multioutput_kern.py @@ -1,7 +1,7 @@ from .kern import Kern, CombinationKernel import numpy as np from functools import reduce, partial -from .independent_outputs import index_to_slices +from GPy.util.multioutput import index_to_slices from paramz.caching import Cache_this class ZeroKern(Kern): diff --git a/GPy/kern/src/splitKern.py b/GPy/kern/src/splitKern.py index 324178d4..43461230 100644 --- a/GPy/kern/src/splitKern.py +++ b/GPy/kern/src/splitKern.py @@ -4,7 +4,7 @@ A new kernel import numpy as np from .kern import Kern, CombinationKernel -from .independent_outputs import index_to_slices +from GPy.util.multioutput import index_to_slices import itertools class DEtime(Kern): diff --git a/GPy/likelihoods/multioutput_likelihood.py b/GPy/likelihoods/multioutput_likelihood.py index 2b407571..85845dda 100644 --- a/GPy/likelihoods/multioutput_likelihood.py +++ b/GPy/likelihoods/multioutput_likelihood.py @@ -14,7 +14,7 @@ from .gaussian import Gaussian from ..core.parameterization import Param from paramz.transformations import Logexp from ..core.parameterization import Parameterized -from ..kern.src.independent_outputs import index_to_slices +from GPy.util.multioutput import index_to_slices import itertools class MultioutputLikelihood(MixedNoise): diff --git a/GPy/util/multioutput.py b/GPy/util/multioutput.py index 2233dbb6..91227838 100644 --- a/GPy/util/multioutput.py +++ b/GPy/util/multioutput.py @@ -2,6 +2,33 @@ import numpy as np import warnings import GPy +def index_to_slices(index): + """ + take a numpy array of integers (index) and return a nested list of slices such that the slices describe the start, stop points for each integer in the index. + + e.g. + >>> index = np.asarray([0,0,0,1,1,1,2,2,2]) + returns + >>> [[slice(0,3,None)],[slice(3,6,None)],[slice(6,9,None)]] + + or, a more complicated example + >>> index = np.asarray([0,0,1,1,0,2,2,2,1,1]) + returns + >>> [[slice(0,2,None),slice(4,5,None)],[slice(2,4,None),slice(8,10,None)],[slice(5,8,None)]] + """ + if len(index)==0: + return[] + + #contruct the return structure + ind = np.asarray(index,dtype=np.int) + ret = [[] for i in range(ind.max()+1)] + + #find the switchpoints + ind_ = np.hstack((ind,ind[0]+ind[-1]+1)) + switchpoints = np.nonzero(ind_ - np.roll(ind_,+1))[0] + + [ret[ind_i].append(slice(*indexes_i)) for ind_i,indexes_i in zip(ind[switchpoints[:-1]],zip(switchpoints,switchpoints[1:]))] + return ret def get_slices(input_list): num_outputs = len(input_list)