from sympy import zeros, symbols
from lie_algebra import adf_g, lie_diff
from sympy.printing import sstr

def from_control_affine_form(state_evolution, input_field, states):
    return ControlSystem(state_evolution, input_field, states)

class ControlSystem:
    def __init__(self, f, g, x):
        self.f = f
        self.g = g
        self.x = x

    def controllability_matrix(self):
        n = len(self.f)
        C = zeros(n)
        for i in range(n):
            C[:, i] = adf_g(self.f, self.g, self.x, i)  # TODO: improve performance
        return C

    def find_dependency(self):
        n = len(self.f)
        c = zeros(n, 1)
        c[n - 1] = symbols('\phi')
        sol = (self.controllability_matrix().T).inv() * c
        j = None
        for i in range(n):
            if sol[i] != 0:
                j = i
        return self.x[j]

    def transformation(self):
        n = len(self.f)
        T = zeros(n, 1)
        T[0] = self.find_dependency()
        for i in range(1, n):
            T[i] = lie_diff(T[i - 1], self.f, self.x)
        return T

    def beta(self):
        T = self.transformation()
        Lg_Tn = lie_diff(T[-1], self.g, self.x)
        return 1 / Lg_Tn

    def alpha(self):
        T = self.transformation()
        Lf_Tn = lie_diff(T[-1], self.f, self.x)
        return self.beta() * Lf_Tn

    def __str__(self):
        return 'System(\n' + sstr(self.f) + ',\n' \
               + sstr(self.g) + ')'

    def __repr__(self):
        return sstr(self)


<filename>ratter/material.py<gh_stars>0
import sympy as sp

class Material(object):
    def __init__(self, name, refractive_index_value=None):
        self.name = name
        self.n_symbol = sp.Symbol("n_"+name)
        self.n_value = refractive_index_value

    @property
    def substitutions(self):
        if self.n_value is None:
            return []
        else:
            return [(self.n_symbol, self.n_value)]


<reponame>rgerkin/psiz
# -*- coding: utf-8 -*-
# Copyright 2020 The PsiZ Authors. All Rights Reserved.
#
# 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.
# ============================================================================
"""Module for testing models.py."""

import pytest

import numpy as np
from scipy.stats import pearsonr
import tensorflow as tf

import psiz

# model_inferred_best = {}
        # val_loss_best = np.inf
        # for i_restart in range(n_restart):
        #     model_inferred = build_model(n_stimuli, n_dim)
        #     model_inferred.compile(**compile_kwargs)

        #     model_inferred.fit(
        #         ds_obs_train, validation_data=ds_obs_val, epochs=epochs,
        #         callbacks=callbacks, verbose=0
        #     )

        #     # d_train = model_inferred.evaluate(ds_obs_train, return_dict=True)
        #     d_val = model_inferred.evaluate(ds_obs_val, return_dict=True)
        #     # d_test = model_inferred.evaluate(ds_obs_test, return_dict=True)

        #     if d_val['loss'] < val_loss_best:
        #         val_loss_best = d_val['loss']
        #         model_inferred_best = model_inferred


<reponame>herrlich10/mripy
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals
import time
import numpy as np
from scipy import optimize, stats
from deepdish import io as dio
from . import utils, math

def basis_vanBergen2015(s, n_channels=8, power=5):
    '''
    Parameters
    ----------
    s : 1D array, n_trials

    Returns
    -------
    fs : 2D array, n_channels * n_trials

    References
    ----------
    {van Bergen2015}
    '''
    phi = np.arange(0, np.pi, np.pi/n_channels)
    fs = np.maximum(0, np.cos(2 * (s[np.newaxis,:] - phi[:,np.newaxis])))**power # n_channels * n_trials
    return fs

def basis_Sprague2013(s, n_channels=6, spacing=2, center=None, size=None, power=7, dim=1, intercept=False):
    '''
    Parameters
    ----------
    s : 2D array, n_trials * dim | 1D array, n_trials

    Returns
    -------
    fs : 2D array, n_channels * n_trials

    References
    ----------
    {Sprague2013}
    '''
    if size is None:
        size = 5.8153/2.0940 * spacing # Ratio is chosen to avoid high corr between channels while accomplish smooth recon
    if center is None:
        center = 0
    centers = (np.arange(n_channels) - (n_channels-1)/2) * spacing + center
    if dim == 1:
        r = np.abs(s[np.newaxis,:] - centers[:,np.newaxis]) # Distance from filter’s center
    elif dim == 2:
        X, Y = np.meshgrid(centers, centers)
        centers = np.c_[X.ravel(), Y.ravel()] # 2D channel array is serialized in row-first order
        r = np.linalg.norm(s.T[np.newaxis,...] - centers[...,np.newaxis], axis=1)
    fs = np.where(r<size, (0.5*np.cos(r/size*np.pi) + 0.5)**power, 0) # n_channels * n_trials
    if intercept:
        fs = np.vstack([fs, np.ones(len(s))]) # Learnable bias
    return fs

class BaseModel(utils.Savable2):
    # get_params() is required by sklearn
    def get_params(self, deep=True):
        raise NotImplementedError('** You must implement this method by yourself in your model.') # Abstrast method

    # set_params() is required by sklearn
    def set_params(self, **parameters):
        for parameter, value in parameters.items():
            setattr(self, parameter, value)
        return self

    def to_dict(self):
        return {k: v for k, v in self.__dict__.items() if k.endswith('_')}

    def from_dict(self, d):
        self.__dict__.update(d)
        return self

class ChannelEncodingModel(BaseModel):
    def __init__(self, n_channels, basis_func, stimulus_domain, circular=False, verbose=2):
        '''
        After (Brouwer et al., 2009).
        '''
        self.n_channels = n_channels
        self.basis_func = basis_func
        self.stimulus_domain = stimulus_domain
        self.circular = circular
        self.verbose = verbose

    # get_params() is required by sklearn
    def get_params(self, deep=True):
        return dict(n_channels=self.n_channels, basis_func=self.basis_func,
            stimulus_domain=self.stimulus_domain, circular=self.circular)

    def fit(self, X, y):
        '''
        Parameters
        ----------
        X : 2D array
            n_trials * n_voxels BOLD response pattern
            (e.g., beta for each trial, or delayed and detrended time points within block plateau)
        y : 1D array
            n_trials stimulus value (e.g., orientation, color)
        '''
        b = X.T # Voxel BOLD response, n_voxels * n_trials
        s = y # Stimulus, n_trials
        fs = self.basis_func(s) # Channel response, n_channels * n_trials
        # Step 1: Estimate W by OLS regression
        W = b @ fs.T @ math.pinv(fs @ fs.T) # Weight, n_voxels * n_channels
        # Store params
        self.W_ = W
        return self # Required by sklearn

    def predict(self, X, stimulus_domain=None, return_all=False):
        stimulus_domain = self.stimulus_domain if stimulus_domain is None else stimulus_domain
        channel_resp = self.inverted_encoding(X) # n_trials * n_channels
        evidence = self.correlation_inversion(X, stimulus_domain=None) # n_trials * n_domain
        # y_map = stimulus_domain[np.argmax(evidence, axis=1)] # n_trials
        y_map = stimulus_domain[math.median_argmax(evidence, axis=1)] # n_trials
        # y_mean = np.sum(stimulus_domain * evidence, axis=1) / np.sum(evidence, axis=1) # n_trials
        # y_std = np.sqrt(np.sum((stimulus_domain[np.newaxis,:] - y_mean[:,np.newaxis])**2 * evidence, axis=1) \
        #     / np.sum(evidence, axis=1)) # n_trials
        # return (y_mean, y_std, y_map, evidence, channel_resp) if return_all else y_mean
        return (y_map, evidence, channel_resp) if return_all else y_map

    _pidx = 1 # Index of posterior (for MAP) if return_all

    def pRF(self, stimulus_domain=None, method='ols', X=None, y=None):
        stimulus_domain = self.stimulus_domain if stimulus_domain is None else stimulus_domain
        fs_domain = self.basis_func(stimulus_domain) # n_channels * n_domain
        if method == 'ols':
            pRF = self.W_ @ fs_domain # n_voxels * n_domain
        elif method == 'ridge':
            # After {Sprague2013}, to ensure that most of the pRFs were sufficiently unimodal
            # However, the method doesn't seem to help much in my simulation...
            b = X.T # Voxel BOLD response, n_voxels * n_trials
            s = y # Stimulus, n_trials
            fs = self.basis_func(s) # Channel response, n_channels * n_trials
            n_trials = len(s)
            # Find best lambda according to BIC
            Ws = []
            BICs = []
            for lambda_ in np.logspace(-3, 3, 19):
                M = fs.T @ math.pinv(fs @ fs.T + lambda_ * np.eye(self.n_channels)) # n_trials * n_channels
                W = b @ M # Weight, n_voxels * n_channels
                H = M @ fs # Projection (or "hat") matrix, n_trials * n_trials
                df = np.trace(H) # Degrees of freedom for ridge (less than the number of predictors as in OLS)
                RSS = np.sum((b @ H - b)**2) # Residual sum of squares
                BIC = n_trials * np.log(RSS) + df * np.log(n_trials) # Scaling problem???
                Ws.append(W)
                BICs.append(BIC)
            W_opt = Ws[np.argmin(BICs)]
            pRF = W_opt @ fs_domain
        return pRF # n_voxels * n_domain

    def inverted_encoding(self, X):
        b = X.T # Voxel BOLD response, n_voxels * n_trials
        fs = math.pinv(self.W_.T @ self.W_) @ self.W_.T @ b # Inverted channel response, n_channels * n_trials
        return fs.T # n_trials * n_channels

    def voxel_inversion(self, X, stimulus_domain=None):
        stimulus_domain = self.stimulus_domain if stimulus_domain is None else stimulus_domain
        recon = X @ self.pRF(stimulus_domain=stimulus_domain) # n_trials * n_domain
        recon /= X.shape[1] # / n_voxels
        return recon

    def channel_inversion(self, X, stimulus_domain=None):
        stimulus_domain = self.stimulus_domain if stimulus_domain is None else stimulus_domain
        fs_domain = self.basis_func(stimulus_domain) # n_channels * n_domain
        fs_data = self.inverted_encoding(X) # n_trials * n_channels
        recon = fs_data @ fs_domain # n_trials * n_domain
        return recon

    def correlation_inversion(self, X, stimulus_domain=None):
        stimulus_domain = self.stimulus_domain if stimulus_domain is None else stimulus_domain
        # Iterate every possible stimulus in the domain
        fs_domain = self.basis_func(stimulus_domain) # n_channels * n_domain
        # Inverted encoding
        fs_data = self.inverted_encoding(X) # n_trials * n_channels
        # Correlation as evidence (similar to likelihood)
        n_trials, n_domain = fs_data.shape[0], fs_domain.shape[1]
        evidence = np.zeros([n_trials, n_domain]) # n_trials * n_domain
        for tid in range(n_trials):
            for did in range(n_domain):
                evidence[tid,did] = np.corrcoef(fs_data[tid,:], fs_domain[:,did])[0,1]
        return evidence

