text:
from rllab.sampler.utils import rollout
from rllab.sampler.stateful_pool import singleton_pool
from rllab.misc import ext
from rllab.misc import logger
from rllab.misc import tensor_utils
import numpy as np

def _worker_init(G, id):
    import os
    os.environ['THEANO_FLAGS'] = 'device=cpu'
    G.worker_id = id

def initialize(n_parallel):
    singleton_pool.initialize(n_parallel)
    singleton_pool.run_each(
        _worker_init, [(id,) for id in range(singleton_pool.n_parallel)])

def _worker_populate_task(G, env, policy, dynamics):
    G.env = env
    G.policy = policy
    G.dynamics = dynamics

def populate_task(env, policy, dynamics):
    logger.log("Populating workers...")
    singleton_pool.run_each(
        _worker_populate_task,
        [(env, policy, dynamics)] * singleton_pool.n_parallel
    )
    logger.log("Populated")

def _worker_set_seed(_, seed):
    ext.set_seed(seed)

def set_seed(seed):
    singleton_pool.run_each(
        _worker_set_seed,
        [(seed + i,) for i in range(singleton_pool.n_parallel)]
    )

def _worker_set_policy_params(G, params):
    G.policy.set_param_values(params)

def _worker_set_dynamics_params(G, params):
    G.dynamics.set_param_values(params)

def _worker_collect_one_path(G, max_path_length, itr, normalize_reward,
                             reward_mean, reward_std, kl_batch_size, n_itr_update, use_replay_pool,
                             obs_mean, obs_std, act_mean, act_std, second_order_update):
    # Path rollout.
    path = rollout(G.env, G.policy, max_path_length)

    # Computing intrinsic rewards.
    # ----------------------------
    # Save original reward.
    path['rewards_orig'] = np.array(path['rewards'])

    if itr > 0:
        # Iterate over all paths and compute intrinsic reward by updating the
        # model on each observation, calculating the KL divergence of the new
        # params to the old ones, and undoing this operation.
        obs = (path['observations'] - obs_mean) / (obs_std + 1e-8)
        act = (path['actions'] - act_mean) / (act_std + 1e-8)
        rew = path['rewards']
        # inputs = (o,a), target = o'
        obs_nxt = np.vstack([obs[1:]])
        _inputs = np.hstack([obs[:-1], act[:-1]])
        _targets = obs_nxt
        # KL vector assumes same shape as reward.
        kl = np.zeros(rew.shape)
        for j in range(int(np.ceil(obs.shape[0] / float(kl_batch_size)))):

            # Save old params for every update.
            G.dynamics.save_old_params()

            start = j * kl_batch_size
            end = np.minimum(
                (j + 1) * kl_batch_size, obs.shape[0] - 1)

            if second_order_update:
                # We do a line search over the best step sizes using
                # step_size * invH * grad
                #                 best_loss_value = np.inf
                for step_size in [0.01]:
                    G.dynamics.save_old_params()
                    loss_value = G.dynamics.train_update_fn(
                         _inputs[start:end], _targets[start:end], step_size)
                    kl_div = np.clip(loss_value, 0, 1000)
                    # If using replay pool, undo updates.
                    if use_replay_pool:
                        G.dynamics.reset_to_old_params()
            else:
                # Update model weights based on current minibatch.
                for _ in range(n_itr_update):
                    G.dynamics.train_update_fn(
                        _inputs[start:end], _targets[start:end])
                # Calculate current minibatch KL.
                kl_div = np.clip(
                    float(G.dynamics.f_kl_div_closed_form()), 0, 1000)

            for k in range(start, end):
                kl[k] = kl_div
            # If using replay pool, undo updates.
            if use_replay_pool:
                G.dynamics.reset_to_old_params()

        # Last element in KL vector needs to be replaced by second last one
        # because the actual last observation has no next observation.
        kl[-1] = kl[-2]

        # Stuff it in path
        path['KL'] = kl
        # ----------------------------

    return path, len(path["rewards"])

def sample_paths(
        policy_params,
        dynamics_params,
        max_samples,
        max_path_length=np.inf,
        itr=None,
        normalize_reward=None,
        reward_mean=None,
        reward_std=None,
        kl_batch_size=None,
        n_itr_update=None,
        use_replay_pool=None,
        obs_mean=None,
        obs_std=None,
        act_mean=None,
        act_std=None,
        second_order_update=None
):
    """
    :param policy_params: parameters for the policy. This will be updated on each worker process
    :param max_samples: desired maximum number of samples to be collected. The actual number of collected samples
    might be greater since all trajectories will be rolled out either until termination or until max_path_length is
    reached
    :param max_path_length: horizon / maximum length of a single trajectory
    :return: a list of collected paths
    """
    singleton_pool.run_each(
        _worker_set_policy_params,
        [(policy_params,)] * singleton_pool.n_parallel
    )

    # Set dynamics params.
    # --------------------
    singleton_pool.run_each(
        _worker_set_dynamics_params,
        [(dynamics_params,)] * singleton_pool.n_parallel
    )
    # --------------------
    return singleton_pool.run_collect(
        _worker_collect_one_path,
        threshold=max_samples,
        args=(max_path_length, itr, normalize_reward, reward_mean,
              reward_std, kl_batch_size, n_itr_update, use_replay_pool, obs_mean, obs_std, act_mean, act_std, second_order_update),
        show_prog_bar=True
    )

def truncate_paths(paths, max_samples):
    """
    Truncate the list of paths so that the total number of samples is exactly equal to max_samples. This is done by
    removing extra paths at the end of the list, and make the last path shorter if necessary
    :param paths: a list of paths
    :param max_samples: the absolute maximum number of samples
    :return: a list of paths, truncated so that the number of samples adds up to max-samples
    """
    # chop samples collected by extra paths
    # make a copy
    paths = list(paths)
    total_n_samples = sum(len(path["rewards"]) for path in paths)
    while len(paths) > 0 and total_n_samples - len(paths[-1]["rewards"]) >= max_samples:
        total_n_samples -= len(paths.pop(-1)["rewards"])
    if len(paths) > 0:
        last_path = paths.pop(-1)
        truncated_last_path = dict()
        truncated_len = len(
            last_path["rewards"]) - (total_n_samples - max_samples)
        for k, v in last_path.iteritems():
            if k in ["observations", "actions", "rewards"]:
                truncated_last_path[k] = tensor_utils.truncate_tensor_list(
                    v, truncated_len)
            elif k in ["env_infos", "agent_infos"]:
                truncated_last_path[k] = tensor_utils.truncate_tensor_dict(
                    v, truncated_len)
            else:
                raise NotImplementedError
        paths.append(truncated_last_path)
    return paths


text:
"""
Script to test agogic accentuation
"""

from __future__ import division

import numpy as np
from time import time
from scipy.signal import hilbert
import matplotlib.pyplot as plt

from daspy import Signal
from daspy.audioio import play_signal
from daspy.processing import onset_detection_signal
from pyrhythm import Pattern

from rhythm import rhythm_model

import matplotlib.pyplot as plt
from pygrfnn.vis import plot_connections
from pygrfnn.vis import tf_detail
from pygrfnn.vis import GrFNN_RT_plot

N = 4       # numerator
D = 4       # denominator
d = D/N     # even event period
# c = 0.025     # percentage deviation
c = 0.0     # percentage deviation

d_star = (1+c)*d  # duration of (after) down beat
d_prime = (D - d_star) / (N-1)  # duration of other events

agogic_pattern = np.array([0, d_star]+[d_prime]*(N-1))

print agogic_pattern
print np.cumsum(agogic_pattern)

p = Pattern(agogic_pattern, numerator=N, denominator=D)

sr = 22050
x, _ = p.as_signal(120, reps=10, sr=sr)
# t = np.arange(len(x))/sr
# plt.plot(t, x)
# plt.show()

x = Signal(x, sr=sr)
x = 0.99*x.normalize()

play_pattern = False
if play_pattern:
    play_signal(x)

s = onset_detection_signal(x)
rms = np.sqrt(np.sum(s**2)/len(s))
s *= 0.06/rms
s = Signal(hilbert(s), sr=s.sr)
t = s.time_vector()
dt = 1/s.sr

model = rhythm_model()
layer1, layer2 = model.layers()

# Simulation
RT_display = True
if RT_display:
    plt.ion()
    plt.plot(t, s);
    plt.title('Stimulus')
    GrFNN_RT_plot(layer1, update_interval=2.0/s.sr, title='First Layer')
    GrFNN_RT_plot(layer2, update_interval=2.0/s.sr, title='Second Layer')

tic = time()
model.run(s, t, dt)
print "Run time: {:0.1f} seconds".format(time() - tic)

TF = layer2.Z
r = np.sum(TF, 0)
rms = np.sqrt(np.sum(r*np.conj(r))/len(r))
r *= 0.06/rms
plt.figure()
plt.plot(t, np.real(r))
plt.plot(t, np.real(s))

ext: py

max_stars_repo_name: jorgehatccrma/pygrfnn

max_issues_repo_name: jorgehatccrma/pygrfnn

max_forks_repo_name: jorgehatccrma/pygrfnn

reason: import numpy,from scipy


text:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 27 12:01:59 2019

Program for field line tracing using the IGRF. Here, the starting point is
chosen to be at the Earth's surface and the field line is traced until it
returns to the starting radius (set to 6371.2km).

Note: conjugate point calculations can be carried out at:
https://omniweb.gsfc.nasa.gov/vitmo/cgm.html

@author: djk
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sys
sys.path.append('C:\\PP\\Science\\python\\dk_libs')
import sha_lib as sha

d2r = np.deg2rad
r2d = np. rad2deg
fcalc = lambda x: np.sqrt(np.dot(x,x))

IGRF12 = 'C:\\PP\\Science\\python\\data\\igrf12coeffs.txt'
igrf12 = pd.read_csv(IGRF12, delim_whitespace=True, header=3)
gh2015 = np.append(0., igrf12['2015.0'])
NMAX   = 13

# Starting position
r0    = 6371.2
th0   = 30
ph0   = 30
step_size = 10

# Initialise variables at starting point
thrd    = d2r(th0)
phrd    = d2r(ph0)
track   = [(r0, thrd, phrd)]  # Store coordinates of points on the field line
bxyz    = sha.shm_calculator(gh2015, NMAX, r0, th0, ph0, 'Geocentric')
eff     = fcalc(bxyz)
lamb    = step_size/eff
rad     = r0

# Allow a maximum number of steps in the iteration for the field line to
# return to the Earth's surface
maxstep = 10000
newrad  = r0+0.001

step    = 0
while step <= maxstep and newrad >= r0:
    rad, th, ph  =  track[step]
    lx,  ly, lz  =  tuple(el*lamb for el in bxyz)
    newrad = rad+lz
    newth  = th+lx/rad
    newph  = ph-ly/(rad*np.sin(th))
    track += [(newrad, newth, newph)]
    bxyz   =  sha.shm_calculator(gh2015, NMAX, newrad, r2d(newth), \
                                 r2d(newph),'Geocentric')
    lamb   =  step_size/fcalc(bxyz)
    step  += 1

rads = np.array([r[0] for r in track])
ths  = np.array([t[1] for t in track])
phs  = np.array([p[2] for p in track])

