Skip to content

Commit 731928a

Browse files
author
lpphd
committed
Moved common functions among variants to utils
1 parent f42baee commit 731928a

File tree

5 files changed

+64
-208
lines changed

5 files changed

+64
-208
lines changed

lightwaves_l1.py

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
warnings.filterwarnings("ignore")
99

1010
from lightwavesl1_functions import _generate_first_phase_kernels, _apply_kernels_feat_only
11-
from lightwaves_utils import ScalePerChannel, anova_feature_selection, mrmr_feature_selection, ScalePerChannelTrain
11+
from lightwaves_utils import ScalePerChannel, anova_feature_selection, mrmr_feature_selection, ScalePerChannelTrain, \
12+
ckd_to_kernels, get_fixed_candidate_kernels, get_ckd_matrix_with_features
1213
from sklearn.linear_model import RidgeClassifierCV
13-
from sympy.utilities.iterables import multiset_permutations
1414
from mpi4py import MPI
1515

1616
## 4 features per scattering level
@@ -21,7 +21,7 @@ def transform(X, matrix, feat_mask, candidate_kernels, dilations):
2121
Transform input array to LightWaveS features
2222
:param X: The input timeseries array of dimension (samples,channels,timesteps)
2323
:param matrix: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
24-
:param feat_mask: Feature mask of LightWaveS of dimension (n_kernels,8). Describes which features to keep from each kernel application
24+
:param feat_mask: Feature mask of LightWaveS of dimension (n_kernels,features_number). Describes which features to keep from each kernel application
2525
:param candidate_kernels: The set of base kernels used by LightWaveS
2626
:param dilations: The set of base dilations used by LightWaveS
2727
:return: Transformed array of dimensions (samples,features)
@@ -31,55 +31,6 @@ def transform(X, matrix, feat_mask, candidate_kernels, dilations):
3131
return feats[:, feat_mask]
3232

3333

34-
def ckd_to_kernels(ckd, candidate_kernels, candidate_dilations):
35-
"""
36-
:param ckd: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
37-
:param candidate_kernels: The set of base kernels used by LightWaveS
38-
:param candidate_dilations: The set of base dilations used by LightWaveS
39-
:return: Tuple of kernels in format suitable for the core algorithm (similar to ROCKET)
40-
"""
41-
num_channel_indices = np.ones(ckd.shape[0], dtype=np.int32)
42-
channel_indices = ckd[:, 0]
43-
biases = np.zeros_like(num_channel_indices, dtype=np.float32)
44-
dilations = 2 ** candidate_dilations[ckd[:, 2]].flatten().astype(np.int32)
45-
lengths = np.array([len(candidate_kernels[i]) for i in ckd[:, 1]], dtype=np.int32)
46-
paddings = np.multiply((lengths - 1), dilations) // 2
47-
weights = candidate_kernels[ckd[:, 1]].flatten().astype(np.float32)
48-
49-
return (
50-
weights,
51-
lengths,
52-
biases,
53-
dilations,
54-
paddings,
55-
num_channel_indices,
56-
channel_indices,
57-
)
58-
59-
60-
def get_ckd_matrix_with_features(fidx, num_channels, n_candidate_kernels, n_dilations, n_features):
61-
"""
62-
During feature generation and selection, transform each feature index number to (channel,kernel,dimension,selected feature) format
63-
:param fidx: Array of feature indices
64-
:param num_channels: Number of channels designated to this node
65-
:param n_candidate_kernels: The number of base kernels used by LightWaveS
66-
:param n_dilations: The number of dilations used by LightWaveS
67-
:param n_features: The number of features for this LightWaveS variant
68-
:return: An array of dimension (n_features,4)
69-
"""
70-
return np.unique(
71-
np.array(np.unravel_index(fidx, (num_channels, n_candidate_kernels, n_dilations, n_features))).T,
72-
axis=0).astype(np.int32)
73-
74-
75-
def get_fixed_candidate_kernels():
76-
"""
77-
:return: The set of base kernels used by LightWaveS (same as that of MINIROCKET)
78-
"""
79-
kernel_set = np.array([np.array(p) for p in multiset_permutations(([2] * 3 + [-1] * 6))], dtype=np.float32)
80-
return kernel_set
81-
82-
8334
## Development dataset indices (randomly generated once)
8435
dev_data = [0, 2, 6, 7, 8, 9, 10, 11, 12, 13, 17, 19, 20, 26, 27]
8536
MAX_DILATION = 32

