#  Copyright 2016 Intel Corporation
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
"""Semi-Supervised Shared Response Model (SS-SRM)

The implementations are based on the following publications:

.. [Turek2016] "A Semi-Supervised Method for Multi-Subject fMRI Functional
   Alignment",
   J. S. Turek, T. L. Willke, P.-H. Chen, P. J. Ramadge
   IEEE International Conference on Acoustics, Speech and Signal Processing
   (ICASSP), 2017, pp. 1098-1102.
   https://doi.org/10.1109/ICASSP.2017.7952326
"""

# Authors: Javier Turek (Intel Labs), 2016

import logging

import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin, ClassifierMixin
from sklearn.utils import assert_all_finite
from sklearn.utils.validation import NotFittedError
from sklearn.utils.multiclass import unique_labels

import tensorflow as tf
from pymanopt.manifolds import Euclidean
from pymanopt.manifolds import Product
from pymanopt.optimizers import ConjugateGradient
from pymanopt import Problem, function
from pymanopt.manifolds import Stiefel

import gc

from brainiak.utils import utils
from brainiak.funcalign import srm

__all__ = [
    "SSSRM",
]

logger = logging.getLogger(__name__)


class SSSRM(BaseEstimator, ClassifierMixin, TransformerMixin):
    """Semi-Supervised Shared Response Model (SS-SRM)

    Given multi-subject data, factorize it as a shared response S among all
    subjects and an orthogonal transform W per subject, using also labeled
    data to train a Multinomial Logistic Regression (MLR) classifier (with
    l2 regularization) in a semi-supervised manner:

    .. math::
        (1-\\alpha) Loss_{SRM}(W_i,S;X_i)
        + \\alpha/\\gamma Loss_{MLR}(\\theta, bias; {(W_i^T \\times Z_i, y_i})
        + R(\\theta)
        :label: sssrm-eq

    (see Equations (1) and (4) in [Turek2016]_).

    Parameters
    ----------

    n_iter : int, default: 10
        Number of iterations to run the algorithm.

    features : int, default: 50
        Number of features to compute.

    gamma : float, default: 1.0
        Regularization parameter for the classifier.

    alpha : float, default: 0.5
        Balance parameter between the SRM term and the MLR term.

    rand_seed : int, default: 0
        Seed for initializing the random number generator.


    Attributes
    ----------

    w_ : list of array, element i has shape=[voxels_i, features]
        The orthogonal transforms (mappings) for each subject.

    s_ : array, shape=[features, samples]
        The shared response.

    theta_ : array, shape=[classes, features]
        The MLR class plane parameters.

    bias_ : array, shape=[classes]
        The MLR class biases.

    classes_ : array of int, shape=[classes]
        Mapping table for each classes to original class label.

    random_state_: `RandomState`
        Random number generator initialized using rand_seed

    Note
    ----

        The number of voxels may be different between subjects. However, the
        number of samples for the alignment data must be the same across
        subjects. The number of labeled samples per subject can be different.

        The Semi-Supervised Shared Response Model is approximated using the
        Block-Coordinate Descent (BCD) algorithm proposed in [Turek2016]_.

        This is a single node version.
    """

    def __init__(self, n_iter=10, features=50, gamma=1.0, alpha=0.5,
                 rand_seed=0):
        self.n_iter = n_iter
        self.features = features
        self.gamma = gamma
        self.alpha = alpha
        self.rand_seed = rand_seed
        return

    def fit(self, X, y, Z):
        """Compute the Semi-Supervised Shared Response Model

        Parameters
        ----------

        X : list of 2D arrays, element i has shape=[voxels_i, n_align]
            Each element in the list contains the fMRI data for alignment of
            one subject. There are n_align samples for each subject.

        y : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the data samples
            in Z.

        Z : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject
            for training the MLR classifier.

        """
        logger.info('Starting SS-SRM')

        # Check that the alpha value is in range (0.0,1.0)
        if 0.0 >= self.alpha or self.alpha >= 1.0:
            raise ValueError("Alpha parameter should be in range (0.0, 1.0)")

        # Check that the regularizer value is positive
        if 0.0 >= self.gamma:
            raise ValueError("Gamma parameter should be positive.")

        # Check the number of subjects
        if len(X) <= 1 or len(y) <= 1 or len(Z) <= 1:
            raise ValueError("There are not enough subjects in the input "
                             "data to train the model.")

        if not (len(X) == len(y)) or not (len(X) == len(Z)):
            raise ValueError("Different number of subjects in data.")

        # Check for input data sizes
        if X[0].shape[1] < self.features:
            raise ValueError(
                "There are not enough samples to train the model with "
                "{0:d} features.".format(self.features))

        # Check if all subjects have same number of TRs for alignment
        # and if alignment and classification data have the same number of
        # voxels per subject. Also check that there labels for all the classif.
        # sample
        number_trs = X[0].shape[1]
        number_subjects = len(X)
        for subject in range(number_subjects):
            assert_all_finite(X[subject])
            assert_all_finite(Z[subject])
            if X[subject].shape[1] != number_trs:
                raise ValueError("Different number of alignment samples "
                                 "between subjects.")
            if X[subject].shape[0] != Z[subject].shape[0]:
                raise ValueError("Different number of voxels between alignment"
                                 " and classification data (subject {0:d})"
                                 ".".format(subject))
            if Z[subject].shape[1] != y[subject].size:
                raise ValueError("Different number of samples and labels in "
                                 "subject {0:d}.".format(subject))

        # Map the classes to [0..C-1]
        new_y = self._init_classes(y)

        # Run SS-SRM
        self.w_, self.s_, self.theta_, self.bias_ = self._sssrm(X, Z, new_y)

        return self

    def _init_classes(self, y):
        """Map all possible classes to the range [0,..,C-1]

        Parameters
        ----------

        y : list of arrays of int, each element has shape=[samples_i,]
            Labels of the samples for each subject


        Returns
        -------
        new_y : list of arrays of int, each element has shape=[samples_i,]
            Mapped labels of the samples for each subject

        Note
        ----
            The mapping of the classes is saved in the attribute classes_.
        """
        self.classes_ = unique_labels(utils.concatenate_not_none(y))
        new_y = [None] * len(y)
        for s in range(len(y)):
            new_y[s] = np.digitize(y[s], self.classes_) - 1
        return new_y

    def transform(self, X, y=None):
        """Use the model to transform matrix to Shared Response space

        Parameters
        ----------

        X : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject
            note that number of voxels and samples can vary across subjects.

        y : not used as it only applies the mappings


        Returns
        -------

        s : list of 2D arrays, element i has shape=[features_i, samples_i]
            Shared responses from input data (X)
        """

        # Check if the model exist
        if hasattr(self, 'w_') is False:
            raise NotFittedError("The model fit has not been run yet.")

        # Check the number of subjects
        if len(X) != len(self.w_):
            raise ValueError("The number of subjects does not match the one"
                             " in the model.")

        s = [None] * len(X)
        for subject in range(len(X)):
            s[subject] = self.w_[subject].T.dot(X[subject])

        return s

    def predict(self, X):
        """Classify the output for given data

        Parameters
        ----------
        X : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject
            The number of voxels should be according to each subject at
            the moment of training the model.

        Returns
        -------
        p: list of arrays, element i has shape=[samples_i]
            Predictions for each data sample.
        """
        # Check if the model exist
        if hasattr(self, 'w_') is False:
            raise NotFittedError("The model fit has not been run yet.")

        # Check the number of subjects
        if len(X) != len(self.w_):
            raise ValueError("The number of subjects does not match the one"
                             " in the model.")

        X_shared = self.transform(X)
        p = [None] * len(X_shared)
        for subject in range(len(X_shared)):
            sumexp, _, exponents = utils.sumexp_stable(
                self.theta_.T.dot(X_shared[subject]) +
                self.bias_[:, np.newaxis])
            p[subject] = self.classes_[
                (exponents / sumexp[np.newaxis, :]).argmax(axis=0)]

        return p

    def _sssrm(self, data_align, data_sup, labels):
        """Block-Coordinate Descent algorithm for fitting SS-SRM.

        Parameters
        ----------

        data_align : list of 2D arrays, element i has shape=[voxels_i, n_align]
            Each element in the list contains the fMRI data for alignment of
            one subject. There are n_align samples for each subject.

        data_sup : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject for
            the classification task.

        labels : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the data samples
            in data_sup.


        Returns
        -------

        w : list of array, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.

        s : array, shape=[features, samples]
            The shared response.
        """
        classes = self.classes_.size

        # Initialization:
        self.random_state_ = np.random.RandomState(self.rand_seed)
        random_states = [
            np.random.RandomState(self.random_state_.randint(2**32,
                                                             dtype=np.int64))
            for i in range(len(data_align))]
        # Set Wi's to a random orthogonal voxels by TRs
        w, _ = srm._init_w_transforms(data_align, self.features, random_states)

        # Initialize the shared response S
        s = SSSRM._compute_shared_response(data_align, w)

        # Initialize theta and bias
        theta, bias = self._update_classifier(data_sup, labels, w, classes)

        # calculate and print the objective function
        if logger.isEnabledFor(logging.INFO):
            objective = self._objective_function(data_align, data_sup, labels,
                                                 w, s, theta, bias)
            logger.info('Objective function %f' % objective)

        # Main loop:
        for iteration in range(self.n_iter):
            logger.info('Iteration %d' % (iteration + 1))

            # Update the mappings Wi
            w = self._update_w(data_align, data_sup, labels, w, s, theta, bias)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating Wi  %f'
                            % objective)

            # Update the shared response S
            s = SSSRM._compute_shared_response(data_align, w)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating S   %f'
                            % objective)

            # Update the MLR classifier, theta and bias
            theta, bias = self._update_classifier(data_sup, labels, w, classes)

            # Output the objective function
            if logger.isEnabledFor(logging.INFO):
                objective = self._objective_function(data_align, data_sup,
                                                     labels, w, s, theta, bias)
                logger.info('Objective function after updating MLR %f'
                            % objective)

        return w, s, theta, bias

    def _update_classifier(self, data, labels, w, classes):
        """Update the classifier parameters theta and bias

        Parameters
        ----------

        data : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject for
            the classification task.

        labels : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the data samples
            in data_sup.

        w : list of 2D array, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.

        classes : int
            The number of classes in the classifier.


        Returns
        -------

        theta : array, shape=[features, classes]
            The MLR parameter for the class planes.

        bias : array shape=[classes,]
            The MLR parameter for class biases.
        """

        # Stack the data and labels for training the classifier
        data_stacked, labels_stacked, weights = \
            SSSRM._stack_list(data, labels, w)

        features = w[0].shape[1]
        total_samples = weights.size

        data_tf = tf.constant(data_stacked, dtype=tf.float64)
        labels_tf = tf.constant(labels_stacked, dtype=tf.int32)
        weights_tf = tf.constant(weights, dtype=tf.float64)
        constf2 = tf.constant(self.alpha / self.gamma, dtype=tf.float64)

        manifold = Product([Euclidean(features, classes),
                            Euclidean(1, classes)])

        @function.tensorflow(manifold)
        def cost(theta_tf, bias_tf):
            logits = tf.matmul(data_tf, theta_tf) + bias_tf
            log_p_y_given_x = tf.nn.log_softmax(logits)
            indices = tf.range(total_samples)
            gather_indices = tf.stack([indices, labels_tf], axis=1)
            log_probs = tf.gather_nd(log_p_y_given_x, gather_indices)
            f = (-constf2 * tf.reduce_sum(log_probs / weights_tf) +
                 0.5 * tf.reduce_sum(theta_tf ** 2))
            return f

        problem = Problem(manifold=manifold, cost=cost)
        solver = ConjugateGradient(min_gradient_norm=1e-6, verbosity=0)

        # Initialize variables
        theta_init = np.zeros((features, classes), dtype=np.float64)
        bias_init = np.zeros((1, classes), dtype=np.float64)
        solution = solver.run(problem, initial_point=[theta_init, bias_init])

        theta = solution.point[0]
        bias = solution.point[1].flatten()

        return theta, bias

    def _update_w(self, data_align, data_sup, labels, w, s, theta, bias):
        """

        Parameters
        ----------
        data_align : list of 2D arrays, element i has shape=[voxels_i, n_align]
            Each element in the list contains the fMRI data for alignment of
            one subject. There are n_align samples for each subject.

        data_sup : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject for
            the classification task.

        labels : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the data samples
            in data_sup.

        w : list of array, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.

        s : array, shape=[features, samples]
            The shared response.

        theta : array, shape=[classes, features]
            The MLR class plane parameters.

        bias : array, shape=[classes]
            The MLR class biases.

        Returns
        -------

        w : list of 2D array, element i has shape=[voxels_i, features]
            The updated orthogonal transforms (mappings).
        """
        subjects = len(data_align)

        s_tf = tf.constant(s.astype(np.float64))
        theta_tf = tf.constant(theta.T.astype(np.float64))
        bias_tf = tf.constant(bias[np.newaxis, :].astype(np.float64))

        for subject in range(subjects):
            logger.info('Subject Wi %d' % subject)
            # Solve for subject i

            data_srm_subject_tf = tf.constant(
                data_align[subject].astype(np.float64))
            constf1 = tf.constant(
                (1 - self.alpha) * 0.5 / data_align[subject].shape[1],
                dtype=tf.float64)

            manifold = Stiefel(w[subject].shape[0], w[subject].shape[1])

            if data_sup[subject] is not None:
                data_sup_subject_tf = tf.constant(
                    data_sup[subject].astype(np.float64))
                labels_tf_subj = tf.constant(labels[subject], dtype=tf.int32)
                constf2 = tf.constant(
                    -self.alpha / self.gamma / data_sup[subject].shape[1],
                    dtype=tf.float64)

                @function.tensorflow(manifold)
                def cost(w_tf):
                    diff = (data_srm_subject_tf - tf.matmul(w_tf, s_tf))
                    f1 = constf1 * tf.reduce_sum(diff ** 2)
                    w_by_data = tf.matmul(tf.transpose(w_tf),
                                          data_sup_subject_tf)
                    logits = tf.transpose(
                        tf.matmul(theta_tf, w_by_data)) + bias_tf
                    log_p_y_given_x = tf.nn.log_softmax(logits)
                    indices = tf.range(data_sup_subject_tf.shape[1],
                                       dtype=tf.int32)
                    gather_indices = tf.stack([indices, labels_tf_subj],
                                              axis=1)
                    log_probs = tf.gather_nd(log_p_y_given_x, gather_indices)
                    f2 = constf2 * tf.reduce_sum(log_probs)
                    return f1 + f2
            else:
                @function.tensorflow(manifold)
                def cost(w_tf):
                    diff = (data_srm_subject_tf - tf.matmul(w_tf, s_tf))
                    f1 = constf1 * tf.reduce_sum(diff ** 2)
                    return f1

            f_subject = self._objective_function_subject(data_align[subject],
                                                         data_sup[subject],
                                                         labels[subject],
                                                         w[subject],
                                                         s, theta, bias)
            minstep = np.amin(((10 ** -np.floor(np.log10(f_subject))), 1e-1))

            problem = Problem(manifold=manifold, cost=cost)
            solver = ConjugateGradient(min_gradient_norm=1e-2,
                                       min_step_size=minstep,
                                       verbosity=0)
            w_init = w[subject].astype(np.float64)
            result = solver.run(problem, initial_point=w_init)
            w[subject] = result.point

        # Run garbage collector to avoid filling up the memory
        gc.collect()
        return w

    @staticmethod
    def _compute_shared_response(data, w):
        """ Compute the shared response S

        Parameters
        ----------

        data : list of 2D arrays, element i has shape=[voxels_i, samples]
            Each element in the list contains the fMRI data of one subject.

        w : list of 2D arrays, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.


        Returns
        -------

        s : array, shape=[features, samples]
            The shared response for the subjects data with the mappings in w.
        """
        s = np.zeros((w[0].shape[1], data[0].shape[1]))
        for m in range(len(w)):
            s = s + w[m].T.dot(data[m])
        s /= len(w)
        return s

    def _objective_function(self, data_align, data_sup, labels, w, s, theta,
                            bias):
        """Compute the objective function of the Semi-Supervised SRM

        See :eq:`sssrm-eq`.

        Parameters
        ----------

        data_align : list of 2D arrays, element i has shape=[voxels_i, n_align]
            Each element in the list contains the fMRI data for alignment of
            one subject. There are n_align samples for each subject.

        data_sup : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject for
            the classification task.

        labels : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the data samples
            in data_sup.

        w : list of array, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.

        s : array, shape=[features, samples]
            The shared response.

        theta : array, shape=[classes, features]
            The MLR class plane parameters.

        bias : array, shape=[classes]
            The MLR class biases.


        Returns
        -------

        f_val : float
            The SS-SRM objective function evaluated based on the parameters to
            this function.
        """
        subjects = len(data_align)

        # Compute the SRM  loss
        f_val = 0.0
        for subject in range(subjects):
            samples = data_align[subject].shape[1]
            f_val += (1 - self.alpha) * (0.5 / samples) \
                * np.linalg.norm(data_align[subject] - w[subject].dot(s),
                                 'fro')**2

        # Compute the MLR loss
        f_val += self._loss_lr(data_sup, labels, w, theta, bias)

        return f_val

    def _objective_function_subject(self, data_align, data_sup, labels, w, s,
                                    theta, bias):
        """Compute the objective function for one subject.

        .. math:: (1-C)*Loss_{SRM}_i(W_i,S;X_i)
        .. math:: + C/\\gamma * Loss_{MLR_i}(\\theta, bias; {(W_i^T*Z_i, y_i})
        .. math:: + R(\\theta)

        Parameters
        ----------

        data_align : 2D array, shape=[voxels_i, samples_align]
            Contains the fMRI data for alignment of subject i.

        data_sup : 2D array, shape=[voxels_i, samples_i]
            Contains the fMRI data of one subject for the classification task.

        labels : array of int, shape=[samples_i]
            The labels for the data samples in data_sup.

        w : array, shape=[voxels_i, features]
            The orthogonal transform (mapping) :math:`W_i` for subject i.

        s : array, shape=[features, samples]
            The shared response.

        theta : array, shape=[classes, features]
            The MLR class plane parameters.

        bias : array, shape=[classes]
            The MLR class biases.


        Returns
        -------

        f_val : float
            The SS-SRM objective function for subject i evaluated on the
            parameters to this function.
        """
        # Compute the SRM  loss
        f_val = 0.0
        samples = data_align.shape[1]
        f_val += (1 - self.alpha) * (0.5 / samples) \
            * np.linalg.norm(data_align - w.dot(s), 'fro')**2

        # Compute the MLR loss
        f_val += self._loss_lr_subject(data_sup, labels, w, theta, bias)

        return f_val

    def _loss_lr_subject(self, data, labels, w, theta, bias):
        """Compute the Loss MLR for a single subject (without regularization)

        Parameters
        ----------

        data : array, shape=[voxels, samples]
            The fMRI data of subject i for the classification task.

        labels : array of int, shape=[samples]
            The labels for the data samples in data.

        w : array, shape=[voxels, features]
            The orthogonal transform (mapping) :math:`W_i` for subject i.

        theta : array, shape=[classes, features]
            The MLR class plane parameters.

        bias : array, shape=[classes]
            The MLR class biases.

        Returns
        -------

        loss : float
            The loss MLR for the subject
        """
        if data is None:
            return 0.0

        samples = data.shape[1]

        thetaT_wi_zi_plus_bias = (theta.T.dot(w.T.dot(data)) +
                                  bias[:, np.newaxis])
        sum_exp, max_value, _ = utils.sumexp_stable(thetaT_wi_zi_plus_bias)
        sum_exp_values = np.log(sum_exp) + max_value

        aux = 0.0
        for sample in range(samples):
            label = labels[sample]
            aux += thetaT_wi_zi_plus_bias[label, sample]
        return self.alpha / samples / self.gamma * (sum_exp_values.sum() - aux)

    def _loss_lr(self, data, labels, w, theta, bias):
        """Compute the Loss MLR (with the regularization)

        Parameters
        ----------

        data : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject for
            the classification task.

        labels : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the samples in
            data.

        w : list of array, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.

        theta : array, shape=[classes, features]
            The MLR class plane parameters.

        bias : array, shape=[classes]
            The MLR class biases.


        Returns
        -------

        loss : float
            The loss MLR for the SS-SRM model
        """
        subjects = len(data)
        loss = 0.0
        for subject in range(subjects):
            if labels[subject] is not None:
                loss += self._loss_lr_subject(data[subject], labels[subject],
                                              w[subject], theta, bias)

        return loss + 0.5 * np.linalg.norm(theta, 'fro')**2

    @staticmethod
    def _stack_list(data, data_labels, w):
        """Construct a numpy array by stacking arrays in a list

        Parameter
        ----------

        data : list of 2D arrays, element i has shape=[voxels_i, samples_i]
            Each element in the list contains the fMRI data of one subject for
            the classification task.

        data_labels : list of arrays of int, element i has shape=[samples_i]
            Each element in the list contains the labels for the samples in
            data.

        w : list of array, element i has shape=[voxels_i, features]
            The orthogonal transforms (mappings) :math:`W_i` for each subject.


        Returns
        -------

        data_stacked : 2D array, shape=[samples, features]
            The data samples from all subjects are stacked into a single
            2D array, where "samples" is the sum of samples_i.

        labels_stacked : array, shape=[samples,]
            The labels from all subjects are stacked into a single
            array, where "samples" is the sum of samples_i.

        weights : array, shape=[samples,]
            The number of samples of the subject that are related to that
            sample. They become a weight per sample in the MLR loss.
        """
        labels_stacked = utils.concatenate_not_none(data_labels)

        weights = np.empty((labels_stacked.size,))
        data_shared = [None] * len(data)
        curr_samples = 0
        for s in range(len(data)):
            if data[s] is not None:
                subject_samples = data[s].shape[1]
                curr_samples_end = curr_samples + subject_samples
                weights[curr_samples:curr_samples_end] = subject_samples
                data_shared[s] = w[s].T.dot(data[s])
                curr_samples += data[s].shape[1]

        data_stacked = utils.concatenate_not_none(data_shared, axis=1).T
        return data_stacked, labels_stacked, weights