xs   = rads*np.sin(ths)
ys   = rads*np.cos(ths)

# Earth's surface co-ordinates
the = np.deg2rad(np.linspace(0,180,200))
xe = r0*np.sin(the)
ye = r0*np.cos(the)

fig, (ax0, ax1, ax2) = plt.subplots(3,1)
plt.rcParams['figure.figsize'] = [10, 12]
#ax[0].set_aspect('equal')
#ax[0].plot(xs, ys)
#ax[0].fill(xe, ye, color='lightgrey')
#plt.ax0.gca().invert_yaxis()
ax0.plot(rads,r2d(ths))
ax0.set_ylim(ax0.get_ylim()[::-1])
ax0.set_xlabel('Radial distance (km)', fontsize=14)
ax0.set_ylabel('Colatitude (degrees)', fontsize=14)
ax0.set_title('Field line tracing using the IGRF', fontsize=20)
ax1.plot(rads,r2d(phs), color='red')
ax1.set_xlabel('Radial distance (km)', fontsize=14)
ax1.set_ylabel('Longitude (degrees)', fontsize=14)
ax2.plot(r2d(phs), r2d(ths), color='green')
ax2.set_xlabel('Longitude (degrees)', fontsize=14)
ax2.set_ylabel('Colatitude (degrees)', fontsize=14)
ax2.set_ylim(ax2.get_ylim()[::-1])
plt.tight_layout()

reason: import numpy


text:
#!/usr/bin/env python
from flask import Flask, render_template, Response
from camera import Camera
import cv2
import numpy as np
import base64

app = Flask(__name__)

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

def gen(Camera):
    cap = cv2.VideoCapture(0)
    width = cap.get(3)  # float
    height = cap.get(4) # float
    while True:
        #frame = Camera.get_frame()
        ret,frame = cap.read()
        #imwrite("temp.jpg",frame

       # frame = np.asarray(frame, dtype=np.uint8).reshape(height, width, 4)
        #cv2.imwrite("temp.jpg",frame)
        #frame=cv2.imread("temp.jpg")

        ret,frame=cv2.imencode('.jpg', frame)
        frame = frame.tostring()#base64.b64encode(frame)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/veena')
def veena():
	return 'its working'

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8000 ,debug=True)

max_forks_repo_name: vchidurala/Flask-live-video-streaming


text:
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""The fsl module provides classes for interfacing with the `FSL
<http://www.fmrib.ox.ac.uk/fsl/index.html>`_ command line tools.  This
was written to work with FSL version 5.0.4.

    Change directory to provide relative paths for doctests
    >>> import os
    >>> filepath = os.path.dirname(os.path.realpath(__file__))
    >>> datadir = os.path.realpath(os.path.join(filepath,
    ...                            '../../testing/data'))
    >>> os.chdir(datadir)