class BayesianChannelModel(BaseModel):
    def __init__(self, n_channels='required', basis_func='required', stimulus_domain='required', circular=False, stimulus_prior=None, global_search=False, verbose=2):
        '''
        After (<NAME> et al., 2015).

        Examples
        --------
        from mripy import encoding
        from sklearn import model_selection, preprocessing, pipeline

        stimulus_domain = np.linspace(0, pi, 181)
        n_channels = 8
        basis_func = lambda s: encoding.basis_vanBergen2015(s, n_channels=n_channels)
        model = encoding.BayesianChannelModel(n_channels=n_channels,
            basis_func=basis_func, stimulus_domain=stimulus_domain, circular=True)
        model = pipeline.make_pipeline(preprocessing.StandardScaler(), model)
        cv = model_selection.LeaveOneGroupOut() # One group of each run
        y_hat = model_selection.cross_val_predict(model, X, y, groups, cv=cv, n_jobs=1)
        '''
        self.n_channels = n_channels
        self.basis_func = basis_func
        self.stimulus_domain = stimulus_domain
        self.circular = circular
        self.stimulus_prior = 1 if stimulus_prior is None else stimulus_prior # TODO: This should be refactored for CV
        self.global_search = global_search
        self.verbose = verbose

    # get_params() is required by sklearn
    def get_params(self, deep=True):
        return dict(n_channels=self.n_channels, basis_func=self.basis_func, stimulus_domain=self.stimulus_domain,
            circular=self.circular, stimulus_prior=self.stimulus_prior, global_search=self.global_search)

    def fit(self, X, y):
        '''
        Parameters
        ----------
        X : 2D array
            n_trials * n_voxels BOLD response pattern
            (e.g., beta for each trial, or delayed and detrended time points within block plateau)
        y : 1D array
            n_trials stimulus value (e.g., orientation, color)
        '''
        b = X.T # Voxel BOLD response, n_voxels * n_trials
        s = y # Stimulus, n_trials
        fs = self.basis_func(s) # Channel response, n_channels * n_trials
        # Step 1: Estimate W by OLS regression
        # W = b @ fs.T @ np.linalg.inv(fs @ fs.T) # Weight, n_voxels * n_channels
        # It is critical to use pinv() here instead of inv():
        # 1) Result in much more accurate estimation for W, and hence tau, rho, sigma
        # 2) Avoid "LinAlgError: Singular matrix" at `inv(fs @ fs.T)`
        # 3) Avoid negative negloglikelihood (i.e., negative `slogdet(Omega)`)
        #    and non positive semidefinite `Omega` (i.e., `all(eigvals(Omega)>0) == False`),
        #    which could occur with randn W
        # Note that Gilles used `np.linalg.lstsq()` here, which should be numerically adept.
        W = b @ fs.T @ math.pinv(fs @ fs.T) # Weight, n_voxels * n_channels
        # Store params
        self.W_ = W
        # Step 2: Estimate tau, rho, sigma by ML optimization (gradient-based)
        z = b - W @ fs # n_voxels * n_trials
        # Initial params
        tau0 = np.std(b, axis=1)
        rho0 = np.mean(np.corrcoef(b)[np.triu_indices(len(tau0), k=1)])
        sigma0 = np.mean(np.std(fs, axis=1))
        params0 = np.r_[tau0, rho0, sigma0/5.0]
        # bounds = np.c_[np.ones(len(params0))*1e-4, np.r_[tau0*5, 1, sigma0*5]]
        bounds = np.c_[np.ones(len(params0))*1e-3, np.r_[tau0*5, 0.99, sigma0*5]]
        # Conjugate gradient algorithm, due to lack of support for bounds, requires multi-start to avoid/alleviate being trapped in local minima.
        if self.verbose > 0:
            print('>> Start maximum likelihood optimization...')
        if self.global_search:
            def accept_test(f_new, x_new, f_old, x_old):
                Omega = self._calc_Omega(self.W_, x_new[:-2], x_new[-2], x_new[-1])
                # is_pos_semi_def = np.all(np.linalg.eigvals(Omega) > 0)
                # return (f_new < f_old and f_new > 0 and is_pos_semi_def)
                is_singular = np.linalg.matrix_rank(Omega, hermitian=True) < Omega.shape[0]
                return (f_new < f_old and f_new > 0 and not is_singular)
            res = optimize.basinhopping(self._negloglikelihood, params0, accept_test=accept_test,
                minimizer_kwargs=dict(args=(z, W), method='L-BFGS-B', jac=self._negloglikelihood_prime, bounds=bounds))
        else:
            class Counter(object):
                def __init__(self, model, args):
                    self.count = 0
                    self.last_time = time.time()
                    self.model = model
                    self.args = args
                def step(self, xk):
                    self.count += 1
                    # cost = self.model._negloglikelihood(xk, *self.args) # The pinv->svd here is too expensive...
                    curr_time = time.time()
                    duration = curr_time - self.last_time
                    self.last_time = curr_time
                    if self.model.verbose > 1:
                        # print(f"iter#{self.count:03d} ({utils.format_duration(duration)}): cost={cost:.4f}, tau[-3:]={xk[-5:-2]}, rho={xk[-2]:.4f}, sigma={xk[-1]:.4f}")
                        print(f"iter#{self.count:03d} ({utils.format_duration(duration)}): tau[-3:]={xk[-5:-2]}, rho={xk[-2]:.4f}, sigma={xk[-1]:.4f}")
                    elif self.model.verbose == 1:
                        print(f"iter#{self.count:03d} ({utils.format_duration(duration)}): tau[-3:]={xk[-5:-2]}, rho={xk[-2]:.4f}, sigma={xk[-1]:.4f}", end='\r')
            # res = optimize.minimize(self._negloglikelihood, params0, args=(z, W), method='L-BFGS-B',
            #     jac=self._negloglikelihood_prime, bounds=bounds, callback=Counter(model=self, args=(z, W)).step)
            res = optimize.minimize(self._negloglikelihood, params0, args=(z, W, True), method='L-BFGS-B',
                jac=True, bounds=bounds, callback=Counter(model=self, args=(z, W)).step)
        params = res.x
        if self.verbose > 0:
            print(f"cost={res.fun}, iter={res.nit}, func_eval={res.nfev}, success={res.success}, {res.message}")
            print(params0[-3:], '-->', params[-3:])
        # Store params
        self.tau_, self.rho_, self.sigma_ = params[:-2], params[-2], params[-1]
        self._Omega = self._calc_Omega(self.W_, self.tau_, self.rho_, self.sigma_)
        self._Omega_inv = math.pinv(self.Omega_) # Update cache
        return self # Required by sklearn

    # Cache backed properties (the "if else" construct is to prevent unnecessary expression evaluation)
    Omega_ = property(lambda self: self.__dict__.setdefault('_Omega', None if hasattr(self, '_Omega') else self._calc_Omega(self.W_, self.tau_, self.rho_, self.sigma_)))
    Omega_inv_ = property(lambda self: self.__dict__.setdefault('_Omega_inv', None if hasattr(self, '_Omega_inv') else math.pinv(self.Omega_)))

    def predict(self, X, stimulus_domain=None, stimulus_prior=None, return_all=False):
        stimulus_domain = self.stimulus_domain if stimulus_domain is None else stimulus_domain
        stimulus_prior = self.stimulus_prior if stimulus_prior is None else stimulus_prior
        posterior = self.bayesian_inversion(X, stimulus_domain=stimulus_domain, stimulus_prior=stimulus_prior) # n_trials * n_domain
        y_map = stimulus_domain[np.argmax(posterior, axis=1)] # n_trials
        # y_map = stimulus_domain[math.median_argmax(posterior, axis=1)] # n_trials
        if self.circular:
            y_mean = math.circular_mean(stimulus_domain, domain=stimulus_domain, weight=posterior, axis=1)
            y_std = math.circular_std(stimulus_domain, domain=stimulus_domain, weight=posterior, axis=1)
        else:
            y_mean = np.sum(stimulus_domain * posterior, axis=1) / np.sum(posterior, axis=1) # n_trials
            y_std = np.sqrt(np.sum((stimulus_domain[np.newaxis,:] - y_mean[:,np.newaxis])**2 * posterior, axis=1) \
                / np.sum(posterior, axis=1)) # n_trials
        return (y_mean, y_std, y_map, posterior) if return_all else y_mean

    _pidx = 3 # Index of posterior (for MAP) if return_all

    def loglikelihood(self, X, y):
        b = X.T
        s = y
        fs = self.basis_func(s)
        z = b - self.W_ @ fs
        return self._calc_L(z, self.W_, self.tau_, self.rho_, self.sigma_)

    def bayesian_inversion(self, X, stimulus_domain=None, stimulus_prior=None, density=True):
        '''
        Parameters
        ----------
        X : 2D array, n_trials * n_voxels
        stimulus_domain : 1D array, n_domain
        stimulus_prior : 2D array, n_trials * n_domain (or 1D array, n_domain)
            None for a flat stimulus prior, same for all trials.

        Returns
        -------
        posterior : 2D array, n_trials * n_domain
        '''
        stimulus_domain = self.stimulus_domain if stimulus_domain is None else stimulus_domain
        stimulus_prior = self.stimulus_prior if stimulus_prior is None else stimulus_prior
        b = X.T[:,np.newaxis,:] # n_voxels * n_domain * n_trials
        fs = self.basis_func(stimulus_domain) # n_channels * n_domain
        predicted_mean_resp = (self.W_ @ fs)[...,np.newaxis] # n_voxels * n_domain * n_trials
        z = b - predicted_mean_resp
        # # mv_norm = stats.multivariate_normal(np.zeros(self.Omega_.shape[0]), self.Omega_)
        # mv_norm = stats.multivariate_normal(np.zeros(self.Omega_.shape[0]), self.Omega_, allow_singular=True)
        # # likelihood = mv_norm.pdf(z.T) # n_trials * n_domain
        # # posterior = likelihood * stimulus_prior # n_trials * n_domain
        # # posterior /= np.sum(posterior, axis=1, keepdims=True)
        # # The above code will underflow
        # loglikelihood = mv_norm.logpdf(z.T) # n_trials * n_domain
        # The following implementation is 10x faster than stats.multivariate_normal:D
        loglikelihood = math.gaussian_logpdf(z.T, np.zeros(self.Omega_.shape[0]), self.Omega_, cov_inv=self.Omega_inv_) # n_trials * n_domain
        logposterior = loglikelihood + np.log(stimulus_prior) # n_trials * n_domain
        posterior = math.normalize_logP(logposterior, axis=1)
        if density:
            posterior /= stimulus_domain[-1] - stimulus_domain[0]
        return posterior

    def _negloglikelihood(self, params, z, W, return_prime=False):
        tau, rho, sigma = params[:-2], params[-2], params[-1]
        if not return_prime:
            return -self._calc_L(z, W, tau, rho, sigma)
        else:
            Omega = self._calc_Omega(W, tau, rho, sigma)
            Omega_inv = math.pinv(Omega) # np.linalg.pinv() may encounter "LinAlgError: SVD did not converge" for some matrices
            L = -self._calc_L(z, W, tau, rho, sigma, Omega=Omega, Omega_inv=Omega_inv)
            L_prime = self._negloglikelihood_prime(params, z, W, Omega=Omega, Omega_inv=Omega_inv)
            return L, L_prime

    def _negloglikelihood_prime(self, params, z, W, Omega=None, Omega_inv=None):
        tau, rho, sigma = params[:-2], params[-2], params[-1]
        Omega = self._calc_Omega(W, tau, rho, sigma) if Omega is None else Omega
        dL_dOmega = self._dL_dOmega(z, Omega, chain=True, Omega_inv=Omega_inv)
        tau_prime = self._dL_dtau(z, Omega, W, tau, rho, sigma, dL_dOmega=dL_dOmega)
        rho_prime = self._dL_drho(z, Omega, W, tau, rho, sigma, dL_dOmega=dL_dOmega)
        sigma_prime = self._dL_dsigma(z, Omega, W, tau, rho, sigma, dL_dOmega=dL_dOmega)
        # print(f"{tau_prime[-3:]}, {rho_prime}, {sigma_prime}")
        return -np.r_[tau_prime, rho_prime, sigma_prime]

    def _negloglikelihood_prime_numerical(self, params, z, W, h=1e-6):
        Hs = np.eye(len(params)) * h
        return np.array([(self._negloglikelihood(params+H, z, W) - self._negloglikelihood(params-H, z, W)) / (2*h) for H in Hs])

    def _test_gradient(self, n_channels=6, n_voxels=10, n_trials=7):
        z = np.random.randn(n_voxels, n_trials)
        W = np.random.rand(n_voxels, n_channels)
        tau = np.random.rand(n_voxels)
        rho = 0.5
        sigma = 0.1
        params = np.r_[tau, rho, sigma]
        np.testing.assert_allclose(self._negloglikelihood_prime(params, z, W),
            self._negloglikelihood_prime_numerical(params, z, W), rtol=1e-6)

    def _test_loglikelihood(self, n_channels=6, n_voxels=10, n_trials=7):
        z = np.random.randn(n_voxels, n_trials)
        W = np.random.rand(n_voxels, n_channels)
        tau = np.random.rand(n_voxels)
        rho = 0.5
        sigma = 0.1
        mv_norm = stats.multivariate_normal(np.zeros(n_voxels), self._calc_Omega(W, tau, rho, sigma))
        np.testing.assert_allclose(self._calc_L(z, W, tau, rho, sigma),
            np.sum(mv_norm.logpdf(z.T)), rtol=1e-6)

    def _calc_Omega(self, W, tau, rho, sigma):
        return (rho + (1-rho)*np.eye(len(tau))) * np.outer(tau, tau) + sigma**2 * W@W.T

    def _calc_L(self, z, W, tau, rho, sigma, Omega=None, Omega_inv=None):
        '''
        L = log(p(b|s; W, Omega))
        z = b - W @ fs
        '''
        Omega = self._calc_Omega(W, tau, rho, sigma) if Omega is None else Omega
        M = math.pinv(Omega) if Omega_inv is None else Omega_inv # Although (4x) slower than inv, pinv is preferred in the numerical world (=inv if invertible and well conditioned)
        n_voxels, n_trials = z.shape
        # For a single sample: -0.5 * (z.T @ M @ z + np.log(np.linalg.det(Omega)) + n_voxels*np.log(2*np.pi))
        # May also use (by Gilles): np.sum(stats.multivariate_normal(np.zeros(n_voxels), Omega).logpdf(z.T))
        # This is less numerically robust: -0.5 * (np.trace(z.T @ M @ z) + n_trials*np.log(np.linalg.det(Omega)) + n_trials*n_voxels*np.log(2*np.pi))
        # return -0.5 * (np.trace(z.T @ M @ z) + n_trials*np.prod(np.linalg.slogdet(Omega)) + n_trials*n_voxels*np.log(2*np.pi))
        return -0.5 * ((z * (M @ z)).sum() + n_trials*np.prod(np.linalg.slogdet(Omega)) + n_trials*n_voxels*np.log(2*np.pi))

    def _dL_dOmega(self, z, Omega, chain=False, Omega_inv=None):
        M = math.pinv(Omega) if Omega_inv is None else Omega_inv # Although (4x) slower than inv, pinv is preferred in the numerical world (=inv if invertible and well conditioned)
        n_voxels, n_trials = z.shape
        # deriv = 0.5 * (M.T @ np.outer(z, z) @ M.T - M.T)
        # For a single sample: deriv = 0.5 * (M @ np.outer(z, z) @ M - M) # M.T == M
        deriv = 0.5 * (M @ z @ z.T @ M - n_trials*M)
        if chain:
            return deriv
        else:
            return (2 - np.eye(n_voxels)) * deriv

    def _dL_dtau(self, z, Omega, W, tau, rho, sigma, dL_dOmega=None):
        N = len(tau)
        dL_dOmega = self._dL_dOmega(z, Omega, chain=True) if dL_dOmega is None else dL_dOmega
        # deriv = np.zeros(N)
        # for n in range(N):
        #     e = np.zeros(N)
        #     e[n] = 1
        #     dOmega_dtau_n = (rho+(1-rho)*np.eye(N)) * (np.outer(e, tau) + np.outer(tau, e))
        #     # deriv[n] = np.trace(dL_dOmega @ dOmega_dtau_n)
        #     # The above expression is wasteful, trace(A@B) == sum(A*B.T), and Einstein summation is even faster (but numerically less stable???)
        #     deriv[n] = np.einsum('ij,ji->', dL_dOmega, dOmega_dtau_n)
        # This function is the bottleneck after cProfile.run()
        # https://stackoverflow.com/questions/18854425/what-is-the-best-way-to-compute-the-trace-of-a-matrix-product-in-numpy
        A = dL_dOmega * (rho+(1-rho)*np.eye(N)).T
        # deriv = (A*tau).sum(axis=1) + (A*tau[:,np.newaxis]).sum(axis=0)
        deriv = 2 * (A*tau).sum(axis=1) # A should be symmetric matrix
        return deriv

    def _dL_drho(self, z, Omega, W, tau, rho, sigma, dL_dOmega=None):
        dL_dOmega = self._dL_dOmega(z, Omega, chain=True) if dL_dOmega is None else dL_dOmega
        dOmega_drho = (1 - np.eye(len(tau))) * np.outer(tau, tau)
        # return np.trace(dL_dOmega @ dOmega_drho)
        return (dL_dOmega * dOmega_drho.T).sum()

    def _dL_dsigma(self, z, Omega, W, tau, rho, sigma, dL_dOmega=None):
        dL_dOmega = self._dL_dOmega(z, Omega, chain=True) if dL_dOmega is None else dL_dOmega
        dOmega_dsigma = W@W.T * 2*sigma
        # return np.trace(dL_dOmega @ dOmega_dsigma)
        return (dL_dOmega * dOmega_dsigma.T).sum()