lightwaves_l1l2.py

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
warnings.filterwarnings("ignore")
99

1010
from lightwavesl1l2_functions import _generate_first_phase_kernels, _apply_2layer_kernels
11-
from lightwaves_utils import ScalePerChannel, anova_feature_selection, mrmr_feature_selection, ScalePerChannelTrain
11+
from lightwaves_utils import ScalePerChannel, anova_feature_selection, mrmr_feature_selection, ScalePerChannelTrain, \
12+
get_fixed_candidate_kernels, get_ckd_matrix_with_features, ckd_to_kernels
1213
from sklearn.linear_model import RidgeClassifierCV
13-
from sympy.utilities.iterables import multiset_permutations
1414
from mpi4py import MPI
1515

1616
## 4 features per scattering level
@@ -23,7 +23,7 @@ def transform(X, matrix, feat_mask, candidate_kernels, dilations):
2323
Transform input array to LightWaveS features
2424
:param X: The input timeseries array of dimension (samples,channels,timesteps)
2525
:param matrix: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
26-
:param feat_mask: Feature mask of LightWaveS of dimension (n_kernels,8). Describes which features to keep from each kernel application
26+
:param feat_mask: Feature mask of LightWaveS of dimension (n_kernels,features_number). Describes which features to keep from each kernel application
2727
:param candidate_kernels: The set of base kernels used by LightWaveS
2828
:param dilations: The set of base dilations used by LightWaveS
2929
:return: Transformed array of dimensions (samples,features)
@@ -33,55 +33,6 @@ def transform(X, matrix, feat_mask, candidate_kernels, dilations):
3333
return feats[:, feat_mask]
3434

3535

36-
def ckd_to_kernels(ckd, candidate_kernels, candidate_dilations):
37-
"""
38-
:param ckd: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
39-
:param candidate_kernels: The set of base kernels used by LightWaveS
40-
:param candidate_dilations: The set of base dilations used by LightWaveS
41-
:return: Tuple of kernels in format suitable for the core algorithm (similar to ROCKET)
42-
"""
43-
num_channel_indices = np.ones(ckd.shape[0], dtype=np.int32)
44-
channel_indices = ckd[:, 0]
45-
biases = np.zeros_like(num_channel_indices, dtype=np.float32)
46-
dilations = 2 ** candidate_dilations[ckd[:, 2]].flatten().astype(np.int32)
47-
lengths = np.array([len(candidate_kernels[i]) for i in ckd[:, 1]], dtype=np.int32)
48-
paddings = np.multiply((lengths - 1), dilations) // 2
49-
weights = candidate_kernels[ckd[:, 1]].flatten().astype(np.float32)
50-
51-
return (
52-
weights,
53-
lengths,
54-
biases,
55-
dilations,
56-
paddings,
57-
num_channel_indices,
58-
channel_indices,
59-
)
60-
61-
62-
def get_ckd_matrix_with_features(fidx, num_channels, n_candidate_kernels, n_dilations, n_features):
63-
"""
64-
During feature generation and selection, transform each feature index number to (channel,kernel,dimension,selected feature) format
65-
:param fidx: Array of feature indices
66-
:param num_channels: Number of channels designated to this node
67-
:param n_candidate_kernels: The number of base kernels used by LightWaveS
68-
:param n_dilations: The number of dilations used by LightWaveS
69-
:param n_features: The number of features for this LightWaveS variant
70-
:return: An array of dimension (n_features,4)
71-
"""
72-
return np.unique(
73-
np.array(np.unravel_index(fidx, (num_channels, n_candidate_kernels, n_dilations, n_features))).T,
74-
axis=0).astype(np.int32)
75-
76-
77-
def get_fixed_candidate_kernels():
78-
"""
79-
:return: The set of base kernels used by LightWaveS (same as that of MINIROCKET)
80-
"""
81-
kernel_set = np.array([np.array(p) for p in multiset_permutations(([2] * 3 + [-1] * 6))], dtype=np.float32)
82-
return kernel_set
83-
84-
8536
## Development dataset indices (randomly generated once)
8637
dev_data = [0, 2, 6, 7, 8, 9, 10, 11, 12, 13, 17, 19, 20, 26, 27]
8738
MAX_DILATION = 32