"""
from __future__ import print_function, division, unicode_literals, \
    absolute_import
from builtins import str

import os
import numpy as np
import nibabel as nb
import warnings

from ...utils.filemanip import split_filename
from ...utils import NUMPY_MMAP

from ..base import (traits, TraitedSpec, InputMultiPath, File, isdefined)
from .base import FSLCommand, FSLCommandInputSpec, Info

class PrepareFieldmapInputSpec(FSLCommandInputSpec):
    scanner = traits.String(
        'SIEMENS',
        argstr='%s',
        position=1,
        desc='must be SIEMENS',
        usedefault=True)
    in_phase = File(
        exists=True,
        argstr='%s',
        position=2,
        mandatory=True,
        desc=('Phase difference map, in SIEMENS format range from '
              '0-4096 or 0-8192)'))
    in_magnitude = File(
        exists=True,
        argstr='%s',
        position=3,
        mandatory=True,
        desc='Magnitude difference map, brain extracted')
    delta_TE = traits.Float(
        2.46,
        usedefault=True,
        mandatory=True,
        argstr='%f',
        position=-2,
        desc=('echo time difference of the '
              'fieldmap sequence in ms. (usually 2.46ms in'
              ' Siemens)'))
    nocheck = traits.Bool(
        False,
        position=-1,
        argstr='--nocheck',
        usedefault=True,
        desc=('do not perform sanity checks for image '
              'size/range/dimensions'))
    out_fieldmap = File(
        argstr='%s', position=4, desc='output name for prepared fieldmap')

class PrepareFieldmapOutputSpec(TraitedSpec):
    out_fieldmap = File(exists=True, desc='output name for prepared fieldmap')

class PrepareFieldmap(FSLCommand):
    """
    Interface for the fsl_prepare_fieldmap script (FSL 5.0)

    Prepares a fieldmap suitable for FEAT from SIEMENS data - saves output in
    rad/s format (e.g. ```fsl_prepare_fieldmap SIEMENS
    images_3_gre_field_mapping images_4_gre_field_mapping fmap_rads 2.65```).

    Examples
    --------

    >>> from nipype.interfaces.fsl import PrepareFieldmap
    >>> prepare = PrepareFieldmap()
    >>> prepare.inputs.in_phase = "phase.nii"
    >>> prepare.inputs.in_magnitude = "magnitude.nii"
    >>> prepare.inputs.output_type = "NIFTI_GZ"
    >>> prepare.cmdline # doctest: +ELLIPSIS
    'fsl_prepare_fieldmap SIEMENS phase.nii magnitude.nii \
.../phase_fslprepared.nii.gz 2.460000'
    >>> res = prepare.run() # doctest: +SKIP

    """
    _cmd = 'fsl_prepare_fieldmap'
    input_spec = PrepareFieldmapInputSpec
    output_spec = PrepareFieldmapOutputSpec

    def _parse_inputs(self, skip=None):
        if skip is None:
            skip = []

        if not isdefined(self.inputs.out_fieldmap):
            self.inputs.out_fieldmap = self._gen_fname(
                self.inputs.in_phase, suffix='_fslprepared')

        if not isdefined(self.inputs.nocheck) or not self.inputs.nocheck:
            skip += ['nocheck']

        return super(PrepareFieldmap, self)._parse_inputs(skip=skip)

    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs['out_fieldmap'] = self.inputs.out_fieldmap
        return outputs

    def _run_interface(self, runtime):
        runtime = super(PrepareFieldmap, self)._run_interface(runtime)

        if runtime.returncode == 0:
            out_file = self.inputs.out_fieldmap
            im = nb.load(out_file, mmap=NUMPY_MMAP)
            dumb_img = nb.Nifti1Image(np.zeros(im.shape), im.affine, im.header)
            out_nii = nb.funcs.concat_images((im, dumb_img))
            nb.save(out_nii, out_file)

        return runtime

class TOPUPInputSpec(FSLCommandInputSpec):
    in_file = File(
        exists=True,
        mandatory=True,
        desc='name of 4D file with images',
        argstr='--imain=%s')
    encoding_file = File(
        exists=True,
        mandatory=True,
        xor=['encoding_direction'],
        desc='name of text file with PE directions/times',
        argstr='--datain=%s')
    encoding_direction = traits.List(
        traits.Enum('y', 'x', 'z', 'x-', 'y-', 'z-'),
        mandatory=True,
        xor=['encoding_file'],
        requires=['readout_times'],
        argstr='--datain=%s',
        desc=('encoding direction for automatic '
              'generation of encoding_file'))
    readout_times = InputMultiPath(
        traits.Float,
        requires=['encoding_direction'],
        xor=['encoding_file'],
        mandatory=True,
        desc=('readout times (dwell times by # '
              'phase-encode steps minus 1)'))
    out_base = File(
        desc=('base-name of output files (spline '
              'coefficients (Hz) and movement parameters)'),
        name_source=['in_file'],
        name_template='%s_base',
        argstr='--out=%s',
        hash_files=False)
    out_field = File(
        argstr='--fout=%s',
        hash_files=False,
        name_source=['in_file'],
        name_template='%s_field',
        desc='name of image file with field (Hz)')
    out_warp_prefix = traits.Str(
        "warpfield",
        argstr='--dfout=%s',
        hash_files=False,
        desc='prefix for the warpfield images (in mm)',
        usedefault=True)
    out_mat_prefix = traits.Str(
        "xfm",
        argstr='--rbmout=%s',
        hash_files=False,
        desc='prefix for the realignment matrices',
        usedefault=True)
    out_jac_prefix = traits.Str(
        "jac",
        argstr='--jacout=%s',
        hash_files=False,
        desc='prefix for the warpfield images',
        usedefault=True)
    out_corrected = File(
        argstr='--iout=%s',
        hash_files=False,
        name_source=['in_file'],
        name_template='%s_corrected',
        desc='name of 4D image file with unwarped images')
    out_logfile = File(
        argstr='--logout=%s',
        desc='name of log-file',
        name_source=['in_file'],
        name_template='%s_topup.log',
        keep_extension=True,
        hash_files=False)

    # TODO: the following traits admit values separated by commas, one value
    # per registration level inside topup.
    warp_res = traits.Float(
        10.0,
        argstr='--warpres=%f',
        desc=('(approximate) resolution (in mm) of warp '
              'basis for the different sub-sampling levels'
              '.'))
    subsamp = traits.Int(1, argstr='--subsamp=%d', desc='sub-sampling scheme')
    fwhm = traits.Float(
        8.0,
        argstr='--fwhm=%f',
        desc='FWHM (in mm) of gaussian smoothing kernel')
    config = traits.String(
        'b02b0.cnf',
        argstr='--config=%s',
        usedefault=True,
        desc=('Name of config file specifying command line '
              'arguments'))
    max_iter = traits.Int(
        5, argstr='--miter=%d', desc='max # of non-linear iterations')
    reg_lambda = traits.Float(
        1.0,
        argstr='--miter=%0.f',
        desc=('lambda weighting value of the '
              'regularisation term'))
    ssqlambda = traits.Enum(
        1,
        0,
        argstr='--ssqlambda=%d',
        desc=('Weight lambda by the current value of the '
              'ssd. If used (=1), the effective weight of '
              'regularisation term becomes higher for the '
              'initial iterations, therefore initial steps'
              ' are a little smoother than they would '
              'without weighting. This reduces the '
              'risk of finding a local minimum.'))
    regmod = traits.Enum(
        'bending_energy',
        'membrane_energy',
        argstr='--regmod=%s',
        desc=('Regularisation term implementation. Defaults '
              'to bending_energy. Note that the two functions'
              ' have vastly different scales. The membrane '
              'energy is based on the first derivatives and '
              'the bending energy on the second derivatives. '
              'The second derivatives will typically be much '
              'smaller than the first derivatives, so input '
              'lambda will have to be larger for '
              'bending_energy to yield approximately the same'
              ' level of regularisation.'))
    estmov = traits.Enum(
        1, 0, argstr='--estmov=%d', desc='estimate movements if set')
    minmet = traits.Enum(
        0,
        1,
        argstr='--minmet=%d',
        desc=('Minimisation method 0=Levenberg-Marquardt, '
              '1=Scaled Conjugate Gradient'))
    splineorder = traits.Int(
        3,
        argstr='--splineorder=%d',
        desc=('order of spline, 2->Qadratic spline, '
              '3->Cubic spline'))
    numprec = traits.Enum(
        'double',
        'float',
        argstr='--numprec=%s',
        desc=('Precision for representing Hessian, double '
              'or float.'))
    interp = traits.Enum(
        'spline',
        'linear',
        argstr='--interp=%s',
        desc='Image interpolation model, linear or spline.')
    scale = traits.Enum(
        0,
        1,
        argstr='--scale=%d',
        desc=('If set (=1), the images are individually scaled'
              ' to a common mean'))
    regrid = traits.Enum(
        1,
        0,
        argstr='--regrid=%d',
        desc=('If set (=1), the calculations are done in a '
              'different grid'))

class TOPUPOutputSpec(TraitedSpec):
    out_fieldcoef = File(
        exists=True, desc='file containing the field coefficients')
    out_movpar = File(exists=True, desc='movpar.txt output file')
    out_enc_file = File(desc='encoding directions file output for applytopup')
    out_field = File(desc='name of image file with field (Hz)')
    out_warps = traits.List(File(exists=True), desc='warpfield images')
    out_jacs = traits.List(File(exists=True), desc='Jacobian images')
    out_mats = traits.List(File(exists=True), desc='realignment matrices')
    out_corrected = File(desc='name of 4D image file with unwarped images')
    out_logfile = File(desc='name of log-file')

class TOPUP(FSLCommand):
    """
    Interface for FSL topup, a tool for estimating and correcting
    susceptibility induced distortions. See FSL documentation for
    `reference <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/TOPUP>`_,
    `usage examples
    <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/topup/ExampleTopupFollowedByApplytopup>`_,
    and `exemplary config files
    <https://github.com/ahheckel/FSL-scripts/blob/master/rsc/fsl/fsl4/topup/b02b0.cnf>`_.

    Examples
    --------

    >>> from nipype.interfaces.fsl import TOPUP
    >>> topup = TOPUP()
    >>> topup.inputs.in_file = "b0_b0rev.nii"
    >>> topup.inputs.encoding_file = "topup_encoding.txt"
    >>> topup.inputs.output_type = "NIFTI_GZ"
    >>> topup.cmdline # doctest: +ELLIPSIS
    'topup --config=b02b0.cnf --datain=topup_encoding.txt \
--imain=b0_b0rev.nii --out=b0_b0rev_base --iout=b0_b0rev_corrected.nii.gz \
--fout=b0_b0rev_field.nii.gz --jacout=jac --logout=b0_b0rev_topup.log \
--rbmout=xfm --dfout=warpfield'
    >>> res = topup.run() # doctest: +SKIP

    """
    _cmd = 'topup'
    input_spec = TOPUPInputSpec
    output_spec = TOPUPOutputSpec

    def _format_arg(self, name, trait_spec, value):
        if name == 'encoding_direction':
            return trait_spec.argstr % self._generate_encfile()
        if name == 'out_base':
            path, name, ext = split_filename(value)
            if path != '':
                if not os.path.exists(path):
                    raise ValueError('out_base path must exist if provided')
        return super(TOPUP, self)._format_arg(name, trait_spec, value)

    def _list_outputs(self):
        outputs = super(TOPUP, self)._list_outputs()
        del outputs['out_base']
        base_path = None
        if isdefined(self.inputs.out_base):
            base_path, base, _ = split_filename(self.inputs.out_base)
            if base_path == '':
                base_path = None
        else:
            base = split_filename(self.inputs.in_file)[1] + '_base'
        outputs['out_fieldcoef'] = self._gen_fname(
            base, suffix='_fieldcoef', cwd=base_path)
        outputs['out_movpar'] = self._gen_fname(
            base, suffix='_movpar', ext='.txt', cwd=base_path)

        n_vols = nb.load(self.inputs.in_file).shape[-1]
        ext = Info.output_type_to_ext(self.inputs.output_type)
        fmt = os.path.abspath('{prefix}_{i:02d}{ext}').format
        outputs['out_warps'] = [
            fmt(prefix=self.inputs.out_warp_prefix, i=i, ext=ext)
            for i in range(1, n_vols + 1)
        ]
        outputs['out_jacs'] = [
            fmt(prefix=self.inputs.out_jac_prefix, i=i, ext=ext)
            for i in range(1, n_vols + 1)
        ]
        outputs['out_mats'] = [
            fmt(prefix=self.inputs.out_mat_prefix, i=i, ext=".mat")
            for i in range(1, n_vols + 1)
        ]

        if isdefined(self.inputs.encoding_direction):
            outputs['out_enc_file'] = self._get_encfilename()
        return outputs

    def _get_encfilename(self):
        out_file = os.path.join(
            os.getcwd(),
            ('%s_encfile.txt' % split_filename(self.inputs.in_file)[1]))
        return out_file

    def _generate_encfile(self):
        """Generate a topup compatible encoding file based on given directions
        """
        out_file = self._get_encfilename()
        durations = self.inputs.readout_times
        if len(self.inputs.encoding_direction) != len(durations):
            if len(self.inputs.readout_times) != 1:
                raise ValueError(('Readout time must be a float or match the'
                                  'length of encoding directions'))
            durations = durations * len(self.inputs.encoding_direction)

        lines = []
        for idx, encdir in enumerate(self.inputs.encoding_direction):
            direction = 1.0
            if encdir.endswith('-'):
                direction = -1.0
            line = [
                float(val[0] == encdir[0]) * direction
                for val in ['x', 'y', 'z']
            ] + [durations[idx]]
            lines.append(line)
        np.savetxt(out_file, np.array(lines), fmt=b'%d %d %d %.8f')
        return out_file

    def _overload_extension(self, value, name=None):
        if name == 'out_base':
            return value
        return super(TOPUP, self)._overload_extension(value, name)

class ApplyTOPUPInputSpec(FSLCommandInputSpec):
    in_files = InputMultiPath(
        File(exists=True),
        mandatory=True,
        desc='name of file with images',
        argstr='--imain=%s',
        sep=',')
    encoding_file = File(
        exists=True,
        mandatory=True,
        desc='name of text file with PE directions/times',
        argstr='--datain=%s')
    in_index = traits.List(
        traits.Int,
        argstr='--inindex=%s',
        sep=',',
        desc='comma separated list of indices corresponding to --datain')
    in_topup_fieldcoef = File(
        exists=True,
        argstr="--topup=%s",
        copyfile=False,
        requires=['in_topup_movpar'],
        desc=('topup file containing the field '
              'coefficients'))
    in_topup_movpar = File(
        exists=True,
        requires=['in_topup_fieldcoef'],
        copyfile=False,
        desc='topup movpar.txt file')
    out_corrected = File(
        desc='output (warped) image',
        name_source=['in_files'],
        name_template='%s_corrected',
        argstr='--out=%s')
    method = traits.Enum(
        'jac',
        'lsr',
        argstr='--method=%s',
        desc=('use jacobian modulation (jac) or least-squares'
              ' resampling (lsr)'))
    interp = traits.Enum(
        'trilinear',
        'spline',
        argstr='--interp=%s',
        desc='interpolation method')
    datatype = traits.Enum(
        'char',
        'short',
        'int',
        'float',
        'double',
        argstr='-d=%s',
        desc='force output data type')

class ApplyTOPUPOutputSpec(TraitedSpec):
    out_corrected = File(
        exists=True, desc=('name of 4D image file with '
                           'unwarped images'))

class ApplyTOPUP(FSLCommand):
    """
    Interface for FSL topup, a tool for estimating and correcting
    susceptibility induced distortions.
    `General reference
    <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/topup/ApplytopupUsersGuide>`_
    and `use example
    <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/topup/ExampleTopupFollowedByApplytopup>`_.

    Examples
    --------

    >>> from nipype.interfaces.fsl import ApplyTOPUP
    >>> applytopup = ApplyTOPUP()
    >>> applytopup.inputs.in_files = ["epi.nii", "epi_rev.nii"]
    >>> applytopup.inputs.encoding_file = "topup_encoding.txt"
    >>> applytopup.inputs.in_topup_fieldcoef = "topup_fieldcoef.nii.gz"
    >>> applytopup.inputs.in_topup_movpar = "topup_movpar.txt"
    >>> applytopup.inputs.output_type = "NIFTI_GZ"
    >>> applytopup.cmdline # doctest: +ELLIPSIS
    'applytopup --datain=topup_encoding.txt --imain=epi.nii,epi_rev.nii \
--inindex=1,2 --topup=topup --out=epi_corrected.nii.gz'
    >>> res = applytopup.run() # doctest: +SKIP

    """
    _cmd = 'applytopup'
    input_spec = ApplyTOPUPInputSpec
    output_spec = ApplyTOPUPOutputSpec

    def _parse_inputs(self, skip=None):
        if skip is None:
            skip = []

        # If not defined, assume index are the first N entries in the
        # parameters file, for N input images.
        if not isdefined(self.inputs.in_index):
            self.inputs.in_index = list(
                range(1,
                      len(self.inputs.in_files) + 1))

        return super(ApplyTOPUP, self)._parse_inputs(skip=skip)

    def _format_arg(self, name, spec, value):
        if name == 'in_topup_fieldcoef':
            return spec.argstr % value.split('_fieldcoef')[0]
        return super(ApplyTOPUP, self)._format_arg(name, spec, value)

class EddyInputSpec(FSLCommandInputSpec):
    in_file = File(
        exists=True,
        mandatory=True,
        argstr='--imain=%s',
        desc=('File containing all the images to estimate '
              'distortions for'))
    in_mask = File(
        exists=True,
        mandatory=True,
        argstr='--mask=%s',
        desc='Mask to indicate brain')
    in_index = File(
        exists=True,
        mandatory=True,
        argstr='--index=%s',
        desc=('File containing indices for all volumes in --imain '
              'into --acqp and --topup'))
    in_acqp = File(
        exists=True,
        mandatory=True,
        argstr='--acqp=%s',
        desc='File containing acquisition parameters')
    in_bvec = File(
        exists=True,
        mandatory=True,
        argstr='--bvecs=%s',
        desc=('File containing the b-vectors for all volumes in '
              '--imain'))
    in_bval = File(
        exists=True,
        mandatory=True,
        argstr='--bvals=%s',
        desc=('File containing the b-values for all volumes in '
              '--imain'))
    out_base = traits.Str(
        'eddy_corrected',
        argstr='--out=%s',
        usedefault=True,
        desc=('basename for output (warped) image'))
    session = File(
        exists=True,
        argstr='--session=%s',
        desc=('File containing session indices for all volumes in '
              '--imain'))
    in_topup_fieldcoef = File(
        exists=True,
        argstr="--topup=%s",
        requires=['in_topup_movpar'],
        desc=('topup file containing the field '
              'coefficients'))
    in_topup_movpar = File(
        exists=True,
        requires=['in_topup_fieldcoef'],
        desc='topup movpar.txt file')

    flm = traits.Enum(
        'linear',
        'quadratic',
        'cubic',
        argstr='--flm=%s',
        desc='First level EC model')

    slm = traits.Enum(
        'none',
        'linear',
        'quadratic',
        argstr='--slm=%s',
        desc='Second level EC model')

    fep = traits.Bool(
        False, argstr='--fep', desc='Fill empty planes in x- or y-directions')

    interp = traits.Enum(
        'spline',
        'trilinear',
        argstr='--interp=%s',
        desc='Interpolation model for estimation step')

    nvoxhp = traits.Int(
        1000,
        argstr='--nvoxhp=%s',
        desc=('# of voxels used to estimate the '
              'hyperparameters'))

    fudge_factor = traits.Float(
        10.0,
        argstr='--ff=%s',
        desc=('Fudge factor for hyperparameter '
              'error variance'))

    dont_sep_offs_move = traits.Bool(
        False,
        argstr='--dont_sep_offs_move',
        desc=('Do NOT attempt to separate '
              'field offset from subject '
              'movement'))

    dont_peas = traits.Bool(
        False,
        argstr='--dont_peas',
        desc="Do NOT perform a post-eddy alignment of "
        "shells")

    fwhm = traits.Float(
        desc=('FWHM for conditioning filter when estimating '
              'the parameters'),
        argstr='--fwhm=%s')

    niter = traits.Int(5, argstr='--niter=%s', desc='Number of iterations')

    method = traits.Enum(
        'jac',
        'lsr',
        argstr='--resamp=%s',
        desc=('Final resampling method (jacobian/least '
              'squares)'))
    repol = traits.Bool(
        False, argstr='--repol', desc='Detect and replace outlier slices')
    num_threads = traits.Int(
        1,
        usedefault=True,
        nohash=True,
        desc="Number of openmp threads to use")
    is_shelled = traits.Bool(
        False,
        argstr='--data_is_shelled',
        desc="Override internal check to ensure that "
        "date are acquired on a set of b-value "
        "shells")
    field = traits.Str(
        argstr='--field=%s',
        desc="NonTOPUP fieldmap scaled in Hz - filename has "
        "to be provided without an extension. TOPUP is "
        "strongly recommended")
    field_mat = File(
        exists=True,
        argstr='--field_mat=%s',
        desc="Matrix that specifies the relative locations of "
        "the field specified by --field and first volume "
        "in file --imain")
    use_cuda = traits.Bool(False, desc="Run eddy using cuda gpu")

class EddyOutputSpec(TraitedSpec):
    out_corrected = File(
        exists=True, desc='4D image file containing all the corrected volumes')
    out_parameter = File(
        exists=True,
        desc=('text file with parameters definining the field and'
              'movement for each scan'))
    out_rotated_bvecs = File(
        exists=True, desc='File containing rotated b-values for all volumes')
    out_movement_rms = File(
        exists=True, desc='Summary of the "total movement" in each volume')
    out_restricted_movement_rms = File(
        exists=True,
        desc=('Summary of the "total movement" in each volume '
              'disregarding translation in the PE direction'))
    out_shell_alignment_parameters = File(
        exists=True,
        desc=('File containing rigid body movement parameters '
              'between the different shells as estimated by a '
              'post-hoc mutual information based registration'))
    out_outlier_report = File(
        exists=True,
        desc=('Text-file with a plain language report on what '
              'outlier slices eddy has found'))

class Eddy(FSLCommand):
    """
    Interface for FSL eddy, a tool for estimating and correcting eddy
    currents induced distortions. `User guide
    <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/Eddy/UsersGuide>`_ and
    `more info regarding acqp file
    <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/eddy/Faq#How_do_I_know_what_to_put_into_my_--acqp_file>`_.

    Examples
    --------

    >>> from nipype.interfaces.fsl import Eddy
    >>> eddy = Eddy()
    >>> eddy.inputs.in_file = 'epi.nii'
    >>> eddy.inputs.in_mask  = 'epi_mask.nii'
    >>> eddy.inputs.in_index = 'epi_index.txt'
    >>> eddy.inputs.in_acqp  = 'epi_acqp.txt'
    >>> eddy.inputs.in_bvec  = 'bvecs.scheme'
    >>> eddy.inputs.in_bval  = 'bvals.scheme'
    >>> eddy.inputs.use_cuda = True
    >>> eddy.cmdline # doctest: +ELLIPSIS
    'eddy_cuda --acqp=epi_acqp.txt --bvals=bvals.scheme --bvecs=bvecs.scheme \
--imain=epi.nii --index=epi_index.txt --mask=epi_mask.nii \
--out=.../eddy_corrected'
    >>> eddy.inputs.use_cuda = False
    >>> eddy.cmdline # doctest: +ELLIPSIS
    'eddy_openmp --acqp=epi_acqp.txt --bvals=bvals.scheme \
--bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \
--mask=epi_mask.nii --out=.../eddy_corrected'
    >>> res = eddy.run() # doctest: +SKIP

    """
    _cmd = 'eddy_openmp'
    input_spec = EddyInputSpec
    output_spec = EddyOutputSpec

    _num_threads = 1

    def __init__(self, **inputs):
        super(Eddy, self).__init__(**inputs)
        self.inputs.on_trait_change(self._num_threads_update, 'num_threads')
        if not isdefined(self.inputs.num_threads):
            self.inputs.num_threads = self._num_threads
        else:
            self._num_threads_update()
        self.inputs.on_trait_change(self._use_cuda, 'use_cuda')
        if isdefined(self.inputs.use_cuda):
            self._use_cuda()

    def _num_threads_update(self):
        self._num_threads = self.inputs.num_threads
        if not isdefined(self.inputs.num_threads):
            if 'OMP_NUM_THREADS' in self.inputs.environ:
                del self.inputs.environ['OMP_NUM_THREADS']
        else:
            self.inputs.environ['OMP_NUM_THREADS'] = str(
                self.inputs.num_threads)

    def _use_cuda(self):
        self._cmd = 'eddy_cuda' if self.inputs.use_cuda else 'eddy_openmp'

    def _run_interface(self, runtime):
        # If 'eddy_openmp' is missing, use 'eddy'
        FSLDIR = os.getenv('FSLDIR', '')
        cmd = self._cmd
        if all((FSLDIR != '', cmd == 'eddy_openmp',
                not os.path.exists(os.path.join(FSLDIR, 'bin', cmd)))):
            self._cmd = 'eddy'
        runtime = super(Eddy, self)._run_interface(runtime)

        # Restore command to avoid side-effects
        self._cmd = cmd
        return runtime

    def _format_arg(self, name, spec, value):
        if name == 'in_topup_fieldcoef':
            return spec.argstr % value.split('_fieldcoef')[0]
        if name == 'out_base':
            return spec.argstr % os.path.abspath(value)
        return super(Eddy, self)._format_arg(name, spec, value)

    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs['out_corrected'] = os.path.abspath(
            '%s.nii.gz' % self.inputs.out_base)
        outputs['out_parameter'] = os.path.abspath(
            '%s.eddy_parameters' % self.inputs.out_base)

        # File generation might depend on the version of EDDY
        out_rotated_bvecs = os.path.abspath(
            '%s.eddy_rotated_bvecs' % self.inputs.out_base)
        out_movement_rms = os.path.abspath(
            '%s.eddy_movement_rms' % self.inputs.out_base)
        out_restricted_movement_rms = os.path.abspath(
            '%s.eddy_restricted_movement_rms' % self.inputs.out_base)
        out_shell_alignment_parameters = os.path.abspath(
            '%s.eddy_post_eddy_shell_alignment_parameters' %
            self.inputs.out_base)
        out_outlier_report = os.path.abspath(
            '%s.eddy_outlier_report' % self.inputs.out_base)

        if os.path.exists(out_rotated_bvecs):
            outputs['out_rotated_bvecs'] = out_rotated_bvecs
        if os.path.exists(out_movement_rms):
            outputs['out_movement_rms'] = out_movement_rms
        if os.path.exists(out_restricted_movement_rms):
            outputs['out_restricted_movement_rms'] = \
                out_restricted_movement_rms
        if os.path.exists(out_shell_alignment_parameters):
            outputs['out_shell_alignment_parameters'] = \
                out_shell_alignment_parameters
        if os.path.exists(out_outlier_report):
            outputs['out_outlier_report'] = out_outlier_report

        return outputs

class SigLossInputSpec(FSLCommandInputSpec):
    in_file = File(
        mandatory=True, exists=True, argstr='-i %s', desc='b0 fieldmap file')
    out_file = File(
        argstr='-s %s', desc='output signal loss estimate file', genfile=True)

    mask_file = File(exists=True, argstr='-m %s', desc='brain mask file')
    echo_time = traits.Float(argstr='--te=%f', desc='echo time in seconds')
    slice_direction = traits.Enum(
        'x', 'y', 'z', argstr='-d %s', desc='slicing direction')

class SigLossOuputSpec(TraitedSpec):
    out_file = File(exists=True, desc='signal loss estimate file')

class SigLoss(FSLCommand):
    """
    Estimates signal loss from a field map (in rad/s)

    Examples
    --------

    >>> from nipype.interfaces.fsl import SigLoss
    >>> sigloss = SigLoss()
    >>> sigloss.inputs.in_file = "phase.nii"
    >>> sigloss.inputs.echo_time = 0.03
    >>> sigloss.inputs.output_type = "NIFTI_GZ"
    >>> sigloss.cmdline # doctest: +ELLIPSIS
    'sigloss --te=0.030000 -i phase.nii -s .../phase_sigloss.nii.gz'
    >>> res = sigloss.run() # doctest: +SKIP

    """
    input_spec = SigLossInputSpec
    output_spec = SigLossOuputSpec
    _cmd = 'sigloss'

    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs['out_file'] = self.inputs.out_file
        if ((not isdefined(outputs['out_file']))
                and (isdefined(self.inputs.in_file))):
            outputs['out_file'] = self._gen_fname(
                self.inputs.in_file, suffix='_sigloss')
        return outputs

    def _gen_filename(self, name):
        if name == 'out_file':
            return self._list_outputs()['out_file']
        return None

class EpiRegInputSpec(FSLCommandInputSpec):
    epi = File(
        exists=True,
        argstr='--epi=%s',
        mandatory=True,
        position=-4,
        desc='EPI image')
    t1_head = File(
        exists=True,
        argstr='--t1=%s',
        mandatory=True,
        position=-3,
        desc='wholehead T1 image')
    t1_brain = File(
        exists=True,
        argstr='--t1brain=%s',
        mandatory=True,
        position=-2,
        desc='brain extracted T1 image')
    out_base = traits.String(
        "epi2struct",
        desc='output base name',
        argstr='--out=%s',
        position=-1,
        usedefault=True)
    fmap = File(
        exists=True, argstr='--fmap=%s', desc='fieldmap image (in rad/s)')
    fmapmag = File(
        exists=True,
        argstr='--fmapmag=%s',
        desc='fieldmap magnitude image - wholehead')
    fmapmagbrain = File(
        exists=True,
        argstr='--fmapmagbrain=%s',
        desc='fieldmap magnitude image - brain extracted')
    wmseg = File(
        exists=True,
        argstr='--wmseg=%s',
        desc='white matter segmentation of T1 image, has to be named \
                 like the t1brain and end on _wmseg')
    echospacing = traits.Float(
        argstr='--echospacing=%f',
        desc='Effective EPI echo spacing  \
                               (sometimes called dwell time) - in seconds')
    pedir = traits.Enum(
        'x',
        'y',
        'z',
        '-x',
        '-y',
        '-z',
        argstr='--pedir=%s',
        desc='phase encoding direction, dir = x/y/z/-x/-y/-z')

    weight_image = File(
        exists=True,
        argstr='--weight=%s',
        desc='weighting image (in T1 space)')
    no_fmapreg = traits.Bool(
        False,
        argstr='--nofmapreg',
        desc='do not perform registration of fmap to T1 \
                        (use if fmap already registered)')
    no_clean = traits.Bool(
        True,
        argstr='--noclean',
        usedefault=True,
        desc='do not clean up intermediate files')

class EpiRegOutputSpec(TraitedSpec):
    out_file = File(exists=True, desc='unwarped and coregistered epi input')
    out_1vol = File(
        exists=True, desc='unwarped and coregistered single volume')
    fmap2str_mat = File(
        exists=True, desc='rigid fieldmap-to-structural transform')
    fmap2epi_mat = File(exists=True, desc='rigid fieldmap-to-epi transform')
    fmap_epi = File(exists=True, desc='fieldmap in epi space')
    fmap_str = File(exists=True, desc='fieldmap in structural space')
    fmapmag_str = File(
        exists=True, desc='fieldmap magnitude image in structural space')
    epi2str_inv = File(exists=True, desc='rigid structural-to-epi transform')
    epi2str_mat = File(exists=True, desc='rigid epi-to-structural transform')
    shiftmap = File(exists=True, desc='shiftmap in epi space')
    fullwarp = File(
        exists=True,
        desc='warpfield to unwarp epi and transform into \
                    structural space')
    wmseg = File(
        exists=True, desc='white matter segmentation used in flirt bbr')
    wmedge = File(exists=True, desc='white matter edges for visualization')

class EpiReg(FSLCommand):
    """

    Runs FSL epi_reg script for simultaneous coregistration and fieldmap
    unwarping.

    Examples
    --------

    >>> from nipype.interfaces.fsl import EpiReg
    >>> epireg = EpiReg()
    >>> epireg.inputs.epi='epi.nii'
    >>> epireg.inputs.t1_head='T1.nii'
    >>> epireg.inputs.t1_brain='T1_brain.nii'
    >>> epireg.inputs.out_base='epi2struct'
    >>> epireg.inputs.fmap='fieldmap_phase_fslprepared.nii'
    >>> epireg.inputs.fmapmag='fieldmap_mag.nii'
    >>> epireg.inputs.fmapmagbrain='fieldmap_mag_brain.nii'
    >>> epireg.inputs.echospacing=0.00067
    >>> epireg.inputs.pedir='y'
    >>> epireg.cmdline # doctest: +ELLIPSIS
    'epi_reg --echospacing=0.000670 --fmap=fieldmap_phase_fslprepared.nii \
--fmapmag=fieldmap_mag.nii --fmapmagbrain=fieldmap_mag_brain.nii --noclean \
--pedir=y --epi=epi.nii --t1=T1.nii --t1brain=T1_brain.nii --out=epi2struct'
    >>> epireg.run() # doctest: +SKIP

    """
    _cmd = 'epi_reg'
    input_spec = EpiRegInputSpec
    output_spec = EpiRegOutputSpec

    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs['out_file'] = os.path.join(os.getcwd(),
                                           self.inputs.out_base + '.nii.gz')
        if (not (isdefined(self.inputs.no_fmapreg) and self.inputs.no_fmapreg)
                and isdefined(self.inputs.fmap)):
            outputs['out_1vol'] = os.path.join(
                os.getcwd(), self.inputs.out_base + '_1vol.nii.gz')
            outputs['fmap2str_mat'] = os.path.join(
                os.getcwd(), self.inputs.out_base + '_fieldmap2str.mat')
            outputs['fmap2epi_mat'] = os.path.join(
                os.getcwd(), self.inputs.out_base + '_fieldmaprads2epi.mat')
            outputs['fmap_epi'] = os.path.join(
                os.getcwd(), self.inputs.out_base + '_fieldmaprads2epi.nii.gz')
            outputs['fmap_str'] = os.path.join(
                os.getcwd(), self.inputs.out_base + '_fieldmaprads2str.nii.gz')
            outputs['fmapmag_str'] = os.path.join(
                os.getcwd(), self.inputs.out_base + '_fieldmap2str.nii.gz')
            outputs['shiftmap'] = os.path.join(
                os.getcwd(),
                self.inputs.out_base + '_fieldmaprads2epi_shift.nii.gz')
            outputs['fullwarp'] = os.path.join(
                os.getcwd(), self.inputs.out_base + '_warp.nii.gz')
            outputs['epi2str_inv'] = os.path.join(
                os.getcwd(), self.inputs.out_base + '_inv.mat')

        outputs['epi2str_mat'] = os.path.join(os.getcwd(),
                                              self.inputs.out_base + '.mat')
        outputs['wmedge'] = os.path.join(
            os.getcwd(), self.inputs.out_base + '_fast_wmedge.nii.gz')
        outputs['wmseg'] = os.path.join(
            os.getcwd(), self.inputs.out_base + '_fast_wmseg.nii.gz')

        return outputs

#######################################
# deprecated interfaces
#######################################

class EPIDeWarpInputSpec(FSLCommandInputSpec):
    mag_file = File(
        exists=True,
        desc='Magnitude file',
        argstr='--mag %s',
        position=0,
        mandatory=True)
    dph_file = File(
        exists=True,
        desc='Phase file assumed to be scaled from 0 to 4095',
        argstr='--dph %s',
        mandatory=True)
    exf_file = File(
        exists=True,
        desc='example func volume (or use epi)',
        argstr='--exf %s')
    epi_file = File(
        exists=True, desc='EPI volume to unwarp', argstr='--epi %s')
    tediff = traits.Float(
        2.46,
        usedefault=True,
        desc='difference in B0 field map TEs',
        argstr='--tediff %s')
    esp = traits.Float(
        0.58, desc='EPI echo spacing', argstr='--esp %s', usedefault=True)
    sigma = traits.Int(
        2,
        usedefault=True,
        argstr='--sigma %s',
        desc="2D spatial gaussing smoothing \
                       stdev (default = 2mm)")
    vsm = traits.String(
        genfile=True, desc='voxel shift map', argstr='--vsm %s')
    exfdw = traits.String(
        desc='dewarped example func volume', genfile=True, argstr='--exfdw %s')
    epidw = traits.String(
        desc='dewarped epi volume', genfile=False, argstr='--epidw %s')
    tmpdir = traits.String(genfile=True, desc='tmpdir', argstr='--tmpdir %s')
    nocleanup = traits.Bool(
        True, usedefault=True, desc='no cleanup', argstr='--nocleanup')
    cleanup = traits.Bool(desc='cleanup', argstr='--cleanup')

class EPIDeWarpOutputSpec(TraitedSpec):
    unwarped_file = File(desc="unwarped epi file")
    vsm_file = File(desc="voxel shift map")
    exfdw = File(desc="dewarped functional volume example")
    exf_mask = File(desc="Mask from example functional volume")

class EPIDeWarp(FSLCommand):
    """
    Wraps the unwarping script `epidewarp.fsl
    <http://surfer.nmr.mgh.harvard.edu/fswiki/epidewarp.fsl>`_.

    .. warning:: deprecated in FSL, please use
      :func:`nipype.workflows.dmri.preprocess.epi.sdc_fmb` instead.

    Examples
    --------

    >>> from nipype.interfaces.fsl import EPIDeWarp
    >>> dewarp = EPIDeWarp()
    >>> dewarp.inputs.epi_file = "functional.nii"
    >>> dewarp.inputs.mag_file = "magnitude.nii"
    >>> dewarp.inputs.dph_file = "phase.nii"
    >>> dewarp.inputs.output_type = "NIFTI_GZ"
    >>> dewarp.cmdline # doctest: +ELLIPSIS
    'epidewarp.fsl --mag magnitude.nii --dph phase.nii --epi functional.nii \
--esp 0.58 --exfdw .../exfdw.nii.gz --nocleanup --sigma 2 --tediff 2.46 \
--tmpdir .../temp --vsm .../vsm.nii.gz'
    >>> res = dewarp.run() # doctest: +SKIP

    """
    _cmd = 'epidewarp.fsl'
    input_spec = EPIDeWarpInputSpec
    output_spec = EPIDeWarpOutputSpec

    def __init__(self, **inputs):
        warnings.warn(("Deprecated: Please use "
                       "nipype.workflows.dmri.preprocess.epi.sdc_fmb instead"),
                      DeprecationWarning)
        return super(EPIDeWarp, self).__init__(**inputs)

    def _run_interface(self, runtime):
        runtime = super(EPIDeWarp, self)._run_interface(runtime)
        if runtime.stderr:
            self.raise_exception(runtime)
        return runtime

    def _gen_filename(self, name):
        if name == 'exfdw':
            if isdefined(self.inputs.exf_file):
                return self._gen_fname(self.inputs.exf_file, suffix="_exfdw")
            else:
                return self._gen_fname("exfdw")
        if name == 'epidw':
            if isdefined(self.inputs.epi_file):
                return self._gen_fname(self.inputs.epi_file, suffix="_epidw")
        if name == 'vsm':
            return self._gen_fname('vsm')
        if name == 'tmpdir':
            return os.path.join(os.getcwd(), 'temp')
        return None

    def _list_outputs(self):
        outputs = self.output_spec().get()
        if not isdefined(self.inputs.exfdw):
            outputs['exfdw'] = self._gen_filename('exfdw')
        else:
            outputs['exfdw'] = self.inputs.exfdw
        if isdefined(self.inputs.epi_file):
            if isdefined(self.inputs.epidw):
                outputs['unwarped_file'] = self.inputs.epidw
            else:
                outputs['unwarped_file'] = self._gen_filename('epidw')
        if not isdefined(self.inputs.vsm):
            outputs['vsm_file'] = self._gen_filename('vsm')
        else:
            outputs['vsm_file'] = self._gen_fname(self.inputs.vsm)
        if not isdefined(self.inputs.tmpdir):
            outputs['exf_mask'] = self._gen_fname(
                cwd=self._gen_filename('tmpdir'), basename='maskexf')
        else:
            outputs['exf_mask'] = self._gen_fname(
                cwd=self.inputs.tmpdir, basename='maskexf')
        return outputs

class EddyCorrectInputSpec(FSLCommandInputSpec):
    in_file = File(
        exists=True,
        desc='4D input file',
        argstr='%s',
        position=0,
        mandatory=True)
    out_file = File(
        desc='4D output file',
        argstr='%s',
        position=1,
        name_source=['in_file'],
        name_template='%s_edc',
        output_name='eddy_corrected')
    ref_num = traits.Int(
        0,
        argstr='%d',
        position=2,
        desc='reference number',
        mandatory=True,
        usedefault=True)

class EddyCorrectOutputSpec(TraitedSpec):
    eddy_corrected = File(
        exists=True, desc='path/name of 4D eddy corrected output file')

class EddyCorrect(FSLCommand):
    """

    .. warning:: Deprecated in FSL. Please use
      :class:`nipype.interfaces.fsl.epi.Eddy` instead

    Example
    -------

    >>> from nipype.interfaces.fsl import EddyCorrect
    >>> eddyc = EddyCorrect(in_file='diffusion.nii',
    ...                     out_file="diffusion_edc.nii", ref_num=0)
    >>> eddyc.cmdline
    'eddy_correct diffusion.nii diffusion_edc.nii 0'

    """
    _cmd = 'eddy_correct'
    input_spec = EddyCorrectInputSpec
    output_spec = EddyCorrectOutputSpec

    def __init__(self, **inputs):
        warnings.warn(("Deprecated: Please use nipype.interfaces.fsl.epi.Eddy "
                       "instead"), DeprecationWarning)
        return super(EddyCorrect, self).__init__(**inputs)

    def _run_interface(self, runtime):
        runtime = super(EddyCorrect, self)._run_interface(runtime)
        if runtime.stderr:
            self.raise_exception(runtime)
        return runtime

max_stars_repo_name: eort/nipype

max_issues_repo_name: eort/nipype

max_forks_repo_name: eort/nipype


text:
# Copyright 2019 Katsuya Shimabukuro. All rights reserved.
# Licensed under the MIT License.
from typing import Tuple, Any, Generator, Union, List, Optional, Dict, Iterable
import pathlib
import time
import random
import multiprocessing
import pandas as pd
import numpy as np
from PIL import Image
import tensorflow as tf
from dataset.mixup import MixupGenerator
from dataset.utils.multiprocess import Map

class DatasetBase(object):
    """Dataset loader base class."""

    def __init__(self) -> None:
        """Load data and setup preprocessing."""
        pass

    def training_data(self) -> Tuple[np.array, np.array]:
        """Return training dataset.

        Return:
            dataset (Tuple[np.array, np.array]): training dataset pair

        """
        pass

    def training_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return training dataset iterator.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset iterator

        """
        pass

    def eval_data(self) -> Tuple[np.array, np.array]:
        """Return evaluation dataset.

        Return:
            dataset (Tuple[np.array, np.array]): evaluation dataset pair

        """
        pass

    def eval_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return evaluation dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        pass