class EnsembleModel(BaseModel):
    def __init__(self, n_ensemble=10, base_model='required', pred_method=None, pred_options=None):
        # Cannot use 1) **kwargs; 2) class as argument. Use instance instead (__class__ + get_params).
        # Otherwise you may get the misleading "TypeError: get_params() missing 1 required positional argument: 'self'".
        # Also cannot modify any argument, otherwise sklearn's clone() method will complain during cross-validation.
        self.n_ensemble = n_ensemble
        self.base_model = base_model
        self.pred_method = pred_method
        self.pred_options = pred_options

    # get_params() is required by sklearn
    def get_params(self, deep=True):
        return dict(n_ensemble=self.n_ensemble, base_model=self.base_model,
            pred_method=self.pred_method, pred_options=self.pred_options)

    def fit(self, X, y):
        # Perform argument validation here (as recommended by sklearn) so that get_params() and __init__() have the same effect
        # This is refactored so that load() can work without fit()
        self._set_default_params()
        self.models_ = [self.base_model.__class__(**self.base_model.get_params()).fit(X[:,k::self.n_ensemble], y) for k in range(self.n_ensemble)]
        return self # Required by sklearn

    def predict(self, X, method=None, options=None, return_all=False, pred_kws=None):
        method = self.pred_method if method is None else method
        options = self.pred_options if options is None else options
        pred_kws = dict(dict(return_all=(True if method in ['map'] else False)), **({} if pred_kws is None else pred_kws))
        preds = [model.predict(X[:,k::self.n_ensemble], **pred_kws) for k, model in enumerate(self.models_)]
        if method == 'mean':
            y_hat = np.mean([pred[0] if isinstance(pred, tuple) else pred for pred in preds], axis=0)
        elif method == 'map':
            stimulus_domain = pred_kws['stimulus_domain'] if 'stimulus_domain' in pred_kws else self.base_model.get_params()['stimulus_domain']
            posterior = np.mean([prep[options['pidx']] for prep in preds], axis=0)
            y_hat = stimulus_domain[np.argmax(posterior, axis=1)]
        return (y_hat, preds) if return_all else y_hat

    def _set_default_params(self):
        if self.pred_method is None:
            self.pred_method = 'map' if hasattr(self.base_model, '_pidx') else 'mean'
        self.pred_options = dict(dict(pidx=(self.base_model._pidx if hasattr(self.base_model, '_pidx') else -1)),
            **({} if self.pred_options is None else self.pred_options))

    def to_dict(self):
        d = super().to_dict()
        d['models_'] = [model.to_dict() for model in d['models_']]
        return d

    def from_dict(self, d):
        self._set_default_params()
        d['models_'] = [self.base_model.__class__(**self.base_model.get_params()).from_dict(model) for model in d['models_']]
        self.__dict__.update(d)
        return self

def shift_distribution(d, stimulus_domain, center_on=None, circular=True):
    '''
    Parameters
    ----------
    d : n_trials * n_domain
    '''
    if center_on is not None:
        tgt_idx = len(stimulus_domain)//2
        src_idx = np.argmin(np.abs(center_on[:,np.newaxis] - stimulus_domain[np.newaxis,:]), axis=-1)
        shift_idx = tgt_idx - src_idx
    shifted = np.zeros_like(d)
    for k, dd in enumerate(d):
        dd = np.roll(dd, shift_idx[k])
        if not circular:
            if shift_idx[k] >= 0:
                dd[:shift_idx[k]] = np.nan
            else:
                dd[shift_idx[k]:] = np.nan
        shifted[k,:] = dd
    return shifted

def discretize_prediction(y_pred, targets, circular_domain=None):
    '''
    Discretize continous prediction to the nearest target.
    Can handle irregular target grid and also circular domain.

    E.g.,
    y_pred = encoding.discretize_prediction(y_hat, arange(8)/8*pi, circular_domain=[0, pi])
    correct = encoding.circular_correct(y_true, y_hat, domain=[0, pi], n_targets=8)
    assert(allclose(mean(y_pred==y_true), mean(correct)))
    '''
    if circular_domain: # Circular domain
        D = circular_domain[-1] - circular_domain[0] # Domain size
        augmented = np.r_[targets, D+targets[0]]
    else: # Non circular domain
        augmented = targets
    idx = np.argmin(np.abs(y_pred[:,np.newaxis] - augmented[np.newaxis,:]), axis=-1) % len(targets)
    return targets[idx]

def circular_correct(y_true, y_pred, domain=None, n_targets=None, tolerance=None, return_dist=False):
    if domain is None:
        domain = [0, 2*np.pi]
    D = domain[-1] - domain[0] # Domain size
    if n_targets is None:
        if tolerance is None:
            raise ValueError('You must provide either "n_targets" or "tolerance".')
        else:
            d = tolerance
    else:
        d = D / n_targets / 2
    dist = np.abs(y_pred - y_true)
    dist = np.minimum(dist, D-dist)
    correct = (dist < d)
    return (correct, dist) if return_dist else correct

if __name__ == '__main__':
    pass


#!/usr/bin/env python3

import roslib; roslib.load_manifest('freemovr_engine')
import rospy

import os
import argparse

import numpy as np
import scipy.misc

import freemovr_engine.srv
import freemovr_engine.display_client as display_client

