"""Utility functions for simulations."""

import numpy as np

###################################################################################################
###################################################################################################

def normalized_sum(sig1, sig2, ratio, select_nonzero=True):
    """Combine two signals, after transforming to have the desired variance ratio.

    Parameters
    ----------
    sig1, sig2 : 1d array
        Vectors of data to normalize variance with respect to each other.
    ratio : float
        Desired ratio of sig1 variance to sig2 variance.
        If > 1 - sig1 is stronger, if < 1 - sig2 is stronger.
    select_nonzero : boolean, optional, default=True
        Whether to calculate the variance of sig1 across only non-zero data points.

    Returns
    -------
    1d array
        New vector with the combined input signals.
    """

    return sum(normalize_variance(sig1, sig2, ratio, select_nonzero))


def normalize_variance(sig1, sig2, ratio, select_nonzero=True):
    """Normalize the variance across two signals to reflect a specified ratio.

    Parameters
    ----------
    sig1, sig2 : 1d array
        Vectors of data to normalize variance with respect to each other.
    ratio : float
        Desired ratio of sig1 variance to sig2 variance.
        If > 1 - sig1 is stronger, if < 1 - sig2 is stronger.
    select_nonzero : boolean, optional, default=True
        Whether to calculate the variance of sig1 across only non-zero data points.

    Returns
    -------
    1d array, 1d array
        New sig1 & sig2, where sig2 has been normalized so they have the desired variance ratio.

    Notes
    -----
    Only sig2 is actually modified by this procedure, relative to the variance in sig1.
    If sig1 is a potentially non-continuous periodic signal (for example, bursty), `select_nonzero`
    allows for normalizing the variance across only segments of the signal with signal present.
    """

    if select_nonzero:
        sig1_var = np.var(np.nonzero(sig1))
    else:
        sig1_var = np.var(sig1)

    sig2 = np.sqrt(sig2**2 * sig1_var / (np.var(sig2) * ratio)) * np.sign(sig2)

    return sig1, sig2

@@ -256,16 +256,1084 @@
 ######%0A%0A
+def demean(array):%0A    %22%22%22Demean an array.%0A%0A    Parameters%0A    ----------%0A    array : 1d array%0A        Data to demean.%0A%0A    Returns%0A    -------%0A    1d array%0A        Demeaned data.%0A    %22%22%22%0A%0A    return array - array.mean()%0A%0A%0Adef normalize_variance(array, variance=1.):%0A    %22%22%22Normalize the variance of an array.%0A%0A    Parameters%0A    ----------%0A    array : 1d array%0A        Data to normalize variance to.%0A    variance : float, optional, default=1.%0A        Variance to normalize to.%0A%0A    Returns%0A    -------%0A    1d array%0A        Variance normalized data.%0A    %22%22%22%0A%0A    return array / array.std() * np.sqrt(variance)%0A%0A%0Adef proportional_sum(signals, proportions):%0A    %22%22%22Sum a set of signals, each with a specified proportional variance.%0A%0A    Parameters%0A    ----------%0A    signals : list of 1d array%0A        Signals to sum together.%0A    proportions : list of float%0A        Proportional variance for each signal.%0A%0A    Returns%0A    -------%0A    1d array%0A        Sumated signal.%0A    %22%22%22%0A%0A    return sum(%5Bnormalize_variance(sig, prop) for prop, sig in zip(proportions, signals)%5D)%0A%0A%0A
 def norm
@@ -1524,38 +1524,37 @@
 d array%0A        
-Vector
+Array
 s of data to nor
@@ -1925,14 +1925,13 @@
 New 
-vector
+array
  wit
@@ -1984,34 +1984,24 @@
 urn sum(
-normalize_
 variance
 (sig1, s
@@ -1984,32 +1984,38 @@
 urn sum(variance
+_ratio
 (sig1, sig2, rat
@@ -2041,34 +2041,24 @@
 )%0A%0A%0Adef 
-normalize_
 variance
 (sig1, s
@@ -2045,24 +2045,30 @@
 def variance
+_ratio
 (sig1, sig2,

Extend set of util funcs