class ImageClassifierDatasetBase(DatasetBase):
    """Image classification dataset loader base class.

    Args:
        batch_size (int): training batch size.
        use_mixup (bool): whether to use mixup augmentation.

    """

    def __init__(
            self,
            batch_size: int = 32,
            use_mixup: bool = False,
            **kwargs: Any
            ) -> None:
        """Initialize data generator."""
        self.batch_size = batch_size
        self.use_mixup = use_mixup
        self.train_data_gen = tf.keras.preprocessing.image.ImageDataGenerator(**kwargs)
        self.eval_data_gen = tf.keras.preprocessing.image.ImageDataGenerator()
        self.input_shape: Tuple[int, int, int]
        self.category_nums: int
        self.steps_per_epoch: int
        self.eval_steps_per_epoch: int

class BinaryImageClassifierDataset(ImageClassifierDatasetBase):
    """Memory loaded classifier dataset."""

    def __init__(self, **kwargs: Any) -> None:
        super(BinaryImageClassifierDataset, self).__init__(**kwargs)
        self.x_train: np.array
        self.x_test: np.array
        self.y_train: np.array
        self.y_test: np.array

    def training_data(self) -> Tuple[np.array, np.array]:
        """Return training dataset.

        Return:
            dataset (Tuple[np.array, np.array]): training dataset pair

        """
        y_train = tf.keras.utils.to_categorical(self.y_train)
        return (self.train_data_gen.random_transform(self.x_train), y_train)

    def training_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return training dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        y_train = tf.keras.utils.to_categorical(self.y_train)
        self.steps_per_epoch = len(self.x_train) // self.batch_size
        if self.use_mixup:
            return MixupGenerator(datagen=self.train_data_gen).flow(self.x_train, y_train, batch_size=self.batch_size)
        else:
            return self.train_data_gen.flow(self.x_train, y=y_train, batch_size=self.batch_size)

    def eval_data(self) -> Tuple[np.array, np.array]:
        """Return evaluation dataset.

        Return:
            dataset (Tuple[np.array, np.array]): evaluation dataset pair

        """
        y_test = tf.keras.utils.to_categorical(self.y_test)
        return (self.x_test, y_test)

    def eval_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return evaluation dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        y_test = tf.keras.utils.to_categorical(self.y_test)
        self.eval_steps_per_epoch = len(self.x_test) // self.batch_size
        return self.eval_data_gen.flow(self.x_test, y=y_test, batch_size=self.batch_size)