def show_image(ds,viewport,fname,white,black,rgb,pixel, ptsize, scale=False):
    rospy.init_node('show_image')

    dsc = display_client.DisplayServerProxy(ds,wait=True)
    dsc.enter_2dblit_mode()

    if viewport:
        mask = dsc.get_virtual_display_mask(viewport)
    else:
        mask = None

    if rgb != (-1,-1,-1):
        arr = dsc.new_image(rgb, mask)
    elif white:
        arr = dsc.new_image(dsc.IMAGE_COLOR_WHITE, mask)
    elif black:
        arr = dsc.new_image(dsc.IMAGE_COLOR_BLACK, mask)
    else:
        arr = scipy.misc.imread(fname)
        if arr.shape!=(dsc.height,dsc.width):
            arr = arr[0:min(dsc.height,arr.shape[0]),0:min(dsc.width,arr.shape[1]),:]
        if mask != None:
            masks = np.dstack([mask for i in range(0,arr.shape[-1])])
            if arr.shape != masks.shape:
                arr = np.resize(arr, masks.shape)
            arr *= masks

    if pixel and (white or black or (rgb != (-1,-1,-1))):
        col,row = map(int,pixel.split(','))

        if white:
            arr = dsc.new_image(dsc.IMAGE_COLOR_BLACK, mask)
            rgb = (dsc.IMAGE_COLOR_WHITE, dsc.IMAGE_COLOR_WHITE, dsc.IMAGE_COLOR_WHITE)
        elif black:
            arr = dsc.new_image(dsc.IMAGE_COLOR_WHITE, mask)
            rgb = (dsc.IMAGE_COLOR_BLACK, dsc.IMAGE_COLOR_BLACK, dsc.IMAGE_COLOR_BLACK)
        else:
            arr = dsc.new_image(dsc.IMAGE_COLOR_BLACK, mask)

        for i,c in enumerate(rgb):
            arr[row-ptsize:row+ptsize,col-ptsize:col+ptsize,i] = c

    if scale:
        orig_aspect = arr.shape[1]/float(arr.shape[0]) # w/h
        native_aspect = dsc.width/float(dsc.height)
        if native_aspect >= orig_aspect:
            # display is wider than image
            new_shape_height_h = int(dsc.width/float(orig_aspect))
            new_shape_full = new_shape_height_h, dsc.width

        else:
            # display is taller than image

            new_shape_wide_w = int(orig_aspect*dsc.height)
            new_shape_full = dsc.height, new_shape_wide_w
        new_image = scipy.misc.imresize( arr, new_shape_full )
        arr = new_image[:dsc.height, :dsc.width]
    dsc.show_pixels(arr)

def main():
    wd = roslib.packages.get_pkg_dir('freemovr_engine')
    default_fname = os.path.join(wd,'data','vienna-morning.jpg')

    parser = argparse.ArgumentParser()
    parser.add_argument('fname',nargs='?',default=default_fname)
    parser.add_argument('--rgb', help='RGB tuple r,g,b (float, 0...1)', default="-1,-1,-1")
    parser.add_argument('--white', action='store_true', help='show a white screen')
    parser.add_argument('--black', action='store_true', help='show a black screen')
    parser.add_argument('--viewport', type=str, help='only show on this viewport')
    parser.add_argument(
        '--display-server', type=str, metavar='/display_server', required=True, help=\
        'the path of the display server to configure')
    parser.add_argument('--pixel', type=str, help='light this pixel', metavar='x,y')
    parser.add_argument('--scale', action='store_true', help='scale the image to fullscreen')
    parser.add_argument('--pxsize', type=int, default=2)

    argv = rospy.myargv()
    args = parser.parse_args(argv[1:])

    show_image(args.display_server, args.viewport, args.fname,
            args.white,
            args.black,
            tuple(map(int,args.rgb.split(','))),
            args.pixel,
            args.pxsize,
            scale = args.scale,
    )

if __name__=='__main__':
    main()


<reponame>sillyemperor/langstudy
import os.path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
import pprint
import numpy as np
import imageio
from scipy import signal as sg
import cifar

meta = cifar.unpickle(os.path.join(BASE_DIR, '../data/cifar-100-python/meta'))

# pprint.pprint(meta)

meta_label_names = meta[b'fine_label_names']

# pprint.pprint(len(meta_label_names))
#
test = cifar.unpickle(os.path.join(BASE_DIR, '../data/cifar-100-python/test'))
# pprint.pprint(test) #batch_label,coarse_labels,data,filenames,fine_labels

# pprint.pprint(test.keys())  # dict_keys([b'filenames', b'batch_label', b'fine_labels', b'coarse_labels', b'data'])
#
# label_ids = test[b'fine_labels']
# pprint.pprint([min(label_ids), max(label_ids)])
# label_names = list(map(lambda i:meta_label_names[i], label_ids))
# # pprint.pprint(label_names)
#
# mdata = test[b'data'];

# for id in range(mdata.size):
#     cifar.save_jpg(mdata, label_names, id)
#     break

train = cifar.unpickle(os.path.join(BASE_DIR, '../data/cifar-100-python/train'))
pprint.pprint(train[b'fine_labels'])


<reponame>spicella/Intro_to_ComplexSystems-Kuramoto
import os
from sympy import *
import pandas as pd
import numpy as np
import scipy.fftpack
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use("seaborn-paper")
from mpl_toolkits.axes_grid1 import make_axes_locatable

def find_nearest(array, value):
    array = np.asarray(array)
    idx = (np.abs(array - value)).argmin()
    return idx

def find_nearest_value(array, value):
    array = np.asarray(array)
    idx = (np.abs(array - value)).argmin()
    return array[idx]

# ## Canvas palette

# In[2]:

#Canvas for single plot
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(figsize=[14,6])
plt.grid(True)
plt.title("Change-me!",fontsize=20)
plt.plot(x,y,label="testvalue")
plt.legend(fontsize=16)
plt.xlabel("XLABEL (unit)",fontsize=18)
plt.ylabel("YLABEL (unit)",fontsize=18)
plt.show()

# In[3]:

#Canvas for side by side
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(14,6))
fig.suptitle("test",y=1.05,fontsize=20)

axes[0].grid(True)
axes[0].plot(x,y,label="testvalue")
axes[0].legend(fontsize=16)
axes[0].set_title("TESTTITLE",fontsize=18)
axes[0].set_xlabel("XLABEL (unit)",fontsize=18)
axes[0].set_ylabel("YLABEL (unit)",fontsize=18)
axes[0].legend(fontsize=16)
axes[0].tick_params(axis='both', which='major', labelsize=15)

axes[1].grid(True)
axes[1].plot(x,y,label="testvalue")
axes[1].legend(fontsize=16)
axes[1].set_title("TESTTITLE",fontsize=18)
axes[1].set_xlabel("XLABEL (unit)",fontsize=18)
axes[1].set_ylabel("YLABEL (unit)",fontsize=18)
axes[1].legend(fontsize=16)
axes[1].tick_params(axis='both', which='major', labelsize=15)

fig.tight_layout()
plt.show()

# In[4]:

#Canvas for side by side
fig, axes = plt.subplots(nrows=2, ncols=4, figsize=(14,6))
fig.suptitle("test",y=1.05,fontsize=20)

axes[0,0].grid(True)
axes[0,0].plot(x,y,label="testvalue")
axes[0,0].legend(fontsize=16)
axes[0,0].set_title("TESTTITLE",fontsize=18)
axes[0,0].set_xlabel("XLABEL (unit)",fontsize=18)
axes[0,0].set_ylabel("YLABEL (unit)",fontsize=18)
axes[0,0].legend(fontsize=16)
axes[0,0].tick_params(axis='both', which='major', labelsize=15)

axes[0,1].grid(True)
axes[0,1].plot(x,y,label="testvalue")
axes[0,1].legend(fontsize=16)
axes[0,1].set_title("TESTTITLE",fontsize=18)
axes[0,1].set_xlabel("XLABEL (unit)",fontsize=18)
axes[0,1].set_ylabel("YLABEL (unit)",fontsize=18)
axes[0,1].legend(fontsize=16)
axes[0,1].tick_params(axis='both', which='major', labelsize=15)

fig.tight_layout()
plt.show()

# ## Read data

# In[2]:

#Folder and paths definitions
main_path  = os.getcwd()
datafolder_path = main_path+"/results"
results_dir = "/output_py"
output_dir = main_path+results_dir
try:
    os.mkdir(output_dir)
except OSError:
    print ("Creation of the directory %s failed" % results_dir)
else:
    print ("Successfully created the directory %s " % results_dir)

# In[27]:

Kvalues = np.linspace(0,6,30)
Kvalues = np.around(Kvalues, decimals=3)

# In[28]:

pvalues = [0, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, .95, 1]

# In[9]:

#Create dataframe dictionary. For each entry, first value is the K of the dataframe (second value)
data = []
for i in range(0,len(Kvalues)):
    for j in pvalues:
        filename = datafolder_path + "/WS_gfreq_uphase_N2000_NOMF_T20000_dt0.0100_nruns10_K%.3f_p=%.3f.tsv"%(Kvalues[i],j)
        #cols refers to timestep, avgmod, stdmod, avgphase,stdphase (of order parameter)
        df = pd.read_csv(filename,sep="\t",header=None)
        data.append([Kvalues[i],j,df])

# In[10]:

#data[0][x]#, x=0=> K, x=1=>p, x=2=> df

# In[11]:

def rinf_avg(df):
    lasts = df[1][int(T*.9):-1]
    return np.mean(lasts)

def rinf_std(df):
    lasts = df[1][int(T*.9):-1]
    return np.std(lasts)

# In[12]:

K_plot = []
p_plot = []
r_inf_avg = []
r_inf_std = []
for i in range(0,len(data)):
    t_plot = data[i][2][0]
    K_plot.append(data[i][0])
    p_plot.append(data[i][1])
    r_inf_avg.append(rinf_avg(data[i][2]))
    r_inf_std.append(rinf_std(data[i][2]))

# In[29]:

r_inf_mat = np.zeros(shape=[len(pvalues),len(Kvalues)])
for i in range(0,len(data)):
    r_inf_mat[find_nearest(pvalues, data[i][1])][find_nearest(Kvalues, data[i][0])] = rinf_avg(data[i][2])

plt.figure(figsize=[10,10])
ax = plt.gca()
#name = "Kuramoto oscillators on Watts-Strogatz network \n N = %d, r = %d, dt = %.3f, %s, n_runs = %d\n$r_{\\infty}$"%(N,2,dt,freq_plot, n_runs)

im = plt.imshow(r_inf_mat)
plt.title("Kuramoto oscillators on Watts-Strogatz network\n N=%d, $r_{WS}$=%d, T=%d, dt=%.3f, %s, n_runs=%d\n $r_{\\infty}$"%(N,3,T,dt,freq_plot,n_runs),fontsize=20)

plt.yticks(np.linspace(0,len(pvalues)-1,len(pvalues)),pvalues)
plt.ylabel("p",fontsize=18,rotation=0)
plt.xticks(np.linspace(0,len(Kvalues)-1,len(Kvalues)),Kvalues,rotation=45)
plt.xlabel("K",fontsize=18)

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.15)
cbar = plt.colorbar(im, cax=cax)

plt.tight_layout()
plt.savefig(output_dir+config_name+"WS_rinf_heatmap.png")

plt.show()

# In[14]:

Kcs_df = pd.DataFrame([p_plot,K_plot,r_inf_avg,r_inf_std]).T

# In[15]:

Kcs_df = Kcs_df.sort_values(by=[0,1])

# In[31]:

Kc_plot = []

for i in range(0,len(pvalues)):

    idx_kc = find_nearest(Kcs_df[0+i*len(Kvalues)+1:len(Kvalues)*(i+1)][2],.5)
    rinf_value = find_nearest_value(Kcs_df[0+i*len(Kvalues)+1:len(Kvalues)*(i+1)][2],.5)
    df = Kcs_df[0+i*len(Kvalues)+1:len(Kvalues)*(i+1)]
    df = df.reset_index(inplace = False)
    Kc_plot.append([pvalues[i],df[1].iloc[idx_kc]])
    print("prob",pvalues[i],", rinf %.3f"%(rinf_value),", K",df[1].iloc[idx_kc])
Kc_plot = pd.DataFrame(Kc_plot, columns=["p","Kc"])

# In[54]:

import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# In[55]:

def func(x, a, b):
    return a+ b/x

# In[66]:

popt, pcov = curve_fit(func, Kc_plot["p"][2:],Kc_plot["Kc"][2:], p0=[1.6,2])
popt

# In[70]:

x_fit = np.linspace(Kc_plot["p"][2],1,100)
y_fit = func(x_fit,popt[0],popt[1])

# In[94]:

