<commit_before>
import argparse as ap
from random import uniform,seed
from timeit import default_timer
from itertools import combinations

from dynamite import config
from dynamite.states import State
from dynamite.operators import sigmax, sigmay, sigmaz
from dynamite.operators import op_sum, op_product, index_sum
from dynamite.extras import majorana
from dynamite.subspaces import Full, Parity, Auto
from dynamite.tools import track_memory, get_max_memory_usage
from dynamite.computations import reduced_density_matrix

# TODO: write tests for the benchmarks

def parse_args(argv=None):

    parser = ap.ArgumentParser(description='Benchmarking test for dynamite.')

    parser.add_argument('-L', type=int, required=True,
                        help='Size of the spin chain.')

    parser.add_argument('-H', choices=['MBL','long_range','SYK','ising','XX'],
                        help='Hamiltonian to use')

    parser.add_argument('--shell', action='store_true',
                        help='Make a shell matrix instead of a regular matrix.')
    parser.add_argument('--gpu', action='store_true',
                        help='Run computations on GPU instead of CPU.')

    parser.add_argument('--slepc_args', type=str, default='',
                        help='Arguments to pass to SLEPc.')
    parser.add_argument('--track_memory', action='store_true',
                        help='Whether to compute max memory usage')

    parser.add_argument('--subspace', choices=['full', 'parity', 'auto', 'nosortauto'],
                        default='full',
                        help='Which subspace to use.')
    parser.add_argument('--which_space', type=str,
                        help='The particular subspace to use. For parity, options are "even" '
                        'and "odd", for auto supply a starting state like UUUUDDDD.')

    parser.add_argument('--evolve', action='store_true',
                        help='Request that the Hamiltonian evolves a state.')
    parser.add_argument('-t', type=float, default=1.0,
                        help='The time to evolve for.')

    parser.add_argument('--mult', action='store_true',
                        help='Simply multiply the Hamiltonian by a vector.')
    parser.add_argument('--mult_count', type=int, default=1,
                        help='Number of times to repeat the multiplication.')

    parser.add_argument('--norm', action='store_true',
                        help='Compute the norm of the matrix.')

    parser.add_argument('--eigsolve', action='store_true',
                        help='Request to solve for eigenvalues of the Hamiltonian.')
    parser.add_argument('--nev', type=int ,default=1,
                        help='The number of eigenpairs to solve for.')
    parser.add_argument('--target', type=float,
                        help='The target for a shift-invert eigensolve.')

    parser.add_argument('--rdm', action='store_true',
                        help='Compute a reduced density matrix')
    parser.add_argument('--keep', type=lambda s: [int(x) for x in s.split(',')],
                        help='A list of spins, separated by commas, to keep during the '
                        'RDM computation. By default, the first half are kept.')

    return parser.parse_args(argv)

def build_subspace(params, hamiltonian=None):
    space = params.which_space

    if params.subspace == 'full':
        rtn = Full()

    elif params.subspace == 'parity':
        if space is None:
            space = 'even'
        rtn = Parity(space)

    elif params.subspace in ['auto','nosortauto']:
        if space is None:
            half_length = params.L // 2
            space = 'U'*half_length + 'D'*(params.L - half_length)
        rtn = Auto(hamiltonian, space, sort=params.subspace=='auto')

    return rtn

def build_hamiltonian(params):

    if params.H == 'MBL':
        # dipolar interaction
        rtn = index_sum(op_sum(s(0)*s(1) for s in (sigmax, sigmay, sigmaz)))
        # quenched disorder in z direction
        seed(0)
        for i in range(params.L):
            rtn += uniform(-1, 1) * sigmaz(i)

    elif params.H == 'long_range':
        # long-range ZZ interaction
        rtn = op_sum(index_sum(sigmaz(0)*sigmaz(i)) for i in range(1, params.L))
        # nearest neighbor XX
        rtn += 0.5 * index_sum(sigmax(0)*sigmax(1))
        # some other fields
        rtn += sum(0.1*index_sum(s()) for s in [sigmax, sigmay, sigmaz])

    elif params.H == 'SYK':
        seed(0)

        # only compute the majoranas once
        majoranas = [majorana(i) for i in range(params.L*2)]

        rtn = op_sum(op_product(majoranas[idx] for idx in idxs).scale(uniform(-1,1))
                     for idxs in combinations(range(params.L*2),4))

    elif params.H == 'ising':
        rtn = index_sum(sigmaz(0)*sigmaz(1)) + 0.2*index_sum(sigmax())

    elif params.H == 'XX':
        rtn = index_sum(sigmax(0)*sigmax(1))

    else:
        raise ValueError('Unrecognized Hamiltonian.')

    return rtn

def compute_norm(hamiltonian):
    config.initialize()
    from petsc4py.PETSc import NormType
    return hamiltonian.get_mat().norm(NormType.INFINITY)

def do_eigsolve(params, hamiltonian):
    hamiltonian.eigsolve(nev=params.nev,target=params.target)

def do_evolve(params, hamiltonian, state, result):
    hamiltonian.evolve(state, t=params.t, result=result)

def do_mult(params, hamiltonian, state, result):
    for _ in range(params.mult_count):
        hamiltonian.dot(state, result)

def do_rdm(state, keep):
    reduced_density_matrix(state, keep)

# this decorator keeps track of and times function calls
def log_call(function, stat_dict):
    config.initialize()
    from petsc4py.PETSc import Sys
    Print = Sys.Print

    fn_name = function.__name__

    def rtn(*args, **kwargs):
        if __debug__:
            Print('beginning', fn_name)

        tick = default_timer()
        rtn_val = function(*args, **kwargs)
        tock = default_timer()

        if __debug__:
            Print('completed', fn_name)

        stat_dict[fn_name] = tock-tick

        return rtn_val

    return rtn