class TfrecordDatasetMixin(object):
    """Tfrecord used datasets mixin."""

    image_feature_description: dict
    label_feature_description: dict

    @classmethod
    def make_example(cls, args):
        """Make tfrecord example."""
        raise NotImplementedError

    @classmethod
    def _convert(
            cls,
            images: Dict[str, str],
            labels: Dict[str, Any],
            keys: Iterable,
            tfrecord_path: pathlib.Path,
            split_num: int):
        """Convert to tfrecord.

        Args:
            images (dict): image id to path dictionary.
            labels (dict): images id to label dictionary.
            keys (list): images id list.
            tfrecord_path (Path): save path.
            split_num (int): images nums per one tfrecord file.

        """
        def generator():
            with Map(cls.make_example, 5, queue_size=split_num*2) as map:
                map.put([[(images[k], labels[k])] for k in keys])
                time.sleep(1)
                while not (map.in_queue_empty() and map.out_queue_empty()):
                    if not map.out_queue_empty():
                        serialized_string = map.get(1)[0]
                        if len(serialized_string) != 0:
                            yield serialized_string

        def write(key, tensors):
            filename = tf.strings.join([str(tfrecord_path), '/data', tf.strings.as_string(key), '.tfrecord'])
            writer = tf.data.experimental.TFRecordWriter(filename)
            writer.write(tf.data.Dataset.from_tensor_slices(tensors))
            return tf.data.Dataset.from_tensors(filename)

        def key(i, *args):
            return i // split_num

        serialized_dataset = tf.data.Dataset.from_generator(
            generator, output_types=tf.string, output_shapes=())
        dataset = (serialized_dataset
                   .batch(split_num)
                   .enumerate()
                   .interleave(write, num_parallel_calls=tf.data.experimental.AUTOTUNE))
        writer = tf.data.experimental.TFRecordWriter(str(tfrecord_path) + '/files.tfrecord')
        writer.write(dataset)

    def _preprocess(
            self,
            image: tf.Tensor,
            label: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
        """Preprocess image and label."""
        raise NotImplementedError

    def _decode_tfrecord(
            self,
            example_proto):
        image_feature, label_feature = tf.io.parse_single_sequence_example(
                example_proto,
                context_features=self.image_feature_description,
                sequence_features=self.label_feature_description)

        height = image_feature['height'][0]
        width = image_feature['width'][0]
        image = tf.io.decode_raw(image_feature['image'], tf.uint8)
        image = tf.reshape(image, tf.stack((height, width, 3)))
        label = label_feature['label']
        return image, label

    def _data_generator(
            self,
            data_path: pathlib.Path,
            batch_size: int,
            repeat: bool = True) -> Generator:
        """Return training dataset.

        Args:
            data_path (pathlib.Path): data directory path.
            repeat (bool): whether or not to repeat data generate.

        Return:
            dataset (Generator): dataset generator

        """
        dataset = (
            tf.data.TFRecordDataset.list_files(str(data_path) + '/data*.tfrecord')
            .interleave(tf.data.TFRecordDataset)
            .map(self._decode_tfrecord)
            .map(self._preprocess)
            .batch(batch_size, drop_remainder=True)
        )

        if repeat:
            dataset = dataset.repeat()

        while True:
            yield next(iter(dataset))

class DirectoryImageClassifierDataset(ImageClassifierDatasetBase, TfrecordDatasetMixin):
    """Directory loaded image classification dataset.

    Args:
        train_data_directory (str): path to training data directory.
        test_data_directory (str): path to test data directory.

    """

    def __init__(
            self,
            train_data_directory: str,
            test_data_directory: str,
            max_boxes: int = 5,
            **kwargs: Any) -> None:
        """Initilize params."""
        super(DirectoryImageClassifierDataset, self).__init__(**kwargs)
        self.train_data_directory = pathlib.Path(train_data_directory)
        self.test_data_directory = pathlib.Path(test_data_directory)
        self.x_train: List
        self.x_test: List

    def training_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return training dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        self.steps_per_epoch = len(self.x_train) // self.batch_size
        return self._data_generator(self.train_data_directory, self.batch_size, repeat=True)

    def eval_data(self) -> Tuple[np.array, np.array]:
        """Return evaluation dataset.

        Return:
            dataset (Tuple[np.array, np.array]): evaluation dataset pair

        """
        images: List[np.array] = []
        labels: List[np.array] = []
        generator = self._data_generator(self.test_data_directory, self.batch_size, repeat=False)
        try:
            _images, _labels = next(generator)
            images.extend(_images)
            labels.extend(_labels)
        except StopIteration:
            pass

        return np.array(images), np.array(labels)

    def eval_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return test dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        self.eval_steps_per_epoch = len(self.x_test) // self.batch_size
        return self._data_generator(self.test_data_directory, self.batch_size, repeat=True)

class ObjectDitectionDatasetBase(ImageClassifierDatasetBase):
    """Object detection dataset loader base class.

    Note:
        label is Ground truth boxes tensor with shape (num_true_boxes, 5)
        containing box relative x_center, y_center, width, height, and class.

    """

    def __init__(
            self,
            **kwargs: Any) -> None:
        """Initilize params."""
        super(ObjectDitectionDatasetBase, self).__init__(**kwargs)
        self.max_boxes: int

class DirectoryObjectDitectionDataset(ObjectDitectionDatasetBase):
    """Directory loaded detection dataset.

    Args:
        train_data_directory (str): path to training data directory.
        test_data_directory (str): path to test data directory.

    """

    image_feature_description: dict
    label_feature_description: dict

    def __init__(
            self,
            train_data_directory: str,
            test_data_directory: str,
            max_boxes: int = 5,
            **kwargs: Any) -> None:
        """Initilize params."""
        super(DirectoryObjectDitectionDataset, self).__init__(**kwargs)
        self.train_data_directory = pathlib.Path(train_data_directory)
        self.test_data_directory = pathlib.Path(test_data_directory)
        self.max_boxes = max_boxes
        self.x_train: List
        self.x_test: List

    def _resize_and_relative(
            self,
            image: np.array,
            box: np.array) -> Tuple[np.array, np.array]:
        """Resize and processing images."""
        def _process(image, box):
            _box = box.numpy()
            width = image.shape[1]
            height = image.shape[0]

            _box[:, 0] /= width
            _box[:, 2] /= width
            _box[:, 1] /= height
            _box[:, 3] /= height

            _box = _box[:self.max_boxes]
            if _box.shape[0] < self.max_boxes:
                zero_padding = np.zeros((self.max_boxes - _box.shape[0], 5), dtype=_box.dtype)
                _box = np.vstack((_box, zero_padding))
            return _box

        box = tf.py_function(_process, [image, box], [tf.float32])[0]
        image = tf.image.resize(image, self.input_shape[:2], tf.image.ResizeMethod.BILINEAR)
        image = image / 255.0
        return image, box

    def _decode_tfrecord(
            self,
            example_proto):
        image_feature, label_feature = tf.io.parse_single_sequence_example(
                example_proto,
                context_features=self.image_feature_description,
                sequence_features=self.label_feature_description)

        height = image_feature['height'][0]
        width = image_feature['width'][0]
        image = tf.io.decode_raw(image_feature['image'], tf.uint8)
        image = tf.reshape(image, tf.stack((height, width, 3)))
        label = label_feature['label']
        return image, label

    def _data_generator(
            self,
            data_path: pathlib.Path,
            repeat: bool = True) -> Generator:
        """Return training dataset.

        Args:
            data_path (pathlib.Path): data directory path.
            repeat (bool): whether or not to repeat data generate.

        Return:
            dataset (Generator): dataset generator

        """
        dataset = (
            tf.data.TFRecordDataset.list_files(str(data_path) + '/data*.tfrecord')
            .interleave(tf.data.TFRecordDataset)
            .map(self._decode_tfrecord)
            .map(self._resize_and_relative)
            .batch(self.batch_size, drop_remainder=True)
        )

        if repeat:
            dataset = dataset.repeat()

        while True:
            yield next(iter(dataset))

    def training_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return training dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        self.steps_per_epoch = len(self.x_train) // self.batch_size
        return self._data_generator(self.train_data_directory, repeat=True)

    def eval_data(self) -> Tuple[np.array, np.array]:
        """Return evaluation dataset.

        Return:
            dataset (Tuple[np.array, np.array]): evaluation dataset pair

        """
        images: List[np.array] = []
        boxes: List[np.array] = []
        generator = self._data_generator(self.test_data_directory, repeat=False)
        while True:
            try:
                _images, _boxes = next(generator)
                images.extend(_images)
                boxes.extend(_boxes)
            except StopIteration:
                break

        return np.array(images), np.array(boxes)

    def eval_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return test dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        self.eval_steps_per_epoch = len(self.x_test) // self.batch_size
        return self._data_generator(self.test_data_directory, repeat=True)

class ImageSegmentationDatasetBase(ImageClassifierDatasetBase):
    """Image segmentation dataset loader base class."""

    pass

class DirectoryImageSegmentationDataset(ImageSegmentationDatasetBase):
    """Directory loaded classifier dataset.

    Args:
        train_image_directory (str): path to training image directory.
        train_label_directory (str): path to training label directory.
        test_image_directory (str): path to test image directory.
        test_image_directory (str): path to test label directory.
        class_csv (str): path to class color definition csv. each rows has `name`, `r`, `g` and `b` columns.
        crop_width (int): crop width. if value is 0, we do not crop.
        crop_height (int): crop width. if value is 0, we do not crop.
        window_size (int): pre images and pot images scope size.
        sample_num_per_image (int): number of random sample per image.

    """

    def __init__(
            self,
            train_image_directory: str,
            train_label_directory: str,
            test_image_directory: str,
            test_label_directory: str,
            class_csv: str,
            crop_width: int = 0,
            crop_height: int = 0,
            window_size: int = 0,
            sample_num_per_image: int = 1,
            **kwargs: Any) -> None:
        """Initilize params."""
        super(DirectoryImageSegmentationDataset, self).__init__(**kwargs)
        self.train_image_directory = pathlib.Path(train_image_directory)
        self.train_label_directory = pathlib.Path(train_label_directory)
        self.test_image_directory = pathlib.Path(test_image_directory)
        self.test_label_directory = pathlib.Path(test_label_directory)
        self.class_dict: pd.DataFrame = pd.read_csv(class_csv)
        self.category_nums = len(self.class_dict)

        for path in self.train_image_directory.glob('**/*.*'):
            try:
                image = np.array(Image.open(path))
            except OSError:
                continue

            self.input_shape = image.shape
            break

        self.input_shape = (self.input_shape[0], self.input_shape[1], self.input_shape[2] * (2 * window_size + 1))
        if crop_height != 0 and crop_width != 0:
            self.input_shape = (crop_width, crop_height, self.input_shape[2])
        self.crop_width = crop_width
        self.crop_height = crop_height
        self.window_size = window_size
        self.sample_num_per_image = sample_num_per_image

    def _random_crop(
            self,
            image: np.array,
            label: np.array) -> Tuple[np.array, np.array]:
        """Crop image and label to correspondence position.

        Args:
            image (np.array): target image.
            label (np.array): target label.

        Return:
            cropped_image (np.array): cropped image
            cropped_label (np.array): cropped label

        """
        if (image.shape[0] != label.shape[0]) or (image.shape[1] != label.shape[1]):
            raise ValueError('Image and label must have the same dimensions!')

        if self.crop_width == 0 or self.crop_height == 0:
            return image, label

        if (self.crop_width <= image.shape[0]) and (self.crop_height <= image.shape[1]):
            x = np.random.randint(0, image.shape[0]-self.crop_width)
            y = np.random.randint(0, image.shape[1]-self.crop_height)

            return image[x:x+self.crop_width, y:y+self.crop_height], label[x:x+self.crop_width, y:y+self.crop_height]
        else:
            raise Exception(
                'Crop shape (%d, %d) exceeds image dimensions (%d, %d)!'.format(
                    (self.crop_width, self.crop_height, image.shape[0], image.shape[1])))

    def _label_image_to_category(
            self,
            image: np.array) -> np.array:
        """Convert label image to category number list.

        Args:
            image (np.array): image array. shape is H x W x 3

        Return:
            label (np.array): label array. shape is H x W x category_nums

        """
        label = np.zeros((image.shape[0], image.shape[1], self.category_nums), dtype=np.bool)
        for i, r in self.class_dict.iterrows():
            equality = np.equal(image, [r['r'], r['g'], r['b']]).all(axis=-1)
            label[:, :, i][equality] = 1
        return label

    def _load_data(
            self,
            image_path: str,
            label_path: str) -> Tuple[np.array, np.array]:
        """Load one data.

        Args:
            image_path (str): path to image.
            label_path (str): path to correspondence label.

        Return:
            image (np.array): image object.
            label (mp.array): correspondence label object.
        """
        try:
            image = np.array(Image.open(image_path)) / 255.0
            label = self._label_image_to_category(np.array(Image.open(label_path).convert('RGB')))
            return image, label
        except OSError:
            return None, None

    def _data(
            self,
            image_path: pathlib.Path,
            label_path: pathlib.Path,
            max_length: Optional[int] = None) -> Tuple[np.array, np.array]:
        """Return training dataset.

        Args:
            image_path (pathlib.Path): image directory path
            label_path (pathlib.Path): label directory path

        Return:
            dataset (Tuple[np.array, np.array]): training dataset pair

        """
        image_paths = sorted(image_path.glob('**/*.*'))
        label_paths = sorted(label_path.glob('**/*.*'))
        if max_length:
            image_paths = random.choices(image_paths, k=max_length)
            label_paths = random.choices(label_paths, k=max_length)
        assert len(label_paths) == len(image_paths)

        pool = multiprocessing.pool.ThreadPool()
        results = []
        for image, label in zip(image_paths, label_paths):
            results.append(pool.apply_async(self._load_data, (image, label)))

        pool.close()
        pool.join()

        X: List = []
        y: List = []
        for res in results:
            image, label = res.get()
            if image is not None and label is not None:
                X.append(image)
                y.append(label)

        X_new = [np.concatenate(X[j-self.window_size:j+self.window_size+1], axis=2)
                 for j in range(self.window_size, len(y)-self.window_size)]
        y_new = [y[j] for j in range(self.window_size, len(y)-self.window_size)]
        X_new, y_new = zip(*[self._random_crop(image, label) for image, label in zip(X_new, y_new)])
        return np.array(X_new), np.array(y_new)

    def training_data(self) -> Tuple[np.array, np.array]:
        """Return training dataset.

        Return:
            dataset (Tuple[np.array, np.array]): training dataset pair

        """
        return self._data(self.train_image_directory, self.train_label_directory)

    def eval_data(self) -> Tuple[np.array, np.array]:
        """Return evaluation dataset.

        Return:
            dataset (Tuple[np.array, np.array]): evaluation dataset pair

        """
        return self._data(self.test_image_directory, self.test_label_directory, max_length=100)

    def _data_generator(
            self,
            image_path: pathlib.Path,
            label_path: pathlib.Path) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return training dataset.

        Args:
            image_path (pathlib.Path): image directory path
            label_path (pathlib.Path): label directory path

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        image_directories = sorted([p for p in image_path.glob('*') if p.is_dir()])
        label_directories = sorted([p for p in label_path.glob('*') if p.is_dir()])

        while True:
            for im, la, in zip(image_directories, label_directories):
                image_paths = sorted(im.glob('*.*'))
                label_paths = sorted(la.glob('*.*'))
                if len(label_paths) != len(image_paths):
                    continue
                sample_num = len(image_paths) // self.sample_num_per_image
                indexes = np.random.choice(len(image_paths), sample_num)
                itr_num = int((sample_num - 2 * self.window_size) // (self.batch_size))

                for i in range(itr_num):
                    batch_ids = indexes[self.window_size + i * self.batch_size:2 * self.window_size + (i + 1) * self.batch_size]
                    X: List = []
                    y: List = []

                    for j in batch_ids:
                        image, label = self._load_data(image_paths[j], label_paths[j])
                        if image is not None and label is not None:
                            X.append(image)
                            y.append(label)

                    if self.window_size > 0:
                        X_new = [np.concatenate(X[j-self.window_size:j+self.window_size+1], axis=2)
                                 for j in range(self.window_size, len(y)-self.window_size)]
                        y_new = [y[j] for j in range(self.window_size, len(y)-self.window_size)]
                    else:
                        X_new, y_new = X, y
                    X_new, y_new = zip(*[self._random_crop(image, label) for image, label in zip(X_new, y_new)])
                    yield np.array(X_new), np.array(y_new)

    def training_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return training dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        self.steps_per_epoch = len(list(self.train_image_directory.glob('**/*.*'))) // self.batch_size // self.sample_num_per_image
        return self._data_generator(self.train_image_directory, self.train_label_directory)

    def eval_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return evaluation dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        self.eval_steps_per_epoch = len(list(self.test_image_directory.glob('*'))) // self.batch_size // self.sample_num_per_image
        return self._data_generator(self.test_image_directory, self.test_label_directory)

class TextDatasetBase(DatasetBase):
    """Image classification dataset loader base class.

    Args:
        batch_size (int): training batch size.

    """

    START_TOKEN = '<S>'
    END_TOKEN = '<E>'

    def __init__(
            self,
            batch_size: int = 32,
            seq_length: int = 128,
            vocab_size: int = 1000,
            **kwargs: Any) -> None:
        """Initialize data generator."""
        self.batch_size = batch_size
        self.tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=vocab_size, **kwargs)
        self.seq_length = seq_length
        self.vocab_size = vocab_size
        self.steps_per_epoch: int
        self.eval_steps_per_epoch: int

class BinaryTextDataset(TextDatasetBase):
    """Memory loaded text dataset."""

    def __init__(self, **kwargs: Any) -> None:
        super(BinaryTextDataset, self).__init__(**kwargs)
        self.x_train: np.array
        self.x_test: np.array

    def training_data(self) -> Tuple[np.array, np.array]:
        """Return training dataset.

        Return:
            dataset (Tuple[np.array, np.array]): training dataset pair

        """
        self.tokenizer.fit_on_texts(self.x_train)
        sequences = self.tokenizer.texts_to_sequences(self.x_train)
        sequences = self.uniformed_sequences(sequences, length=self.seq_length+1)
        sequences = np.array(sequences)
        return sequences[:, :-1], sequences[:, 1:]

    def training_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return training dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        inputs, targets = self.training_data()
        self.steps_per_epoch = len(inputs) // self.batch_size
        dataset = tf.data.Dataset \
                    .from_tensor_slices((inputs, targets)) \
                    .shuffle(self.batch_size * 4, reshuffle_each_iteration=True) \
                    .repeat() \
                    .batch(self.batch_size, drop_remainder=True)
        return dataset

    def eval_data(self) -> Tuple[np.array, np.array]:
        """Return evaluation dataset.

        Return:
            dataset (Tuple[np.array, np.array]): evaluation dataset pair

        """
        sequences = self.tokenizer.texts_to_sequences(self.x_test)
        sequences = self.uniformed_sequences(sequences, length=self.seq_length+1)
        sequences = np.array(sequences)
        return sequences[:, :-1], sequences[:, 1:]

    def eval_data_generator(self) -> Union[tf.keras.utils.Sequence, Generator]:
        """Return evaluation dataset.

        Return:
            dataset (Union[tf.keras.utils.Sequence, Generator]): dataset generator

        """
        inputs, targets = self.eval_data()
        self.eval_steps_per_epoch = len(inputs) // self.batch_size
        dataset = tf.data.Dataset \
                    .from_tensor_slices((inputs, targets)) \
                    .batch(self.batch_size, drop_remainder=False)
        return dataset

    def uniformed_sequences(
            self,
            sequences: np.array,
            length: Optional[int] = None) -> np.array:
        """Return decode texts from word id sequences.

        Args:
            sequences (np.array): word id sequences.
            length (int): referenced length. default is self.seq_length.

        Return:
            padded_sequences (np.array]): word id sequences uniformed length.

        """
        if length is None:
            length = self.seq_length
        return tf.keras.preprocessing.sequence.pad_sequences(sequences, padding='post', truncating='post', maxlen=length)

    def decode(
            self,
            sequences: np.array) -> List[str]:
        """Return decode texts from word id sequences.

        Args:
            sequences (np.array): word id sequences.

        Return:
            texts (List[str]): decoded text.

        """
        return self.tokenizer.sequences_to_texts(sequences)

    def encode(
            self,
            texts: List[str]) -> np.array:
        """Return encode word id sequences from texts.

        Args:
            texts (List[str]): decoded text.

        Return:
            sequences (np.array): word id sequences.

        """
        return self.tokenizer.texts_to_sequences(texts)

max_stars_repo_name: katsugeneration/ml-project-template

max_issues_repo_name: katsugeneration/ml-project-template

max_forks_repo_name: katsugeneration/ml-project-template


text:
import cv2
from darkflow.net.build import TFNet
import numpy as np
import time
import socket
import io
import struct

client_socket = socket.socket()
client_socket.connect(('192.168.1.76', 8000))
connection = client_socket.accept()[0].makefile(mode="rb")

options = {
'model': 'cfg/yolov2-tiny.cfg',
'load' : 'bin/yolov2-tiny.weights',
'threshold': 0.5
}
#capture = cv2.VideoCapture(0)
tfnet = TFNet(options)
colors = [tuple(255*np.random.rand(3)) for i in range(100)]

while (True):
    image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
    if not image_len:
        breakom
    image_stream = io.BytesIO()
    image_stream.write(connection.read(image_len))
    image_stream.seek(0)
    frame = cv2.imdecode(np.fromstring(image_stream.read(), np.uint8), 1)
    stime = time.time()
    results = tfnet.return_predict(frame)
    #no of objects detected
    objs_detected = len(results)
    if ret:
        for color,result in zip(colors,results):
            #bounding box topleft, bottomright coords, label & confidence
            tl = (result['topleft']['x'], result['topleft']['y'])
            br = (result['bottomright']['x'], result['bottomright']['y'])
            label = result['label']
            conf = result['confidence']
            #code to draw bounding box
            frame = cv2.rectangle(frame, tl, br, color, 3)
            frame = cv2.putText(frame, label,tl , cv2.FONT_HERSHEY_COMPLEX, 0.75, (225,255,0), 2)
            frame = cv2.putText(frame, str(int(conf*100))+'%', br, cv2.FONT_HERSHEY_DUPLEX, 0.75, (225,255,0), 2)
        cv2.imshow('frame',frame)
        print('fps {:.2f}'.format(1/(time.time()-stime)))
        print('objects detected:', objs_detected)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
cv2.destroyAllWindows()


text:
import os
import unittest

import numpy.testing as npt

class UtilitiesTestCase(unittest.TestCase):
    def test_checksum(self):
        import astroNN
        from astroNN.shared.downloader_tools import filehash
        anderson2017_path = os.path.join(os.path.dirname(astroNN.__path__[0]), 'astroNN', 'data',
                                         'anderson_2017_dr14_parallax.npz')
        md5_pred = filehash(anderson2017_path, algorithm='md5')
        sha1_pred = filehash(anderson2017_path, algorithm='sha1')
        sha256_pred = filehash(anderson2017_path, algorithm='sha256')

        # read answer hashed by Windows Get-FileHash
        self.assertEqual(md5_pred, '9C714F5FE22BB7C4FF9EA32F3E859D73'.lower())
        self.assertEqual(sha1_pred, '733C0227CF93DB0CD6106B5349402F251E7ED735'.lower())
        self.assertEqual(sha256_pred, '36C265C907F440114D747DA21D2A014D32B5E442D541F183C0EE862F5865FD26'.lower())
        self.assertRaises(ValueError, filehash, anderson2017_path, algorithm='sha123')

    def test_normalizer(self):
        from astroNN.nn.utilities.normalizer import Normalizer
        from astroNN.config import MAGIC_NUMBER
        import numpy as np

        data = np.random.normal(0, 1, (100, 10))
        magic_idx = (10, 5)
        data[magic_idx] = MAGIC_NUMBER

        # create a normalizer instance for mode 0
        normer = Normalizer(mode=0)
        norm_data = normer.normalize(data)
        self.assertEqual(norm_data[magic_idx], MAGIC_NUMBER)  # make sure normalizer preserve magic_number
        # test demoralize
        data_denorm = normer.denormalize(norm_data)
        # make sure demoralizer preserve magic_number
        self.assertEqual(data_denorm[magic_idx], MAGIC_NUMBER)
        npt.assert_array_almost_equal(data_denorm, data)
        npt.assert_array_almost_equal(norm_data, data)

        # create a normalizer instance for mode 1
        normer = Normalizer(mode=1)
        norm_data = normer.normalize(data)
        self.assertEqual(norm_data[magic_idx], MAGIC_NUMBER)  # make sure normalizer preserve magic_number
        # test demoralize
        data_denorm = normer.denormalize(norm_data)
        # make sure demoralizer preserve magic_number
        self.assertEqual(data_denorm[magic_idx], MAGIC_NUMBER)
        npt.assert_array_almost_equal(data_denorm, data)

        # test mode='3s' can do identity transformation
        s3_norm = Normalizer(mode='3s')
        data = np.random.normal(0, 1, (100, 10))
        npt.assert_array_almost_equal(s3_norm.denormalize(s3_norm.normalize(data)), data, decimal=5)

        data_8bit = np.random.randint(0, 256, (100, 50, 50))
        normer = Normalizer(mode=255)
        norm_data_8bit = normer.normalize(data_8bit)
        self.assertEqual(np.max(norm_data_8bit), 1.)  # make sure max of normalized image is 1.
        self.assertEqual(np.min(norm_data_8bit), 0.)  # make sure max of normalized image is 0.

        normer = Normalizer(mode={'input': 255, 'aux': 0})
        norm_data_dict = normer.normalize({'input': data_8bit, 'aux': data})
        self.assertEqual(np.max(norm_data_dict['input']), 1.)  # make sure max of normalized image is 1.
        self.assertEqual(np.min(norm_data_dict['input']), 0.)  # make sure max of normalized image is 0.
        npt.assert_array_almost_equal(norm_data_dict['aux'], data)  # make sure aux data is not normalized in this case

        errorous_norm = Normalizer(mode=-1234)
        self.assertRaises(ValueError, errorous_norm.normalize, data)

    def test_cpu_gpu_management(self):
        from astroNN.shared.nn_tools import cpu_fallback

        cpu_fallback(flag=True)
        cpu_fallback(flag=False)

        # make sure flag=2 raise error
        self.assertRaises(ValueError, cpu_fallback, flag=2)

    def test_h5name_check(self):
        from astroNN.datasets.h5 import h5name_check

        # make sure h5name=None raise error
        self.assertRaises(ValueError, h5name_check, None)

    def test_config(self):
        from astroNN.config import config_path

        config_path(flag=0)
        config_path(flag=1)
        config_path(flag=2)

    def test_patching(self):
        import astroNN.data
        from astroNN.shared.patch_util import Patch

        diff = os.path.join(astroNN.data.datapath(), 'tf1_12.patch')
        patch = Patch(diff)
        patch_file_path = "travis_tf_1_12.py"
        if os.path.exists(patch_file_path) is False:
            patch_file_path = os.path.join("tests", "travis_tf_1_12.py")

        with open(patch_file_path, 'r') as f:
            original_text = f.read()

        patch.apply(patch_file_path)
        with open(patch_file_path, 'r') as f:
            patched_text = f.read()

        patch.apply(patch_file_path)
        with open(patch_file_path, 'r') as f:
            patched_twice_text = f.read()

        patch.revert(patch_file_path)
        with open(patch_file_path, 'r') as f:
            unpatched_text = f.read()

        patch.revert(patch_file_path)
        with open(patch_file_path, 'r') as f:
            unpatched_twice_text = f.read()

        # assert patching, patching twice and unpatching work correctly
        self.assertNotEqual(original_text, patched_text)
        self.assertEqual(patched_twice_text, patched_text)
        self.assertEqual(original_text, unpatched_text)
        self.assertEqual(unpatched_twice_text, unpatched_text)

    def test_pltstyle(self):
        from astroNN.shared import pylab_style

        pylab_style()

    # def test_loader(self):
    #     import numpy as np
    #
    #     a = np.random.normal(0, 1, (100000, 7514))
    #     b = np.random.normal(0, 1, (7514))
    #     c = np.random.normal(0, 1, (7514))
    #
    #     func = lambda a,b,c: (a - b)/c
    #
    #     for i in range(int(100000/10000)):
    #         func(a[i:i*10000], b, c)

if __name__ == '__main__':
    unittest.main()

max_stars_repo_name: igomezv/astroNN

max_issues_repo_name: igomezv/astroNN

max_forks_repo_name: igomezv/astroNN