plt.figure(figsize=[14,6])
plt.grid(True,alpha=.3)
plt.title("Kuramoto oscillators on Watts-Strogatz network\n N=%d, $r_{WS}$=%d, T=%d, dt=%.3f, %s, n_runs=%d\n $K_{c}(p)$"%(N,3,T,dt,freq_plot,n_runs),fontsize=20)
plt.plot(x_fit,y_fit,label="y(p) = a + $\\frac{b}{p}$ fit")
plt.plot(Kc_plot["p"],Kc_plot["Kc"],c='b',marker='o',markersize=12,ls='',label="Raw Data")
plt.plot(Kc_plot["p"][2:],Kc_plot["Kc"][2:],c='r',marker='o',markersize=9,ls='--',linewidth=.7,label="Fitted Data")
plt.xticks(pvalues,pvalues)
plt.xlabel("p",fontsize=18,rotation=0)
plt.yticks(Kvalues,Kvalues)
plt.ylim(min(Kc_plot["Kc"])*.85,max(Kc_plot["Kc"])*1.1)
plt.legend(fontsize=18)

plt.text(.5,4,"a = %.4f$\pm$%.4f, b = %.4f$\pm$%.4f"%(popt[0],pcov[0,0],popt[1],pcov[1,1]),fontsize=20)

plt.tight_layout()
plt.savefig(output_dir+config_name+"WS_Kc(p).png")


<filename>sdt_metrics/plotting/_roc_plot.py
from __future__ import print_function
from __future__ import division

# Copyright (c) 2012, <NAME> [see LICENSE.txt]

import pylab
import numpy as np
import scipy

from numpy import pi
from scipy.stats import norm

import sdt_metrics
from .._sdt_metrics import ltqnorm,HI,MI,CR,FA
from ._mult_roc_plot import mult_roc_plot

_normdist = lambda x : np.exp(-x**2/2.)/np.sqrt(2*pi)

def roc_plot(*args, **kwds):
    """
    Receiver Operating Characteristic (ROC) Plot

       args:
          1 argument:
             sdt_metrics.SDT object

          2 arguments:
             pHI
             pFA

          4 arguments:
             hit count
             miss count
             correction rejection count
             false alarm count

       kwds:
          metric: dprime, aprime, amzs (default is dprime)

          isopleths: None, beta, c, bppd, bmz

          fname: outputname

          dpi: resolution of plot
    """
    # wrap mult_roc_plot
    if len(args) == 1:
        mult_roc_plot([args[0],''], **kwds)
    else:
        mult_roc_plot([args,''], **kwds)


"""
An interative GUI designed for a low pass filter
author: <NAME>
year: 2016
"""

from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import FileDialog
import scipy.signal as signal
import matplotlib.pyplot as plt
import numpy as np
import tkMessageBox
import tkFileDialog

np.set_printoptions(suppress=True) # surpress scientific notation

def main():
	root = Tk()
	root.wm_title("FIR low pass filter")
	# making the grid

	#main frame
	frame = Frame(root)
	frame.pack(fill=BOTH,expand=YES)

	#left side of gui
	leftframe = Frame(frame, bd=20)
	leftframe.pack(side=TOP,fill=X,expand=YES)

	#entry = Entry(leftframe)
        #entry.grid(row=0, columnspan=3, sticky=W+E)

	# add some widgets
	L1 = Label(leftframe, text="Number of coeffs",bd=12,font=("arial", "11", "normal"))
	L1.grid( row = 0, column = 0)
	ttp1 = CreateToolTip(L1, "The number of coefficents that will \n be used in windowing")

	#L2 = Label(leftframe, text="Nqyst freq",bd=12,font=("arial", "11", "normal"))
	#L2.grid( row = 0, column = 1)
	L4 = Label(leftframe, text="Width",bd=12,font=("arial", "11", "normal"))
	L4.grid( row = 0, column = 1)
	ttp2 = CreateToolTip(L4, "The effective width of the transistion region")

	L3 = Label(leftframe, text="Cut off",bd=12,font=("arial", "11", "normal"))
	L3.grid( row = 0, column = 2)
	ttp3 = CreateToolTip(L3, "The cut off point of the window (-3dB)")

	coeff_entry = Entry(leftframe, bd =6,justify='center')
	coeff_entry.grid( row = 1, column = 0)

	#nyqst_entry = Entry(leftframe, bd =6,justify='ctkFileDialogenter')
	#nyqst_entry.grid( row = 1, column = 1)

	width_entry = Entry(leftframe, bd =6,justify='center')
	width_entry.grid( row = 1, column = 1)

	cutoff_entry = Entry(leftframe, bd =6,justify='center')
	cutoff_entry.grid( row = 1, column = 2)

	variable = StringVar(root)
	variable.set("Window") # default value
	w = OptionMenu(leftframe, variable, "boxcar", "triang", "blackman", "hamming", "hann", "bartlett", "flattop", "parzen", "bohman", "blackmanharris", "nuttall", "barthann",
		"kaiser (needs beta)", "gaussian (needs standard deviation)", "general_gaussian (needs power, width)",
		"slepian (needs width)", "chebwin (needs attenuation)", "exponential (needs decay scale)", "tukey (needs taper fraction)")
	w.grid(row = 4,column = 0,pady=(10, 10))

	option1_entry = Entry(leftframe, bd =6,justify='center',state=DISABLED)
	option1_entry.grid( row = 4, column = 1,pady=(20, 10))
	option2_entry = Entry(leftframe, bd =6,justify='center',state=DISABLED)
	option2_entry.grid( row = 4, column = 2,pady=(20, 10))

	def option_changed(*args):
		opt = variable.get()
		print opt
		if "kaiser" in opt:
			option1_entry.configure(state = "normal")
			option2_entry.configure(state = "disabled")
			return 0
		if "gaussian" in opt and "_" not in opt:
			option1_entry.configure(state = "normal")
			option2_entry.configure(state = "disabled")
			return 0
		if "general_gaussian" in opt:
			option1_entry.configure(state = "normal")
			option2_entry.configure(state = "normal")
			return 0
		if "slepian" in opt:
			option1_entry.configure(state = "normal")
			option2_entry.configure(state = "disabled")
			return 0
		if "chewbin" in opt:
			option1_entry.configure(state = "normal")
			option2_entry.configure(state = "disabled")
			return 0
		if "exponential" in opt:
			option1_entry.configure(state = "normal")
			option2_entry.configure(state = "disabled")
			return 0
		if "tukey" in opt:
			option1_entry.configure(state = "normal")
			option2_entry.configure(state = "disabled")
			return 0
		else:
			option1_entry.configure(state = "disabled")
			option2_entry.configure(state = "disabled")

	variable.trace("w", option_changed)
	def graph(b,a=1):
			#make a graph
			w,h = signal.freqz(b,a)
			h_dB = 20 * np.log10 (abs(h))
			plt.figure()
			#plt.subplot(311)
			plt.plot(w/max(w),h_dB)
			plt.ylim(-150, 5)
			plt.ylabel('Magnitude (db)')
			plt.xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
			plt.title(r'Frequency response')
			plt.show()
			plt.figure()
			l = len(b)
			impulse = np.repeat(0.,l); impulse[0] =1.
			x = arange(0,l)
			response = signal.lfilter(b,a,impulse)
			#plt.subplot(312)
			plt.stem(x, response)
			plt.ylabel('Amplitude')
			plt.xlabel(r'n (samples)')
			plt.title(r'Impulse response')
			plt.show()
			#plt.figure()
			#plt.subplot(313)
			#step = np.cumsum(response)
			#plt.stem(x, step)
			#plt.ylabel('Amplitude')
			#plt.xlabel(r'n (samples)')
			#plt.title(r'Step response')
			#plt.subplots_adjust(hspace=0.5)
			#plt.show()
			return 1

	def run():
		"""
		Function of the run button

		"""
		numtaps = coeff_entry.get() # number of coeffs
		# check to see if number
		if numtaps.isdigit() == False:
			tkMessageBox.showinfo( "ERROR","Coefficent entry is not an integer number",icon="error")
			return 0
		else:
			numtaps = float(numtaps)

		mywindow = variable.get() #cut off is the normalised cut off in terms of nqy
		# window is the function
		mywindow = str(mywindow)
		mylist = ["kaiser","slepian","chewbin","exponential","tukey" ]
		if any(x in mywindow for x in mylist):
			for x in mylist:
				if x in mywindow:
					try:
						mynum = float(option1_entry.get())
						mywindow  = (str(x), mynum)
					except:
						tkMessageBox.showinfo( "ERROR","window entry not a number",icon="error")
						return 0

		elif "gaussiantkFileDialog" in mywindow and "general" not in mywindow:
			try:
				mynum = float(option1_entry.get())
				mywindow  = ("gaussian", mynum)
			except:
				tkMessageBox.showinfo( "ERROR","window entry not a number",icon="error")
		elif "general_gaussian" in mywindow:
			try:
				mynum = float(option1_entry.get())
				mynum2 = float(option2_entry.get())
				mywindow  = ("general_gaussian", mynum,mynum2)
			except:
				tkMessageBox.showinfo( "ERROR","one of the window entry not a number",icon="error")
				return 0
		else:
			mywindow = mywindow

		mycutoff = cutoff_entry.get()

		try:
			float(mycutoff)
			mycutoff = float(mycutoff)
		except:
			tkMessageBox.showinfo( "ERROR","Cut off entry is not a floating point number",icon="error")
			return 0

		mywidth = width_entry.get()
		try:
			float(mywidth)
			mycutoff = float(mywidth)
		except:
			tkMessageBox.showinfo( "ERROR","Width entry is not a floating point number",icon="error")
			return 0
		# options = boxcar, triang, blackman, hamming, hann, bartlett, flattop, parzen, bohman, blackmanharris, nuttall, barthann,
		#kaiser (needs beta), gaussian (needs standard deviation), general_gaussian (needs power, width),
		#slepian (needs width), chebwin (needs attenuation), exponential (needs decay scale), tukey (needs taper fraction)

		fir_coeff = signal.firwin(numtaps, cutoff = mycutoff, window = mywindow, nyq = 1000, width = mywidth) #
		plt.ion()
		graph(fir_coeff)
		T1.delete("1.0",END) # clear text
		mymulti = Multi_entry.get()
		if mymulti is not None:
			if mymulti.isdigit():
				mymulti = float(mymulti)
				#fir_coeff = fir_coeff.tolist()
				coeff_sum = np.sum(fir_coeff)
				fir_coeff = fir_coeff/coeff_sum
				fir_coeff = np.rint(fir_coeff*mymulti)
				fir_coeff = fir_coeff.astype(int)

				#for x in fir_coeff:
				#	print type(x)
				#fir_coeff = [ x*mymulti for x in fir_coeff ]

		T1.insert(END, fir_coeff)
		return 1

	MultiLabel = Label(leftframe, text="Multiply",bd=12,font=("arial", "11", "normal"))
	MultiLabel.grid( row = 5, column = 0)

	Multi_entry = Entry(leftframe, bd =6,justify='center')
	Multi_entry.grid( row = 5, column = 1)

	run_button = Button(leftframe, text ="Run",command=run)
	run_button.grid(row = 5,column = 2,pady=(20, 10))
	T1 = Text(leftframe, height=7)
	T1.grid(row = 6, column = 0, columnspan = 3)
	S = Scrollbar(leftframe)
	S.grid(row = 6, column = 3, sticky=NS)
	T1.config(yscrollcommand=S.set)
	S.config(command=T1.yview)
	root.mainloop()

