text:
"""Generate html report from MNE database
"""

# Authors: Alex Gramfort <alexandre.gramfort@telecom-paristech.fr>
#          Mainak Jas <mainak@neuro.hut.fi>
#
# License: BSD (3-clause)

import os
import os.path as op
import fnmatch
import re
import codecs
import time
from glob import glob
import warnings
import base64

import numpy as np

from . import read_evokeds, read_events, Covariance
from .io import Raw, read_info
from .utils import _TempDir, logger, verbose, get_subjects_dir
from .viz import plot_events, plot_trans, plot_cov
from .viz._3d import _plot_mri_contours
from .forward import read_forward_solution
from .epochs import read_epochs
from .minimum_norm import read_inverse_operator
from .parallel import parallel_func, check_n_jobs

from .externals.decorator import decorator
from .externals.tempita import HTMLTemplate, Template
from .externals.six import BytesIO
from .externals.six import moves

tempdir = _TempDir()
temp_fname = op.join(tempdir, 'test')

VALID_EXTENSIONS = ['raw.fif', 'raw.fif.gz', 'sss.fif', 'sss.fif.gz',
                    '-eve.fif', '-eve.fif.gz', '-cov.fif', '-cov.fif.gz',
                    '-trans.fif', '-trans.fif.gz', '-fwd.fif', '-fwd.fif.gz',
                    '-epo.fif', '-epo.fif.gz', '-inv.fif', '-inv.fif.gz',
                    '-ave.fif', '-ave.fif.gz', 'T1.mgz']
SECTION_ORDER = ['raw', 'events', 'epochs', 'evoked', 'covariance', 'trans',
                 'mri', 'forward', 'inverse']

###############################################################################
# PLOTTING FUNCTIONS

@decorator
def _check_report_mode(function, *args, **kwargs):
    """Check whether to actually render or not.

    Parameters
    ----------
    function : function
        Function to be decorated by setting the verbosity level.

    Returns
    -------
    dec : function
        The decorated function
    """

    if 'MNE_REPORT_TESTING' not in os.environ:
        return function(*args, **kwargs)
    else:
        return ''

@_check_report_mode
def _fig_to_img(function=None, fig=None, close_fig=True, **kwargs):
    """Wrapper function to plot figure and
       for fig <-> binary image.
    """
    import matplotlib.pyplot as plt

    if function is not None:
        plt.close('all')
        fig = function(**kwargs)

    output = BytesIO()
    fig.savefig(output, format='png', bbox_inches='tight')
    if close_fig is True:
        plt.close(fig)

    return base64.b64encode(output.getvalue()).decode('ascii')

@_check_report_mode
def _fig_to_mrislice(function, orig_size, sl, **kwargs):
    import matplotlib.pyplot as plt
    from PIL import Image

    plt.close('all')
    fig = _plot_mri_contours(**kwargs)
    temp_sl_fname = temp_fname + str(sl)

    fig_size = fig.get_size_inches()
    w, h = orig_size[0], orig_size[1]
    w2, h2 = fig_size[0], fig_size[1]
    fig.set_size_inches([(w2 / w) * w, (w2 / w) * h])
    a = fig.gca()
    a.set_xticks([]), a.set_yticks([])
    plt.xlim(0, h), plt.ylim(w, 0)
    fig.savefig(temp_sl_fname, bbox_inches='tight',
                pad_inches=0, format='png')
    Image.open(temp_sl_fname).resize((w, h)).save(temp_sl_fname,
                                                  format='png')
    output = BytesIO()
    Image.open(temp_sl_fname).save(output, format='png')
    return base64.b64encode(output.getvalue()).decode('ascii')

@_check_report_mode
def _iterate_trans_views(function, **kwargs):
    """Auxiliary function to iterate over views in trans fig.
    """
    from PIL import Image
    import matplotlib.pyplot as plt
    import mayavi

    fig = function(**kwargs)

    if isinstance(fig, mayavi.core.scene.Scene):

        views = [(90, 90), (0, 90), (0, -90)]
        fig2, axes = plt.subplots(1, len(views))
        for view, ax in zip(views, axes):
            mayavi.mlab.view(view[0], view[1])
            # XXX: save_bmp / save_png / ...
            fig.scene.save_bmp(temp_fname)
            im = Image.open(temp_fname)
            ax.imshow(im)
            ax.axis('off')

        img = _fig_to_img(fig=fig2)
        mayavi.mlab.close(all=True)

        return img
    else:
        return None

###############################################################################
# TOC FUNCTIONS

def _is_bad_fname(fname):
    """Auxiliary function for identifying bad file naming patterns
       and highlighting them in red in the TOC.
    """
    if not fname.endswith(tuple(VALID_EXTENSIONS + ['bem', 'custom'])):
        return 'red'
    else:
        return ''

def _get_toc_property(fname):
    """Auxiliary function to assign class names to TOC
       list elements to allow toggling with buttons.
    """
    if fname.endswith(('-eve.fif', '-eve.fif.gz')):
        div_klass = 'events'
        tooltip = fname
        text = op.basename(fname)
    elif fname.endswith(('-ave.fif', '-ave.fif.gz')):
        div_klass = 'evoked'
        tooltip = fname
        text = op.basename(fname)
    elif fname.endswith(('-cov.fif', '-cov.fif.gz')):
        div_klass = 'covariance'
        tooltip = fname
        text = op.basename(fname)
    elif fname.endswith(('raw.fif', 'raw.fif.gz',
                         'sss.fif', 'sss.fif.gz')):
        div_klass = 'raw'
        tooltip = fname
        text = op.basename(fname)
    elif fname.endswith(('-trans.fif', '-trans.fif.gz')):
        div_klass = 'trans'
        tooltip = fname
        text = op.basename(fname)
    elif fname.endswith(('-fwd.fif', '-fwd.fif.gz')):
        div_klass = 'forward'
        tooltip = fname
        text = op.basename(fname)
    elif fname.endswith(('-inv.fif', '-inv.fif.gz')):
        div_klass = 'inverse'
        tooltip = fname
        text = op.basename(fname)
    elif fname.endswith(('-epo.fif', '-epo.fif.gz')):
        div_klass = 'epochs'
        tooltip = fname
        text = op.basename(fname)
    elif fname.endswith(('.nii', '.nii.gz', '.mgh', '.mgz')):
        div_klass = 'mri'
        tooltip = 'MRI'
        text = 'MRI'
    elif fname.endswith(('bem')):
        div_klass = 'mri'
        tooltip = 'MRI'
        text = 'MRI'
    else:
        div_klass = fname.split('-#-')[1]
        tooltip = fname.split('-#-')[0]
        text = fname.split('-#-')[0]

    return div_klass, tooltip, text

def _iterate_files(report, fnames, info, sfreq):
    """Auxiliary function to parallel process in batch mode.
    """
    htmls, report_fnames, report_sectionlabels = [], [], []
    for fname in fnames:
        logger.info("Rendering : %s"
                    % op.join('...' + report.data_path[-20:],
                              fname))
        try:
            if fname.endswith(('raw.fif', 'raw.fif.gz',
                               'sss.fif', 'sss.fif.gz')):
                html = report._render_raw(fname)
                report_fname = fname
                report_sectionlabel = 'raw'
            elif fname.endswith(('-fwd.fif', '-fwd.fif.gz')):
                html = report._render_forward(fname)
                report_fname = fname
                report_sectionlabel = 'forward'
            elif fname.endswith(('-inv.fif', '-inv.fif.gz')):
                html = report._render_inverse(fname)
                report_fname = fname
                report_sectionlabel = 'inverse'
            elif fname.endswith(('-ave.fif', '-ave.fif.gz')):
                html = report._render_evoked(fname)
                report_fname = fname
                report_sectionlabel = 'evoked'
            elif fname.endswith(('-eve.fif', '-eve.fif.gz')):
                html = report._render_eve(fname, sfreq)
                report_fname = fname
                report_sectionlabel = 'events'
            elif fname.endswith(('-epo.fif', '-epo.fif.gz')):
                html = report._render_epochs(fname)
                report_fname = fname
                report_sectionlabel = 'epochs'
            elif (fname.endswith(('-cov.fif', '-cov.fif.gz'))
                  and report.info_fname is not None):
                html = report._render_cov(fname, info)
                report_fname = fname
                report_sectionlabel = 'covariance'
            elif (fname.endswith(('-trans.fif', '-trans.fif.gz'))
                  and report.info_fname is not None and report.subjects_dir
                  is not None and report.subject is not None):
                html = report._render_trans(fname, report.data_path, info,
                                            report.subject,
                                            report.subjects_dir)
                report_fname = fname
                report_sectionlabel = 'trans'
            else:
                html = None
                report_fname = None
                report_sectionlabel = None
        except Exception as e:
            logger.info(e)
            html = None
            report_fname = None
            report_sectionlabel = None
        htmls.append(html)
        report_fnames.append(report_fname)
        report_sectionlabels.append(report_sectionlabel)

    return htmls, report_fnames, report_sectionlabels

###############################################################################
# IMAGE FUNCTIONS

def _build_image(data, cmap='gray'):
    """Build an image encoded in base64.
    """

    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

    figsize = data.shape[::-1]
    if figsize[0] == 1:
        figsize = tuple(figsize[1:])
        data = data[:, :, 0]
    fig = Figure(figsize=figsize, dpi=1.0, frameon=False)
    FigureCanvas(fig)
    cmap = getattr(plt.cm, cmap, plt.cm.gray)
    fig.figimage(data, cmap=cmap)
    output = BytesIO()
    fig.savefig(output, dpi=1.0, format='png')
    return base64.b64encode(output.getvalue()).decode('ascii')

def _iterate_sagittal_slices(array, limits=None):
    """Iterate sagittal slice.
    """
    shape = array.shape[0]
    for ind in xrange(shape):
        if limits and ind not in limits:
            continue
        yield ind, array[ind, :, :]

def _iterate_axial_slices(array, limits=None):
    """Iterate axial slice.
    """
    shape = array.shape[1]
    for ind in xrange(shape):
        if limits and ind not in limits:
            continue
        yield ind, array[:, ind, :]

def _iterate_coronal_slices(array, limits=None):
    """Iterate coronal slice.
    """
    shape = array.shape[2]
    for ind in xrange(shape):
        if limits and ind not in limits:
            continue
        yield ind, np.flipud(np.rot90(array[:, :, ind]))

def _iterate_mri_slices(name, ind, global_id, slides_klass, data, cmap):
    """Auxiliary function for parallel processing of mri slices.
    """
    img_klass = 'slideimg-%s' % name

    caption = u'Slice %s %s' % (name, ind)
    slice_id = '%s-%s-%s' % (name, global_id, ind)
    div_klass = 'span12 %s' % slides_klass
    img = _build_image(data, cmap=cmap)
    first = True if ind == 0 else False
    html = _build_html_image(img, slice_id, div_klass,
                             img_klass, caption,
                             first)
    return ind, html

def _iterate_bem_slices(name, global_id, slides_klass, orig_size,
                        mri_fname, surf_fnames, orientation, sl):
    """Auxiliary function for parallel processing of bem slices.
    """

    img_klass = 'slideimg-%s' % name
    logger.info('Rendering BEM contours : orientation = %s, '
                'slice = %d' % (orientation, sl))
    caption = u'Slice %s %s' % (name, sl)
    slice_id = '%s-%s-%s' % (name, global_id, sl)
    div_klass = 'span12 %s' % slides_klass

    kwargs = dict(mri_fname=mri_fname, surf_fnames=surf_fnames,
                  orientation=orientation, slices=[sl],
                  show=False)
    img = _fig_to_mrislice(function=_plot_mri_contours,
                           orig_size=orig_size, sl=sl, **kwargs)
    first = True if sl == 0 else False
    return _build_html_image(img, slice_id, div_klass,
                             img_klass, caption,
                             first)

