コード例 #1
0
def convert(input_file, outputfile, recenter=False, nrn_order=False, single_point_soma=False):
    '''Run the appropriate converter

    Args:
        input_file(str): path to input file
        outputfile(str): path to output file
        recenter(bool): whether to recenter the morphology based on the
        center of gravity of the soma
        nrn_order(bool): whether to traverse the neuron in the NEURON fashion
        single_point_soma(bool):For SWC only
    '''
    kwargs = {}
    if nrn_order:
        kwargs['options'] = Option.nrn_order

    neuron = Morphology(input_file, **kwargs)

    output_ext = Path(outputfile).suffix

    if single_point_soma and output_ext.lower() != '.swc':
        raise Exception('Single point soma is only applicable for swc output')

    if output_ext.lower() not in ('.swc', '.asc', '.h5', ):
        raise Exception('Output file format should be one swc, asc or h5')

    output_ext = output_ext[1:]  # Remove the dot

    try:
        converter = {MorphologyVersion.MORPHOLOGY_VERSION_SWC_1: from_swc,
                     MorphologyVersion.MORPHOLOGY_VERSION_ASC_1: from_h5_or_asc,
                     MorphologyVersion.MORPHOLOGY_VERSION_H5_1: from_h5_or_asc,
                     MorphologyVersion.MORPHOLOGY_VERSION_H5_1_1: from_h5_or_asc,
                     MorphologyVersion.MORPHOLOGY_VERSION_H5_2: from_h5_or_asc
                     }[neuron.version]
    except KeyError as e:
        raise Exception(
            'No converter for morphology type: {}'.format(neuron.version)) from e

    logger.info('Original soma type: %s', neuron.soma_type)
    new = converter(neuron, output_ext)

    if single_point_soma:
        soma_to_single_point(new.soma)

    if recenter:
        transform.translate(new, -1 * new.soma.center)

    new.write(outputfile)

    try:
        # pylint: disable=import-outside-toplevel
        from morph_tool.neuron_surface import get_NEURON_surface
        logger.info('Soma surface as computed by NEURON:\n'
                    'before conversion: %s\n'
                    'after conversion: %s',
                    get_NEURON_surface(input_file),
                    get_NEURON_surface(outputfile))
    except:  # noqa pylint: disable=bare-except
        logger.info('Final NEURON soma surface check was skipped probably because BluePyOpt'
                    ' or NEURON is not installed')
コード例 #2
0
def _get_surface(filename, extension):
    if extension == 'h5':
        raise NotImplementedError
    if extension == 'asc':
        return get_NEURON_surface(filename)
    if extension == 'swc':
        return get_NEURON_surface(filename)
    raise NotImplementedError
コード例 #3
0
def soma_surface(input_file, quiet):
    '''Get soma surface as computed by NEURON'''
    # pylint: disable=import-outside-toplevel
    from morph_tool.neuron_surface import get_NEURON_surface
    if quiet:
        L.setLevel(logging.WARNING)

    try:
        click.echo('Soma surface: {}'.format(get_NEURON_surface(input_file)))
    except ImportError as e:
        raise ImportError('''the NEURON module is not installed.
        - if you are on the cluster, you can try: module load nix/hpc/neuron
        - otherwise, get it here: https://github.com/neuronsimulator/nrn and compile it...'''
                          ) from e
コード例 #4
0
def convert(input_file,
            outputfile,
            recenter=False,
            nrn_order=False,
            single_point_soma=False,
            sanitize=False):
    """Run the appropriate converter.

    Args:
        input_file(str): path to input file
        outputfile(str): path to output file
        recenter(bool): whether to recenter the morphology based on the
        center of gravity of the soma
        nrn_order(bool): whether to traverse the neuron in the NEURON fashion
        single_point_soma(bool): For SWC only
        sanitize(bool): whether to sanitize the morphology
    """
    kwargs = {}
    if nrn_order:
        kwargs['options'] = Option.nrn_order

    neuron = Morphology(input_file, **kwargs)
    if sanitize:
        neuron.remove_unifurcations()

    output_ext = Path(outputfile).suffix

    if single_point_soma and output_ext.lower() != '.swc':
        raise Exception('Single point soma is only applicable for swc output')

    if output_ext.lower() not in (
            '.swc',
            '.asc',
            '.h5',
    ):
        raise Exception('Output file format should be one swc, asc or h5')

    output_ext = output_ext[1:]  # Remove the dot

    try:
        converter = {
            'swc': from_swc,
            'asc': from_h5_or_asc,
            'h5': from_h5_or_asc,
        }[neuron.version[0]]
    except KeyError as e:
        raise Exception(
            f'No converter for morphology type: {neuron.version}') from e

    L.info('Original soma type: %s', neuron.soma_type)
    new = converter(neuron, output_ext)

    if single_point_soma:
        soma_to_single_point(new.soma)

    if recenter:
        transform.translate(new, -1 * new.soma.center)

    try:
        new.write(outputfile)
    except WriterError as e:
        raise MorphToolException('Use `sanitize` option for converting') from e

    try:
        # pylint: disable=import-outside-toplevel
        from morph_tool.neuron_surface import get_NEURON_surface
        L.info(
            'Soma surface as computed by NEURON:\n'
            'before conversion: %s\n'
            'after conversion: %s', get_NEURON_surface(input_file),
            get_NEURON_surface(outputfile))
    except:  # noqa pylint: disable=bare-except
        L.info(
            'Final NEURON soma surface check was skipped probably because BluePyOpt'
            ' or NEURON is not installed')