class CreateToolTip(object):
    '''
    create a tooltip for a given widget
    '''
    def __init__(self, widget, text='widget info'):
        self.widget = widget
        self.text = text
        self.widget.bind("<Enter>", self.enter)
        self.widget.bind("<Leave>", self.close)
    def enter(self, event=None):
        x = y = 0
        x, y, cx, cy = self.widget.bbox("insert")
        x += self.widget.winfo_rootx() + 25
        y += self.widget.winfo_rooty() - 50
        # creates a toplevel window
        self.tw = Toplevel(self.widget)
        # Leaves only the label and removes the app window
        self.tw.wm_overrideredirect(True)
        self.tw.wm_geometry("+%d+%d" % (x, y))
        label = Label(self.tw, text=self.text, justify='left',
                       background='lightgrey', relief='solid', borderwidth=3,
                       font=("arial", "12", "normal"))
        label.pack(ipadx=1)
    def close(self, event=None):
        if self.tw:
            self.tw.destroy()

if __name__ == '__main__':
	main() # run the program


<filename>notebooks/sCSC.py
# Python 3 file to generate sCSC dendrogram
# <NAME>, <NAME>, and <NAME>
# Stanford University
# 2017-2018
# Instructions: Execute this code in the terminal, e.g.
# >>> python3 sCSC.py protein_g_adjmat.npy 7 my_directory
# Adjacency matrix must be in numpy format

import numpy as np
import matplotlib.pyplot as plt
import os, sys, pickle

# The following was written using SciPy Version 0.19.1
import scipy.linalg
from scipy.cluster.hierarchy import average, linkage, fcluster
from scipy.spatial.distance import pdist

# Compute the color field and obtains the
# user-specified number of eigenvectors
def get_eigenvectors_for_dendrogram(adjacency_matrix,
                                    numeigvecs):
    A = adjacency_matrix

    # Create column vector of row-sums of adjacency matrix
    Adegree = np.sum(A, axis=1)

    # Create diagonal degree matrix with row-sums of adjacency matrix
    DD = np.diag(Adegree)

    # Create graph Laplacian
    L = DD - A

    # Compute eigenvectors and eigenvalues of generalized eigenvalue problem.
    eigval, eigvec = scipy.linalg.eig(L, DD)

    # Sort eigenvalues in decending order
    lambdaindex = np.argsort(eigval)[::-1]
    sortedeigs = eigval[lambdaindex]

    # Sort eigenvectors in order corresponding to sorted eigenvalues
    CSC_fullset = eigvec[:, lambdaindex]

    # Select number of eigenvectors to include in dendrogram analysis
    evec = CSC_fullset[:,:numeigvecs]

    # This normalization makes results consistent with matlab
    for ind, ev in enumerate(evec.T):
        constant = np.matmul(np.matmul(ev.T, DD), ev)
        evec[:,ind] = ev/np.sqrt(constant)

    return evec, L

# Obtain binary codes for each orthogonal process
# using agglomerative hierarchical clustering
def get_binary_codes(evec, numeigvecs):
    binary = np.empty(evec.shape)

    #Loop through eigenvectors to create binary code for each state in dataset
    for col in np.arange(numeigvecs):

        # Create agglomerative hierarchical tree using average linkage
        p = pdist([[x] for x in evec[:,col]])
        lnk = linkage(p, 'average')

        # Assign value of 1 or 2 to each state state depending on membership
        # in bifurcated tree
        T = fcluster(lnk, criterion='maxclust', t=2) - 1

        binary[:, col] = T

    return binary

# Initialize the matrices that will be used
# to plot the dendrogram and a dictionary that will
# store cluster assignments
def initialize_dendrogram_matrices(numeigvecs):
    # Initialize matrix containing length of each dendrogram branch
    groupdist = np.zeros([2 ** (numeigvecs),numeigvecs])

    # Initialize matrix containing x coordinates of each dendrogram node
    plotcoordsx = np.zeros([2 ** (numeigvecs),numeigvecs])

    # Initialize matrix containing y coordinates of each dendrogram node
    plotcoordsy = np.zeros([2 ** (numeigvecs),numeigvecs])

    return groupdist, plotcoordsx, plotcoordsy, {}

# Get subgroup information
def get_subgroup_similarity(evec, L, binary, combos, split, index_dict):
    n = evec.shape[0]

    # Create binary code for group 1 of a pair of branches
    group1 = np.array([int(x) for x in np.binary_repr(combos, split+1)])

    # Create binary code for group 2 of a pair of branches
    group2 = np.array([int(x) for x in np.binary_repr(combos+1, split+1)])

    # Create string-compatible version of binary code for group 1 for plotting
    g1label = np.binary_repr(combos, split+1)

    # Create strong-compatible version of binary code for group 2 for plotting
    g2label = np.binary_repr(combos+1, split+1)

    # Initialize eigenvector for current level of dendrogram
    Xgroup = evec[:,split]

    # Remove eigenvector elements not in in group 1 or group 2
    not_in_g1 = np.delete(np.arange(n), np.where((binary[:,:split+1][:]==group1).all(1)))
    not_in_g2 = np.delete(np.arange(n), np.where((binary[:,:split+1][:]==group2).all(1)))
    Xgroup = np.delete(Xgroup, np.intersect1d(not_in_g1, not_in_g2))

    # Initialize graph Laplacian
    Lgroup = L

    # Remove columns not in group 1 or group 2
    Lgroup = np.delete(Lgroup, np.intersect1d(not_in_g1, not_in_g2), axis=0)

    # Remove rows not in group 1 or group 2
    Lgroup = np.delete(Lgroup, np.intersect1d(not_in_g1, not_in_g2), axis=1)

    # Calculate number of states in group 1
    Z1count = sum((binary[:,:split+1][:]==group1).all(1))

    # If group 1 is occupied by any states
    if Z1count > 0:
        # Calculate dissimilarity metric for states in groups 1 and 2
        Z1 = np.matmul(np.matmul(Xgroup.transpose(), Lgroup), Xgroup)

        index_dict[g1label] = [i for i, x in enumerate(
                               (binary[:,:split+1][:]==group1).all(1))
                               if x]

    else:
        # dissimilarity metric is undefined if group 1 is unoccupied
        Z1 = np.nan

    # Calculate number of states in group 2
    Z2count = sum((binary[:,:split+1][:]==group2).all(1))

    # if group 2 is occupied by any states
    if Z2count > 0:
        # Calculate dissimilarity metric for states in groups 1 and 2
        Z2 = np.matmul(np.matmul(Xgroup.transpose(), Lgroup), Xgroup)

        index_dict[g2label] = [i for i, x in enumerate(
                               (binary[:,:split+1][:]==group2).all(1))
                               if x]

    else:
        # Dissimilarity metric is undefined if group 2 is unoccupied
        Z2 = np.nan

    return g1label, g2label, Z1count, Z2count, Z1, Z2, index_dict

# Helper function for plotting lines
def plot_line(x1, x2, y1, y2, count, adjacency_matrix, maxlinewidth,
              style='-o', color='black', markerfacecolor='black',
              markeredgecolor='black'):
    plt.plot([x1, x2], [y1, y2], style, color=color,
             markerfacecolor=markerfacecolor,
             markeredgecolor=markeredgecolor,
             linewidth=maxlinewidth*(count/len(adjacency_matrix[:,1])))

# Plot the next level of the dendrogram
def plot_next_dendrogram_split(plotcoordsx, plotcoordsy, row, split, adjacency_matrix,
                               g1label, g2label, Z1count, Z2count, Z1, Z2,
                               figsize=(14,8), lineangle=np.pi/4, maxlinewidth=15):

    A = adjacency_matrix

    xrel1 = -Z1*np.sin(lineangle)
    xrel2 = Z2*np.sin(lineangle)

    yrel1 = -Z1*np.cos(lineangle)
    yrel2 = -Z2*np.cos(lineangle)

    if split == 0:
        plotcoordsx[row, split] = xrel1
        plotcoordsx[row+1, split] = xrel2

        plotcoordsy[row, split] = yrel1
        plotcoordsy[row+1, split] = yrel2

        fig, ax = plt.subplots(figsize=figsize)

        plot_line(0, plotcoordsx[row, split], 0, plotcoordsy[row, split],
                  Z1count, adjacency_matrix, maxlinewidth)

        plot_line(0, plotcoordsx[row+1, split], 0, plotcoordsy[row+1, split],
                  Z2count, adjacency_matrix, maxlinewidth)

        plt.text(plotcoordsx[row, split],
                 np.mean([0, plotcoordsy[row, split]]),
                 g1label, fontsize=12)

        plt.text(plotcoordsx[row+1, split],
                 np.mean([0, plotcoordsy[row+1, split]]),
                 g2label, fontsize=12)

        plt.text(plotcoordsx[row, split], plotcoordsy[row, split],
                 '     '+str(Z1count), color='red',
                 fontsize=12)

        plt.text(plotcoordsx[row+1, split], plotcoordsy[row+1, split],
                 '     '+str(Z2count), color='red',
                 fontsize=12)

    else:
        plotcoordsx[row, split] = xrel1 + plotcoordsx[int((row+1)/2), split-1]
        plotcoordsx[row+1, split] = xrel2 + plotcoordsx[int((row+1)/2), split-1]

        plotcoordsy[row, split] = yrel1 + plotcoordsy[int((row+1)/2), split-1]
        plotcoordsy[row+1, split] = yrel2 + plotcoordsy[int((row+1)/2), split-1]

        plot_line(plotcoordsx[int((row+1)/2), split-1],
                  plotcoordsx[row, split],
                  plotcoordsy[int((row+1)/2), split-1],
                  plotcoordsy[row, split],
                  Z1count, adjacency_matrix, maxlinewidth)

        plot_line(plotcoordsx[int((row+1)/2), split-1],
                  plotcoordsx[row+1, split],
                  plotcoordsy[int((row+1)/2), split-1],
                  plotcoordsy[row+1, split],
                  Z2count, adjacency_matrix, maxlinewidth)

        if not np.isnan(plotcoordsx[row, split]):
            plt.text(plotcoordsx[row, split],
                     np.mean([plotcoordsy[int((row+1)/2), split-1],
                     plotcoordsy[row, split]]), g1label, fontsize=12)

            plt.text(plotcoordsx[row, split], plotcoordsy[row, split],
                     '     '+str(Z1count), color='red', fontsize=12)

        if not np.isnan(plotcoordsx[row+1, split]):
            plt.text(plotcoordsx[row+1, split],
                     np.mean([plotcoordsy[int((row+1)/2), split-1],
                     plotcoordsy[row+1, split]]), g2label, fontsize=12)

            plt.text(plotcoordsx[row+1, split], plotcoordsy[row+1, split],
                     '     '+str(Z2count), color='red', fontsize=12)

    return plotcoordsx, plotcoordsy