def main():
    arg_params = parse_args()
    slepc_args = arg_params.slepc_args.split(' ')
    config.initialize(slepc_args, gpu=arg_params.gpu)
    config.L = arg_params.L
    config.shell = arg_params.shell

    from petsc4py.PETSc import Sys
    Print = Sys.Print

    if not __debug__:
        Print('---ARGUMENTS---')
        for k,v in vars(arg_params).items():
            Print(str(k)+','+str(v))

    if arg_params.track_memory:
        track_memory()

    stats = {}

    # build our Hamiltonian, if we need it
    if arg_params.H is not None:
        H = log_call(build_hamiltonian, stats)(arg_params)
        if __debug__:
            Print('nnz:', H.nnz, '\ndensity:', H.density, '\nMSC size:', H.msc_size)
    else:
        if (arg_params.subspace == 'auto' or
                any(getattr(arg_params, x) for x in ['norm', 'eigsolve', 'evolve', 'mult'])):
            raise ValueError('Must specify Hamiltonian for this benchmark.')
        H = None

    # build our subspace
    subspace = log_call(build_subspace, stats)(arg_params, H)
    if H is not None:
        H.subspace = subspace
        log_call(H.build_mat, stats)()

    # build some states to use in the computations
    in_state = State(L=arg_params.L, subspace=subspace)
    out_state = State(L=arg_params.L, subspace=subspace)
    in_state.set_random()

    # compute the norm
    if arg_params.norm:
        log_call(compute_norm, stats)(H)

    if arg_params.eigsolve:
        log_call(do_eigsolve, stats)(arg_params, H)

    if arg_params.evolve:
        log_call(do_evolve, stats)(arg_params, H, in_state, out_state)

    if arg_params.mult:
        log_call(do_mult, stats)(arg_params, H, in_state, out_state)

    if arg_params.rdm:
        keep_idxs = arg_params.keep
        if keep_idxs is None:
            keep_idxs = range(0, arg_params.L//2)
        log_call(do_rdm, stats)(in_state, keep_idxs)

    # trigger memory measurement
    out_state.vec.destroy()

    if arg_params.track_memory:
        stats['Gb_memory'] = get_max_memory_usage() / 1E9

    Print('---RESULTS---')
    for k,v in stats.items():
        Print('{0}, {1:0.4f}'.format(k, v))

if __name__ == '__main__':
    main()
<commit_msg>update benchmarking script with spin conserve<commit_after>
import argparse as ap
from random import uniform,seed
from timeit import default_timer
from itertools import combinations

from dynamite import config
from dynamite.states import State
from dynamite.operators import sigmax, sigmay, sigmaz
from dynamite.operators import op_sum, op_product, index_sum
from dynamite.extras import majorana
from dynamite.subspaces import Full, Parity, SpinConserve, Auto
from dynamite.tools import track_memory, get_max_memory_usage
from dynamite.computations import reduced_density_matrix

# TODO: write tests for the benchmarks

def parse_args(argv=None):

    parser = ap.ArgumentParser(description='Benchmarking test for dynamite.')

    parser.add_argument('-L', type=int, required=True,
                        help='Size of the spin chain.')

    parser.add_argument('-H', choices=['MBL','long_range','SYK','ising','XX'],
                        help='Hamiltonian to use')

    parser.add_argument('--shell', action='store_true',
                        help='Make a shell matrix instead of a regular matrix.')
    parser.add_argument('--gpu', action='store_true',
                        help='Run computations on GPU instead of CPU.')

    parser.add_argument('--slepc_args', type=str, default='',
                        help='Arguments to pass to SLEPc.')
    parser.add_argument('--track_memory', action='store_true',
                        help='Whether to compute max memory usage')

    parser.add_argument('--subspace', choices=['full', 'parity', 'spinconserve', 'auto', 'nosortauto'],
                        default='full',
                        help='Which subspace to use.')
    parser.add_argument('--which_space', type=str,
                        help='The particular subspace to use. For parity, options are "even" '
                        'and "odd", for spinconserve an integer number of up spins, for auto '
                        'supply a starting state like UUUUDDDD.')

    parser.add_argument('--evolve', action='store_true',
                        help='Request that the Hamiltonian evolves a state.')
    parser.add_argument('-t', type=float, default=1.0,
                        help='The time to evolve for.')

    parser.add_argument('--mult', action='store_true',
                        help='Simply multiply the Hamiltonian by a vector.')
    parser.add_argument('--mult_count', type=int, default=1,
                        help='Number of times to repeat the multiplication.')

    parser.add_argument('--norm', action='store_true',
                        help='Compute the norm of the matrix.')

    parser.add_argument('--eigsolve', action='store_true',
                        help='Request to solve for eigenvalues of the Hamiltonian.')
    parser.add_argument('--nev', type=int ,default=1,
                        help='The number of eigenpairs to solve for.')
    parser.add_argument('--target', type=float,
                        help='The target for a shift-invert eigensolve.')

    parser.add_argument('--rdm', action='store_true',
                        help='Compute a reduced density matrix')
    parser.add_argument('--keep', type=lambda s: [int(x) for x in s.split(',')],
                        help='A list of spins, separated by commas, to keep during the '
                        'RDM computation. By default, the first half are kept.')

    return parser.parse_args(argv)

def build_subspace(params, hamiltonian=None):
    space = params.which_space

    if params.subspace == 'full':
        rtn = Full()

    elif params.subspace == 'parity':
        if space is None:
            space = 'even'
        rtn = Parity(space)

    elif params.subspace == 'spinconserve':
        if space is None:
            space = params.L//2
        else:
            space = int(space)
        rtn = SpinConserve(params.L, space)

    elif params.subspace in ['auto','nosortauto']:
        if space is None:
            half_length = params.L // 2
            space = 'U'*half_length + 'D'*(params.L - half_length)
        rtn = Auto(hamiltonian, space, sort=params.subspace=='auto')

    else:
        raise ValueError("invalid subspace")

    return rtn

def build_hamiltonian(params):

    if params.H == 'MBL':
        # dipolar interaction
        rtn = index_sum(op_sum(s(0)*s(1) for s in (sigmax, sigmay, sigmaz)))
        # quenched disorder in z direction
        seed(0)
        for i in range(params.L):
            rtn += uniform(-1, 1) * sigmaz(i)

    elif params.H == 'long_range':
        # long-range ZZ interaction
        rtn = op_sum(index_sum(sigmaz(0)*sigmaz(i)) for i in range(1, params.L))
        # nearest neighbor XX
        rtn += 0.5 * index_sum(sigmax(0)*sigmax(1))
        # some other fields
        rtn += sum(0.1*index_sum(s()) for s in [sigmax, sigmay, sigmaz])

    elif params.H == 'SYK':
        seed(0)

        # only compute the majoranas once
        majoranas = [majorana(i) for i in range(params.L*2)]

        rtn = op_sum(op_product(majoranas[idx] for idx in idxs).scale(uniform(-1,1))
                     for idxs in combinations(range(params.L*2),4))

    elif params.H == 'ising':
        rtn = index_sum(sigmaz(0)*sigmaz(1)) + 0.2*index_sum(sigmax())

    elif params.H == 'XX':
        rtn = index_sum(sigmax(0)*sigmax(1))

    else:
        raise ValueError('Unrecognized Hamiltonian.')

    return rtn

def compute_norm(hamiltonian):
    config.initialize()
    from petsc4py.PETSc import NormType
    return hamiltonian.get_mat().norm(NormType.INFINITY)

def do_eigsolve(params, hamiltonian):
    hamiltonian.eigsolve(nev=params.nev,target=params.target)

def do_evolve(params, hamiltonian, state, result):
    hamiltonian.evolve(state, t=params.t, result=result)

def do_mult(params, hamiltonian, state, result):
    for _ in range(params.mult_count):
        hamiltonian.dot(state, result)

def do_rdm(state, keep):
    reduced_density_matrix(state, keep)

# this decorator keeps track of and times function calls
def log_call(function, stat_dict):
    config.initialize()
    from petsc4py.PETSc import Sys
    Print = Sys.Print

    fn_name = function.__name__

    def rtn(*args, **kwargs):
        if __debug__:
            Print('beginning', fn_name)

        tick = default_timer()
        rtn_val = function(*args, **kwargs)
        tock = default_timer()

        if __debug__:
            Print('completed', fn_name)

        stat_dict[fn_name] = tock-tick

        return rtn_val

    return rtn

def main():
    arg_params = parse_args()
    slepc_args = arg_params.slepc_args.split(' ')
    config.initialize(slepc_args, gpu=arg_params.gpu)
    config.L = arg_params.L
    config.shell = arg_params.shell

    from petsc4py.PETSc import Sys
    Print = Sys.Print

    if not __debug__:
        Print('---ARGUMENTS---')
        for k,v in vars(arg_params).items():
            Print(str(k)+','+str(v))

    if arg_params.track_memory:
        track_memory()

    stats = {}

    # build our Hamiltonian, if we need it
    if arg_params.H is not None:
        H = log_call(build_hamiltonian, stats)(arg_params)
        if __debug__:
            Print('nnz:', H.nnz, '\ndensity:', H.density, '\nMSC size:', H.msc_size)
    else:
        if (arg_params.subspace == 'auto' or
                any(getattr(arg_params, x) for x in ['norm', 'eigsolve', 'evolve', 'mult'])):
            raise ValueError('Must specify Hamiltonian for this benchmark.')
        H = None

    # build our subspace
    subspace = log_call(build_subspace, stats)(arg_params, H)
    if H is not None:
        H.subspace = subspace
        log_call(H.build_mat, stats)()

    # build some states to use in the computations
    in_state = State(L=arg_params.L, subspace=subspace)
    out_state = State(L=arg_params.L, subspace=subspace)
    in_state.set_random()

    # compute the norm
    if arg_params.norm:
        log_call(compute_norm, stats)(H)

    if arg_params.eigsolve:
        log_call(do_eigsolve, stats)(arg_params, H)

    if arg_params.evolve:
        log_call(do_evolve, stats)(arg_params, H, in_state, out_state)

    if arg_params.mult:
        log_call(do_mult, stats)(arg_params, H, in_state, out_state)

    if arg_params.rdm:
        keep_idxs = arg_params.keep
        if keep_idxs is None:
            keep_idxs = range(0, arg_params.L//2)
        log_call(do_rdm, stats)(in_state, keep_idxs)

    # trigger memory measurement
    out_state.vec.destroy()

    if arg_params.track_memory:
        stats['Gb_memory'] = get_max_memory_usage() / 1E9

    Print('---RESULTS---')
    for k,v in stats.items():
        Print('{0}, {1:0.4f}'.format(k, v))

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>from os import close, unlink
from random import random
from tempfile import mkstemp

import pytest

from bepasty.app import create_app, create_storage

@pytest.fixture(scope='module')
def testclient(request):
    '''
    creates a Flask-testclient instance for bepasty
    '''
    app = create_app()
    db_file, app.config['DATABASE'] = mkstemp()
    # reset default permissions
    app.config['DEFAULT_PERMISSIONS'] = ''
    # setup a secret key
    app.config['SECRET_KEY'] = str(random())
    # setup permissions
    app.config['PERMISSIONS'] = {
        'l': 'list',
        'c': 'create',
        'r': 'read',
        'd': 'delete',
        'a': 'admin'
    }

    def teardown():
        close(db_file)
        unlink(app.config['DATABASE'])
    request.addfinalizer(teardown)
    return app.test_client()
<commit_msg>use triple-double-quotes for docstrings<commit_after>from os import close, unlink
from random import random
from tempfile import mkstemp

import pytest

from bepasty.app import create_app, create_storage

@pytest.fixture(scope='module')
def testclient(request):
    """
    creates a Flask-testclient instance for bepasty
    """
    app = create_app()
    db_file, app.config['DATABASE'] = mkstemp()
    # reset default permissions
    app.config['DEFAULT_PERMISSIONS'] = ''
    # setup a secret key
    app.config['SECRET_KEY'] = str(random())
    # setup permissions
    app.config['PERMISSIONS'] = {
        'l': 'list',
        'c': 'create',
        'r': 'read',
        'd': 'delete',
        'a': 'admin'
    }

    def teardown():
        close(db_file)
        unlink(app.config['DATABASE'])
    request.addfinalizer(teardown)
    return app.test_client()
<|endoftext|>


<commit_before># -*- 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:
"""
ReportCapableInterfaces for segmentation tools

"""
from __future__ import absolute_import, division, print_function, unicode_literals
import os

from nipype.interfaces.base import File, BaseInterface, isdefined
from nipype.interfaces import fsl, freesurfer
from nipype.interfaces.mixins import reporting
from . import report_base as nrc
from .. import NIWORKFLOWS_LOG

class FASTInputSpecRPT(nrc.SVGReportCapableInputSpec,
                       fsl.preprocess.FASTInputSpec):
    pass

class FASTOutputSpecRPT(reporting.ReportCapableOutputSpec,
                        fsl.preprocess.FASTOutputSpec):
    pass

class FASTRPT(nrc.SegmentationRC,
              fsl.FAST):
    input_spec = FASTInputSpecRPT
    output_spec = FASTOutputSpecRPT

    def _run_interface(self, runtime):
        if self.generate_report:
            self.inputs.segments = True

        return super(FASTRPT, self)._run_interface(runtime)

    def _post_run_hook(self, runtime):
        ''' generates a report showing nine slices, three per axis, of an
        arbitrary volume of `in_files`, with the resulting segmentation
        overlaid '''
        self._anat_file = self.inputs.in_files[0]
        outputs = self.aggregate_outputs(runtime=runtime)
        self._mask_file = outputs.tissue_class_map
        # We are skipping the CSF class because with combination with others
        # it only shows the skullstriping mask
        self._seg_files = outputs.tissue_class_files[1:]
        self._masked = False

        NIWORKFLOWS_LOG.info('Generating report for FAST (in_files %s, '
                             'segmentation %s, individual tissue classes %s).',
                             self.inputs.in_files,
                             outputs.tissue_class_map,
                             outputs.tissue_class_files)

        return super(FASTRPT, self)._post_run_hook(runtime)

class ReconAllInputSpecRPT(nrc.SVGReportCapableInputSpec,
                           freesurfer.preprocess.ReconAllInputSpec):
    pass

class ReconAllOutputSpecRPT(reporting.ReportCapableOutputSpec,
                            freesurfer.preprocess.ReconAllOutputSpec):
    pass

class ReconAllRPT(nrc.SurfaceSegmentationRC, freesurfer.preprocess.ReconAll):
    input_spec = ReconAllInputSpecRPT
    output_spec = ReconAllOutputSpecRPT

    def _post_run_hook(self, runtime):
        ''' generates a report showing nine slices, three per axis, of an
        arbitrary volume of `in_files`, with the resulting segmentation
        overlaid '''
        outputs = self.aggregate_outputs(runtime=runtime)
        self._anat_file = os.path.join(outputs.subjects_dir,
                                       outputs.subject_id,
                                       'mri', 'brain.mgz')
        self._contour = os.path.join(outputs.subjects_dir,
                                     outputs.subject_id,
                                     'mri', 'ribbon.mgz')
        self._masked = False

        NIWORKFLOWS_LOG.info('Generating report for ReconAll (subject %s)',
                             outputs.subject_id)

        return super(ReconAllRPT, self)._post_run_hook(runtime)

class MELODICInputSpecRPT(nrc.SVGReportCapableInputSpec,
                          fsl.model.MELODICInputSpec):
    out_report = File(
        'melodic_reportlet.svg', usedefault=True, desc='Filename for the visual'
                                                       ' report generated '
                                                       'by Nipype.')
    report_mask = File(desc='Mask used to draw the outline on the reportlet. '
                            'If not set the mask will be derived from the data.')

class MELODICOutputSpecRPT(reporting.ReportCapableOutputSpec,
                           fsl.model.MELODICOutputSpec):
    pass

class MELODICRPT(fsl.MELODIC):
    input_spec = MELODICInputSpecRPT
    output_spec = MELODICOutputSpecRPT
    _out_report = None
    generate_report = False

    def __init__(self, generate_report=False, **kwargs):
        super(MELODICRPT, self).__init__(**kwargs)
        self.generate_report = generate_report

    def _post_run_hook(self, runtime):
        # Run _post_run_hook of super class
        runtime = super(MELODICRPT, self)._post_run_hook(runtime)
        # leave early if there's nothing to do
        if not self.generate_report:
            return runtime

        NIWORKFLOWS_LOG.info('Generating report for MELODIC.')
        _melodic_dir = runtime.cwd
        if isdefined(self.inputs.out_dir):
            _melodic_dir = self.inputs.out_dir
        self._melodic_dir = os.path.abspath(_melodic_dir)

        self._out_report = self.inputs.out_report
        if not os.path.isabs(self._out_report):
            self._out_report = os.path.abspath(os.path.join(runtime.cwd,
                                                            self._out_report))

        mix = os.path.join(self._melodic_dir, "melodic_mix")
        if not os.path.exists(mix):
            NIWORKFLOWS_LOG.warning("MELODIC outputs not found, assuming it didn't converge.")
            self._out_report = self._out_report.replace('.svg', '.html')
            snippet = '
MELODIC did not converge, no output
'
            with open(self._out_report, 'w') as fobj:
                fobj.write(snippet)
            return runtime

        self._generate_report()
        return runtime

    def _list_outputs(self):
        try:
            outputs = super(MELODICRPT, self)._list_outputs()
        except NotImplementedError:
            outputs = {}
        if self._out_report is not None:
            outputs['out_report'] = self._out_report
        return outputs

    def _generate_report(self):
        from niworkflows.viz.utils import plot_melodic_components
        plot_melodic_components(melodic_dir=self._melodic_dir,
                                in_file=self.inputs.in_files[0],
                                tr=self.inputs.tr_sec,
                                out_file=self._out_report,
                                compress=self.inputs.compress_report,
                                report_mask=self.inputs.report_mask)

class ICA_AROMAInputSpecRPT(nrc.SVGReportCapableInputSpec,
                            fsl.aroma.ICA_AROMAInputSpec):
    out_report = File(
        'ica_aroma_reportlet.svg', usedefault=True, desc='Filename for the visual'
                                                         ' report generated '
                                                         'by Nipype.')
    report_mask = File(desc='Mask used to draw the outline on the reportlet. '
                            'If not set the mask will be derived from the data.')

class ICA_AROMAOutputSpecRPT(reporting.ReportCapableOutputSpec,
                             fsl.aroma.ICA_AROMAOutputSpec):
    pass

class ICA_AROMARPT(reporting.ReportCapableInterface, fsl.ICA_AROMA):
    input_spec = ICA_AROMAInputSpecRPT
    output_spec = ICA_AROMAOutputSpecRPT

    def _generate_report(self):
        from niworkflows.viz.utils import plot_melodic_components
        plot_melodic_components(melodic_dir=self.inputs.melodic_dir,
                                in_file=self.inputs.in_file,
                                out_file=self.inputs.out_report,
                                compress=self.inputs.compress_report,
                                report_mask=self.inputs.report_mask,
                                noise_components_file=self._noise_components_file
                                )

    def _post_run_hook(self, runtime):
        outputs = self.aggregate_outputs(runtime=runtime)
        self._noise_components_file = os.path.join(outputs.out_dir,
                                                   "classified_motion_ICs.txt")

        NIWORKFLOWS_LOG.info('Generating report for ICA AROMA')

        return super(ICA_AROMARPT, self)._post_run_hook(runtime)
<commit_msg>fix: unused import<commit_after># -*- 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:
"""
ReportCapableInterfaces for segmentation tools

"""
from __future__ import absolute_import, division, print_function, unicode_literals
import os

from nipype.interfaces.base import File, isdefined
from nipype.interfaces import fsl, freesurfer
from nipype.interfaces.mixins import reporting
from . import report_base as nrc
from .. import NIWORKFLOWS_LOG

class FASTInputSpecRPT(nrc.SVGReportCapableInputSpec,
                       fsl.preprocess.FASTInputSpec):
    pass

class FASTOutputSpecRPT(reporting.ReportCapableOutputSpec,
                        fsl.preprocess.FASTOutputSpec):
    pass

class FASTRPT(nrc.SegmentationRC,
              fsl.FAST):
    input_spec = FASTInputSpecRPT
    output_spec = FASTOutputSpecRPT

    def _run_interface(self, runtime):
        if self.generate_report:
            self.inputs.segments = True

        return super(FASTRPT, self)._run_interface(runtime)

    def _post_run_hook(self, runtime):
        ''' generates a report showing nine slices, three per axis, of an
        arbitrary volume of `in_files`, with the resulting segmentation
        overlaid '''
        self._anat_file = self.inputs.in_files[0]
        outputs = self.aggregate_outputs(runtime=runtime)
        self._mask_file = outputs.tissue_class_map
        # We are skipping the CSF class because with combination with others
        # it only shows the skullstriping mask
        self._seg_files = outputs.tissue_class_files[1:]
        self._masked = False

        NIWORKFLOWS_LOG.info('Generating report for FAST (in_files %s, '
                             'segmentation %s, individual tissue classes %s).',
                             self.inputs.in_files,
                             outputs.tissue_class_map,
                             outputs.tissue_class_files)

        return super(FASTRPT, self)._post_run_hook(runtime)

class ReconAllInputSpecRPT(nrc.SVGReportCapableInputSpec,
                           freesurfer.preprocess.ReconAllInputSpec):
    pass

class ReconAllOutputSpecRPT(reporting.ReportCapableOutputSpec,
                            freesurfer.preprocess.ReconAllOutputSpec):
    pass

class ReconAllRPT(nrc.SurfaceSegmentationRC, freesurfer.preprocess.ReconAll):
    input_spec = ReconAllInputSpecRPT
    output_spec = ReconAllOutputSpecRPT

    def _post_run_hook(self, runtime):
        ''' generates a report showing nine slices, three per axis, of an
        arbitrary volume of `in_files`, with the resulting segmentation
        overlaid '''
        outputs = self.aggregate_outputs(runtime=runtime)
        self._anat_file = os.path.join(outputs.subjects_dir,
                                       outputs.subject_id,
                                       'mri', 'brain.mgz')
        self._contour = os.path.join(outputs.subjects_dir,
                                     outputs.subject_id,
                                     'mri', 'ribbon.mgz')
        self._masked = False

        NIWORKFLOWS_LOG.info('Generating report for ReconAll (subject %s)',
                             outputs.subject_id)

        return super(ReconAllRPT, self)._post_run_hook(runtime)

class MELODICInputSpecRPT(nrc.SVGReportCapableInputSpec,
                          fsl.model.MELODICInputSpec):
    out_report = File(
        'melodic_reportlet.svg', usedefault=True, desc='Filename for the visual'
                                                       ' report generated '
                                                       'by Nipype.')
    report_mask = File(desc='Mask used to draw the outline on the reportlet. '
                            'If not set the mask will be derived from the data.')

class MELODICOutputSpecRPT(reporting.ReportCapableOutputSpec,
                           fsl.model.MELODICOutputSpec):
    pass

class MELODICRPT(fsl.MELODIC):
    input_spec = MELODICInputSpecRPT
    output_spec = MELODICOutputSpecRPT
    _out_report = None
    generate_report = False

    def __init__(self, generate_report=False, **kwargs):
        super(MELODICRPT, self).__init__(**kwargs)
        self.generate_report = generate_report

    def _post_run_hook(self, runtime):
        # Run _post_run_hook of super class
        runtime = super(MELODICRPT, self)._post_run_hook(runtime)
        # leave early if there's nothing to do
        if not self.generate_report:
            return runtime

        NIWORKFLOWS_LOG.info('Generating report for MELODIC.')
        _melodic_dir = runtime.cwd
        if isdefined(self.inputs.out_dir):
            _melodic_dir = self.inputs.out_dir
        self._melodic_dir = os.path.abspath(_melodic_dir)

        self._out_report = self.inputs.out_report
        if not os.path.isabs(self._out_report):
            self._out_report = os.path.abspath(os.path.join(runtime.cwd,
                                                            self._out_report))

        mix = os.path.join(self._melodic_dir, "melodic_mix")
        if not os.path.exists(mix):
            NIWORKFLOWS_LOG.warning("MELODIC outputs not found, assuming it didn't converge.")
            self._out_report = self._out_report.replace('.svg', '.html')
            snippet = '
MELODIC did not converge, no output
'
            with open(self._out_report, 'w') as fobj:
                fobj.write(snippet)
            return runtime

        self._generate_report()
        return runtime

    def _list_outputs(self):
        try:
            outputs = super(MELODICRPT, self)._list_outputs()
        except NotImplementedError:
            outputs = {}
        if self._out_report is not None:
            outputs['out_report'] = self._out_report
        return outputs

    def _generate_report(self):
        from niworkflows.viz.utils import plot_melodic_components
        plot_melodic_components(melodic_dir=self._melodic_dir,
                                in_file=self.inputs.in_files[0],
                                tr=self.inputs.tr_sec,
                                out_file=self._out_report,
                                compress=self.inputs.compress_report,
                                report_mask=self.inputs.report_mask)

class ICA_AROMAInputSpecRPT(nrc.SVGReportCapableInputSpec,
                            fsl.aroma.ICA_AROMAInputSpec):
    out_report = File(
        'ica_aroma_reportlet.svg', usedefault=True, desc='Filename for the visual'
                                                         ' report generated '
                                                         'by Nipype.')
    report_mask = File(desc='Mask used to draw the outline on the reportlet. '
                            'If not set the mask will be derived from the data.')

class ICA_AROMAOutputSpecRPT(reporting.ReportCapableOutputSpec,
                             fsl.aroma.ICA_AROMAOutputSpec):
    pass

class ICA_AROMARPT(reporting.ReportCapableInterface, fsl.ICA_AROMA):
    input_spec = ICA_AROMAInputSpecRPT
    output_spec = ICA_AROMAOutputSpecRPT

    def _generate_report(self):
        from niworkflows.viz.utils import plot_melodic_components
        plot_melodic_components(melodic_dir=self.inputs.melodic_dir,
                                in_file=self.inputs.in_file,
                                out_file=self.inputs.out_report,
                                compress=self.inputs.compress_report,
                                report_mask=self.inputs.report_mask,
                                noise_components_file=self._noise_components_file
                                )

    def _post_run_hook(self, runtime):
        outputs = self.aggregate_outputs(runtime=runtime)
        self._noise_components_file = os.path.join(outputs.out_dir,
                                                   "classified_motion_ICs.txt")

        NIWORKFLOWS_LOG.info('Generating report for ICA AROMA')

        return super(ICA_AROMARPT, self)._post_run_hook(runtime)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
The Django Admin Generator is a project which can automatically generate
(scaffold) a Django Admin for you. By doing this it will introspect your
models and automatically generate an Admin with properties like:

 - `list_display` for all local fields
 - `list_filter` for foreign keys with few items
 - `raw_id_fields` for foreign keys with a lot of items
 - `search_fields` for name and `slug` fields
 - `prepopulated_fields` for `slug` fields
 - `date_hierarchy` for `created_at`, `updated_at` or `joined_at` fields

The original source and latest version can be found here:
https://github.com/WoLpH/django-admin-generator/
'''

import optparse
import re
import sys

from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import models
from django.db.models.loading import get_models

from django_extensions.compat import get_apps
from django_extensions.management.color import color_style
from django_extensions.management.utils import signalcommand

# Configurable constants
MAX_LINE_WIDTH = getattr(settings, 'MAX_LINE_WIDTH', 78)
INDENT_WIDTH = getattr(settings, 'INDENT_WIDTH', 4)
LIST_FILTER_THRESHOLD = getattr(settings, 'LIST_FILTER_THRESHOLD', 25)
RAW_ID_THRESHOLD = getattr(settings, 'RAW_ID_THRESHOLD', 100)

LIST_FILTER = getattr(settings, 'LIST_FILTER', (
    models.DateField,
    models.DateTimeField,
    models.ForeignKey,
    models.BooleanField,
))

SEARCH_FIELD_NAMES = getattr(settings, 'SEARCH_FIELD_NAMES', (
    'name',
    'slug',
))

DATE_HIERARCHY_NAMES = getattr(settings, 'DATE_HIERARCHY_NAMES', (
    'joined_at',
    'updated_at',
    'created_at',
))

PREPOPULATED_FIELD_NAMES = getattr(settings, 'PREPOPULATED_FIELD_NAMES', (
    'slug=name',
))

PRINT_IMPORTS = getattr(settings, 'PRINT_IMPORTS', '''# -*- coding: utf-8 -*-
from django.contrib import admin

from .models import %(models)s
''')

PRINT_ADMIN_CLASS = getattr(settings, 'PRINT_ADMIN_CLASS', '''

class %(name)sAdmin(admin.ModelAdmin):%(class_)s
admin.site.register(%(name)s, %(name)sAdmin)
''')

PRINT_ADMIN_PROPERTY = getattr(settings, 'PRINT_ADMIN_PROPERTY', '''
    %(key)s = %(value)s''')

class UnicodeMixin(object):
    """Mixin class to handle defining the proper __str__/__unicode__
    methods in Python 2 or 3."""

    if sys.version_info[0] >= 3:  # Python 3
        def __str__(self):
            return self.__unicode__()
    else:  # Python 2
        def __str__(self):
            return self.__unicode__().encode('utf8')

class AdminApp(UnicodeMixin):
    def __init__(self, app, model_res, **options):
        self.app = app
        self.model_res = model_res
        self.options = options

    def __iter__(self):
        for model in get_models(self.app):
            admin_model = AdminModel(model, **self.options)

            for model_re in self.model_res:
                if model_re.search(admin_model.name):
                    break
            else:
                if self.model_res:
                    continue

            yield admin_model

    def __unicode__(self):
        return ''.join(self._unicode_generator())

    def _unicode_generator(self):
        models_list = [admin_model.name for admin_model in self]
        yield PRINT_IMPORTS % dict(models=', '.join(models_list))

        admin_model_names = []
        for admin_model in self:
            yield PRINT_ADMIN_CLASS % dict(
                name=admin_model.name,
                class_=admin_model,
            )
            admin_model_names.append(admin_model.name)

    def __repr__(self):
        return '<%s[%s]>' % (
            self.__class__.__name__,
            self.app,
        )

class AdminModel(UnicodeMixin):
    PRINTABLE_PROPERTIES = (
        'list_display',
        'list_filter',
        'raw_id_fields',
        'search_fields',
        'prepopulated_fields',
        'date_hierarchy',
    )

    def __init__(self, model, raw_id_threshold=RAW_ID_THRESHOLD,
                 list_filter_threshold=LIST_FILTER_THRESHOLD,
                 search_field_names=SEARCH_FIELD_NAMES,
                 date_hierarchy_names=DATE_HIERARCHY_NAMES,
                 prepopulated_field_names=PREPOPULATED_FIELD_NAMES, **options):
        self.model = model
        self.list_display = []
        self.list_filter = []
        self.raw_id_fields = []
        self.search_fields = []
        self.prepopulated_fields = {}
        self.date_hierarchy = None
        self.search_field_names = search_field_names
        self.raw_id_threshold = raw_id_threshold
        self.list_filter_threshold = list_filter_threshold
        self.date_hierarchy_names = date_hierarchy_names
        self.prepopulated_field_names = prepopulated_field_names

    def __repr__(self):
        return '<%s[%s]>' % (
            self.__class__.__name__,
            self.name,
        )

    @property
    def name(self):
        return self.model.__name__

    def _process_many_to_many(self, meta):
        raw_id_threshold = self.raw_id_threshold
        for field in meta.local_many_to_many:
            related_objects = field.related.parent_model.objects.all()
            if(related_objects[:raw_id_threshold].count() < raw_id_threshold):
                yield field.name

    def _process_fields(self, meta):
        parent_fields = meta.parents.values()
        for field in meta.fields:
            name = self._process_field(field, parent_fields)
            if name:
                yield name

    def _process_foreign_key(self, field):
        raw_id_threshold = self.raw_id_threshold
        list_filter_threshold = self.list_filter_threshold
        max_count = max(list_filter_threshold, raw_id_threshold)
        related_count = field.related.parent_model.objects.all()
        related_count = related_count[:max_count].count()

        if related_count >= raw_id_threshold:
            self.raw_id_fields.append(field.name)

        elif related_count < list_filter_threshold:
            self.list_filter.append(field.name)

        else:  # pragma: no cover
            pass  # Do nothing :)

    def _process_field(self, field, parent_fields):
        if field in parent_fields:
            return

        self.list_display.append(field.name)
        if isinstance(field, LIST_FILTER):
            if isinstance(field, models.ForeignKey):
                self._process_foreign_key(field)
            else:
                self.list_filter.append(field.name)

        if field.name in self.search_field_names:
            self.search_fields.append(field.name)

        return field.name

    def __unicode__(self):
        return ''.join(self._unicode_generator())

    def _yield_value(self, key, value):
        if isinstance(value, (list, set, tuple)):
            return self._yield_tuple(key, tuple(value))
        elif isinstance(value, dict):
            return self._yield_dict(key, value)
        elif isinstance(value, str):
            return self._yield_string(key, value)
        else:  # pragma: no cover
            raise TypeError('%s is not supported in %r' % (type(value), value))

    def _yield_string(self, key, value, converter=repr):
        return PRINT_ADMIN_PROPERTY % dict(
            key=key,
            value=converter(value),
        )

    def _yield_dict(self, key, value):
        row_parts = []
        row = self._yield_string(key, value)
        if len(row) > MAX_LINE_WIDTH:
            row_parts.append(self._yield_string(key, '{', str))
            for k, v in value.items():
                row_parts.append('%s%r: %r' % (2 * INDENT_WIDTH * ' ', k, v))

            row_parts.append(INDENT_WIDTH * ' ' + '}')
            row = '\n'.join(row_parts)

        return row

    def _yield_tuple(self, key, value):
        row_parts = []
        row = self._yield_string(key, value)
        if len(row) > MAX_LINE_WIDTH:
            row_parts.append(self._yield_string(key, '(', str))
            for v in value:
                row_parts.append(2 * INDENT_WIDTH * ' ' + repr(v) + ',')

            row_parts.append(INDENT_WIDTH * ' ' + ')')
            row = '\n'.join(row_parts)

        return row

    def _unicode_generator(self):
        self._process()
        for key in self.PRINTABLE_PROPERTIES:
            value = getattr(self, key)
            if value:
                yield self._yield_value(key, value)

    def _process(self):
        meta = self.model._meta

        self.raw_id_fields += list(self._process_many_to_many(meta))
        field_names = list(self._process_fields(meta))

        for field_name in self.date_hierarchy_names[::-1]:
            if field_name in field_names and not self.date_hierarchy:
                self.date_hierarchy = field_name
                break

        for k in sorted(self.prepopulated_field_names):
            k, vs = k.split('=', 1)
            vs = vs.split(',')
            if k in field_names:
                incomplete = False
                for v in vs:
                    if v not in field_names:
                        incomplete = True
                        break

                if not incomplete:
                    self.prepopulated_fields[k] = vs

        self.processed = True

class Command(BaseCommand):
    help = '''Generate a `admin.py` file for the given app (models)'''
    option_list = BaseCommand.option_list + (
        optparse.make_option(
            '-s', '--search-field', action='append',
            default=SEARCH_FIELD_NAMES,
            help='Fields named like this will be added to `search_fields`'
            ' [default: %default]'),
        optparse.make_option(
            '-d', '--date-hierarchy', action='append',
            default=DATE_HIERARCHY_NAMES,
            help='A field named like this will be set as `date_hierarchy`'
            ' [default: %default]'),
        optparse.make_option(
            '-p', '--prepopulated-fields', action='append',
            default=PREPOPULATED_FIELD_NAMES,
            help='These fields will be prepopulated by the other field.'
            'The field names can be specified like `spam=eggA,eggB,eggC`'
            ' [default: %default]'),
        optparse.make_option(
            '-l', '--list-filter-threshold', type='int',
            default=LIST_FILTER_THRESHOLD, metavar='LIST_FILTER_THRESHOLD',
            help='If a foreign key has less than LIST_FILTER_THRESHOLD items '
            'it will be added to `list_filter` [default: %default]'),
        optparse.make_option(
            '-r', '--raw-id-threshold', type='int',
            default=RAW_ID_THRESHOLD, metavar='RAW_ID_THRESHOLD',
            help='If a foreign key has more than RAW_ID_THRESHOLD items '
            'it will be added to `list_filter` [default: %default]'),
    )
    can_import_settings = True

    @signalcommand
    def handle(self, *args, **kwargs):
        self.style = color_style()

        installed_apps = dict((a.__name__.rsplit('.', 1)[0], a) for a in get_apps())

        # Make sure we always have args
        if not args:
            args = [False]

        app = installed_apps.get(args[0])
        if not app:
            print(self.style.WARN('This command requires an existing app name as argument'))
            print(self.style.WARN('Available apps:'))
            for app in sorted(installed_apps):
                print(self.style.WARN('    %s' % app))
            sys.exit(1)

        model_res = []
        for arg in args[1:]:
            model_res.append(re.compile(arg, re.IGNORECASE))

        self.handle_app(app, model_res, **kwargs)

    def handle_app(self, app, model_res, **options):
        print(AdminApp(app, model_res, **options))
<commit_msg>Fixed issue with Django 1.8<commit_after># -*- coding: utf-8 -*-
'''
The Django Admin Generator is a project which can automatically generate
(scaffold) a Django Admin for you. By doing this it will introspect your
models and automatically generate an Admin with properties like:

 - `list_display` for all local fields
 - `list_filter` for foreign keys with few items
 - `raw_id_fields` for foreign keys with a lot of items
 - `search_fields` for name and `slug` fields
 - `prepopulated_fields` for `slug` fields
 - `date_hierarchy` for `created_at`, `updated_at` or `joined_at` fields

The original source and latest version can be found here:
https://github.com/WoLpH/django-admin-generator/
'''

import optparse
import re
import sys

from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import models
from django.db.models.loading import get_models

from django_extensions.compat import get_apps
from django_extensions.management.color import color_style
from django_extensions.management.utils import signalcommand

# Configurable constants
MAX_LINE_WIDTH = getattr(settings, 'MAX_LINE_WIDTH', 78)
INDENT_WIDTH = getattr(settings, 'INDENT_WIDTH', 4)
LIST_FILTER_THRESHOLD = getattr(settings, 'LIST_FILTER_THRESHOLD', 25)
RAW_ID_THRESHOLD = getattr(settings, 'RAW_ID_THRESHOLD', 100)

LIST_FILTER = getattr(settings, 'LIST_FILTER', (
    models.DateField,
    models.DateTimeField,
    models.ForeignKey,
    models.BooleanField,
))

SEARCH_FIELD_NAMES = getattr(settings, 'SEARCH_FIELD_NAMES', (
    'name',
    'slug',
))

DATE_HIERARCHY_NAMES = getattr(settings, 'DATE_HIERARCHY_NAMES', (
    'joined_at',
    'updated_at',
    'created_at',
))

PREPOPULATED_FIELD_NAMES = getattr(settings, 'PREPOPULATED_FIELD_NAMES', (
    'slug=name',
))

PRINT_IMPORTS = getattr(settings, 'PRINT_IMPORTS', '''# -*- coding: utf-8 -*-
from django.contrib import admin

from .models import %(models)s
''')

PRINT_ADMIN_CLASS = getattr(settings, 'PRINT_ADMIN_CLASS', '''

class %(name)sAdmin(admin.ModelAdmin):%(class_)s
admin.site.register(%(name)s, %(name)sAdmin)
''')

PRINT_ADMIN_PROPERTY = getattr(settings, 'PRINT_ADMIN_PROPERTY', '''
    %(key)s = %(value)s''')

class UnicodeMixin(object):
    """Mixin class to handle defining the proper __str__/__unicode__
    methods in Python 2 or 3."""

    if sys.version_info[0] >= 3:  # Python 3
        def __str__(self):
            return self.__unicode__()
    else:  # Python 2
        def __str__(self):
            return self.__unicode__().encode('utf8')

class AdminApp(UnicodeMixin):
    def __init__(self, app, model_res, **options):
        self.app = app
        self.model_res = model_res
        self.options = options

    def __iter__(self):
        for model in get_models(self.app):
            admin_model = AdminModel(model, **self.options)

            for model_re in self.model_res:
                if model_re.search(admin_model.name):
                    break
            else:
                if self.model_res:
                    continue

            yield admin_model

    def __unicode__(self):
        return ''.join(self._unicode_generator())

    def _unicode_generator(self):
        models_list = [admin_model.name for admin_model in self]
        yield PRINT_IMPORTS % dict(models=', '.join(models_list))

        admin_model_names = []
        for admin_model in self:
            yield PRINT_ADMIN_CLASS % dict(
                name=admin_model.name,
                class_=admin_model,
            )
            admin_model_names.append(admin_model.name)

    def __repr__(self):
        return '<%s[%s]>' % (
            self.__class__.__name__,
            self.app,
        )

class AdminModel(UnicodeMixin):
    PRINTABLE_PROPERTIES = (
        'list_display',
        'list_filter',
        'raw_id_fields',
        'search_fields',
        'prepopulated_fields',
        'date_hierarchy',
    )

    def __init__(self, model, raw_id_threshold=RAW_ID_THRESHOLD,
                 list_filter_threshold=LIST_FILTER_THRESHOLD,
                 search_field_names=SEARCH_FIELD_NAMES,
                 date_hierarchy_names=DATE_HIERARCHY_NAMES,
                 prepopulated_field_names=PREPOPULATED_FIELD_NAMES, **options):
        self.model = model
        self.list_display = []
        self.list_filter = []
        self.raw_id_fields = []
        self.search_fields = []
        self.prepopulated_fields = {}
        self.date_hierarchy = None
        self.search_field_names = search_field_names
        self.raw_id_threshold = raw_id_threshold
        self.list_filter_threshold = list_filter_threshold
        self.date_hierarchy_names = date_hierarchy_names
        self.prepopulated_field_names = prepopulated_field_names

    def __repr__(self):
        return '<%s[%s]>' % (
            self.__class__.__name__,
            self.name,
        )

    @property
    def name(self):
        return self.model.__name__

    def _process_many_to_many(self, meta):
        raw_id_threshold = self.raw_id_threshold
        for field in meta.local_many_to_many:
            related_model = getattr(field.related, 'related_model', field.related.model)
            related_objects = related_model.objects.all()
            if(related_objects[:raw_id_threshold].count() < raw_id_threshold):
                yield field.name

    def _process_fields(self, meta):
        parent_fields = meta.parents.values()
        for field in meta.fields:
            name = self._process_field(field, parent_fields)
            if name:
                yield name

    def _process_foreign_key(self, field):
        raw_id_threshold = self.raw_id_threshold
        list_filter_threshold = self.list_filter_threshold
        max_count = max(list_filter_threshold, raw_id_threshold)
        related_model = getattr(field.related, 'related_model', field.related.model)
        related_count = related_model.objects.all()
        related_count = related_count[:max_count].count()

        if related_count >= raw_id_threshold:
            self.raw_id_fields.append(field.name)

        elif related_count < list_filter_threshold:
            self.list_filter.append(field.name)

        else:  # pragma: no cover
            pass  # Do nothing :)

    def _process_field(self, field, parent_fields):
        if field in parent_fields:
            return

        self.list_display.append(field.name)
        if isinstance(field, LIST_FILTER):
            if isinstance(field, models.ForeignKey):
                self._process_foreign_key(field)
            else:
                self.list_filter.append(field.name)

        if field.name in self.search_field_names:
            self.search_fields.append(field.name)

        return field.name

    def __unicode__(self):
        return ''.join(self._unicode_generator())

    def _yield_value(self, key, value):
        if isinstance(value, (list, set, tuple)):
            return self._yield_tuple(key, tuple(value))
        elif isinstance(value, dict):
            return self._yield_dict(key, value)
        elif isinstance(value, str):
            return self._yield_string(key, value)
        else:  # pragma: no cover
            raise TypeError('%s is not supported in %r' % (type(value), value))

    def _yield_string(self, key, value, converter=repr):
        return PRINT_ADMIN_PROPERTY % dict(
            key=key,
            value=converter(value),
        )

    def _yield_dict(self, key, value):
        row_parts = []
        row = self._yield_string(key, value)
        if len(row) > MAX_LINE_WIDTH:
            row_parts.append(self._yield_string(key, '{', str))
            for k, v in value.items():
                row_parts.append('%s%r: %r' % (2 * INDENT_WIDTH * ' ', k, v))

            row_parts.append(INDENT_WIDTH * ' ' + '}')
            row = '\n'.join(row_parts)

        return row

    def _yield_tuple(self, key, value):
        row_parts = []
        row = self._yield_string(key, value)
        if len(row) > MAX_LINE_WIDTH:
            row_parts.append(self._yield_string(key, '(', str))
            for v in value:
                row_parts.append(2 * INDENT_WIDTH * ' ' + repr(v) + ',')

            row_parts.append(INDENT_WIDTH * ' ' + ')')
            row = '\n'.join(row_parts)

        return row

    def _unicode_generator(self):
        self._process()
        for key in self.PRINTABLE_PROPERTIES:
            value = getattr(self, key)
            if value:
                yield self._yield_value(key, value)

    def _process(self):
        meta = self.model._meta

        self.raw_id_fields += list(self._process_many_to_many(meta))
        field_names = list(self._process_fields(meta))

        for field_name in self.date_hierarchy_names[::-1]:
            if field_name in field_names and not self.date_hierarchy:
                self.date_hierarchy = field_name
                break

        for k in sorted(self.prepopulated_field_names):
            k, vs = k.split('=', 1)
            vs = vs.split(',')
            if k in field_names:
                incomplete = False
                for v in vs:
                    if v not in field_names:
                        incomplete = True
                        break

                if not incomplete:
                    self.prepopulated_fields[k] = vs

        self.processed = True

class Command(BaseCommand):
    help = '''Generate a `admin.py` file for the given app (models)'''
    option_list = BaseCommand.option_list + (
        optparse.make_option(
            '-s', '--search-field', action='append',
            default=SEARCH_FIELD_NAMES,
            help='Fields named like this will be added to `search_fields`'
            ' [default: %default]'),
        optparse.make_option(
            '-d', '--date-hierarchy', action='append',
            default=DATE_HIERARCHY_NAMES,
            help='A field named like this will be set as `date_hierarchy`'
            ' [default: %default]'),
        optparse.make_option(
            '-p', '--prepopulated-fields', action='append',
            default=PREPOPULATED_FIELD_NAMES,
            help='These fields will be prepopulated by the other field.'
            'The field names can be specified like `spam=eggA,eggB,eggC`'
            ' [default: %default]'),
        optparse.make_option(
            '-l', '--list-filter-threshold', type='int',
            default=LIST_FILTER_THRESHOLD, metavar='LIST_FILTER_THRESHOLD',
            help='If a foreign key has less than LIST_FILTER_THRESHOLD items '
            'it will be added to `list_filter` [default: %default]'),
        optparse.make_option(
            '-r', '--raw-id-threshold', type='int',
            default=RAW_ID_THRESHOLD, metavar='RAW_ID_THRESHOLD',
            help='If a foreign key has more than RAW_ID_THRESHOLD items '
            'it will be added to `list_filter` [default: %default]'),
    )
    can_import_settings = True

    @signalcommand
    def handle(self, *args, **kwargs):
        self.style = color_style()

        installed_apps = dict((a.__name__.rsplit('.', 1)[0], a) for a in get_apps())

        # Make sure we always have args
        if not args:
            args = [False]

        app = installed_apps.get(args[0])
        if not app:
            print(self.style.WARN('This command requires an existing app name as argument'))
            print(self.style.WARN('Available apps:'))
            for app in sorted(installed_apps):
                print(self.style.WARN('    %s' % app))
            sys.exit(1)

        model_res = []
        for arg in args[1:]:
            model_res.append(re.compile(arg, re.IGNORECASE))

        self.handle_app(app, model_res, **kwargs)

    def handle_app(self, app, model_res, **options):
        print(AdminApp(app, model_res, **options))
<|endoftext|>


<commit_before>import nibabel as nb
import numpy as np

import pytest
from ..images import update_header_fields, overwrite_header, dseg_label

def random_image():
    return nb.Nifti1Image(np.random.random((5, 5, 5, 5)), np.eye(4))

@pytest.mark.parametrize("fields", [
    {},
    {'intent_code': 0},
    {'intent_code': 0, 'sform_code': 4},
    {'sform_code': 3},
    # Changes to these fields have no effect
    {'scl_slope': 3., 'scl_inter': 3.},
    {'vox_offset': 20.},
])
@pytest.mark.parametrize("slope, inter", [
    (None, None),
    (1., 0.),
    (2., 2.)
])
def test_update_header_fields(tmp_path, fields, slope, inter):
    fname = str(tmp_path / 'test_file.nii')

    # Generate file
    init_img = random_image()
    init_img.header.set_slope_inter(slope, inter)
    init_img.to_filename(fname)

    # Reference load
    pre_img = nb.load(fname, mmap=False)
    pre_data = pre_img.get_fdata()

    update_header_fields(fname, **fields)

    # Post-rewrite load
    post_img = nb.load(fname)

    # Data should be identical
    assert np.array_equal(pre_data, post_img.get_fdata())

@pytest.mark.parametrize("fields", [
    {'datatype': 2},
])
@pytest.mark.parametrize("slope, inter", [
    (None, None),
    (2., 2.)
])
def test_update_header_fields_exceptions(tmp_path, fields, slope, inter):
    fname = str(tmp_path / 'test_file.nii')

    # Generate file
    img = random_image()
    img.header.set_slope_inter(slope, inter)
    img.to_filename(fname)

    with pytest.raises(ValueError):
        update_header_fields(fname, **fields)

def test_overwrite_header_reject_mmap(tmp_path):
    fname = str(tmp_path / 'test_file.nii')

    random_image().to_filename(fname)

    img = nb.load(fname, mmap=True)
    with pytest.raises(ValueError):
        overwrite_header(img, fname)

def test_dseg_label(tmp_path):
    fname = str(tmp_path / 'test_file.nii.gz')

    data = np.dstack((
        np.zeros((20, 20), dtype='int16'),
        np.ones((20, 20), dtype='int16'),
        np.ones((20, 20), dtype='int16') * 2,
        np.ones((20, 20), dtype='int16') * 3,
    ))
    nb.Nifti1Image(data, np.eye(4), None).to_filename(fname)

    new_im = nb.load(dseg_label(fname, label=2))
    assert np.all((data == 2).astype('int16') == np.int16(new_im.dataobj))
<commit_msg>Update niworkflows/utils/tests/test_images.py<commit_after>import nibabel as nb
import numpy as np

import pytest
from ..images import update_header_fields, overwrite_header, dseg_label

def random_image():
    return nb.Nifti1Image(np.random.random((5, 5, 5, 5)), np.eye(4))

@pytest.mark.parametrize("fields", [
    {},
    {'intent_code': 0},
    {'intent_code': 0, 'sform_code': 4},
    {'sform_code': 3},
    # Changes to these fields have no effect
    {'scl_slope': 3., 'scl_inter': 3.},
    {'vox_offset': 20.},
])
@pytest.mark.parametrize("slope, inter", [
    (None, None),
    (1., 0.),
    (2., 2.)
])
def test_update_header_fields(tmp_path, fields, slope, inter):
    fname = str(tmp_path / 'test_file.nii')

    # Generate file
    init_img = random_image()
    init_img.header.set_slope_inter(slope, inter)
    init_img.to_filename(fname)

    # Reference load
    pre_img = nb.load(fname, mmap=False)
    pre_data = pre_img.get_fdata()

    update_header_fields(fname, **fields)

    # Post-rewrite load
    post_img = nb.load(fname)

    # Data should be identical
    assert np.array_equal(pre_data, post_img.get_fdata())

@pytest.mark.parametrize("fields", [
    {'datatype': 2},
])
@pytest.mark.parametrize("slope, inter", [
    (None, None),
    (2., 2.)
])
def test_update_header_fields_exceptions(tmp_path, fields, slope, inter):
    fname = str(tmp_path / 'test_file.nii')

    # Generate file
    img = random_image()
    img.header.set_slope_inter(slope, inter)
    img.to_filename(fname)

    with pytest.raises(ValueError):
        update_header_fields(fname, **fields)

def test_overwrite_header_reject_mmap(tmp_path):
    fname = str(tmp_path / 'test_file.nii')

    random_image().to_filename(fname)

    img = nb.load(fname, mmap=True)
    with pytest.raises(ValueError):
        overwrite_header(img, fname)

def test_dseg_label(tmp_path):
    fname = str(tmp_path / 'test_file.nii.gz')

    data = np.dstack((
        np.zeros((20, 20), dtype='int16'),
        np.ones((20, 20), dtype='int16'),
        np.ones((20, 20), dtype='int16') * 2,
        np.ones((20, 20), dtype='int16') * 3,
    ))
    nb.Nifti1Image(data, np.eye(4), None).to_filename(fname)

    new_im = nb.load(dseg_label(fname, label=2, newpath=tmp_path))
    assert np.all((data == 2).astype('int16') == np.int16(new_im.dataobj))
<|endoftext|>


<commit_before>from django.conf import settings
from django.middleware.cache import UpdateCacheMiddleware, FetchFromCacheMiddleware
from django.utils.cache import patch_response_headers, get_cache_key, get_max_age, learn_cache_key

from bettercache.utils import set_post_pre_check_headers

class BetterUpdateCacheMiddleware(UpdateCacheMiddleware):

    def set_headers(self, request, response):
        """ Hook for setting additional headers """
        set_post_pre_check_headers(response)
        response['Edge-control'] = "cache-maxage=%s" %settings.BETTERCACHE_EDGE_CONTROL

    def process_response(self, request, response):
        """ Sets the cache, if needed.
            Copied from django so headers are only updated when appropriate.
        """
        if not self._should_update_cache(request, response):
            # We don't need to update the cache, just return.
            return response
        if not response.status_code == 200:
            return response
        # Try to get the timeout from the "max-age" section of the "Cache-
        # Control" header before reverting to using the default cache_timeout
        # length.
        timeout = get_max_age(response)
        if timeout == None:
            timeout = self.cache_timeout
        elif timeout == 0:
            # max-age was set to 0, don't bother caching.
            return response
        patch_response_headers(response, timeout)
        # This is the only difference from django for now
        self.set_headers(request, response)
        if timeout:
            cache_key = learn_cache_key(request, response, timeout, settings.CACHE_MIDDLEWARE_KEY_PREFIX, cache=self.cache)
            if hasattr(response, 'render') and callable(response.render):
                response.add_post_render_callback(
                    lambda r: self.cache.set(cache_key, r, timeout)
                )
            else:
                self.cache.set(cache_key, response, timeout)
        return response

class BetterFetchFromCacheMiddleware(FetchFromCacheMiddleware):
    def process_request(self, request):
        """
        Checks whether the page is already cached and returns the cached
        version if available.
        """
        if not request.method in ('GET', 'HEAD'):
            request._cache_update_cache = False
            return None # Don't bother checking the cache.

        # try and get the cached GET response
        cache_key = get_cache_key(request, settings.CACHE_MIDDLEWARE_KEY_PREFIX, 'GET', cache=self.cache)
        if cache_key is None:
            request._cache_update_cache = True
            return None # No cache information available, need to rebuild.
        response = self.cache.get(cache_key, None)
        # if it wasn't found and we are looking for a HEAD, try looking just for that
        if response is None and request.method == 'HEAD':
            cache_key = get_cache_key(request, self.key_prefix, 'HEAD', cache=self.cache)
            response = self.cache.get(cache_key, None)

        if response is None:
            request._cache_update_cache = True
            return None # No cache information available, need to rebuild.
        # hit, return cached response
        request._cache_update_cache = False
        return response

<commit_msg>[CMSSAI-548] fetch middleware should respect no-cache header<commit_after>from django.conf import settings
from django.middleware.cache import UpdateCacheMiddleware, FetchFromCacheMiddleware
from django.utils.cache import patch_response_headers, get_cache_key, get_max_age, learn_cache_key

from bettercache.utils import set_post_pre_check_headers

class BetterUpdateCacheMiddleware(UpdateCacheMiddleware):

    def set_headers(self, request, response):
        """ Hook for setting additional headers """
        set_post_pre_check_headers(response)
        response['Edge-control'] = "cache-maxage=%s" %settings.BETTERCACHE_EDGE_CONTROL

    def process_response(self, request, response):
        """ Sets the cache, if needed.
            Copied from django so headers are only updated when appropriate.
        """
        if not self._should_update_cache(request, response):
            # We don't need to update the cache, just return.
            return response
        if not response.status_code == 200:
            return response
        # Try to get the timeout from the "max-age" section of the "Cache-
        # Control" header before reverting to using the default cache_timeout
        # length.
        timeout = get_max_age(response)
        if timeout == None:
            timeout = self.cache_timeout
        elif timeout == 0:
            # max-age was set to 0, don't bother caching.
            return response
        patch_response_headers(response, timeout)
        # This is the only difference from django for now
        self.set_headers(request, response)
        if timeout:
            cache_key = learn_cache_key(request, response, timeout, settings.CACHE_MIDDLEWARE_KEY_PREFIX, cache=self.cache)
            if hasattr(response, 'render') and callable(response.render):
                response.add_post_render_callback(
                    lambda r: self.cache.set(cache_key, r, timeout)
                )
            else:
                self.cache.set(cache_key, response, timeout)
        return response

class BetterFetchFromCacheMiddleware(FetchFromCacheMiddleware):
    def process_request(self, request):
        """
        Checks whether the page is already cached and returns the cached
        version if available.
        """
        if not request.method in ('GET', 'HEAD'):
            request._cache_update_cache = False
            return None # Don't bother checking the cache.

        try:
            if 'no-cache' in request.META['HTTP_CACHE_CONTROL']:
                request._cache_update_cache = False
                return None
        except KeyError:
            pass

        # try and get the cached GET response
        cache_key = get_cache_key(request, settings.CACHE_MIDDLEWARE_KEY_PREFIX, 'GET', cache=self.cache)
        if cache_key is None:
            request._cache_update_cache = True
            return None # No cache information available, need to rebuild.
        response = self.cache.get(cache_key, None)
        # if it wasn't found and we are looking for a HEAD, try looking just for that
        if response is None and request.method == 'HEAD':
            cache_key = get_cache_key(request, self.key_prefix, 'HEAD', cache=self.cache)
            response = self.cache.get(cache_key, None)

        if response is None:
            request._cache_update_cache = True
            return None # No cache information available, need to rebuild.
        # hit, return cached response
        request._cache_update_cache = False
        return response

<|endoftext|>


<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import pprint
import six
import time

from nixnet import constants
from nixnet import nx

pp = pprint.PrettyPrinter(indent=4)

def main():
    database_name = ':memory:'
    cluster_name = ''
    list = ''
    interface1 = 'CAN1'
    interface2 = 'CAN2'
    input_mode = constants.CreateSessionMode.FRAME_IN_STREAM
    output_mode = constants.CreateSessionMode.FRAME_OUT_STREAM

    with nx.Session(database_name, cluster_name, list, interface1, input_mode) as input_session:
        print('Input session created successfully.')
        with nx.Session(database_name, cluster_name, list, interface2, output_mode) as output_session:
            print('Output session created successfully.')

            print('Are you using a terminated cable? Enter Y or N')
            terminated_cable = six.input()
            if terminated_cable.lower() == "y":
                output_session.intf_can_term = contants.CanTerm.OFF
            else:
                input_session.intf_can_term = contants.CanTerm.ON
                output_session.intf_can_term = contants.CanTerm.ON

            input_session.intf_baud_rate = 125000
            output_session.intf_baud_rate = 125000
            print('Properties set successfully.')

            # Start the input session manually to make sure that the first
            # frame value sent before the initial read will be received.
            input_session.start(constants.StartStopScope.NORMAL)
            print('Input session started manually.')

            try:
                id = int(six.input('Enter identifier: '))
            except ValueError:
                print('Not a number')

            try:
                payloadList = [int(x) for x in six.input('Enter payload: ').split()]
            except ValueError:
                print('Not a number')

            payload = bytearray(payloadList)
            number_of_bytes_for_frames = len(payload)
            extended = False

            print('The same values should be received. Press q to quit')
            i = 0
            while true:
                for byte in payload:
                    byte = byte + i
                with CANFrame(identifier, extended, constants.FRAME_TYPE_CAN_DATA, payload) as frame:
                    output_session.write_frame(list(frame), number_of_bytes_for_frames, 10)
                    print('Sent frame with ID %s payload: %s' % (id, list(payload)))

                # Wait 100 ms and then read the received values.
                # They should be the same as the ones sent.
                time.sleep(0.1)

                count = constants.READ_ALL_AVAILABLE
                timeout = constants.TIMEOUT_NONE
                frames = input_session.read_frame(count, timeout)
                for frame in frames:
                    print('Received frame: ')
                    pp.pprint(frame)

                i += 1
                if max(payload) + i > 0xFF:
                    i = 0

                inp = six.input()
                if inp == 'q':
                    break

            print('Data acquisition stopped.')

if __name__ == '__main__':
    main()
<commit_msg>Fixing compilation errors<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import pprint
import six
import time

from nixnet import constants
from nixnet import nx
from nixnet import types

pp = pprint.PrettyPrinter(indent=4)

def main():
    database_name = ':memory:'
    cluster_name = ''
    list = ''
    interface1 = 'CAN1'
    interface2 = 'CAN2'
    input_mode = constants.CreateSessionMode.FRAME_IN_STREAM
    output_mode = constants.CreateSessionMode.FRAME_OUT_STREAM

    with nx.Session(database_name, cluster_name, list, interface1, input_mode) as input_session:
        print('Input session created successfully.')
        with nx.Session(database_name, cluster_name, list, interface2, output_mode) as output_session:
            print('Output session created successfully.')

            print('Are you using a terminated cable? Enter Y or N')
            terminated_cable = six.input()
            if terminated_cable.lower() == "y":
                output_session.intf_can_term = constants.CanTerm.OFF
            else:
                input_session.intf_can_term = constants.CanTerm.ON
                output_session.intf_can_term = constants.CanTerm.ON

            input_session.intf_baud_rate = 125000
            output_session.intf_baud_rate = 125000
            print('Properties set successfully.')

            # Start the input session manually to make sure that the first
            # frame value sent before the initial read will be received.
            input_session.start(constants.StartStopScope.NORMAL)
            print('Input session started manually.')

            try:
                id = int(six.input('Enter identifier: '))
            except ValueError:
                print('Not a number')

            try:
                payload_list = [int(x) for x in six.input('Enter payload: ').split()]
            except ValueError:
                print('Not a number')

            payload = bytearray(payload_list)
            number_of_bytes_for_frames = len(payload)
            extended = False

            print('The same values should be received. Press q to quit')
            i = 0
            while True:
                for byte in payload:
                    byte = byte + i
                with types.CANFrame(id, extended, constants.FRAME_TYPE_CAN_DATA, payload) as frame:
                    output_session.write_frame(list(frame), number_of_bytes_for_frames, 10)
                    print('Sent frame with ID %s payload: %s' % (id, list(payload)))

                # Wait 100 ms and then read the received values.
                # They should be the same as the ones sent.
                time.sleep(0.1)

                count = constants.READ_ALL_AVAILABLE
                timeout = constants.TIMEOUT_NONE
                frames = input_session.read_frame(count, timeout)
                for frame in frames:
                    print('Received frame: ')
                    pp.pprint(frame)

                i += 1
                if max(payload) + i > 0xFF:
                    i = 0

                inp = six.input()
                if inp == 'q':
                    break

            print('Data acquisition stopped.')

if __name__ == '__main__':
    main()
<|endoftext|>