lightwaves_l1l2_UCR_example.py

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
warnings.filterwarnings("ignore")
99

1010
from lightwavesl1l2_functions import _generate_first_phase_kernels, _apply_2layer_kernels
11-
from lightwaves_utils import ScalePerChannel, anova_feature_selection, mrmr_feature_selection, ScalePerChannelTrain
11+
from lightwaves_utils import ScalePerChannel, anova_feature_selection, mrmr_feature_selection, ScalePerChannelTrain, \
12+
ckd_to_kernels, get_fixed_candidate_kernels, get_ckd_matrix_with_features
1213
from sklearn.linear_model import RidgeClassifierCV
13-
from sympy.utilities.iterables import multiset_permutations
1414
from mpi4py import MPI
1515

1616
## 4 features per scattering level
@@ -23,7 +23,7 @@ def transform(X, matrix, feat_mask, candidate_kernels, dilations):
2323
Transform input array to LightWaveS features
2424
:param X: The input timeseries array of dimension (samples,channels,timesteps)
2525
:param matrix: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
26-
:param feat_mask: Feature mask of LightWaveS of dimension (n_kernels,8). Describes which features to keep from each kernel application
26+
:param feat_mask: Feature mask of LightWaveS of dimension (n_kernels,features_number). Describes which features to keep from each kernel application
2727
:param candidate_kernels: The set of base kernels used by LightWaveS
2828
:param dilations: The set of base dilations used by LightWaveS
2929
:return: Transformed array of dimensions (samples,features)
@@ -33,55 +33,6 @@ def transform(X, matrix, feat_mask, candidate_kernels, dilations):
3333
return feats[:, feat_mask]
3434

3535

36-
def ckd_to_kernels(ckd, candidate_kernels, candidate_dilations):
37-
"""
38-
:param ckd: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
39-
:param candidate_kernels: The set of base kernels used by LightWaveS
40-
:param candidate_dilations: The set of base dilations used by LightWaveS
41-
:return: Tuple of kernels in format suitable for the core algorithm (similar to ROCKET)
42-
"""
43-
num_channel_indices = np.ones(ckd.shape[0], dtype=np.int32)
44-
channel_indices = ckd[:, 0]
45-
biases = np.zeros_like(num_channel_indices, dtype=np.float32)
46-
dilations = 2 ** candidate_dilations[ckd[:, 2]].flatten().astype(np.int32)
47-
lengths = np.array([len(candidate_kernels[i]) for i in ckd[:, 1]], dtype=np.int32)
48-
paddings = np.multiply((lengths - 1), dilations) // 2
49-
weights = candidate_kernels[ckd[:, 1]].flatten().astype(np.float32)
50-
51-
return (
52-
weights,
53-
lengths,
54-
biases,
55-
dilations,
56-
paddings,
57-
num_channel_indices,
58-
channel_indices,
59-
)
60-
61-
62-
def get_ckd_matrix_with_features(fidx, num_channels, n_candidate_kernels, n_dilations, n_features):
63-
"""
64-
During feature generation and selection, transform each feature index number to (channel,kernel,dimension,selected feature) format
65-
:param fidx: Array of feature indices
66-
:param num_channels: Number of channels designated to this node
67-
:param n_candidate_kernels: The number of base kernels used by LightWaveS
68-
:param n_dilations: The number of dilations used by LightWaveS
69-
:param n_features: The number of features for this LightWaveS variant
70-
:return: An array of dimension (n_features,4)
71-
"""
72-
return np.unique(
73-
np.array(np.unravel_index(fidx, (num_channels, n_candidate_kernels, n_dilations, n_features))).T,
74-
axis=0).astype(np.int32)
75-
76-
77-
def get_fixed_candidate_kernels():
78-
"""
79-
:return: The set of base kernels used by LightWaveS (same as that of MINIROCKET)
80-
"""
81-
kernel_set = np.array([np.array(p) for p in multiset_permutations(([2] * 3 + [-1] * 6))], dtype=np.float32)
82-
return kernel_set
83-
84-
8536
MAX_DILATION = 32
8637