if __name__ == '__main__':
    if len(sys.argv) < 4:
        print('Usage:')
        print('   python3 sCSC.py [adjaency matrix] [num eigvecs] ' +
              '[directory for files]')
        sys.exit(0)
    else:
        adj_mat_file = sys.argv[1]
        numeigvecs = int(sys.argv[2])
        direc = sys.argv[3]

    print('Loading adjacency matrix...')
    try:
        adjacency_matrix = np.load(adj_mat_file)
    except:
        raise RuntimeError('Adjacency matrix file must be a numpy array.')

    # Obtain eigenvectors for clustering and graph Laplacian
    evec, L = get_eigenvectors_for_dendrogram(adjacency_matrix, numeigvecs)

    # Get binary codes for each orthogonal process
    binary = get_binary_codes(evec, numeigvecs)

    # Initialize dendrogram matrices
    groupdist, plotcoordsx, plotcoordsy, index_dict = initialize_dendrogram_matrices(numeigvecs)

    # Loop through eigenvectors, from largest to last one included in analysis
    print('Constructing sCSC dendrogram...')
    for split in np.arange(numeigvecs):
        # Output current dendrogram level
        print("  Split %i of %i" % (split+1, numeigvecs))

        # Initialize level of dendrogram
        row = 0

        # Loop through binary codes
        for combos in np.arange(0, 2**(split+1), 2):
            g1label, g2label, Z1count, Z2count, Z1, Z2, index_dict = get_subgroup_similarity(
                                                    evec, L, binary, combos, split, index_dict)

            # Record dissimilarity metric for group 1
            groupdist[row, split] = Z1

            # Record dissimilarity metric for group 2
            groupdist[row+1, split] = Z2

            # Plot this level of the dendrogram
            plotcoordsx, plotcoordsy = plot_next_dendrogram_split(
                                       plotcoordsx, plotcoordsy, row, split, adjacency_matrix,
                                       g1label, g2label, Z1count, Z2count, Z1, Z2)

            # Advance dendrogram level counter
            row = row + 2

    # Optionally add plot specifications
    plt.xticks(fontsize=12)
    plt.yticks(fontsize=12)
    plt.tight_layout()

    # Make sure directory exists
    try:
        os.stat(direc)
        print('Writing files to existing directory %s...' % direc)
    except:
        os.mkdir(direc)
        print('Creating new directory %s...' % direc)

    # Save figure
    dend_filename = direc  + '/dendrogram.pdf'
    print('Saving dendrogram to %s...' % dend_filename)
    plt.savefig(dend_filename)

    # Export sCSC Dendrogram distance matrix
    dist_filename = direc + '/groupdist.npy'
    print('Saving distance matrix to %s...' % dist_filename)
    np.save(dist_filename, groupdist)

    # Save dictionary of member indices
    index_filename = direc  + '/index_dict.pkl'
    print('Saving index dictionary to %s...' % index_filename)
    with open(index_filename, 'wb') as f:
        pickle.dump(index_dict, f)

    print('Done.')


<reponame>willyspinner/High-Performance-Face-Recognition<filename>src/ResNet/CASIA_WEB_FACE.PyTorch/data_loader.py
import collections
import os.path as osp
# from __future__ import division

import numpy as np
import PIL.Image
import scipy.io
import skimage
import skimage.color as color
from skimage.transform import rescale
from skimage.transform import resize
import torch
from torch.utils import data

DEBUG = False

class DemoFaceDataset(data.Dataset):
    '''
        Dataset subclass for demonstrating how to load images in PyTorch.

    '''

    # -----------------------------------------------------------------------------
    def __init__(self, root, split='train', set='tiny', im_size=250):
    # -----------------------------------------------------------------------------
        '''
            Parameters
            ----------
            root        -   Path to root of ImageNet dataset
            split       -   Either 'train' or 'val'
            set         -   Can be 'full', 'small' or 'tiny' (5 images)
        '''
        self.root = root  # E.g. '.../ImageNet/images' or '.../vgg-face/images'
        self.split = split
        self.files = collections.defaultdict(list)
        self.im_size = im_size # scale image to im_size x im_size
        self.set = set

        if set == 'small':
            raise NotImplementedError()

        elif set == 'tiny':
            # DEBUG: 5 images
            files_list = osp.join(root, 'tiny_face_' + self.split + '.txt')

        elif set == 'full':
            raise NotImplementedError()

        else:
        	raise ValueError('Valid sets: `full`, `small`, `tiny`.')

        assert osp.exists(files_list), 'File does not exist: %s' % files_list

        imfn = []
        with open(files_list, 'r') as ftrain:
            for line in ftrain:
                imfn.append(osp.join(root, line.strip()))
        self.files[split] =  imfn

    # -----------------------------------------------------------------------------
    def __len__(self):
    # -----------------------------------------------------------------------------
        return len(self.files[self.split])

    # -----------------------------------------------------------------------------
    def __getitem__(self, index):
    # -----------------------------------------------------------------------------
        img_file = self.files[self.split][index]
        img = PIL.Image.open(img_file)

        # HACK: for non-RGB images - 4-channel CMYK or 1-channel grayscale
        if len(img.getbands()) != 3:
            while len(img.getbands()) != 3:
                index -= 1
                img_file = self.files[self.split][index] # if -1, wrap-around
                img = PIL.Image.open(img_file)

        if self.im_size > 0:
        	# Scales image to a square of default size 250x250
        	scaled_dim = (self.im_size.astype(np.int32),
        				  self.im_size.astype(np.int32))
        	img = img.resize(scaled_dim, PIL.Image.BILINEAR)

        label = 1 # TODO: read in a class label for each image

        img = np.array(img, dtype=np.uint8)
        im_out = torch.from_numpy(im_out).float()
        im_out = im_out.permute(2,0,1) # C x H x W

        return im_out, label

class LFWDataset(data.Dataset):
    '''
        Dataset subclass for loading LFW images in PyTorch.
        This returns multiple images in a batch.
    '''

    def __init__(self, path_list, issame_list, transforms, split = 'test'):
        '''
            Parameters
            ----------
            path_list    -   List of full path-names to LFW images
        '''
        self.files = collections.defaultdict(list)
        self.split = split
        self.files[split] =  path_list
        self.pair_label = issame_list
        self.transforms = transforms

    def __len__(self):
        return len(self.files[self.split])

    def __getitem__(self, index):
        img_file = self.files[self.split][index]
        img = PIL.Image.open(img_file)
        if DEBUG:
            print(img_file)
        im_out = self.transforms(img)
        return im_out

class IJBADataset(data.Dataset):
    '''
        Dataset subclass for loading IJB-A images in PyTorch.
        This returns multiple images in a batch.
        Path_list -- full paths to cropped images saved as <sighting_id>.jpg
    '''
    def __init__(self, path_list, transforms, split=1):
        '''
            Parameters
            ----------
            path_list    -   List of full path-names to IJB-A images of one split
        '''
        self.files = collections.defaultdict(list)
        self.split = split
        self.files[split] =  path_list
        self.transforms = transforms

    def __len__(self):
        return len(self.files[self.split])

    def __getitem__(self, index):
        img_file = self.files[self.split][index]
        img = PIL.Image.open(img_file)
        if not img.mode == 'RGB':
            img = img.convert('RGB')
        if DEBUG:
            print(img_file)
        im_out = self.transforms(img)
        return im_out


"""

.. _multi-taper-coh:

================================
Multi-taper coherence estimation
================================

Coherence estimation can be done using windowed-spectra. This is the method
used in the example :ref:`resting-state`. In addition, multi-taper spectral
estimation can be used in order to calculate coherence and also confidence
intervals for the coherence values that result (see :ref:`multi-taper-psd`)

The data analyzed here is an fMRI data-set contributed by <NAME>. The
data is taken from a single subject in a"resting-state" scan, in which subjects
are fixating on a cross and maintaining alert wakefulness, but not performing
any other behavioral task.

We start by importing modules/functions we will use in this example and define
variables which will be used as the sampling interval of the TimeSeries
objects and as upper and lower bounds on the frequency range analyzed:

"""

import os

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import csv2rec
import scipy.stats.distributions as dist
from scipy import fftpack

import nitime
from nitime.timeseries import TimeSeries
from nitime import utils
import nitime.algorithms as alg
import nitime.viz
from nitime.viz import drawmatrix_channels
from nitime.analysis import CoherenceAnalyzer, MTCoherenceAnalyzer

TR = 1.89
f_ub = 0.15
f_lb = 0.02

"""

We read in the data into a recarray from a csv file:

"""

data_path = os.path.join(nitime.__path__[0], 'data')

data_rec = csv2rec(os.path.join(data_path, 'fmri_timeseries.csv'))

"""

The first line in the file contains the names of the different brain regions
(or ROI = regions of interest) from which the time-series were derived. We
extract the data into a regular array, while keeping the names to be used later:

"""

roi_names = np.array(data_rec.dtype.names)
nseq = len(roi_names)
n_samples = data_rec.shape[0]
data = np.zeros((nseq, n_samples))

for n_idx, roi in enumerate(roi_names):
    data[n_idx] = data_rec[roi]

"""

We normalize the data in each of the ROIs to be in units of % change:

"""

pdata = utils.percent_change(data)

"""

We start by performing the detailed analysis, but note that a significant
short-cut is presented below, so if you just want to know how to do this
(without needing to understand the details), skip on down.

We start by defining how many tapers will be used and calculate the values of
the tapers and the associated eigenvalues of each taper:

"""

NW = 4
K = 2 * NW - 1

tapers, eigs = alg.dpss_windows(n_samples, NW, K)

"""

We multiply the data by the tapers and derive the fourier transform and the
magnitude of the squared spectra (the power) for each tapered time-series:

"""

tdata = tapers[None, :, :] * pdata[:, None, :]
tspectra = fftpack.fft(tdata)
## mag_sqr_spectra = np.abs(tspectra)
## np.power(mag_sqr_spectra, 2, mag_sqr_spectra)

"""

Coherence for real sequences is symmetric, so we calculate this for only half
the spectrum (the other half is equal):

"""

L = n_samples // 2 + 1
sides = 'onesided'

"""

We estimate adaptive weighting of the tapers, based on the data (see
:ref:`multi-taper-psd` for an explanation and references):

"""

w = np.empty((nseq, K, L))
for i in range(nseq):
    w[i], _ = utils.adaptive_weights(tspectra[i], eigs, sides=sides)

"""

We proceed to calculate the coherence. We initialize empty data containers:

"""

csd_mat = np.zeros((nseq, nseq, L), 'D')
psd_mat = np.zeros((2, nseq, nseq, L), 'd')
coh_mat = np.zeros((nseq, nseq, L), 'd')
coh_var = np.zeros_like(coh_mat)

"""

Looping over the ROIs:

"""

for i in range(nseq):
    for j in range(i):

        """

        We calculate the multi-tapered cross spectrum between each two
        time-series:

        """

        sxy = alg.mtm_cross_spectrum(
           tspectra[i], tspectra[j], (w[i], w[j]), sides='onesided'
         )

        """

        And the individual PSD for each:

        """

        sxx = alg.mtm_cross_spectrum(
           tspectra[i], tspectra[i], w[i], sides='onesided'
           )
        syy = alg.mtm_cross_spectrum(
           tspectra[j], tspectra[j], w[j], sides='onesided'
           )

        psd_mat[0, i, j] = sxx
        psd_mat[1, i, j] = syy

        """

        Coherence is : $Coh_{xy}(\lambda) = \frac{|{f_{xy}(\lambda)}|^2}{f_{xx}(\lambda) \cdot f_{yy}(\lambda)}$

        """

        coh_mat[i, j] = np.abs(sxy) ** 2
        coh_mat[i, j] /= (sxx * syy)
        csd_mat[i, j] = sxy

        """

        The variance from the different samples is calculated using a jack-knife
        approach:

        """

        if i != j:
            coh_var[i, j] = utils.jackknifed_coh_variance(
               tspectra[i], tspectra[j], eigs, adaptive=True,
               )

"""

This measure is normalized, based on the number of tapers:

"""

coh_mat_xform = utils.normalize_coherence(coh_mat, 2 * K - 2)

"""

We calculate 95% confidence intervals based on the jack-knife variance
calculation:

"""

t025_limit = coh_mat_xform + dist.t.ppf(.025, K - 1) * np.sqrt(coh_var)
t975_limit = coh_mat_xform + dist.t.ppf(.975, K - 1) * np.sqrt(coh_var)

utils.normal_coherence_to_unit(t025_limit, 2 * K - 2, t025_limit)
utils.normal_coherence_to_unit(t975_limit, 2 * K - 2, t975_limit)

if L < n_samples:
    freqs = np.linspace(0, 1 / (2 * TR), L)
else:
    freqs = np.linspace(0, 1 / TR, L, endpoint=False)

"""

We look only at frequencies between 0.02 and 0.15 (the physiologically
relevant band, see http://imaging.mrc-cbu.cam.ac.uk/imaging/DesignEfficiency:

"""