###############################################################################
# HTML functions

def _build_html_image(img, id, div_klass, img_klass, caption=None, show=True):
    """Build a html image from a slice array.
    """
    html = []
    add_style = u'' if show else u'style="display: none"'
    html.append(u'
' % (div_klass, id, add_style))
    html.append(u'
')
    html.append(u''
                % (img_klass, img))
    html.append(u'
')
    if caption:
        html.append(u'
%s
' % caption)
    html.append(u'
')
    return u'\n'.join(html)

slider_template = HTMLTemplate(u"""

""")

def _build_html_slider(slices_range, slides_klass, slider_id):
    """Build an html slider for a given slices range and a slices klass.
    """
    startvalue = (slices_range[0] + slices_range[-1]) // 2 + 1
    return slider_template.substitute(slider_id=slider_id,
                                      klass=slides_klass,
                                      minvalue=slices_range[0],
                                      maxvalue=slices_range[-1],
                                      startvalue=startvalue)

###############################################################################
# HTML scan renderer

header_template = Template(u"""
<!DOCTYPE html>

{{include}}

                >

{{title}}

        {{for section in sections}}

    {{section if section != 'mri' else 'MRI'}}

        {{endfor}}

""")

footer_template = HTMLTemplate(u"""

        © Copyright 2012-2013, MNE Developers.
      Created on {{date}}.
      Powered by MNE.

""")

image_template = Template(u"""

{{default interactive = False}}
{{default width = 50}}
{{default id = False}}

{{if caption}}

{{caption}}

{{endif}}

{{if not interactive}}

{{else}}
    {{interactive}}
{{endif}}

""")

repr_template = Template(u"""

{{caption}}

{{repr}}

""")

toc_list = Template(u"""

    {{if id}}

    {{endif}}
 {{text}}
{{if id}}{{endif}}

""")

class Report(object):
    """Object for rendering HTML

    Parameters
    ----------
    info_fname : str
        Name of the file containing the info dictionary.
    subjects_dir : str | None
        Path to the SUBJECTS_DIR. If None, the path is obtained by using
        the environment variable SUBJECTS_DIR.
    subject : str | None
        Subject name.
    title : str
        Title of the report.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).
    """

    def __init__(self, info_fname=None, subjects_dir=None, subject=None,
                 title=None, verbose=None):

        self.info_fname = info_fname
        self.subjects_dir = subjects_dir
        self.subject = subject
        self.title = title
        self.verbose = verbose

        self.initial_id = 0
        self.html = []
        self.fnames = []  # List of file names rendered
        self.sections = []  # List of sections
        self._sectionlabels = []  # Section labels
        self._sectionvars = {}  # Section variable names in js

        self._init_render(verbose=self.verbose)  # Initialize the renderer

    def _get_id(self):
        """Get id of plot.
        """
        self.initial_id += 1
        return self.initial_id

    def add_section(self, figs, captions, section='custom'):
        """Append custom user-defined figures.

        Parameters
        ----------
        figs : list of matplotlib.pyplot.Figure
            A list of figures to be included in the report.
        captions : list of str
            A list of captions to the figures.
        section : str
            Name of the section. If section already exists, the figures
            will be appended to the end of the section
        """

        if not isinstance(figs, (list, tuple)):
            figs = [figs]
        if not isinstance(captions, (list, tuple)):
            captions = [captions]
        if not len(figs) == len(captions):
            raise ValueError('Captions and figures must have the same length.')
        if section not in self.sections:
            self.sections.append(section)
            self._sectionvars[section] = _clean_varnames(section)

        for fig, caption in zip(figs, captions):
            sectionvar = self._sectionvars[section]
            global_id = self._get_id()
            div_klass = self._sectionvars[section]
            img_klass = self._sectionvars[section]
            img = _fig_to_img(fig=fig)
            html = image_template.substitute(img=img, id=global_id,
                                             div_klass=div_klass,
                                             img_klass=img_klass,
                                             caption=caption,
                                             show=True)
            self.fnames.append('%s-#-%s-#-custom' % (caption, sectionvar))
            self._sectionlabels.append(sectionvar)
            self.html.append(html)

    ###########################################################################
    # HTML rendering
    def _render_one_axe(self, slices_iter, name, global_id=None, cmap='gray',
                        n_jobs=1):
        """Render one axe of the array.
        """
        global_id = global_id or name
        html = []
        slices, slices_range = [], []
        html.append(u'
')
        slides_klass = '%s-%s' % (name, global_id)

        parallel, p_fun, _ = parallel_func(_iterate_mri_slices, n_jobs)
        r = parallel(p_fun(name, ind, global_id, slides_klass, data, cmap)
                     for ind, data in slices_iter)
        slices_range, slices = zip(*r)

        # Render the slider
        slider_id = 'select-%s-%s' % (name, global_id)
        html.append(u'

' % slider_id)
        html.append(u'
')
        # Render the slices
        html.append(u'\n'.join(slices))
        html.append(u'
')
        html.append(_build_html_slider(slices_range, slides_klass, slider_id))
        html.append(u'
')
        return '\n'.join(html)

    ###########################################################################
    # global rendering functions
    @verbose
    def _init_render(self, verbose=None):
        """Initialize the renderer.
        """

        inc_fnames = ['jquery-1.10.2.min.js', 'jquery-ui.min.js',
                      'bootstrap.min.js', 'jquery-ui.min.css',
                      'bootstrap.min.css']

        include = list()
        for inc_fname in inc_fnames:
            logger.info('Embedding : %s' % inc_fname)
            f = open(op.join(op.dirname(__file__), 'html', inc_fname),
                     'r')
            if inc_fname.endswith('.js'):
                include.append(u'')
            elif inc_fname.endswith('.css'):
                include.append(u'')
            f.close()

        self.include = ''.join(include)

    @verbose
    def parse_folder(self, data_path, pattern='*.fif', n_jobs=1, verbose=None):
        """Renders all the files in the folder.

        Parameters
        ----------
        data_path : str
            Path to the folder containing data whose HTML report will be
            created.
        pattern : str
            Filename pattern to include in the report. e.g., -ave.fif will
            include all evoked files.
        n_jobs : int
          Number of jobs to eggie in parallel.
        verbose : bool, str, int, or None
            If not None, override default verbose level (see mne.verbose).
        """
        n_jobs = check_n_jobs(n_jobs)
        self.data_path = data_path

        if self.title is None:
            self.title = 'MNE Report for ...%s' % self.data_path[-20:]

        fnames = _recursive_search(self.data_path, pattern)

        if self.info_fname is not None:
            info = read_info(self.info_fname)
            sfreq = info['sfreq']
        else:
            warnings.warn('`info_fname` not provided. Cannot render'
                          '-cov.fif(.gz) and -trans.fif(.gz) files.')
            info, sfreq = None, None

        # render plots in parallel
        parallel, p_fun, _ = parallel_func(_iterate_files, n_jobs)
        r = parallel(p_fun(self, fname, info, sfreq) for fname in
                     np.array_split(fnames, n_jobs))
        htmls, report_fnames, report_sectionlabels = zip(*r)

        # combine results from n_jobs discarding plots not rendered
        self.html = [html for html in sum(htmls, []) if html is not None]
        self.fnames = [fname for fname in sum(report_fnames, []) if
                       fname is not None]
        self._sectionlabels = [slabel for slabel in
                               sum(report_sectionlabels, [])
                               if slabel is not None]

        # find unique section labels
        self.sections = sorted(set(self._sectionlabels))
        self._sectionvars = dict(zip(self.sections, self.sections))

        # render mri
        if self.subjects_dir is not None and self.subject is not None:
            self.html.append(self._render_bem(subject=self.subject,
                                              subjects_dir=
                                              self.subjects_dir,
                                              n_jobs=n_jobs))
            self.fnames.append('bem')
            self._sectionlabels.append('mri')
        else:
            warnings.warn('`subjects_dir` and `subject` not provided.'
                          ' Cannot render MRI and -trans.fif(.gz) files.')

    def save(self, fname=None, open_browser=True, overwrite=False):
        """Save html report and open it in browser.

        Parameters
        ----------
        fname : str
            File name of the report.
        open_browser : bool
            Open html browser after saving if True.
        overwrite : bool
            If True, overwrite report if it already exists.
        """

        if fname is None:
            if not hasattr(self, 'data_path'):
                self.data_path = op.dirname(__file__)
                warnings.warn('`data_path` not provided. Using %s instead'
                              % self.data_path)
            fname = op.realpath(op.join(self.data_path, 'report.html'))
        else:
            fname = op.realpath(fname)

        self._render_toc(verbose=self.verbose)

        html = footer_template.substitute(date=time.strftime("%B %d, %Y"))
        self.html.append(html)

        if not overwrite and op.isfile(fname):
            msg = ('Report already exists at location %s. '
                   'Overwrite it (y/[n])? '
                   % fname)
            answer = moves.input(msg)
            if answer.lower() == 'y':
                overwrite = True

        if overwrite or not op.isfile(fname):
            logger.info('Saving report to location %s' % fname)
            fobj = codecs.open(fname, 'w', 'utf-8')
            fobj.write(_fix_global_ids(u''.join(self.html)))
            fobj.close()

            # remove header, TOC and footer to allow more saves
            self.html.pop(0)
            self.html.pop(0)
            self.html.pop()

        if open_browser:
            import webbrowser
            webbrowser.open_new_tab('file://' + fname)

        return fname

    @verbose
    def _render_toc(self, verbose=None):
        """Render the Table of Contents.
        """

        logger.info('Rendering : Table of Contents')

        html_toc = u'
'
        html_toc += u'

CONTENTS
'

        global_id = 1

        # Reorder self.sections to reflect natural ordering
        sections = list(set(self.sections) & set(SECTION_ORDER))
        custom = [section for section in self.sections if section
                  not in SECTION_ORDER]
        order = [sections.index(section) for section in SECTION_ORDER if
                 section in sections]
        self.sections = np.array(sections)[order].tolist() + custom

        # Sort by section
        html, fnames, sectionlabels = [], [], []
        for section in self.sections:
            logger.info('%s' % section)
            for sectionlabel, this_html, fname in (zip(self._sectionlabels,
                                                   self.html, self.fnames)):
                if self._sectionvars[section] == sectionlabel:
                    html.append(this_html)
                    fnames.append(fname)
                    sectionlabels.append(sectionlabel)
                    logger.info('\t... %s' % fname[-20:])
                    color = _is_bad_fname(fname)
                    div_klass, tooltip, text = _get_toc_property(fname)

                    # loop through conditions for evoked
                    if fname.endswith(('-ave.fif', '-ave.fif.gz')):
                       # XXX: remove redundant read_evokeds
                        evokeds = read_evokeds(fname, verbose=False)

                        html_toc += toc_list.substitute(div_klass=div_klass,
                                                        id=None, tooltip=fname,
                                                        color='#428bca',
                                                        text=
                                                        os.path.basename(fname)
                                                        )

                        html_toc += u'

'
                        for ev in evokeds:
                            html_toc += toc_list.substitute(div_klass=
                                                            div_klass,
                                                            id=global_id,
                                                            tooltip=fname,
                                                            color=color,
                                                            text=ev.comment)
                            global_id += 1
                        html_toc += u'

'

                    elif fname.endswith(tuple(VALID_EXTENSIONS +
                                        ['bem', 'custom'])):
                        html_toc += toc_list.substitute(div_klass=div_klass,
                                                        id=global_id,
                                                        tooltip=tooltip,
                                                        color=color,
                                                        text=text)
                        global_id += 1

        html_toc += u'\n

'
        html_toc += u'
'

        # The sorted html (according to section)
        self.html = html
        self.fnames = fnames
        self._sectionlabels = sectionlabels

        html_header = header_template.substitute(title=self.title,
                                                 include=self.include,
                                                 sections=self.sections,
                                                 sectionvars=self._sectionvars)
        self.html.insert(0, html_header)  # Insert header at position 0
        self.html.insert(1, html_toc)  # insert TOC

    def _render_array(self, array, global_id=None, cmap='gray',
                      limits=None, n_jobs=1):
        """Render mri without bem contours.
        """
        html = []
        html.append(u'
')
        # Axial
        limits = limits or {}
        axial_limit = limits.get('axial')
        axial_slices_gen = _iterate_axial_slices(array, axial_limit)
        html.append(
            self._render_one_axe(axial_slices_gen, 'axial', global_id, cmap,
                                 n_jobs=n_jobs))
        # Sagittal
        sagittal_limit = limits.get('sagittal')
        sagittal_slices_gen = _iterate_sagittal_slices(array, sagittal_limit)
        html.append(self._render_one_axe(sagittal_slices_gen, 'sagittal',
                    global_id, cmap, n_jobs=n_jobs))
        html.append(u'
')
        html.append(u'
')
        # Coronal
        coronal_limit = limits.get('coronal')
        coronal_slices_gen = _iterate_coronal_slices(array, coronal_limit)
        html.append(
            self._render_one_axe(coronal_slices_gen, 'coronal',
                                 global_id, cmap, n_jobs=n_jobs))
        # Close section
        html.append(u'
')
        return '\n'.join(html)

    def _render_one_bem_axe(self, mri_fname, surf_fnames, global_id,
                            shape, orientation='coronal', n_jobs=1):
        """Render one axe of bem contours.
        """

        orientation_name2axis = dict(sagittal=0, axial=1, coronal=2)
        orientation_axis = orientation_name2axis[orientation]
        n_slices = shape[orientation_axis]
        orig_size = np.roll(shape, orientation_axis)[[1, 2]]

        name = orientation
        html, img = [], []
        slices, slices_range = [], []
        html.append(u'
')
        slides_klass = '%s-%s' % (name, global_id)

        slices_range = range(0, n_slices, 2)

        parallel, p_fun, _ = parallel_func(_iterate_bem_slices, n_jobs)
        slices = parallel(p_fun(name, global_id, slides_klass, orig_size,
                          mri_fname, surf_fnames, orientation, sl)
                          for sl in slices_range)

        # Render the slider
        slider_id = 'select-%s-%s' % (name, global_id)
        html.append(u'

' % slider_id)
        html.append(u'
')
        # Render the slices
        html.append(u'\n'.join(slices))
        html.append(u'
')
        html.append(_build_html_slider(slices_range, slides_klass, slider_id))
        html.append(u'
')

        return '\n'.join(html)

    def _render_image(self, image, cmap='gray', n_jobs=1):
        """Render one slice of mri without bem.
        """
        import nibabel as nib

        global_id = self._get_id()

        if 'mri' not in self.sections:
            self.sections.append('mri')
            self._sectionvars['mri'] = 'mri'

        nim = nib.load(image)
        data = nim.get_data()
        shape = data.shape
        limits = {'sagittal': range(0, shape[0], 2),
                  'axial': range(0, shape[1], 2),
                  'coronal': range(0, shape[2], 2)}
        name = op.basename(image)
        html = u'
\n' % global_id
        html += u'
%s
\n' % name
        html += self._render_array(data, global_id=global_id,
                                   cmap=cmap, limits=limits,
                                   n_jobs=n_jobs)
        html += u'
\n'
        return html

    def _render_raw(self, raw_fname):
        """Render raw.
        """
        global_id = self._get_id()
        div_klass = 'raw'
        caption = u'Raw : %s' % raw_fname

        raw = Raw(raw_fname)

        repr_raw = re.sub('>', '', re.sub('<', '', repr(raw)))
        repr_info = re.sub('\\n', '\\n
',
                           re.sub('>', '',
                                  re.sub('<', '',
                                         repr(raw.info))))

        repr_html = repr_raw + '%s
%s' % (repr_raw, repr_info)

        html = repr_template.substitute(div_klass=div_klass,
                                        id=global_id,
                                        caption=caption,
                                        repr=repr_html)
        return html

    def _render_forward(self, fwd_fname):
        """Render forward.
        """
        div_klass = 'forward'
        caption = u'Forward: %s' % fwd_fname
        fwd = read_forward_solution(fwd_fname)
        repr_fwd = re.sub('>', '', re.sub('<', '', repr(fwd)))
        global_id = self._get_id()
        html = repr_template.substitute(div_klass=div_klass,
                                        id=global_id,
                                        caption=caption,
                                        repr=repr_fwd)
        return html

    def _render_inverse(self, inv_fname):
        """Render inverse.
        """
        div_klass = 'inverse'
        caption = u'Inverse: %s' % inv_fname
        inv = read_inverse_operator(inv_fname)
        repr_inv = re.sub('>', '', re.sub('<', '', repr(inv)))
        global_id = self._get_id()
        html = repr_template.substitute(div_klass=div_klass,
                                        id=global_id,
                                        caption=caption,
                                        repr=repr_inv)
        return html

    def _render_evoked(self, evoked_fname, figsize=None):
        """Render evoked.
        """
        evokeds = read_evokeds(evoked_fname, verbose=False)

        html = []
        for ev in evokeds:
            global_id = self._get_id()

            kwargs = dict(show=False)
            img = _fig_to_img(function=ev.plot, **kwargs)

            caption = u'Evoked : %s (%s)' % (evoked_fname, ev.comment)
            div_klass = 'evoked'
            img_klass = 'evoked'
            show = True
            html.append(image_template.substitute(img=img, id=global_id,
                                                  div_klass=div_klass,
                                                  img_klass=img_klass,
                                                  caption=caption,
                                                  show=show))

            for ch_type in ['eeg', 'grad', 'mag']:
                kwargs = dict(ch_type=ch_type, show=False)
                img = _fig_to_img(function=ev.plot_topomap, **kwargs)
                caption = u'Topomap (ch_type = %s)' % ch_type
                html.append(image_template.substitute(img=img,
                                                      div_klass=div_klass,
                                                      img_klass=img_klass,
                                                      caption=caption,
                                                      show=show))

        return '\n'.join(html)

    def _render_eve(self, eve_fname, sfreq=None):
        """Render events.
        """
        global_id = self._get_id()
        events = read_events(eve_fname)

        kwargs = dict(events=events, sfreq=sfreq, show=False)
        img = _fig_to_img(function=plot_events, **kwargs)

        caption = 'Events : ' + eve_fname
        div_klass = 'events'
        img_klass = 'events'
        show = True

        html = image_template.substitute(img=img, id=global_id,
                                         div_klass=div_klass,
                                         img_klass=img_klass,
                                         caption=caption,
                                         show=show)
        return html

    def _render_epochs(self, epo_fname):
        """Render epochs.
        """
        global_id = self._get_id()

        epochs = read_epochs(epo_fname)
        kwargs = dict(subject=self.subject, show=False, return_fig=True)
        img = _fig_to_img(function=epochs.plot_drop_log, **kwargs)
        caption = 'Epochs : ' + epo_fname
        div_klass = 'epochs'
        img_klass = 'epochs'
        show = True
        html = image_template.substitute(img=img, id=global_id,
                                         div_klass=div_klass,
                                         img_klass=img_klass,
                                         caption=caption,
                                         show=show)
        return html

    def _render_cov(self, cov_fname, info_fname):
        """Render cov.
        """
        global_id = self._get_id()
        cov = Covariance(cov_fname)
        fig, _ = plot_cov(cov, info_fname, show=False)

        img = _fig_to_img(fig=fig)
        caption = 'Covariance : ' + cov_fname
        div_klass = 'covariance'
        img_klass = 'covariance'
        show = True
        html = image_template.substitute(img=img, id=global_id,
                                         div_klass=div_klass,
                                         img_klass=img_klass,
                                         caption=caption,
                                         show=show)
        return html

    def _render_trans(self, trans_fname, path, info, subject,
                      subjects_dir):
        """Render trans.
        """
        kwargs = dict(info=info, trans_fname=trans_fname, subject=subject,
                      subjects_dir=subjects_dir)
        img = _iterate_trans_views(function=plot_trans, **kwargs)

        if img is not None:

            global_id = self._get_id()

            caption = 'Trans : ' + trans_fname
            div_klass = 'trans'
            img_klass = 'trans'
            show = True
            html = image_template.substitute(img=img, id=global_id,
                                             div_klass=div_klass,
                                             img_klass=img_klass,
                                             caption=caption,
                                             width=75,
                                             show=show)
            return html

    def _render_bem(self, subject, subjects_dir, n_jobs=1):
        """Render mri+bem.
        """
        import nibabel as nib

        subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)

        # Get the MRI filename
        mri_fname = op.join(subjects_dir, subject, 'mri', 'T1.mgz')
        if not op.isfile(mri_fname):
            warnings.warn('MRI file "%s" does not exist' % mri_fname)

        # Get the BEM surface filenames
        bem_path = op.join(subjects_dir, subject, 'bem')

        if not op.isdir(bem_path):
            warnings.warn('Subject bem directory "%s" does not exist' %
                          bem_path)
            return self._render_image(mri_fname, cmap='gray', n_jobs=n_jobs)

        surf_fnames = []
        for surf_name in ['*inner_skull', '*outer_skull', '*outer_skin']:
            surf_fname = glob(op.join(bem_path, surf_name + '.surf'))
            if len(surf_fname) > 0:
                surf_fname = surf_fname[0]
            else:
                warnings.warn('No surface found for %s.' % surf_name)
                return self._render_image(mri_fname, cmap='gray')
            surf_fnames.append(surf_fname)

        # XXX : find a better way to get max range of slices
        nim = nib.load(mri_fname)
        data = nim.get_data()
        shape = data.shape
        del data  # free up memory

        html = []

        global_id = self._get_id()

        if 'mri' not in self.sections:
            self.sections.append('mri')
            self._sectionvars['mri'] = 'mri'

        name, caption = 'BEM', 'BEM contours'

        html += u'
\n' % global_id
        html += u'
%s
\n' % name
        html += u'
'
        html += self._render_one_bem_axe(mri_fname, surf_fnames, global_id,
                                         shape, orientation='axial',
                                         n_jobs=n_jobs)
        html += self._render_one_bem_axe(mri_fname, surf_fnames, global_id,
                                         shape, orientation='sagittal',
                                         n_jobs=n_jobs)
        html += u'

'
        html += self._render_one_bem_axe(mri_fname, surf_fnames, global_id,
                                         shape, orientation='coronal',
                                         n_jobs=n_jobs)
        html += u'
'
        html += u'
\n'
        return ''.join(html)

def _clean_varnames(s):

    # Remove invalid characters
    s = re.sub('[^0-9a-zA-Z_]', '', s)

    # Remove leading characters until we find a letter or underscore
    s = re.sub('^[^a-zA-Z_]+', '', s)

    return s

def _recursive_search(path, pattern):
    """Auxiliary function for recursive_search of the directory.
    """
    filtered_files = list()
    for dirpath, dirnames, files in os.walk(path):
        for f in fnmatch.filter(files, pattern):
            # only the following file types are supported
            # this ensures equitable distribution of jobs
            if f.endswith(tuple(VALID_EXTENSIONS)):
                filtered_files.append(op.realpath(op.join(dirpath, f)))

    return filtered_files

def _fix_global_ids(html):
    """Auxiliary function for fixing the global_ids after reordering in
       _render_toc().
    """
    html = re.sub('id="\d+"', 'id="###"', html)
    global_id = 1
    while len(re.findall('id="###"', html)) > 0:
        html = re.sub('id="###"', 'id="%s"' % global_id, html, count=1)
        global_id += 1
    return html

max_stars_repo_name: jaeilepp/eggie

max_issues_repo_name: jaeilepp/eggie

max_forks_repo_name: jaeilepp/eggie


text:
# UniSlicer class.
# This slicer simply returns the indexes of all data points. No slicing done at all.

import numpy as np
from functools import wraps

from .baseSlicer import BaseSlicer

__all__ = ['UniSlicer']

class UniSlicer(BaseSlicer):
    """UniSlicer."""
    def __init__(self, verbose=True, badval=-666):
        """Instantiate unislicer. """
        super(UniSlicer, self).__init__(verbose=verbose, badval=badval)
        self.nslice = 1
        self.shape = self.nslice
        self.slicePoints['sid'] = np.array([0,], int)
        self.plotFuncs = []

    def setupSlicer(self, simData, maps=None):
        """Use simData to set indexes to return."""
        self._runMaps(maps)
        simDataCol = simData.dtype.names[0]
        self.indices = np.ones(len(simData[simDataCol]),  dtype='bool')
        @wraps(self._sliceSimData)
        def _sliceSimData(islice):
            """Return all indexes in simData. """
            idxs = self.indices
            return {'idxs':idxs,
                    'slicePoint':{'sid':islice}}
        setattr(self, '_sliceSimData', _sliceSimData)

    def __eq__(self, otherSlicer):
        """Evaluate if slicers are equivalent."""
        if isinstance(otherSlicer, UniSlicer):
            return True
        else:
            return False

ext: py

max_stars_repo_name: RileyWClarke/flarubin

max_issues_repo_name: RileyWClarke/flarubin

max_forks_repo_name: RileyWClarke/flarubin

reason: import numpy


text:
import pandas as pd
import numpy as np
from .meta import pyrolite_datafolder
from .text import titlecase
from collections import ChainMap, defaultdict
from .log import Handle

logger = Handle(__name__)

__data__ = pyrolite_datafolder(subfolder="timescale") / "geotimescale_202003.csv"
__colors__ = pyrolite_datafolder(subfolder="timescale") / "timecolors.csv"

def listify(df, axis=1):
    """
    Consdense text information across columns into a single list.

    Parameters
    ----------
    df : :class:`pandas.DataFrame`
        Dataframe (or slice of dataframe) to condense along axis.
    axis : :class:`int`
        Axis to condense along.
    """
    return df.copy(deep=True).apply(list, axis=axis)

def age_name(
    agenamelist, prefixes=["Lower", "Middle", "Upper"], suffixes=["Stage", "Series"]
):
    """
    Condenses an agename list to a specific agename, given a subset of
    ambiguous_names.

    Parameters
    ----------
    agenamelist : :class:`list`
        List of name components (i.e. :code:`[Eon, Era, Period, Epoch]`)
    prefixes : :class:`list`
        Name components which occur prior to the higher order classification
        (e.g. :code:`"Upper Triassic"`).
    suffixes : :class:`list`
        Name components which occur after the higher order classification
        (e.g. :code:`"Cambrian Series 2"`).
    """
    ambiguous_names = prefixes + suffixes
    ambig_vars = [s.lower().strip() for s in ambiguous_names]
    nameguess = agenamelist[-1]
    # Process e.g. Stage 1 => Stage
    nn_nameguess = "".join([i for i in nameguess if not i.isdigit()]).strip()

    # check if the name guess corresponds to any of the ambiguous names
    hit = [
        ambiguous_names[ix]
        for ix, vars in enumerate(ambig_vars)
        if nn_nameguess.lower().strip() in vars
    ][0:1]

    if hit:
        indexstart = len(agenamelist) - 1
        outname = [agenamelist[indexstart]]
        out_index_previous = 0
        ambiguous_name = True
        while ambiguous_name:
            hitphrase = hit[0]
            indexstart -= 1
            nextup = agenamelist[indexstart]
            if hitphrase in prefixes:
                # insert the higher order component after the previous one
                outname.insert(out_index_previous + 1, nextup)
                out_index_previous += 1
            else:
                # insert the higher order component before the previous one
                outname.insert(out_index_previous - 1, nextup)
                out_index_previous -= 1

            _nn_nextupguess = "".join([i for i in nextup if not i.isdigit()]).strip()
            hit = [
                ambiguous_names[ix]
                for ix, vars in enumerate(ambig_vars)
                if _nn_nextupguess.lower().strip() in vars
            ][0:1]
            if not hit:
                ambiguous_name = False
        return " ".join(outname)
    else:
        return nameguess

def import_colors(filename=__colors__, delim="/"):
    """
    Import a list of timescale names with associated colors.
    """
    c = pd.read_csv(filename).dropna(how="all")
    if delim is not None:  # and ("RGB" in c.columns):
        c["RGB"] = c["RGB"].apply(
            lambda x: tuple(
                [float(i) / 255.0 for i in x.split(delim)] + [1.0]
            )  # add alpha
        )
    return {name: rgb for name, rgb in c.values}

def timescale_reference_frame(
    filename=__data__, info_cols=["Start", "End", "Aliases"], color_info=None
):
    """
    Rearrange the text-based timescale dataframe. Utility function for
    timescale class.

    Parameters
    ----------
    filename : :class:`str` | :class:`pathlib.Path`
        File from which to generate the timescale information.
    info_cols : :class:`list`
        List of columns beyond hierarchial group labels (e.g. Eon, Era..).

    Returns
    -------
    :class:`pandas.DataFrame`
        Dataframe containing timescale information.
    """

    df = pd.read_csv(filename)
    df[["Start", "End"]] = df.loc[:, ["Start", "End"]].apply(
        pd.to_numeric, errors="coerce"
    )
    _df = df.copy(deep=True)
    grps = [i for i in _df.columns if not i in info_cols]
    condensed = _df.loc[:, [i for i in _df.columns if not i in info_cols]].fillna(
        value=""
    )
    _df["Level"] = condensed.apply(
        lambda x: grps[[ix for ix, v in enumerate(x) if v][-1]], axis=1
    )
    condensed = listify(condensed).apply(lambda x: [i for i in x if i])
    _df["Name"] = condensed.apply(age_name)
    _df["Ident"] = condensed.apply("-".join)
    _df["MeanAge"] = _df.apply(lambda x: (x.Start + x.End) / 2, axis=1)
    _df["Unc"] = _df.apply(lambda x: np.abs((x.Start - x.End)) / 2, axis=1)

    # Aliases
    _df.Aliases = _df.Aliases.apply(lambda x: [] if pd.isnull(x) else x.split(";"))
    _df.Aliases = _df.apply(lambda x: [x.Name, x.Ident] + x.Aliases, axis=1)
    _df.Aliases = _df.Aliases.apply(lambda x: [i.lower().strip() for i in x])

    colors = color_info or import_colors()
    _df["Color"] = _df.Name.apply(lambda x: colors.get(x, None))
    col_order = (
        ["Ident", "Name", "Level", "Start", "End", "MeanAge", "Unc"]
        + grps
        + ["Aliases", "Color"]
    )

    return _df.loc[:, col_order]

class Timescale(object):
    def __init__(self, filename=None):
        """
        Geological Timescale class to provide time-focused utility functions.

        Parameters
        -----------
        filename : :class:`str` | :class:`pathlib.Path`
            Path to the timescale data file.

        Attributes
        ----------
        data : :class:`pandas.DataFrame`
            Timescale dataframe.
        levels : :class:`list`
            Hierarchial levels within the timescale.
        """
        if filename is None:
            self.data = timescale_reference_frame()
        else:
            self.data = timescale_reference_frame(filename)
        self.levels = [i for i in self.data.Level.unique() if not pd.isnull(i)]
        self.levels = [i for i in self.data.columns if i in self.levels]

        def getnan():
            return np.nan, np.nan

        self.locate = defaultdict(getnan)
        self.build()

    def build(self):
        """
        Build the timescale from data within file.
        """
        for ix, g in enumerate(self.levels):
            others = self.levels[ix + 1 :]
            fltr = (
                self.data.loc[:, others].isnull().all(axis=1)
                & ~self.data.loc[:, g].isnull()
            )
            setattr(self, g + "s", self.data.loc[fltr, :])

        dicts = self.data.apply(
            lambda x: {a: (x.Start, x.End) for a in x.Aliases}, axis=1
        )
        # should check that the keys are unique across all of these
        self.locate.update(dict(ChainMap(*dicts)))
        self.data = self.data.set_index("Ident")

    def text2age(self, entry, nulls=[None, "None", "none", np.nan, "NaN"]):
        """
        Converts a text-based age to the corresponding age range (in Ma).

        String-based entries return (max_age, min_age). Collection-based entries
        return a list of tuples.

        Parameters
        ------------
        entry : :class:`str`
            String name, or series of string names, for geological age range.

        Returns
        -------
        :class:`tuple` | :class:`list` (:class:`tuple`)
            Tuple or list of tuples.
        """
        try:
            entry = np.float(entry)
            return (entry, entry)
        except ValueError:
            return self.locate[entry.lower().strip()]

    def named_age(self, age, level="Specific", **kwargs):
        """
        Converts a numeric age (in Ma) to named age at a specific level.

        Parameters
        ----------
        age : :class:`float`
            Numeric age in Ma.
        level : :class:`str`, :code:`{'Eon', 'Era', 'Period', 'Superepoch', 'Epoch', 'Age', 'Specific'}`
            Level of specificity.

        Returns
        -------
        :class:`str`
            String representation for the entry.
        """

        level = titlecase(level)
        wthn_rng = lambda x: (age <= x.Start) & (age >= x.End)
        relevant = self.data.loc[self.data.apply(wthn_rng, axis=1).values, :]
        if level == "Specific":  # take the rightmost grouping
            relevant = relevant.loc[:, self.levels]
            counts = (~pd.isnull(relevant)).count(axis=1)
            if sum(counts == counts.max()) > 1:
                idx_rel_row = counts.index[
                    max([ix for (ix, r) in enumerate(counts) if r == counts[0]])
                ]
            else:
                idx_rel_row = counts.idxmax()
            rel_row = relevant.loc[idx_rel_row, :]
            return age_name(rel_row[~pd.isnull(rel_row)], **kwargs)
        else:
            unique_values = relevant.loc[:, level].unique()
            return unique_values[~pd.isnull(unique_values)][0]

max_stars_repo_name: bomtuckle/pyrolite

max_issues_repo_name: bomtuckle/pyrolite

max_forks_repo_name: bomtuckle/pyrolite


text:
#!/usr/bin/env python
import slippy.patch
import numpy as np
import unittest
import matplotlib.pyplot as plt

class Test(unittest.TestCase):
  def test_patch1(self):
    strike = 0.0
    dip = 0.0
    length = 10.0
    width = 5.0
    pos = [0.0,0.0,0.0]
    pos_patch = [0.0,1.0,0.0]
    P = slippy.patch.Patch(pos,length,width,strike,dip,pos_patch=pos_patch)
    corners_patch = np.array([[0.0,0.0,0.0],
                              [1.0,0.0,0.0],
                              [1.0,1.0,0.0],
                              [0.0,1.0,0.0]])
    corners_data = P.patch_to_user(corners_patch)
    true_corners_data = np.array([[5.0,0.0,0.0],
                                  [5.0,10.0,0.0],
                                  [0.0,10.0,0.0],
                                  [0.0,0.0,0.0]])
    self.assertTrue(np.all(np.isclose(corners_data,true_corners_data)))

max_stars_repo_name: kmaterna/SlipPy

max_issues_repo_name: kmaterna/SlipPy

max_forks_repo_name: kmaterna/SlipPy


text:
#!/usr/bin/env python
# ------------------------------------------------------------------------------------------------------%
# Created by "Thieu Nguyen" at 09:32, 10/08/2020                                                        %
#                                                                                %
#       Email:      nguyenthieu2102@gmail.com                                                           %
#       Homepage:   https://www.researchgate.net/profile/Thieu_Nguyen6                                  %
#       Github:     https://github.com/thieunguyen5991                                                  %
#-------------------------------------------------------------------------------------------------------%

import lasagne
import theano.tensor as T
from theano import function as make_function
from models.transfer.model_util import RootModel

class Model(RootModel):

    def __init__(self, char_cnt, label_cnt, word_cnt, char_emb_size=25, word_emb_size=100, epoch=10, learning_rate=1e-3,
                 batch_size=10, test_batch_size=10, char_hidden_size=80, word_hidden_size=300, max_epoch=10, word_embedding_values=False,
                 char_double_layer=False, word_double_layer=True, dropout=True, dropout_rate=0.5, tanh=True, tanh_size=150,
                 joint=False, top_joint=True):
        super().__init__(char_cnt, label_cnt, word_cnt, char_emb_size, word_emb_size, epoch, learning_rate, batch_size, test_batch_size,
                         char_hidden_size, word_hidden_size, max_epoch)
        self.use_crf = False
        self.word_embedding_values = word_embedding_values
        self.char_double_layer = char_double_layer
        self.word_double_layer = word_double_layer
        self.dropout = dropout
        self.dropout_rate = dropout_rate

        self.tanh = tanh
        self.tanh_size = tanh_size

        self.joint = joint
        self.top_joint = top_joint

    def build(self, x, y, m, wx, cm, embedding=None):
        self.x, self.y, self.m, self.wx, self.cm = x, y, m, wx, cm
        x_sym = T.itensor3('x')
        y_sym = T.imatrix('y')
        m_sym = T.matrix('mask')
        wx_sym = T.imatrix('wx')
        cm_sym = T.tensor3('cmask')

        ### There are 4 components that is matter in this kind of models
        ###     Word Embedding
        ###     Char Neural Network --> GRU
        ###     Word Neural Network --> GRU / CNN

        ###  Create Char Embedding
        model = lasagne.layers.InputLayer(shape=(None, self.x.shape[1], self.x.shape[2]), input_var=x_sym)
        model = lasagne.layers.EmbeddingLayer(model, self.char_cnt, self.char_embedding_size)
        model_char = lasagne.layers.ReshapeLayer(model, (-1, [2], [3]))

        # Create Char Mask Embedding
        model_char_mask = lasagne.layers.InputLayer(shape=(None, self.x.shape[1], self.x.shape[2]), input_var=cm_sym)
        model_char_mask = lasagne.layers.ReshapeLayer(model_char_mask, (-1, [2]))

        ### Create Char Neural Network
        model_gru = lasagne.layers.GRULayer(model_char, self.char_hidden_size, mask_input=model_char_mask)
        model_gru_2 = lasagne.layers.GRULayer(model_char, self.char_hidden_size, mask_input=model_char_mask, backwards=True)

        if self.char_double_layer:
            model_char_rnn = lasagne.layers.ConcatLayer([model_gru, model_gru_2], axis=2)
            model_gru = lasagne.layers.GRULayer(model_char_rnn, self.char_hidden_size, mask_input=model_char_mask)
            model_gru_2 = lasagne.layers.GRULayer(model_char_rnn, self.char_hidden_size, mask_input=model_char_mask, backwards=True)

        model_gru = lasagne.layers.ReshapeLayer(model_gru, (-1, self.x.shape[1], [1], [2]))
        model_gru = lasagne.layers.SliceLayer(model_gru, -1, axis=2)

        model_gru_2 = lasagne.layers.ReshapeLayer(model_gru_2, (-1, self.x.shape[1], [1], [2]))
        model_gru_2 = lasagne.layers.SliceLayer(model_gru_2, 0, axis=2)

        ### Create Word Embedding
        list_word_layers = lasagne.layers.InputLayer(shape=(None, self.x.shape[1]), input_var=wx_sym)
        list_word_layers = lasagne.layers.EmbeddingLayer(list_word_layers, self.word_cnt, self.word_embedding_size, W=lasagne.init.Normal(std=1e-3))
        if self.word_embedding_values:
            list_word_layers.W.set_value(embedding)

        layer_list = [list_word_layers]  # No char embedding
        if self.tanh:
            model_grus = lasagne.layers.ConcatLayer([model_gru, model_gru_2], axis=2)
            model_grus = lasagne.layers.ReshapeLayer(model_grus, (-1, [2]))

            model_gru = lasagne.layers.DenseLayer(model_grus, self.tanh_size, nonlinearity=lasagne.nonlinearities.tanh)
            model_gru = lasagne.layers.ReshapeLayer(model_gru, (-1, self.x.shape[1], [1]))
            self.shared_layer = model_gru
            layer_list.append(model_gru)
        elif self.joint:
            model_grus = lasagne.layers.ConcatLayer([model_gru, model_gru_2], axis=2)
            self.shared_layer = model_grus
            layer_list.append(model_grus)
            char_output = lasagne.layers.get_output(self.shared_layer)
            self.char_fn = make_function([x_sym, cm_sym], char_output, on_unused_input='ignore')
        else:
            ## Word_Embedding + Char NN (Model_GRU, Model_GRU2) ==> Word NN
            layer_list += [model_gru, model_gru_2]

        if len(layer_list) > 1:
            model = lasagne.layers.ConcatLayer(layer_list, axis=2)
        else:
            model = layer_list[0]

        if not self.joint:
            self.shared_layer = model

        ### Word NN is GRU networks
        model_mask = lasagne.layers.InputLayer(shape=(None, self.x.shape[1]), input_var=m_sym)
        model_word_1 = lasagne.layers.GRULayer(model, self.word_hidden_size, mask_input=model_mask)
        model = lasagne.layers.GRULayer(model, self.word_hidden_size, mask_input=model_mask, backwards=True)
        if self.dropout:
            model_word_1 = lasagne.layers.DropoutLayer(model_word_1, self.dropout_rate)
            model = lasagne.layers.DropoutLayer(model, self.dropout_rate)
        if self.word_double_layer:
            model = lasagne.layers.ConcatLayer([model_word_1, model], axis=2)
            model_word_1 = lasagne.layers.GRULayer(model, self.word_hidden_size, mask_input=model_mask)
            model = lasagne.layers.GRULayer(model, self.word_hidden_size, mask_input=model_mask, backwards=True)
            if self.dropout:
                model_word_1 = lasagne.layers.DropoutLayer(model_word_1, self.dropout_rate)
                model = lasagne.layers.DropoutLayer(model, self.dropout_rate)
        layer_list = [model_word_1, model]
        model = lasagne.layers.ConcatLayer(layer_list, axis=2)

        if self.top_joint:  # Char Neural Network use for CRF
            self.shared_layer = model

        ### No CRF
        model = lasagne.layers.ReshapeLayer(model, (-1, [2]))
        model = lasagne.layers.DenseLayer(model, self.label_cnt, nonlinearity=lasagne.nonlinearities.softmax)
        self.model = model
        py = lasagne.layers.get_output(model, deterministic=True)
        loss = T.dot(lasagne.objectives.categorical_crossentropy(py, y_sym.flatten()), m_sym.flatten())
        params = lasagne.layers.get_all_params(model, trainable=True)
        updates = lasagne.updates.adagrad(loss, params, learning_rate=self.learning_rate)
        self.train_fn = make_function(inputs=[x_sym, y_sym, m_sym, wx_sym, cm_sym], outputs=loss, updates=updates, on_unused_input='ignore')
        self.test_fn = make_function(inputs=[x_sym, m_sym, wx_sym, cm_sym], outputs=py, on_unused_input='ignore')

max_stars_repo_name: fukuda-lab/LogDTL

max_issues_repo_name: fukuda-lab/LogDTL

max_forks_repo_name: fukuda-lab/LogDTL

reason: import theano,from theano


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 11 10:08:27 2018

@author: rflamary
"""

import numpy as np
import pylab as pl
import scipy
import scipy.optimize

import stdgrb
import time

t_start=time.clock()

def tic():
    global t_start
    t_start=time.clock()

def toc():
    global t_start
    t=time.clock()-t_start
    print('Elapsed time: {:1.3f}s'.format(t))
    return t

#%%

n=2000
d=200

np.random.seed(0)

c=-np.random.rand(d)
A=np.random.rand(n,d)
b=np.random.rand(n)

lb=np.zeros(d)
ub=np.ones(d)

#%%

print('Scipy simplex solver')
tic()
sol=scipy.optimize.linprog(c,A,b)
x0=sol.x
v0=sol.fun
toc()

print('Scipy interior point solver')
tic()
sol=scipy.optimize.linprog(c,A,b,method='interior-point')
x00=sol.x
v00=sol.fun
toc()

print('Default method')
tic()
x1,v1=stdgrb.lp_solve(c,A,b,lb,ub,logtoconsole=0)
toc()

print('Simplex method')
tic()
x2,v2=stdgrb.lp_solve(c,A,b,lb,ub,1,logtoconsole=0)
toc()

print('Interior point method')
tic()
x3,v3=stdgrb.lp_solve(c,A,b,lb,ub,2,logtoconsole=0,crossover=0)
toc()
#%%

max_stars_repo_name: StephaneCanu/stdgrb

max_issues_repo_name: StephaneCanu/stdgrb

max_forks_repo_name: StephaneCanu/stdgrb

reason: import numpy,import scipy


text:
import numpy as np
from pygram11._helpers import limits_1d, limits_2d, likely_uniform_bins

def test_limits_1d():
    x = np.array([1, 5, 2, 18, 202, 0.5, 0.5])
    assert limits_1d(x) == (0.5, 202)
    assert limits_1d(x, range=(1, 505)) == (1, 505)

def test_limits_2d():
    x = np.array([3, 1, 3, 18, 21, 19])
    y = np.array([5, 9, 3, 25, 12, 88])
    ranges = ((5, 20), (4, 18))
    assert limits_2d(x, y) == (1, 21, 3, 88)
    assert limits_2d(x, y, range=ranges) == (5, 20, 4, 18)

def test_likely_uniform_bins():
    b1 = np.array([1, 2, 3, 4, 5])
    b2 = np.array([1.1, 2, 3, 4])
    assert likely_uniform_bins(b1)
    assert not likely_uniform_bins(b2)


text:
import tensorflow as tf
import numpy as np

import settings
import train
import datasets

@tf.function
def train_epoch(
    models,
    dataset,
    optimizers,
    loss_student_sum=0.0,
    temperature=settings.TEMPERATURE,
    num_train_data=settings.NUM_TRAIN_DATA,
    batch_size=settings.BATCH_SIZE,
):
    num_batches = num_train_data // batch_size

    for (batch_index, (x, y)) in dataset.enumerate():
        x = tf.map_fn(
            lambda data: datasets.utils.augment(data[0], data[1])[0],
            (x, y),
            dtype=tf.float32,
        )
        x = tf.map_fn(
            lambda data: datasets.utils.normalize(data[0], data[1])[0],
            (x, y),
            dtype=tf.float32,
        )

        # Train student
        with tf.GradientTape(watch_accessed_variables=False) as tape:
            tape.watch(models["student"].trainable_weights)
            logits_from_student = models["student"](x)
            logits_from_teacher = models["teacher"](x)
            loss_student = train.losses.loss_student.loss_fn(
                logits_from_student,
                logits_from_teacher,
                y,
                tf.constant(temperature, tf.float32),
            )
        grads = tape.gradient(loss_student, models["student"].trainable_weights)
        optimizers["student"].apply_gradients(
            grads_and_vars=zip(grads, models["student"].trainable_weights)
        )
        loss_student_sum += loss_student
    return loss_student_sum / num_batches


text:
'''
Created on Aug 31, 2015

@author: ash
'''

import networkx as nx
import matplotlib.pyplot as plt
import random
import math
import numpy as np

from scipy.interpolate import interp1d
from scipy.interpolate import UnivariateSpline
from scipy import interpolate
from scipy.interpolate import splprep, splev
from numpy import arange, cos, linspace, pi, sin

# urlShpFile = "/home/ash/Data/tl_2014_39049_roads/tl_2014_39049_roads.shp"
urlShpFile = "/home/ash/Data/fixedRoads/tl_2014_39049_roads.shp"

def roadNetStats(_urlShpFile):
    roadGraphd = nx.read_shp(_urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    print "graph has been read"
    nodeList = roadGraph.nodes(data=True)
    _nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)

    pos = []
    for i in xrange(nNode):
        pos.append(nodeList[i][0])
        pass

    shpLayout = dict(zip(roadGraph,pos))
    print "number of nodes: " + str(nx.number_of_nodes(roadGraph))
    print "number of edges: " + str(nx.number_of_edges(roadGraph))

    neighborCount = {}
    for node in _nodeList:
        neighbors = list(nx.all_neighbors(roadGraph, node))
        n = len(neighbors)

        if neighborCount.has_key(n):
            neighborCount[n] += 1
            pass
        else:
            neighborCount[n] = 1
        pass

    for item in neighborCount.keys():
        print item, ":", neighborCount[item]
    pass

def rndRoad(_urlShpFile):
    roadGraphd = nx.read_shp(_urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    print "graph has been read"
    nodeList = roadGraph.nodes(data=True)
    _nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)

    pos = []
    for i in xrange(nNode):
        pos.append(nodeList[i][0])
        pass

    shpLayout = dict(zip(roadGraph,pos))

    _node1 = random.choice(_nodeList)
    _node2 = random.choice(_nodeList)

#     paths = list(nx.all_simple_paths(roadGraph, _node1, _node2))
#     print len(paths)
#     _path = paths[0]

    #_path = nx.astar_path(roadGraph, _node1, _node2)
    _path = nx.dijkstra_path(roadGraph, source=_node1, target=_node2)

    _pathedges = nx.to_edgelist(roadGraph, nodelist=_path)
    termnodelist = [_node1,_node2]

    plt.figure(1, figsize=(12,12))
    nx.draw_networkx_edges(roadGraph, pos=shpLayout, edgelist=None, width=1, edge_color='b')
    nx.draw_networkx(roadGraph, pos=shpLayout, nodelist=_path, edgelist = _pathedges, node_size=40, node_color='r', node_shape='d', edgewidth=10, edge_color='r', with_labels=False)
    nx.draw_networkx_nodes(roadGraph, pos=shpLayout, nodelist=termnodelist, node_size=60, node_color='k', node_shape='o')
    plt.show()

    pass

def rndTraversal(_urlShpFile):
    roadGraphd = nx.read_shp(_urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    nodeList = roadGraph.nodes(data=True)

    _nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)
    pos = []
    for i in xrange(nNode):
        pos.append(nodeList[i][0])
        pass
    shpLayout = dict(zip(roadGraph,pos))
    _node1 = random.choice(_nodeList)
    _node2 = random.choice(_nodeList)

    _path = nx.astar_path(roadGraph, _node1, _node2, None)
    _pathedges = zip(_path[0:-1], _path[1:])

    termnodelist = [_node1,_node2]
    plt.figure(1, figsize=(12,12))
    nx.draw_networkx_edges(roadGraph, pos=shpLayout, edgelist=None, width=1, edge_color='b')
    nx.draw_networkx(roadGraph, pos=shpLayout, nodelist=_path, edgelist = _pathedges, node_size=40, node_color='r', node_shape='d', edgewidth=10, edge_color='r', with_labels=False)
    nx.draw_networkx_nodes(roadGraph, pos=shpLayout, nodelist=termnodelist, node_size=60, node_color='k', node_shape='o')
    plt.show()
    pass

def genFeat(slpAngle, dictSize):
    if abs(slpAngle) > 360:
        slpAngle = slpAngle % 360
        pass

    if slpAngle < 0:
        slpAngle += 360
        pass

    binSize = 360 / dictSize
    feat = int(slpAngle // binSize)
    return feat
    pass

def dispNodeEdgeGraph(edgePointsLst, figNo =1):
    plt.figure(figNo, figsize=(12,12))
    for edgePoints in edgePointsLst:
        edgeGraph = nx.Graph()
        nNode = len(edgePoints)
        nIdx = range(0, nNode)
        pos = dict(zip(nIdx, edgePoints))

        edgeGraph.add_nodes_from(pos.keys())
        for n, p in pos.iteritems():
            edgeGraph.node[n]['pos'] = p
            pass

        for n in xrange(nNode-1):
            edgeGraph.add_edge(n, n+1)
            pass
        nx.draw_networkx(edgeGraph, pos, node_size=9, node_shape='o', alpha=0.25,node_color='r', edge_width=1, edge_color='b', font_size=8, with_labels=False)

    plt.show()

    pass

def dispNodeEdgeGraphClean(edgePointsLst, figNo =1):
    plt.figure(figNo, figsize=(12,12))
    for edgePoints in edgePointsLst:
        edgeGraph = nx.Graph()
        nNode = len(edgePoints)
        nIdx = range(0, nNode)
        pos = dict(zip(nIdx, edgePoints))

        edgeGraph.add_nodes_from(pos.keys())
        for n, p in pos.iteritems():
            edgeGraph.node[n]['pos'] = p
            pass

        for n in xrange(nNode-1):
            edgeGraph.add_edge(n, n+1)
            pass

        nx.draw_networkx(edgeGraph, pos, node_size=0, node_shape='o', alpha=0.25,node_color='k', edge_width=5, edge_color='k', font_size=8, with_labels=False)

    plt.show()

    pass

def getUniformSampledPoints(_r,_c):
    x = _r
    y = _c
    M = 1000
    t = np.linspace(0, len(x), M)
    x = np.interp(t, np.arange(len(x)), x)
    y = np.interp(t, np.arange(len(y)), y)
    # ----------------------------------------------------------
    tol = 0.0004
    # ----------------------------------------------------------
    approxEdgeLength = math.sqrt((x[0]-x[-1])**2 + (y[0]-y[-1])**2)

    if approxEdgeLength/5 < tol:
        tol = approxEdgeLength/5
        pass

    i, idx = 0, [0]
    while i < len(x):
        total_dist = 0
        for j in range(i+1, len(x)):
            total_dist += math.sqrt((x[j]-x[j-1])**2 + (y[j]-y[j-1])**2)
            if total_dist > tol:
                idx.append(j)
                break
        i = j+1
    idx.append(len(x)-1)
    xn = x[idx]
    yn = y[idx]
    return xn,yn

def getEdgePoints(roadEdge):
    roadEdgeData = roadEdge[2]
    edgeKeyList = roadEdgeData.keys()
    edgePointStr = roadEdgeData['Wkt']
    b1 = edgePointStr.find('(')
    b2 = edgePointStr.find(')')
    edgePointsSubStr = edgePointStr[b1+1:b2]

    edgePointPairStrLst = edgePointsSubStr.split(',')
    edgePointPairLst = []
    for edgePointPairStr in edgePointPairStrLst:
        edgePointPair = [float(x) for x in edgePointPairStr.split(' ')]
        edgePointPairLst.append(edgePointPair)
        pass

    return edgePointPairLst
    pass

def rndTrajectory(_urlShpFile):
    roadGraphd = nx.read_shp(_urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    nodeList = roadGraph.nodes(data=True)

    _nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)
    pos = []
    for i in xrange(nNode):
        pos.append(nodeList[i][0])
        pass
    shpLayout = dict(zip(roadGraph,pos))
    _node1 = random.choice(_nodeList)
    _node2 = random.choice(_nodeList)

    _path = nx.astar_path(roadGraph, _node1, _node2, None)
    _pathedges = zip(_path[0:-1], _path[1:])

    pathedges = []
    for i, node1 in enumerate(_path[0:-1]):
        edges = roadGraph.edges(node1, data=True)
        for edge in edges:
            if edge[1] == _path[i+1]:
                pathedges.append(edge)
        pass

    termnodelist = [_node1,_node2]
    plt.figure(1, figsize=(12,12))
    nx.draw_networkx_edges(roadGraph, pos=shpLayout, edgelist=None, width=1, edge_color='b')
    nx.draw_networkx(roadGraph, pos=shpLayout, nodelist=_path, edgelist = _pathedges, node_size=40, node_color='r', node_shape='d', edgewidth=10, edge_color='r', with_labels=False)
    nx.draw_networkx_nodes(roadGraph, pos=shpLayout, nodelist=termnodelist, node_size=60, node_color='k', node_shape='o')
#     plt.show()

    _edgePts = []
    for roadEdge in pathedges:
        edgePointPairLst = getEdgePoints(roadEdge)
        _edgePts.extend(edgePointPairLst)

    interPtsLst =[]
    print _edgePts

    _r = [item[0] for item in _edgePts]
    _c = [item[1] for item in _edgePts]

    _r2,_c2 = getUniformSampledPoints(_r,_c)

    x = _r2
    y = _c2
#     M = len(x)
    M = 10*len(x)

    print 'M', M

    t = np.linspace(0, len(x), M)
    x = np.interp(t, np.arange(len(x)), x)
    y = np.interp(t, np.arange(len(y)), y)
    z = t
    # spline parameters
    s=0 # smoothness parameter
    k=5 # spline order
    nest=-1 # estimate of number of knots needed (-1 = maximal)

    # find the knot points
    tckp,u = splprep([x,y,z],s=s,k=k,nest=-1)

    # evaluate spline, including interpolated points
    xnew,ynew,znew = splev(linspace(0,1,M),tckp)
    dx,dy,dz = splev(linspace(0,1,M), tckp, der=1)

    slp = []
    cnv = 180/ np.pi
    for i in xrange(len(dx)):
        slp.append(np.arctan((dy[i]/dx[i]))*cnv )
    print slp

    # -------------------
    # quantize the slope and assign to edge descriptor list

    roadLetFeat = []
    for elem in slp:
        feat = genFeat(elem, 72)
        roadLetFeat.append(feat)

#     plt.plot(x, y, 'kd', label='data point')

#     for X, Y, Z in zip(x, y, roadLetFeat):
#         # Annotate the points 5 _points_ above and to the left of the vertex
#         plt.annotate('{}'.format(Z), xy=(X,Y), xytext=(-.5, .5), color='blue', ha='right',textcoords='offset points')
    fig = plt.figure(2, figsize=(12,12))
    ax = plt.plot(xnew,ynew,'k-',label='Trajectory', linewidth=3)
    #plt.legend()

    #ax.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')

    plt.xticks([])
    plt.xlabel("")
    plt.yticks([])
    plt.ylabel("")

#     plt.xlabel('x')
#     plt.ylabel('y')
#     plt.hold(True)

    plt.show()

#     dispNodeEdgeGraph(_edgePts,2)
#     dispNodeEdgeGraphClean(_edgePts, 3)
    pass

def mysubgraph(_urlShpFile):
    roadGraphd = nx.read_shp(_urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    nodeList = roadGraph.nodes(data=True)

    _nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)
    pos = []
    for i in xrange(nNode):
        pos.append(nodeList[i][0])
        pass
    shpLayout = dict(zip(roadGraph,pos))
    _node1 = random.choice(_nodeList)
    _node2 = random.choice(_nodeList)
    _path = nx.astar_path(roadGraph, _node1, _node2, None)

    subG = nx.subgraph(roadGraph, _path)
    plt.figure(1, figsize=(12,12))
    nx.draw_networkx(subG, pos=shpLayout, edgelist=None, node_size=40, node_color='r', node_shape='d', edgewidth=10, edge_color='r', with_labels=False)
    plt.show()

if __name__ == '__main__':
#     roadNetStats(urlShpFile)
#     rndRoad(urlShpFile)
#     rndTraversal(urlShpFile)

    rndTrajectory(urlShpFile)
#     mysubgraph(urlShpFile)
    pass

reason: import numpy,from numpy,from scipy,import networkx


text:
""" Module for risk-exploiting reward perimeter scenario
Environment will contain:
- A 2D reward distribution. The objective of the multi-agent system is to form a "perimeter" around the reward distribution
    such that is maximize the surface integral of the reward distribution across the surface of the largest convex region defined by the agents
- Rewards will be normalized by the total, true integral of the reward function
- The reward distribution goes negative toward the outskirts of the domain, thus the optimal policy would distribute agents along the curve
    of where the reward function crosses zero
- The reward function has a corresponding risk function; i.e. when the reward becomes non-zero, so does the risk of agent failure
- If an agent fails, it is still used as part of the reward calculation, but it is not able to move for the rest of the episode
Agents will:
- observe the position of other agents as well as thier local measurement of the risk/reward function

- Scenario is derived from ergo_perimeter_small making the following
    settable parameters instead of hard coded
    - number of agents
    - whether rewards are shared or "local"
    - whether observations are direct per entity or histogram based
    - Hazard failure risk
    - Collision failure risk
"""

import numpy as np
from bisect import bisect
from shapely.geometry import Polygon, Point, MultiPoint
from multiagent.scenario import BaseScenario
from particle_environments.mager.world import MortalAgent, HazardousWorld, RiskRewardLandmark
from particle_environments.mager.observation import format_observation
from particle_environments.common import is_collision, distance, delta_pos, delta_vel
from particle_environments.common import ExtendedRadialPolynomialRewardFunction2D as ExtendedRadialReward
from particle_environments.common import RadialBernoulliRiskFunction2D as RadialRisk
from particle_environments.common import truncate_or_pad
from rl_algorithms.scenariolearning import ScenarioHeuristicAgentTrainer

# Scenario Parameters
_MAX_COMMUNICATION_DISTANCE = np.inf
_MAX_OBSERVATION_DISTANCE = 2.0
_AGENT_SIZE = 0.01
_LANDMARK_SIZE = 0.2
_AGENT_OBSERVATION_LEN = 6
_LANDMARK_OBSERVATION_LEN = 1
_PEAK_REWARD = 1.0
_TERMINATION_REWARD = -0.0
_N_RADIAL_BINS = 4
_N_ANGULAR_BINS = 8
_N_OBSERVED_TERMINATIONS = 3
_PRECISION_WEIGHT = 1.0
_COVERAGE_WEIGHT = 0.01

# _LANDMARKS = []
# _LANDMARKS.append(
#     RiskRewardLandmark( risk_fn=RadialRisk(_LANDMARK_SIZE), reward_fn=ExtendedRadialReward(_LANDMARK_SIZE, 1.0)))
# _N_LANDMARKS = len(_LANDMARKS)
# _POSITIVE_REGION_INTEGRAL = _LANDMARKS[0].reward_fn.get_radial_integral(_LANDMARK_SIZE)

class Scenario(BaseScenario):

    def __init__(self, *, num_agents, num_hazards, identical_rewards, observation_type, hazard_risk=0.5, collision_risk=0.0):
        '''
        Args:
         - num_agents [int] number of agents in scenario
         - num_hazards [int] number of hazards landmarks in the scenario
         - identical_rewards [bool] true if all agents receieve exact same reward, false if rewards are "local" to agents
         - observation_type [str] "direct" if observation directly of each entity, "histogram" if bin entities in spacial grid
         - hazard_risk [float] max probability of failure caused by hazard landmark
         - collision_risk [float] probability of failure caused by collision
        '''

        # check inputs
        assert isinstance(num_agents, int); assert num_agents >= 1
        assert isinstance(num_hazards, int); assert (num_hazards == 0 or num_hazards == 1)
        assert isinstance(identical_rewards, bool)
        assert (observation_type == "direct" or observation_type == "histogram")
        assert (hazard_risk >= 0.0 and hazard_risk <= 1.0)
        assert (collision_risk >= 0.0 and collision_risk <= 1.0)

        # set member vars
        self.num_agents = num_agents
        self.num_hazards = num_hazards
        self.identical_rewards = identical_rewards
        self.observation_type = observation_type
        self.hazard_risk = hazard_risk
        self.collision_risk = collision_risk

        # Create landmark
        landmarks = []
        if self.num_hazards == 0:
            landmarks.append(
                RiskRewardLandmark(risk_fn=None,
                reward_fn=ExtendedRadialReward(_LANDMARK_SIZE, _PEAK_REWARD)))
        else:
            landmarks.append(
                RiskRewardLandmark(risk_fn=RadialRisk(_LANDMARK_SIZE, self.hazard_risk),
                reward_fn=ExtendedRadialReward(_LANDMARK_SIZE, _PEAK_REWARD)))
        self.scenario_landmarks = landmarks
        self.n_landmarks = len(self.scenario_landmarks)

    def make_world(self):
        world = HazardousWorld(
                    collision_termination_probability=self.collision_risk,
                    flyoff_termination_radius = 10.0,
                    flyoff_termination_speed = 50.0,
                    spontaneous_termination_probability = 1.0/(8.0*50.0*self.num_agents))

        # set scenario-specific world parameters
        world.collaborative = True
        world.systemic_rewards = True
        world.identical_rewards = self.identical_rewards
        world.dim_c = 0 # observation-based communication
        world.termination_reward = _TERMINATION_REWARD
        world.render_connections = True

        # add landmarks to world
        world.landmarks = []
        for lm in self.scenario_landmarks:
            world.landmarks.append(lm)

        for i, landmark in enumerate(world.landmarks):
            landmark.name = 'landmark_%d' % i
            landmark.collide = False
            landmark.movable = False
            landmark.size = _LANDMARK_SIZE
            # properties for landmarks
            if isinstance(landmark, RiskRewardLandmark) and landmark.is_hazard:
                #TODO: make colors heatmap of risk probability over all bounds
                landmark.color = np.array([landmark.risk_fn.get_failure_probability(0,0) + .1, 0, 0])
            else:
                landmark.color = np.array([0.25, 0.25, 0.25])

        # make initial conditions
        self.reset_world(world)
        return world

    def reset_world(self, world):
        # random properties for agents
        # add agents
        world.agents = [MortalAgent() for i in range(self.num_agents)]
        for i, agent in enumerate(world.agents):
            agent.name = 'agent_%d' % i
            agent.collide = False
            agent.blind = False
            agent.silent = True
            agent.deaf = False
            agent.terminated = False
            agent.size = _AGENT_SIZE
            agent.state.p_pos = np.random.uniform(-1, +1, world.dim_p)
            agent.state.p_vel = np.zeros(world.dim_p)
            agent.state.c = np.zeros(world.dim_c)
            agent.color = np.array([0.35, 0.35, 0.85])
            agent.previous_observation = None

        # place landmarks
        for landmark in world.landmarks:
            landmark.state.p_pos = np.random.uniform(-1, +1, world.dim_p)
            landmark.state.p_vel = np.zeros(world.dim_p)

    def benchmark_data(self, agent, world):
        collisions = 0
        occupied_landmarks = 0
        min_dists = 0
        for l in world.landmarks:
            dists = [np.linalg.norm(a.state.p_pos - l.state.p_pos) for a in world.agents]
            min_dists += min(dists)
            if min(dists) < 0.1:
                occupied_landmarks += 1
        if agent.collide:
            for a in world.agents:
                if is_collision(a, agent):
                    collisions += 1
        return (self.reward(agent, world), collisions, min_dists, occupied_landmarks)

    def done_callback(self, agent, world):
        ''' indicate a terminated agent as done (still being decided)
        Notes:
            - Even though terminated agent cannot take actions, it may be more appropriate
            to NOT set the agent is done in order to keep collecting data for training
            purposes
        '''
        # if agent.terminated:
        #     return True
        # else:
        #     return False
        return False

    def reward(self, agent, world, systemic_call=False):
        if systemic_call:
            if self.identical_rewards and world.identical_rewards:
                return self._identical_systemic_reward(world)
            elif not self.identical_rewards and not world.identical_rewards:
                return self._local_systemic_reward(world)
            else:
                raise Exception(
                    "Inconsistent reward options: self.identical_rewards={} world.identical_rewards={}".format(
                    self.identical_rewards, world.identical_rewards))

        else:
            return 0.0

    def _local_systemic_reward(self, world):
        ''' reward agent if they are part of complete connection between terminals
        Notes:
        '''
        assert self.identical_rewards == False
        assert world.identical_rewards == False
        raise NotImplementedError()

    def _identical_systemic_reward(self, world):
        ''' reward all agents the same for perimeter forming around reward function
        Notes:
         - computes surface integral over surface defined by convex hull of agents
        '''
        assert self.identical_rewards == True
        assert world.identical_rewards == True

        # reward function is designed for a single landmark case
        assert len(world.landmarks) == 1
        assert len(self.scenario_landmarks) == 1

        # get convex hull created by agents
        landmark_circle = Point(world.landmarks[0].state.p_pos).buffer(world.landmarks[0].size)
        agent_positions = [ag.state.p_pos for ag in world.agents]
        agent_hull = MultiPoint(agent_positions).convex_hull
        if agent_hull.geom_type == 'LineString':
            # if hull is degenerate (e.g. all points collinear), then set integral to zero
            return [0.0]*self.num_agents

        assert agent_hull.geom_type == 'Polygon', "Unexpected geometry type for convex hull: {}".format(agent_hull.geom_type)
        assert agent_hull.is_valid, "Invalid Polygon for convex hull"
        assert landmark_circle.is_valid, "Invalid Polygon for convex hull"

        # find intersection of convex hull and landmark
        landmark_coverage = agent_hull.intersection(landmark_circle)

        # compute reward
        A = agent_hull.area
        B = landmark_circle.area
        AB = landmark_coverage.area
        w_p = _PRECISION_WEIGHT;    assert w_p >= 0.0
        w_c = _COVERAGE_WEIGHT;     assert w_c >= 0.0
        reward_signal = w_p*AB/A - w_c*(B-AB)/B
        assert (reward_signal >= -w_c and reward_signal <= w_p)

        # normalize by number of agents
        reward_signal /= float(self.num_agents)

        # assign to all agents
        reward_n = [reward_signal]*self.num_agents

        return reward_n

    def observation(self, agent, world):
        ''' call observation function based on type of observation function '''
        if self.observation_type == "direct":
            return self._direct_observation(agent, world)
        elif self.observation_type == "histogram":
            return self._histogram_observation(agent, world)
        else:
            raise Exception("Unrecognized observation type: {}".format(self.observation_type))

    def _histogram_observation(self, agent, world):
        # get positions of all entities in this agent's reference frame

        # check reward signal received from landmark
        landmark_sensor_reading = 0.0
        for lm in world.landmarks:
            landmark_sensor_reading += lm.reward_fn.get_value(*agent.state.p_pos)

        # Format agent histograms
        bin_depth = _MAX_OBSERVATION_DISTANCE/10.0
        radial_bins = np.logspace(np.log10(bin_depth), np.log10(_MAX_OBSERVATION_DISTANCE), num=_N_RADIAL_BINS)
        bin_angle = 2.0*np.pi/float(_N_ANGULAR_BINS)
        angular_bins = np.linspace(bin_angle/2.0, 2*np.pi - bin_angle/2.0, num=_N_ANGULAR_BINS)
        agent_histogram_2d = np.array([[0]*_N_ANGULAR_BINS]*_N_RADIAL_BINS)
        reward_histogram_2d = np.array([[0.0]*_N_ANGULAR_BINS]*_N_RADIAL_BINS)

        # establish observation of failures
        observed_terminations_2d = []
        observed_terminations_dists = []

        # count agents in each bin
        for a in world.agents:
            dist = distance(a, agent)

            # skip if agent is agent
            if a == agent:
                continue

            # record observed termination
            if a.terminated:
                insert_index = bisect(observed_terminations_dists, dist)
                observed_terminations_dists.insert(insert_index, dist)
                observed_terminations_2d.insert(insert_index, delta_pos(a, agent))
                # don't "continue", record terminated agent in histogram like live agent

            # find radial bin
            rad_bin = np.searchsorted(radial_bins, dist)
            if rad_bin == _N_RADIAL_BINS:
                # agent is too far away and observation is not stored
                continue

            # calculate angle
            dx, dy = delta_pos(a, agent)
            ang = np.arctan2(dy, dx)
            if ang < 0:
                ang += 2*np.pi

            # find angular bin
            ang_bin = np.searchsorted(angular_bins, ang)
            if ang_bin == _N_ANGULAR_BINS:
                ang_bin = 0

            # add count to histogram
            agent_histogram_2d[rad_bin][ang_bin] = agent_histogram_2d[rad_bin][ang_bin] + 1

            # add aggregate landmark sensor reading to histogram
            # Note: should not need to compute average reading over agents in bin
            #   because neural net should be able to learn to do this using agent count
            #   histogram
            for lm in world.landmarks:
                reward_histogram_2d[rad_bin][ang_bin] += lm.reward_fn.get_value(*a.state.p_pos)

        # flatten histogram to 1d list
        agent_histogram = [val for sublist in agent_histogram_2d for val in sublist]

        # flatten reward histogram to 1d list and compute average
        reward_histogram = [val for sublist in reward_histogram_2d for val in sublist]

        # flatten, truncate/pad observed terminations to fixed length
        observed_terminations = [val for sublist in observed_terminations_2d for val in sublist]
        observed_terminations = truncate_or_pad(observed_terminations, 2*_N_OBSERVED_TERMINATIONS)

        # package new observation
        new_obs = np.asarray([agent.terminated] + agent.state.p_vel.tolist() + agent.state.p_pos.tolist() + [landmark_sensor_reading] + agent_histogram + reward_histogram + observed_terminations)

        # append previous observation for velocity estimation
        if agent.previous_observation is None:
            agent.previous_observation = 0.0*new_obs
        obs = np.append(new_obs, agent.previous_observation)
        agent.previous_observation = new_obs

        return obs

    def _direct_observation(self, agent, world):
        # get positions of all entities in this agent's reference frame

        def observe_agents(other_agent):
            ''' fill in information communicated/observed between agents
            '''

            # check if node is terminated
            is_terminated = 0
            if isinstance(other_agent, MortalAgent) and other_agent.terminated:
                is_terminated = 1

            # relative speed and position of other agent
            # dx = dy = dvx = dvy = 0.
            # if not is_terminated:
            dx, dy = delta_pos(other_agent, agent)
            dvx, dvy = delta_vel(other_agent, agent)

            # get local reward function at position of other agent
            other_landmark_sensor_reading = 0.0
            for lm in world.landmarks:
                other_landmark_sensor_reading += lm.reward_fn.get_value(*other_agent.state.p_pos)

            ag_obs = [is_terminated, dx, dy, dvx, dvy, other_landmark_sensor_reading]
            assert(len(ag_obs) == _AGENT_OBSERVATION_LEN)
            return ag_obs

        def observe_landmarks(landmark):
            ''' fill in information observed about landmarks
            '''
            # ld_obs = delta_pos(landmark, agent).tolist()

            # # check if within observation range and is observable
            # d = distance(landmark, agent)
            # if d > world.max_communication_distance:
            #     ld_obs = [0.0]*len(ld_obs)

            ld_obs = []

            # check reward signal received from landmark
            landmark_sensor_reading = 0.0
            for lm in world.landmarks:
                landmark_sensor_reading += lm.reward_fn.get_value(*agent.state.p_pos)

            ld_obs += [landmark_sensor_reading]

            assert(len(ld_obs) == _LANDMARK_OBSERVATION_LEN)
            return ld_obs

        landmark_observations = format_observation(observe = observe_landmarks,
                                                objects = world.landmarks,
                                                num_observations = len(world.landmarks),
                                                observation_size = _LANDMARK_OBSERVATION_LEN)

        agent_observations = format_observation(observe = observe_agents,
                                            objects = [a for a in world.agents if a is not agent],
                                            num_observations = self.num_agents-1,
                                            observation_size = _AGENT_OBSERVATION_LEN)

        new_obs = np.asarray([agent.terminated] + agent.state.p_vel.tolist() + agent.state.p_pos.tolist() + landmark_observations + agent_observations)

        # append previous observation for velocity estimation
        if agent.previous_observation is None:
            agent.previous_observation = 0.0*new_obs
        obs = np.append(new_obs, agent.previous_observation)
        agent.previous_observation = new_obs

        return obs

class ScenarioHeuristicComputer(ScenarioHeuristicAgentTrainer):
    ''' representation of an individual agent's embedded processor and memory tailor
    Notes:
     - This is meant to be used as a scenario-specific alternative to
     the more general purpose, scenario-agnostic "trainers". It can hold an agents model
     of the world (transition and reward functions), policy, and learning process, if any.
    '''
    def __init__(self, name, model, obs_shape_n, act_space_n, agent_index, args, **kwargs):
        ScenarioHeuristicAgentTrainer.__init__(self, name, model, obs_shape_n, act_space_n, agent_index, args)

        raise NotImplementedError()

    def get_initial_policy_distribution(self):
        ''' method for "jumpstarting" monte carlo group distribution
        '''

        raise NotImplementedError()

    def action(self, obs):
        ''' maps observation array to action forces in x,y directions
        Notes:
         - Assumes observation array formated as:
            [0:2] = agent.state.p_vel.tolist()
            [2:4] = agent.state.p_pos.tolist()
            [4:8] = terminals
            [8:40] = agent_histogram +
            failures)
        '''

        raise NotImplementedError()

    def experience(self, obs, act, rew, new_obs, done, terminal):
        ''' Monte Carlo learning only record cumulative reward
        '''
        # record cumulative reward
        raise NotImplementedError()

    def preupdate(self):
        '''unused function handle compatibility with train.py
        '''
        raise NotImplementedError()

    def update(self, agents, t):
        '''unused function handle compatibility with train.py
        '''
        raise NotImplementedError()

    def group_policy_update(self, group_policy):
        '''update behavior parameters based on group policy
        '''
        raise NotImplementedError()

max_stars_repo_licenses: FSFULLR FSFUL

max_issues_repo_licenses: FSFULLR FSFUL

max_forks_repo_licenses: FSFULLR FSFUL


text:
import argparse
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from scipy.ndimage import gaussian_filter1d, maximum_filter1d

parser = argparse.ArgumentParser(description='Recognize running, walking and standing.')
parser.add_argument('file', type=str, nargs=1,
                    help='Data file from Sensorfusion.')
args = parser.parse_args()

data = pd.read_csv(args.file[0], sep='\t', header=None, names=range(8))

# remain the necessary data
acceleration = data[data[1] == 'ACC'][[0,2,3,4]].to_numpy(dtype=np.float)

# print(acceleration)

# Calculate acceleration magnitude and Subtract gravity
start_time = acceleration[0,0]/1000
time = acceleration[:,0]/1000 - start_time
magnitude = np.abs(np.sqrt(np.sum(acceleration[:,1:4]**2, 1)) - 9.81)

# smooth the data with maximum filter and then gaussian filter
# max_magnitude = maximum_filter1d(magnitude, 50)
# smoothed_magnitude = gaussian_filter1d(magnitude, 100)
max_smooth_magnitude = gaussian_filter1d(maximum_filter1d(magnitude, 50), 100)

plt.plot(time, magnitude)
# plt.plot(time, max_magnitude, label='max')
# plt.plot(time, smoothed_magnitude, label='smoothed')
plt.plot(time, max_smooth_magnitude, label='max smooth')
plt.show()

# set threshold
running_threshold = 20
walking_threshold = 5
time_threshold = 10

# discriminate data and visualize the results
current_state = None
start = 0
fig, ax = plt.subplots()

for t, data in zip(time, max_smooth_magnitude):
    if data > running_threshold:
        new_state = 'running'
    elif data > walking_threshold:
        new_state = 'walking'
    else:
        new_state = 'sitting'

    if new_state != current_state and current_state is not None:
        if t - start >= time_threshold:  # activity state needs to be active for minimum of 10 s
            if current_state == 'sitting':
                ax.axvspan(start, t, alpha=0.5, color='green')
            if current_state == 'walking':
                ax.axvspan(start, t, alpha=0.5, color='yellow')
            if current_state == 'running':
                ax.axvspan(start, t, alpha=0.5, color='red')
            start = t

    current_state = new_state

if current_state == 'sitting':
    ax.axvspan(start, t, alpha=0.5, color='green')

if current_state == 'walking':
    ax.axvspan(start, t, alpha=0.5, color='yellow')
if current_state == 'running':
    ax.axvspan(start, t, alpha=0.5, color='red')

green_patch = mpatches.Patch(color='green', label='Stand still')
yellow_patch = mpatches.Patch(color='yellow', label='Walking')
red_patch = mpatches.Patch(color='red', label='Running')
plt.legend(handles=[green_patch, yellow_patch, red_patch])

plt.plot(time, max_smooth_magnitude, label='max smooth')
plt.show()

reason: import numpy,from scipy