8738
# Final number of features

lightwaves_l2.py

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
warnings.filterwarnings("ignore")
99

1010
from lightwavesl2_functions import _generate_first_phase_kernels, _apply_2layer_kernels
11-
from lightwaves_utils import ScalePerChannel, anova_feature_selection, mrmr_feature_selection, ScalePerChannelTrain
11+
from lightwaves_utils import ScalePerChannel, anova_feature_selection, mrmr_feature_selection, ScalePerChannelTrain, \
12+
ckd_to_kernels, get_fixed_candidate_kernels, get_ckd_matrix_with_features
1213
from sklearn.linear_model import RidgeClassifierCV
13-
from sympy.utilities.iterables import multiset_permutations
1414
from mpi4py import MPI
1515

1616
## 4 features per scattering level
@@ -22,7 +22,7 @@ def transform(X, matrix, feat_mask, candidate_kernels, dilations):
2222
Transform input array to LightWaveS features
2323
:param X: The input timeseries array of dimension (samples,channels,timesteps)
2424
:param matrix: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
25-
:param feat_mask: Feature mask of LightWaveS of dimension (n_kernels,8). Describes which features to keep from each kernel application
25+
:param feat_mask: Feature mask of LightWaveS of dimension (n_kernels,features_number). Describes which features to keep from each kernel application
2626
:param candidate_kernels: The set of base kernels used by LightWaveS
2727
:param dilations: The set of base dilations used by LightWaveS
2828
:return: Transformed array of dimensions (samples,features)
@@ -32,55 +32,6 @@ def transform(X, matrix, feat_mask, candidate_kernels, dilations):
3232
return feats[:, feat_mask]
3333

3434

35-
def ckd_to_kernels(ckd, candidate_kernels, candidate_dilations):
36-
"""
37-
:param ckd: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
38-
:param candidate_kernels: The set of base kernels used by LightWaveS
39-
:param candidate_dilations: The set of base dilations used by LightWaveS
40-
:return: Tuple of kernels in format suitable for the core algorithm (similar to ROCKET)
41-
"""
42-
num_channel_indices = np.ones(ckd.shape[0], dtype=np.int32)
43-
channel_indices = ckd[:, 0]
44-
biases = np.zeros_like(num_channel_indices, dtype=np.float32)
45-
dilations = 2 ** candidate_dilations[ckd[:, 2]].flatten().astype(np.int32)
46-
lengths = np.array([len(candidate_kernels[i]) for i in ckd[:, 1]], dtype=np.int32)
47-
paddings = np.multiply((lengths - 1), dilations) // 2
48-
weights = candidate_kernels[ckd[:, 1]].flatten().astype(np.float32)
49-
50-
return (
51-
weights,
52-
lengths,
53-
biases,
54-
dilations,
55-
paddings,
56-
num_channel_indices,
57-
channel_indices,
58-
)
59-
60-
61-
def get_ckd_matrix_with_features(fidx, num_channels, n_candidate_kernels, n_dilations, n_features):
62-
"""
63-
During feature generation and selection, transform each feature index number to (channel,kernel,dimension,selected feature) format
64-
:param fidx: Array of feature indices
65-
:param num_channels: Number of channels designated to this node
66-
:param n_candidate_kernels: The number of base kernels used by LightWaveS
67-
:param n_dilations: The number of dilations used by LightWaveS
68-
:param n_features: The number of features for this LightWaveS variant
69-
:return: An array of dimension (n_features,4)
70-
"""
71-
return np.unique(
72-
np.array(np.unravel_index(fidx, (num_channels, n_candidate_kernels, n_dilations, n_features))).T,
73-
axis=0).astype(np.int32)
74-
75-
76-
def get_fixed_candidate_kernels():
77-
"""
78-
:return: The set of base kernels used by LightWaveS (same as that of MINIROCKET)
79-
"""
80-
kernel_set = np.array([np.array(p) for p in multiset_permutations(([2] * 3 + [-1] * 6))], dtype=np.float32)
81-
return kernel_set
82-
83-
8435
## Development dataset indices (randomly generated once)
8536
dev_data = [0, 2, 6, 7, 8, 9, 10, 11, 12, 13, 17, 19, 20, 26, 27]
8637
MAX_DILATION = 32