freq_idx = np.where((freqs > f_lb) * (freqs < f_ub))[0]

"""

We extract the coherence and average over all these frequency bands:

"""

coh = np.mean(coh_mat[:, :, freq_idx], -1)  # Averaging on the last dimension

"""

The next line calls the visualization routine which displays the data

"""

fig01 = drawmatrix_channels(coh,
                            roi_names,
                            size=[10., 10.],
                            color_anchor=0,
                            title='MTM Coherence')

"""

.. image:: fig/multi_taper_coh_01.png

Next we perform the same analysis, using the nitime object oriented interface.

We start by initializing a TimeSeries object with this data and with the
sampling_interval provided above. We set the metadata 'roi' field with the ROI
names.

"""

T = TimeSeries(pdata, sampling_interval=TR)
T.metadata['roi'] = roi_names

"""

We initialize an MTCoherenceAnalyzer object with the TimeSeries object

"""

C2 = MTCoherenceAnalyzer(T)

"""

The relevant indices in the Analyzer object are derived:

"""

freq_idx = np.where((C2.frequencies > 0.02) * (C2.frequencies < 0.15))[0]

"""
The call to C2.coherence triggers the computation and this is averaged over the
frequency range of interest in the same line and then displayed:

"""

coh = np.mean(C2.coherence[:, :, freq_idx], -1)  # Averaging on the last dimension
fig02 = drawmatrix_channels(coh,
                            roi_names,
                            size=[10., 10.],
                            color_anchor=0,
                            title='MTCoherenceAnalyzer')

"""

.. image:: fig/multi_taper_coh_02.png

For comparison, we also perform the analysis using the standard
CoherenceAnalyzer object, which does the analysis using Welch's windowed
periodogram, instead of the multi-taper spectral estimation method (see
:ref:`resting_state` for a more thorough analysis of this data using this
method):

"""

C3 = CoherenceAnalyzer(T)

freq_idx = np.where((C3.frequencies > f_lb) * (C3.frequencies < f_ub))[0]

#Extract the coherence and average across these frequency bands:
coh = np.mean(C3.coherence[:, :, freq_idx], -1)  # Averaging on the last dimension
fig03 = drawmatrix_channels(coh,
                            roi_names,
                            size=[10., 10.],
                            color_anchor=0,
                            title='CoherenceAnalyzer')

"""

.. image:: fig/multi_taper_coh_03.png

plt.show() is called in order to display the figures:

"""

plt.show()


<reponame>Mingoose/WriteNoise<filename>app.py
from flask import Flask, render_template, url_for, request, send_file
import scipy.io.wavfile
import math
from PIL import Image

import numpy as np
import matplotlib.pyplot as plt
import os
import sys
app = Flask(__name__)
os.makedirs(os.path.join(app.instance_path, 'htmlfi'), exist_ok=True)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/generic', methods = ['POST', 'GET'])
def generic():
    if request.method == 'POST':
        uploaded_file = request.files['file']
        if uploaded_file.filename != '':
            uploaded_file.save('input.wav')
            wavtoPNG('input.wav')
        return render_template('generic.html')
    return render_template('generic.html')

def wavtoPNG(wavfile):
    def sigmoid(x): # condense variables down to 0<x<1
        if x < -700:
            x = -700

        ans = 1/(1 + math.e ** (-x))

        return ans

    def soundToColor(x):
        a = x//256
        b = x%256
        a = min(a,255)
        b = min(b,255)
        a = int(a)
        b = int(b)
        return a,b

    def stupidBinary(x):
        print(type(x),file=sys.stderr)
        r = "0b"
        g = "0b"
        b = "0b"
        a = 255
        r += x[-9] + x[-6] + x[-12] + x[-15] + x[-1] + "0"*3
        g += x[-8] + x[-5] + x[-11] + x[-14] + x[-3] + "0"*3
        b += x[-7] + x[-4] + x[-10] + x[-13] + x[-2] + "0"*3
        if x[0] == "-":
            a = 128
        r = int(r,2)
        g = int(g,2)
        b = int(b,2)
        return(r,g,b,a)

    out = scipy.io.wavfile.read(wavfile, mmap=False) # read file
    a,b = out
    print(out)
    #plt.plot(b, label="Sound Data")
    #plt.legend()
    #plt.xlabel("Time [s]")
    #plt.ylabel("Amplitude")
    #plt.show()
    list1 = b.tolist()
    list2 = []

    if isinstance(list1[0],list): # if 2d array average elements in subarrays to make 1d array
        for i in range(len(list1)):
            total = 0
            for j in range(len(list1[i])):
                total += list1[i][j]
            list2.append(total/len(list1[i]))

    if isinstance(list1[0],list):
        list1 = list2

    for i in range(len(list1)):#turn list into binary
        list1[i] = bin(int(list1[i]))
        indB = list1[i].index("b") + 1
        binary = list1[i][indB:]
        binary = binary.zfill(16)
        list1[i] = list1[i][:indB] + binary

#for i in range(len(list1)):
#    list1[i] = int(255*sigmoid(list1[i])) # compressing array down to 0<x255

#for i in range(len(list1)):
 #   list1[i] += 32768

    img = Image.new('RGBA',(300,300)) # image creation -- 300,300 is arbitrary
    pixels = img.load()
    count = 0

    for i in range(img.size[0]):
        for j in range(img.size[1]):
            (r,g,b,a) = stupidBinary(list1[count])
            pixels[i,j] = (r,g,b,a) # setting rgb values equal to values from list1
            count += 1

    print("done")
    img.show()
    img.save("output.png")
    # send_file('output.png', as_attachment=True, attachment_filename='output_photo.png')
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True)


<gh_stars>1-10
"""

https://en.wikipedia.org/wiki/Orthogonal_coordinates
https://en.wikipedia.org/wiki/Covariance_and_contravariance_of_vectors
https://en.wikipedia.org/wiki/Log-polar_coordinates
https://en.wikipedia.org/wiki/Parabolic_coordinates
https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant
https://archive.lib.msu.edu/crcmath/math/math/p/p055.htm
https://en.wikiversity.org/wiki/PlanetPhysics/Transformation_Between_Cartesian_Coordinates_and_Polar_Coordinates
https://en.wikipedia.org/wiki/Elliptic_coordinate_system
https://en.wikipedia.org/wiki/Bipolar_coordinates
https://www.researchgate.net/publication/309803947_A_Note_on_Elliptic_Coordinates

"""

from sympy import *
from sympy.abc import *

"""

The matrix for the coordinate transform are not unique, we can parameterize it in some other way.
Depending on the parameterization, the determinants are different even though the matrix transform gives the same formula at the end.
If we want consistency, we like the transformation to and from to preserve the determinant, though the determinant can be 0 for the most part,
since in a different coordinate system orientation/volumes are usually not preserved.

These coordinate transformation from one coordinate to another are not constant values, so writing it as a matrix doesn't have advantages.
If the values were constant we can think of them as a transformation in that coordinate system rather than going to a different coordinate system.

Cartesian <-> Polar
r = sqrt(x^2 + y^2)
t = atan2(y, x)

x = r*cos(t)
y = r*sin(t)

Cartesian <-> Log Polar
p = log(sqrt(x^2 + y^2))
t = atan2(y, x)

x = e^p*cos(t)
y = e^p*sin(t)

Cartesian <-> Parabolic
x = s*t
y = 0.5*(s*s - t*t)

s = sqrt(sqrt(x^2 + y^2) + y)
t = sqrt(sqrt(x^2 + y^2) - y)

Cartesian <-> Elliptic

Let c be a fixed constant
u >= 0
0 <= v <= 2*pi
x = c*cosh(u)*sinh(v)
y = c*sinh(u)*sin(v)

B = x^2 + y^2 - c^2
d = sqrt(B^2 + 4*c^2*y^2)
e = 2*c^2
p = (-B+d)/e
q = (-B-d)/e
u = 0.5*ln(1 - 2*q + 2*sqrt(q^2 - q))

v0 = asin(sqrt(p))
v = v0        [x >= 0, y >= 0]
v = pi - v0   [x < 0, y >= 0]
v = pi + v0   [x <= 0, y < 0]
v = 2*pi - v0 [x > 0, y < 0]

"""
def test_coordinate_matrix():
    print("Coordinate Transformation Matrix\n")

    l = 0.5*sqrt(x**2 + y**2)
    t = 0.5*atan(y/x)
    M = Matrix([[l/x, l/y], [t/x, t/y]])
    v = Matrix([x, y])
    print("Cartesian to Polar det(M)={}".format(M.det()))
    pprint(simplify(M))
    pprint(simplify(M@v))
    print("\n")

    M = Matrix([[cos(theta), 0], [sin(theta), 0]])
    v = Matrix([r, t])
    print("Polar to Cartesian det(M)={}.".format(M.det()))
    pprint(simplify(M))
    pprint(simplify(M@v))
    print("\n")

    l = log(sqrt(x**2 + y**2))/2
    M = Matrix([[l/x, l/y], [t/x, t/y]])
    v = Matrix([x, y])
    print("Cartesian to Log Polar det(M)={}".format(M.det()))
    pprint(simplify(M))
    pprint(simplify(M@v))
    print("\n")

    M = Matrix([[exp(phi)*cos(theta)/phi, 0], [exp(phi)*sin(theta)/phi, 0]])
    v = Matrix([phi, theta])
    print("Log Polar to Cartesian det(M)={}".format(M.det()))
    pprint(simplify(M))
    pprint(simplify(M@v))
    print("\n")

    a = 0.5*sigma*tau
    b = 0.25*(sigma**2 - tau**2)
    M = Matrix([[a/sigma, a/tau], [b/sigma, b/tau]])
    v = Matrix([sigma, tau])
    print("Parabolic to Cartesian det(M)={}".format(M.det()))
    pprint(simplify(M))
    pprint(simplify(M@v))
    print("\n")

    print("Cartesian to Parabolic det(M)={}".format(M.det()))
    a = sqrt(x**2 + y**2)
    b = 0.5*sqrt(a + y)
    c = 0.5*sqrt(a - y)
    M = Matrix([[b/x, b/y], [c/x, c/y]])
    v = Matrix([x, y])
    pprint(simplify(M))
    pprint(simplify(M@v))
    print("\n")

"""

Given a coordinate system, we want to generate a transformation matrix that transforms a covariant vector
The general matrix can be generated using the following method:

Given a coordinate system of N variables, we want to transform it into another system of N variables, hence
we will have a NxN matrix we shall call A

Aij = partial derivative of the ith variable in the second coordinate system with respect to the jth derivative in the first derivative

For example, for polar to cartesian we have two variables:
(r, theta) -> (x, y)

A11 = (x, r)
A12 = (x, theta)
A21 = (y, r)
A21 = (y, theta)

We calculate the partial derivative of those variables and fill the matrix, in general they are also not constant values but some function

If they were just constant values, the matrix transform will be just be inverse of that matrix.
This matrix is also known as the jacobian matrix and its determinant is known as the jacobian determinant.

"""

def test_contravariant_matrix():
    print("Contravariant Vector Transformation Matrix\n")

    l = sqrt(x**2 + y**2)
    M = Matrix([[x/l, y/l], [-y/l**2, x/l**2]])
    v = Matrix([x, y])
    print("Cartesian to Polar det(M)={}".format(M.det()))
    pprint(simplify(M))
    pprint(simplify(M@v))
    print("")

    print("Polar to Cartesian det(M)={}".format(M.det()))
    M = Matrix([[cos(theta), -r*sin(theta)], [sin(theta), r*cos(theta)]])
    v = Matrix([r, theta])
    pprint(simplify(M))
    pprint(simplify(M@v))
    print("")

test_coordinate_matrix()
test_contravariant_matrix()