lightwaves_utils.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from sklearn.feature_selection import VarianceThreshold, f_classif
33
import numpy as np
44
from scipy.stats import rankdata
5+
from sympy.utilities.iterables import multiset_permutations
6+
7+
from lightwavesl1l2_functions import _apply_2layer_kernels
58

69

710
def ScalePerChannel(train_x, test_x):
@@ -150,3 +153,52 @@ def anova_feature_selection(X, y, N=100):
150153
or_idx[np.where(var_mask)[0][idces]] = True
151154
idces = np.where(or_idx)[0].astype(np.int32)
152155
return idces, scores
156+
157+
158+
def ckd_to_kernels(ckd, candidate_kernels, candidate_dilations):
159+
"""
160+
:param ckd: A channel-kernel-dilation 2d array of dimensions (n_kernels,3)
161+
:param candidate_kernels: The set of base kernels used by LightWaveS
162+
:param candidate_dilations: The set of base dilations used by LightWaveS
163+
:return: Tuple of kernels in format suitable for the core algorithm (similar to ROCKET)
164+
"""
165+
num_channel_indices = np.ones(ckd.shape[0], dtype=np.int32)
166+
channel_indices = ckd[:, 0]
167+
biases = np.zeros_like(num_channel_indices, dtype=np.float32)
168+
dilations = 2 ** candidate_dilations[ckd[:, 2]].flatten().astype(np.int32)
169+
lengths = np.array([len(candidate_kernels[i]) for i in ckd[:, 1]], dtype=np.int32)
170+
paddings = np.multiply((lengths - 1), dilations) // 2
171+
weights = candidate_kernels[ckd[:, 1]].flatten().astype(np.float32)
172+
173+
return (
174+
weights,
175+
lengths,
176+
biases,
177+
dilations,
178+
paddings,
179+
num_channel_indices,
180+
channel_indices,
181+
)
182+
183+
184+
def get_ckd_matrix_with_features(fidx, num_channels, n_candidate_kernels, n_dilations, n_features):
185+
"""
186+
During feature generation and selection, transform each feature index number to (channel,kernel,dimension,selected feature) format
187+
:param fidx: Array of feature indices
188+
:param num_channels: Number of channels designated to this node
189+
:param n_candidate_kernels: The number of base kernels used by LightWaveS
190+
:param n_dilations: The number of dilations used by LightWaveS
191+
:param n_features: The number of features for this LightWaveS variant
192+
:return: An array of dimension (len(fidx),n_features)
193+
"""
194+
return np.unique(
195+
np.array(np.unravel_index(fidx, (num_channels, n_candidate_kernels, n_dilations, n_features))).T,
196+
axis=0).astype(np.int32)
197+
198+
199+
def get_fixed_candidate_kernels():
200+
"""
201+
:return: The set of base kernels used by LightWaveS (same as that of MINIROCKET)
202+
"""
203+
kernel_set = np.array([np.array(p) for p in multiset_permutations(([2] * 3 + [-1] * 6))], dtype=np.float32)
204+
return kernel_set

0 commit comments

Comments
 (0)