text:
import numpy as np
from nose.plugins.attrib import attr

import yt
from yt.testing import (
    ANSWER_TEST_TAG,
    fake_amr_ds,
    fake_hexahedral_ds,
    fake_tetrahedral_ds,
    small_fake_hexahedral_ds,
)
from yt.utilities.answer_testing.framework import GenericImageTest
from yt.utilities.lib.geometry_utils import triangle_plane_intersect
from yt.utilities.lib.mesh_triangulation import triangulate_indices

def setup():
    """Test specific setup."""
    from yt.config import ytcfg

    ytcfg["yt", "__withintesting"] = "True"

def compare(ds, field, idir, test_prefix, test_name, decimals=12, annotate=False):
    def slice_image(filename_prefix):
        sl = yt.SlicePlot(ds, idir, field)
        if annotate:
            sl.annotate_mesh_lines()
        sl.set_log("all", False)
        image_file = sl.save(filename_prefix)
        return image_file

    slice_image.__name__ = "slice_{}".format(test_prefix)
    test = GenericImageTest(ds, slice_image, decimals)
    test.prefix = test_prefix
    test.answer_name = test_name
    return test

@attr(ANSWER_TEST_TAG)
def test_mesh_slices_amr():
    ds = fake_amr_ds()
    for field in ds.field_list:
        prefix = "%s_%s_%s" % (field[0], field[1], 0)
        yield compare(ds, field, 0, test_prefix=prefix, test_name="mesh_slices_amr")

@attr(ANSWER_TEST_TAG)
def test_mesh_slices_tetrahedral():
    ds = fake_tetrahedral_ds()

    mesh = ds.index.meshes[0]
    ad = ds.all_data()

    for field in ds.field_list:
        for idir in [0, 1, 2]:
            prefix = "%s_%s_%s" % (field[0], field[1], idir)
            yield compare(
                ds,
                field,
                idir,
                test_prefix=prefix,
                test_name="mesh_slices_tetrahedral",
                annotate=True,
            )

            sl_obj = ds.slice(idir, ds.domain_center[idir])
            assert sl_obj[field].shape[0] == mesh.count(sl_obj.selector)
            assert sl_obj[field].shape[0] < ad[field].shape[0]

@attr(ANSWER_TEST_TAG)
def test_mesh_slices_hexahedral():
    # hexahedral ds
    ds = fake_hexahedral_ds()
    ad = ds.all_data()
    mesh = ds.index.meshes[0]

    for field in ds.field_list:
        for idir in [0, 1, 2]:
            prefix = "%s_%s_%s" % (field[0], field[1], idir)
            yield compare(
                ds,
                field,
                idir,
                test_prefix=prefix,
                test_name="mesh_slices_hexahedral",
                annotate=True,
            )

            sl_obj = ds.slice(idir, ds.domain_center[idir])
            assert sl_obj[field].shape[0] == mesh.count(sl_obj.selector)
            assert sl_obj[field].shape[0] < ad[field].shape[0]

def test_perfect_element_intersection():
    # This test tests mesh line annotation where a z=0 slice
    # perfectly intersects the top of a hexahedral element with node
    # z-coordinates containing both -0 and +0. Before
    # https://github.com/yt-project/yt/pull/1437 this test falsely
    # yielded three annotation lines, whereas the correct result is four
    # corresponding to the four edges of the top hex face.

    ds = small_fake_hexahedral_ds()
    indices = ds.index.meshes[0].connectivity_indices
    coords = ds.index.meshes[0].connectivity_coords
    tri_indices = triangulate_indices(indices)
    tri_coords = coords[tri_indices]
    lines = triangle_plane_intersect(2, 0, tri_coords)
    non_zero_lines = 0
    for i in range(lines.shape[0]):
        norm = np.linalg.norm(lines[i][0] - lines[i][1])
        if norm > 1e-8:
            non_zero_lines += 1
    np.testing.assert_equal(non_zero_lines, 4)

ext: py

reason: import numpy


text:
#!/usr/bin/env python
# coding: utf-8

# In[ ]:

#updated 12/25/2020 to use zarr data

def get_monthly_data(stype):
    from pathlib import Path
    import xarray as xr
    import numpy as np

    if np.char.lower(stype)=='oscar':
        dir_data = 'F:/data/sat_data/oscar/L4/oscar_third_deg/oscar_vel*.nc'
        ds=xr.open_mfdataset(dir_data,combine='nested',concat_dim='time').isel(depth=0).drop({'um','vm'}).rename({'latitude':'lat','longitude':'lon'})
        ds = ds.sel(lon=slice(20.0,379.9))
        ds = ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180)).sortby('lon').sortby('lat').drop({'year'})
        ds = ds.resample(time='M',keep_attrs=True).mean(skipna=False,keep_attrs=True)
    if np.char.lower(stype)=='godas_mld':
        dir_data = dir_data_mld='F:/data/model_data/godas/*.nc'
        ds=xr.open_mfdataset(dir_data,combine='nested',concat_dim='time')
        ds = ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180)).sortby('lon').sortby('lat')
        ds = ds.resample(time='M',keep_attrs=True).mean(skipna=False,keep_attrs=True)
    if np.char.lower(stype)=='ccmp':
        dir_data = 'F:/data/sat_data/ccmp/'
        ds = xr.open_zarr(dir_data+'zarr2',consolidated=True).rename({'latitude':'lat','longitude':'lon'})
#        filelist=[]
#        dir_data = 'F:/data/sat_data/ccmp/v02.0/'
#        for filename in Path(dir_data).rglob('*L3.0_RSS.nc'):
#            filelist.append(filename)
#            ds=xr.open_mfdataset(filelist,combine='nested',concat_dim='time').rename({'latitude':'lat','longitude':'lon'})
        ds = ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180)).sortby('lon').sortby('lat')
#        _, index = np.unique(ds['time'], return_index=True)
#        ds=ds.isel(time=index)
        ds = ds.resample(time='M',keep_attrs=True).mean(skipna=False,keep_attrs=True)
    if np.char.lower(stype)=='mur_sst':
        filelist=[]
        dir_data = 'F:/data/sst/jpl_mur/v4.1/'+str(iyr).zfill(4)+'/'
        for filename in Path(dir_data).rglob('*90000-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1.nc'):
            filelist.append(filename)
        ds=xr.open_mfdataset(filelist,combine='nested',concat_dim='time').drop({'analysis_error'})
        ds = ds.resample(time='M',keep_attrs=True).mean(skipna=False,keep_attrs=True)
    if np.char.lower(stype)=='cmc_sst':
        dir_data='F:\data\sat_data\sst\cmc\zarr'
        ds = xr.open_zarr(dir_data)
#        dir_data='F:\data\sat_data\sst\cmc\zarr'
#        ds = xr.open_zarr(dir_data+'zarr2',consolidated=True)
        #combine 0.2 and 0.1 deg data into 0.2 dataset
#        filelist=[]
#        dir_data = 'F:/data/sst/cmc/CMC0.2deg/v2/data/'
#        for filename in Path(dir_data).rglob('*CMC-L4_GHRSST-SSTfnd-CMC0.2deg-GLOB-v02.0-fv02.0.nc'):
#            filelist.append(filename)
#        ds=xr.open_mfdataset(filelist,combine='nested',concat_dim='time')
#        filelist=[]
#        dir_data = 'F:/data/sst/cmc/CMC0.1deg/v3/'
#        for filename in Path(dir_data).rglob('*CMC-L4_GHRSST-SSTfnd-CMC0.1deg-GLOB-v02.0-fv03.0.nc'):
#            filelist.append(filename)
#        ds2=xr.open_mfdataset(filelist,combine='nested',concat_dim='time')
#        ds2=ds2.interp(lat=ds.lat,lon=ds.lon)
#        ds=xr.concat([ds,ds2],dim='time')
        ds = ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180)).sortby('lon').sortby('lat')
        ds = ds.resample(time='M',keep_attrs=True).mean(skipna=False,keep_attrs=True)
    if np.char.lower(stype)=='cmem_sss':
        filelist=[]
        dir_data = 'F:/data/model_data/CMEM/global-reanalysis-phy-001-030-monthly/'
        for filename in Path(dir_data).rglob('subset_mercatorglorys12v1_gl12_mean_*.nc'):
            filelist.append(filename)
        ds=xr.open_mfdataset(filelist,combine='nested',concat_dim='time').rename({'latitude':'lat','longitude':'lon'})
        ds=ds.drop({'zos','bottomT','sithick','usi','vsi','thetao','uo','vo','mlotst'})
        ds = ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180)).sortby('lon').sortby('lat')
        ds = ds.resample(time='M',keep_attrs=True).mean(skipna=False,keep_attrs=True)
    if np.char.lower(stype)=='aviso':
        dir_data = 'F:/data/sat_data/aviso/zarr2'
        ds = xr.open_zarr(dir_data,consolidated=True).rename({'latitude':'lat','longitude':'lon'})
#        dir_data = 'F:/data/sat_data/aviso/zarr2'
#        ds = xr.open_zarr(dir_data+'zarr2',consolidated=True).rename({'latitude':'lat','longitude':'lon'})
#        filelist=[]
#        dir_data = 'F:/data/sat_data/aviso/data/'
#        from pathlib import Path
#        for filename in Path(dir_data).rglob('*.nc'):
#            filelist.append(filename)
#      ds=xr.open_mfdataset(filelist,combine='nested',concat_dim='time').drop({'ugosa','vgosa','err'}).rename({'latitude':'lat','longitude':'lon'})
        ds = ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180)).sortby('lon').sortby('lat')
        ds = ds.resample(time='M',keep_attrs=True).mean(skipna=False,keep_attrs=True)
    return ds

max_stars_repo_name: cgentemann/Biophysical

max_issues_repo_name: cgentemann/Biophysical

max_forks_repo_name: cgentemann/Biophysical


text:
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD.
# See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import numpy as np            # pylint: disable=unused-import
import typing                 # pylint: disable=unused-import
from nomad.metainfo import (  # pylint: disable=unused-import
    MSection, MCategory, Category, Package, Quantity, Section, SubSection, SectionProxy,
    Reference
)
from nomad.metainfo.legacy import LegacyDefinition

from nomad.datamodel.metainfo import public
from nomad.datamodel.metainfo import common

m_package = Package(
    name='fplo_nomadmetainfo_json',
    description='None',
    a_legacy=LegacyDefinition(name='fplo.nomadmetainfo.json'))

class section_run(public.section_run):

    m_def = Section(validate=False, extends_base_section=True, a_legacy=LegacyDefinition(name='section_run'))

    x_fplo_program_version_sub = Quantity(
        type=str,
        shape=[],
        description='''
        FPLO sub version
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_program_version_sub'))

    x_fplo_program_compilation_options = Quantity(
        type=str,
        shape=[],
        description='''
        FPLO compilation options
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_program_compilation_options'))

class section_system(public.section_system):

    m_def = Section(validate=False, extends_base_section=True, a_legacy=LegacyDefinition(name='section_system'))

    x_fplo_reciprocal_cell = Quantity(
        type=np.dtype(np.float64),
        shape=[3, 3],
        unit='1 / meter',
        description='''
        Reciprocal Lattice vectors (in Cartesian coordinates). The first index runs over
        the $x,y,z$ Cartesian coordinates, and the second index runs over the 3 lattice
        vectors.
        ''',
        categories=[public.configuration_core],
        a_legacy=LegacyDefinition(name='x_fplo_reciprocal_cell'))

    x_fplo_atom_idx = Quantity(
        type=np.dtype(np.int32),
        shape=['number_of_atoms'],
        description='''
        FPLO-internal index for each atom
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_atom_idx'))

    x_fplo_atom_wyckoff_idx = Quantity(
        type=np.dtype(np.int32),
        shape=['number_of_atoms'],
        description='''
        Wyckoff position index of each atom
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_atom_wyckoff_idx'))

    x_fplo_atom_cpa_block = Quantity(
        type=np.dtype(np.int32),
        shape=['number_of_atoms'],
        description='''
        CPA block of each atom
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_atom_cpa_block'))

    x_fplo_structure_type = Quantity(
        type=str,
        shape=[],
        description='''
        FPLO structure type: Crystal/Molecule
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_structure_type'))

class section_method(public.section_method):

    m_def = Section(validate=False, extends_base_section=True, a_legacy=LegacyDefinition(name='section_method'))

    x_fplo_xc_functional_number = Quantity(
        type=np.dtype(np.int32),
        shape=[],
        description='''
        FPLO number xc functional
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_xc_functional_number'))

    x_fplo_xc_functional = Quantity(
        type=str,
        shape=[],
        description='''
        FPLO notation of xc functional
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_xc_functional'))

    x_fplo_dft_plus_u_projection_type = Quantity(
        type=str,
        shape=[],
        description='''
        FPLO notation of DFT+U projection
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_dft_plus_u_projection_type'))

    x_fplo_dft_plus_u_functional = Quantity(
        type=str,
        shape=[],
        description='''
        FPLO notation of DFT+U functional
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_dft_plus_u_functional'))

class section_dft_plus_u_orbital(common.section_dft_plus_u_orbital):

    m_def = Section(validate=False, extends_base_section=True, a_legacy=LegacyDefinition(name='section_dft_plus_u_orbital'))

    x_fplo_dft_plus_u_orbital_element = Quantity(
        type=str,
        shape=[],
        description='''
        FPLO: Atom/Orbital dependent DFT+U property: element
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_dft_plus_u_orbital_element'))

    x_fplo_dft_plus_u_orbital_species = Quantity(
        type=np.dtype(np.int32),
        shape=[],
        description='''
        FPLO: Atom/Orbital dependent DFT+U property: species index
        ''',
        a_legacy=LegacyDefinition(name='x_fplo_dft_plus_u_orbital_species'))

    x_fplo_dft_plus_u_orbital_F0 = Quantity(
        type=np.dtype(np.float64),
        shape=[],
        description='''
        FPLO: Atom/Orbital dependent DFT+U property: value F0
        ''',
        categories=[public.energy_value],
        a_legacy=LegacyDefinition(name='x_fplo_dft_plus_u_orbital_F0'))

    x_fplo_dft_plus_u_orbital_F2 = Quantity(
        type=np.dtype(np.float64),
        shape=[],
        description='''
        FPLO: Atom/Orbital dependent DFT+U property: value F2
        ''',
        categories=[public.energy_value],
        a_legacy=LegacyDefinition(name='x_fplo_dft_plus_u_orbital_F2'))

    x_fplo_dft_plus_u_orbital_F4 = Quantity(
        type=np.dtype(np.float64),
        shape=[],
        description='''
        FPLO: Atom/Orbital dependent DFT+U property: value F4
        ''',
        categories=[public.energy_value],
        a_legacy=LegacyDefinition(name='x_fplo_dft_plus_u_orbital_F4'))

    x_fplo_dft_plus_u_orbital_F6 = Quantity(
        type=np.dtype(np.float64),
        shape=[],
        description='''
        FPLO: Atom/Orbital dependent DFT+U property: value F6
        ''',
        categories=[public.energy_value],
        a_legacy=LegacyDefinition(name='x_fplo_dft_plus_u_orbital_F6'))

m_package.__init_metainfo__()

max_stars_repo_name: nomad-coe/nomad-parser-fplo

max_issues_repo_name: nomad-coe/nomad-parser-fplo

max_forks_repo_name: nomad-coe/nomad-parser-fplo


text:
import os
import sys

import logging
logging.basicConfig()
logger = logging.getLogger("MSI_GUI")

import numpy as np
from matplotlib import pyplot as pypl

from PyQt5 import QtCore, QtGui, uic
import pyqtgraph as pg

from expiringdict import ExpiringDict

import spts.gui.ui as ui

# MSI modules
import spts.worker as worker

from spts.gui.spts_conf import Conf
from spts.gui.options import Options
from spts.gui.view import View, ViewOptions
from spts.gui.preferences import Preferences

from spts.gui.dummy_worker import DummyWorker

#from IPython.core.debugger import Tracer
#Tracer()()

class MainWindow(ui.MainUI, ui.MainBaseUI):

    def __init__(self, parent=None):
        ui.MainBaseUI.__init__(self, parent)
        self.ui = ui.MainUI()
        self.ui.setupUi(self)

        self.settings = QtCore.QSettings(QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope, "fxihub", "spts")

        self.preferences = Preferences(self)

        self.view = View(self)
        self.ui.horizontalLayout.addWidget(self.view)

        self.ui.dataTypeTabWidget.setCurrentIndex(0)

        self.clear_cache()

        self.conf = Conf(self)

        self.read_settings()

        self.i_frame = 0
        self.data_type = "1_raw"
        self._create_worker(silent=True)

        self.options = Options(self)
        self.options.load_all()
        self.options.connect_all()

        self.view_options = ViewOptions(self)
        self.view_options.connect_all()

        self.ui.actionOpen.triggered.connect(self.conf.open)
        self.ui.actionSave.triggered.connect(self.conf.save)
        self.ui.actionSaveAs.triggered.connect(self.conf.save_as)
        self.ui.actionOpen_Data.triggered.connect(self.options.general_box._on_open_data)
        self.ui.actionPreferences.triggered.connect(self.preferences.open_preferences_dialog)
        self.ui.dataTypeTabWidget.currentChanged.connect(self._on_tab_changed)

        self.ui.previousPushButton.clicked.connect(self._show_previous)
        self.ui.nextPushButton.clicked.connect(self._show_next)
        self.ui.iFrameSpinBox.valueChanged.connect(self._on_i_frame_changed)

        #from IPython.terminal.debugger import set_trace
        #set_trace()
        pgDown = QtGui.QShortcut(QtGui.QKeySequence('PgDown'), self)
        pgDown.activated.connect(self._show_next)
        pgUp = QtGui.QShortcut(QtGui.QKeySequence('PgUp'), self)
        pgUp.activated.connect(self._show_previous)

        self._show_frame()

    def clear_cache(self):
        self.cache = ExpiringDict(max_len=10, max_age_seconds=1000)

    def clear_cache_types(self, data_types):
        for i in self.cache.keys():
            for dt in data_types:
                if dt in self.cache[i]:
                    del self.cache[i][dt]
                    if i == self.i_frame and dt == self.data_type:
                        self._show_frame()

    def _init_worker(self):
        self._create_worker()
        self._set_index(0)
        self.clear_cache()
        self._show_frame()

    def _create_worker(self, silent=False):
        try:
            self.worker = worker.Worker(self.conf, pipeline_mode=True, data_mount_prefix=self.preferences.data_mount_prefix)
        except IOError:
            if not silent:
                logger.warning("Cannot create worker instance (IOError). Data might not be mounted.")
            self.worker = DummyWorker(self.conf)

    def _on_i_frame_changed(self):
        i_frame = self.ui.iFrameSpinBox.value()
        if self.i_frame is not None and i_frame == self.i_frame:
            return
        elif i_frame is None or self.worker.N is None or (i_frame+1 >= self.worker.N) or (i_frame < 0):
            self._set_index(0)
        else:
            self.i_frame = i_frame
        self._show_frame()

    def _set_index(self, i_frame):
        self.i_frame = i_frame
        self.ui.iFrameSpinBox.setValue(i_frame)

    def _on_tab_changed(self, tab_index):
        data_types = ["1_raw", "2_process", "3_denoise", "4_threshold", "5_detect", "6_analyse"]
        self.data_type = data_types[tab_index]
        self._show_frame()

    def _get_data(self, i_frame, data_type):
        data = None
        if i_frame in self.cache:
            if self.cache[i_frame] is not None and data_type in self.cache[i_frame]:
                data = self.cache[i_frame][data_type]
                return data
        else:
            self.cache[i_frame] = None
        out_package = self.cache[i_frame]
        data = self.worker.work(work_package={"i": i_frame}, tmp_package=out_package, target=data_type)
        self.cache[i_frame] = data
        return data[data_type]

    def _get_frame(self, i_frame, data_type):
        tdata = self._get_data(i_frame, data_type)
        if data_type == "1_raw":
            return tdata["image_raw"]
        elif data_type == "2_process":
            return tdata["image"]
        elif data_type == "3_denoise":
            return tdata["image_denoised"]
        elif data_type == "4_threshold":
            return tdata["image_thresholded"]
        elif data_type == "5_detect":
            return tdata["image_labels"]
        elif data_type == "6_analyse":
            return tdata["masked_image"]

    def _get_xy(self, i_frame):
        tdata = self._get_data(i_frame, "5_detect")
        x = tdata["x"]
        y = tdata["y"]
        s = (x!=-1)*(y!=-1)
        return s, x, y

    def _get_peaks(self, i_frame):
        tdata = self._get_data(i_frame, "6_analyse")
        s = (tdata["peak_sum"]!=-1) * (tdata["peak_size"]!=-1)

        c = np.zeros_like(tdata["peak_size"])
        c[s] = np.sqrt(tdata["peak_size"][s]/np.pi)/(tdata["peak_circumference"][s]/(2*np.pi))

        s *= np.isfinite(c)

        return s, tdata["peak_sum"], c

    def _show_next(self):
        if self.i_frame is None or self.worker.N is None or (self.i_frame+1 >= self.worker.N):
            return
        else:
            self._set_index(self.i_frame+1)
            self._show_frame()

    def _show_previous(self):
        if self.i_frame is None or self.worker.N is None or (self.i_frame-1 <= -1):
            return
        else:
            self._set_index(self.i_frame-1)
            self._show_frame()

    def _show_frame(self):
        if self.i_frame is None:
            return
        frame = self._get_frame(self.i_frame, self.data_type)

        cmap = None
        auto_range = None
        if self.data_type == "4_threshold":
            auto_range = True
        elif self.data_type == "5_detect":
            cmap = pypl.cm.gnuplot
            auto_range = True
        self.view.show_image(frame.T, force_auto_range=auto_range, force_cmap=cmap)
        self.view.remove_all_circles_and_annotations()
        if (self.data_type == "5_detect") or (self.data_type == "6_analyse"):
            s_xy, x, y = self._get_xy(self.i_frame)
            if (self.data_type == "6_analyse"):
                s_p, intensities, circlescore = self._get_peaks(self.i_frame)
                s = s_xy * s_p
                if s.sum() > 0:
                    self.view.draw_new_annotations(x[s], y[s], intensities[s], circlescore[s])
            else:
                s = s_xy
            if s.sum() > 0:
                self.view.draw_new_circles(x[s], y[s])

    def closeEvent(self, event):
        if self.conf.any_unsaved_changes():
            saveChanges = QtGui.QMessageBox(QtGui.QMessageBox.Question, "Save changes?",
                                            "Would you like to save changes?",
                                            QtGui.QMessageBox.Save | QtGui.QMessageBox.Discard | QtGui.QMessageBox.Cancel).exec_()
            if saveChanges == QtGui.QMessageBox.Save:
                self.conf.save()
            if saveChanges == QtGui.QMessageBox.Cancel:
                return event.ignore()
        self.preferences.closeEvent()
        self.view_options.closeEvent()

        settings = QtCore.QSettings("biox.io", "spts")
        settings.setValue("geometry", self.saveGeometry())
        settings.setValue("windowState", self.saveState())
        settings.setValue("conf", dict(self.conf))

        QtGui.QMainWindow.closeEvent(self, event)

    def read_settings(self):
        settings = QtCore.QSettings("biox.io", "spts")
        geo = settings.value("geometry")
        if geo is not None:
            self.restoreGeometry(geo)
        win = settings.value("windowState")
        if win is not None:
            self.restoreState(win)
        d = settings.value("conf")
        if d is not None:
            for k, v in d.items():
                self.conf[k] = v

def main():
    app = QtGui.QApplication(sys.argv)
    #app.processEvents()

    mainWindow = MainWindow()
    mainWindow.show()

    #if len(sys.argv) > 1:
    #    mainWindow.conf.load(sys.argv[1])

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

max_stars_repo_name: FilipeMaia/spts

max_issues_repo_name: FilipeMaia/spts

max_forks_repo_name: FilipeMaia/spts


text:
import os
import h5py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

class PrisonersDilemmaFile:

    def __init__(self, filePath, championshipName):
        # HDF5 File
        self.filePath = filePath
        self.file = h5py.File(filePath, 'r')
        # Championship
        self.championshipName = championshipName
        self.championship = self.file[championshipName]
        self.numberOfTurns = self.championship.attrs['numberOfTurns']
        # Players
        self.playerNames = self.__getPlayerNames()
        return

    def playerPlot(self, playerName):
        fig, ax = self.__createBasicPlayerPlot(playerName)
        self.__plotScorePlayerPlot(playerName, ax)
        self.__savePlayerPlot(playerName, fig)
        return

    def allPlayersPlot(self):
        for playerName in self.playerNames:
            self.playerPlot(playerName)
        return

    def __getPlayerNames(self):
        return list(self.championship.keys())

    def __createBasicPlayerPlot(self, playerName):
        figSize = (10, 5)
        fig, ax = plt.subplots(figsize=figSize)
        ax.set_title((playerName + '\n' + "Championship " + self.championshipName).title())
        ax.set_ylabel('Score')
        ax.set_xlabel('Strategy')
        yMax = 3*self.numberOfTurns*1.1
        ax.set_ylim([0, yMax])
        xMax = 1.15*len(self.__getPartners(playerName))
        ax.set_xlim([-0.3, xMax])
        ax.grid(which='major', color='gray', linestyle='-', linewidth=1, alpha=0.5, axis='y')
        hlines = {'linestyle':'--', 'color':'black'}
        score_and_labels = {3:'suckers', 2:'punishment', 1:'reward', 0:'temptation'}
        for score, label in score_and_labels.items():
            plt.text(y=score*self.numberOfTurns+0.01*yMax, x=len(self.__getPartners(playerName)), s=label)
            plt.axhline(y=score*self.numberOfTurns, **hlines)
        xtickLabels = [self.file[self.championshipName][partnerName]['strategy'].attrs['shortName'] for partnerName in self.__getPartners(playerName)]
        ax.set_xticks(np.arange(len(xtickLabels)))
        ax.set_xticklabels(labels=xtickLabels, rotation='vertical')
        return fig, ax

    def __plotScorePlayerPlot(self, playerName, ax):
        colors = cm.gist_rainbow(np.linspace(0, 1, len(self.__getPartners(playerName))))
        for index, partnerName in enumerate(self.__getPartners(playerName)):
            payoff = np.sum(self.championship[playerName][partnerName]['payoff'])
            ax.bar(index, payoff, color=colors[index])

    def __getPartners(self, playerName):
        partnerNames = list(self.championship[playerName].keys())
        partnerNames.remove('strategy')
        return partnerNames

    def __savePlayerPlot(self, playerName, fig):
        fig.tight_layout()
        folder = '{:s}_figures/'.format(self.championshipName).lower().replace(' ','_')
        os.makedirs(folder, exist_ok=True)
        figurePath = folder + '{:s}_{:s}.png'.format(playerName, self.championshipName).lower().replace(' ','_')
        plt.savefig(figurePath)
        plt.close(fig)
        return

    def championshipPlot(self):
        fig, ax = self.__createBasicChampionshipPlot()
        self.__plotChampionshipPlot(ax)
        self.__saveChampionshipPlot(fig)
        return

    def __getPlayerPayoff(self, playerName):
        payoff = [np.sum(self.championship[playerName][partnerName]['payoff']) for partnerName in self.__getPartners(playerName)]
        return np.sum(payoff)

    def __plotChampionshipPlot(self, ax):
        playerPayoff     = np.array([ self.__getPlayerPayoff(playerName)                            for playerName in self.playerNames ])
        playerShortNames = np.array([ self.championship[playerName]['strategy'].attrs['shortName']  for playerName in self.playerNames ])
        sortedIndices    = np.flip(np.argsort(playerPayoff).flatten())
        # yticks
        ax.set_yticks(np.arange(len(playerShortNames)))
        ax.set_yticklabels(labels=playerShortNames[sortedIndices])
        # bar plot
        colors = cm.gist_rainbow(np.linspace(0, 1, len(self.playerNames)))
        ax.barh(y=np.arange(len(self.playerNames)), width=playerPayoff[sortedIndices], color=colors)
        return

    def __createBasicChampionshipPlot(self):
        figSize = (5, 10)
        fig, ax = plt.subplots(figsize=figSize)
        ax.set_title(("Championship " + self.championshipName).title())
        ax.set_xlabel('Score')
        ax.set_ylabel('Strategy')
        xMax = 3*self.numberOfTurns*1.1*len(self.playerNames)
        ax.set_xlim([0, xMax])
        yMax = 1.15*len(self.playerNames)
        ax.set_ylim([-0.3, yMax])
        ax.grid(which='major', color='gray', linestyle='-', linewidth=1, alpha=0.5, axis='x')
        hlines = {'linestyle':'--', 'color':'black'}
        score_and_labels = {3:'suckers', 2:'punishment', 1:'reward', 0:'temptation'}
        for score, label in score_and_labels.items():
            plt.text(x=score*self.numberOfTurns*len(self.playerNames)+0.01*xMax, y=len(self.playerNames), s=label, rotation='vertical')
            plt.axvline(x=score*self.numberOfTurns*len(self.playerNames), **hlines)
        return fig, ax

    def __saveChampionshipPlot(self, fig):
        fig.tight_layout()
        figurePath = 'championship_{:s}.png'.format(self.championshipName).lower().replace(' ','_')
        plt.savefig(figurePath)
        plt.close(fig)
        return


text:
#pyCGM

# Copyright (c) 2015 Mathew Schwartz <umcadop@gmail.com>
# Core Developers: Seungeun Yeon, Mathew Schwartz
# Contributors Filipe Alves Caixeta, Robert Van-wesep
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Input and output of pycgm functions

import sys
from .pyCGM import *
if sys.version_info[0]==2:
    import c3d
    pyver = 2
    print("Using python 2 c3d loader")

else:
    from . import c3dpy3 as c3d
    pyver = 3
    print("Using python 3 c3d loader - c3dpy3")

try:
    from ezc3d import c3d as ezc
    useEZC3D = True
    print("EZC3D Found, using instead of Python c3d")
except:
    useEZC3D = False

from math import *
import numpy as np
import xml.etree.ElementTree as ET
import os
import errno

#Used to split the arrays with angles and axis
#Start Joint Angles
SJA=0
#End Joint Angles
EJA=SJA+19*3
#Start Axis
SA=EJA
#End Axis
EA=SA+72*3

def createMotionDataDict(labels,data):
	"""Creates an array of motion capture data given labels and data.

	Parameters
	----------
	labels : array
	    List of marker position names.
	data : array
	    List of xyz coordinates corresponding to the marker names in `labels`.
	    Indices of `data` correspond to frames in the trial.

	Returns
	-------
	motiondata : array
	    List of dict. Indices of `motiondata` correspond to frames
	    in the trial. Keys in the dictionary are marker names and
	    values are xyz coordinates of the corresponding marker.

	Examples
	--------
	This example uses a loop and ``numpy.array_equal`` to test the equality
	of individual dictionary elements since python does not guarantee
	the order of dictionary elements.

	Example for three markers and two frames of trial.

	>>> from numpy import array, array_equal
	>>> labels = ['LFHD', 'RFHD', 'LBHD']
	>>> data = [[array([184.55160796, 409.68716101, 1721.34289625]),
	...          array([325.82985131, 402.55452959, 1722.49816649]),
	...          array([197.8621642 , 251.28892152, 1696.90197756])],
	...         [array([185.55160796, 408.68716101, 1722.34289625]),
	...          array([326.82985131, 403.55452959, 1723.49816649]),
	...          array([198.8621642 , 252.28892152, 1697.90197756])]]
	>>> result = createMotionDataDict(labels, data)
	>>> expected = [{'RFHD': array([325.82985131, 402.55452959, 1722.49816649]),
	...              'LBHD': array([197.8621642 , 251.28892152, 1696.90197756]),
	...              'LFHD': array([184.55160796, 409.68716101, 1721.34289625])},
	...             {'RFHD': array([326.82985131, 403.55452959, 1723.49816649]),
	...              'LBHD': array([198.8621642 , 252.28892152, 1697.90197756]),
	...              'LFHD': array([185.55160796, 408.68716101, 1722.34289625])}]

	>>> flag = True #False if any values are not equal
	>>> for i in range(len(result)):
	...     for key in result[i]:
	...         if (not array_equal(result[i][key], expected[i][key])):
	...             flag = False
	>>> flag
	True
	"""
	motiondata = []
	for frame in data:
		mydict={}
		for label,xyz in zip(labels,frame):
			l=str(label.rstrip())
			mydict[l] = xyz
		motiondata.append(mydict)
	return motiondata

def splitMotionDataDict(motiondata):
    """Splits an array of motion capture data into separate labels and data.

    Parameters
    ----------
    motiondata : array
        List of dict. Indices of `motiondata` correspond to frames
        in the trial. Keys in the dictionary are marker names and
        values are xyz coordinates of the corresponding marker.

    Returns
    -------
    labels, data : tuple
        `labels` is a list of marker position names from the dictionary
        keys in `motiondata`. `data` is a list of xyz coordinate
        positions corresponding to the marker names in `labels`.
        Indices of `data` correspond to frames in the trial.

    Examples
    --------
    Example for three markers and two frames of trial.

    >>> from numpy import array
    >>> motiondata = [{'RFHD': array([325.82985131, 402.55452959, 1722.49816649]),
    ...                'LFHD': array([184.55160796, 409.68716101, 1721.34289625]),
    ...                'LBHD': array([197.8621642 , 251.28892152, 1696.90197756])},
    ...               {'RFHD': array([326.82985131, 403.55452959, 1723.49816649]),
    ...                'LFHD': array([185.55160796, 408.68716101, 1722.34289625]),
    ...                'LBHD': array([198.8621642 , 252.28892152, 1697.90197756])}]
    >>> labels, data = splitMotionDataDict(motiondata)
    >>> labels
    ['RFHD', 'LFHD', 'LBHD']
    >>> data #doctest: +NORMALIZE_WHITESPACE
    array([[[ 325.82985131,  402.55452959, 1722.49816649],
            [ 184.55160796,  409.68716101, 1721.34289625],
            [ 197.8621642 ,  251.28892152, 1696.90197756]],
           [[ 326.82985131,  403.55452959, 1723.49816649],
            [ 185.55160796,  408.68716101, 1722.34289625],
            [ 198.8621642 ,  252.28892152, 1697.90197756]]])
    """
    if pyver == 2:
        labels=motiondata[0].keys()
        data=np.zeros((len(motiondata),len(labels),3))
        counter=0
        for md in motiondata:
            data[counter]=np.asarray(md.values())
            counter+=1
        return labels,data
    if pyver == 3:
        labels=list(motiondata[0].keys())
        data=np.zeros((len(motiondata),len(labels),3))
        counter=0
        for md in motiondata:
            data[counter]=np.asarray(list(md.values()))
            counter+=1
        return labels,data

def createVskDataDict(labels,data):
	"""Creates a dictionary of vsk file values from labels and data.

	Parameters
	----------
	labels : array
	    List of label names for vsk file values.
	data : array
	    List of subject measurement values corresponding to the label
            names in `labels`.

	Returns
	-------
	vsk : dict
	    Dictionary of vsk file values. Dictionary keys correspond to
	    names in `labels` and dictionary values correspond to values in
	    `data`.

	Examples
	--------
	This example tests for dictionary equality through python instead of
	doctest since python does not guarantee the order in which dictionary
	elements are printed.

	>>> labels = ['MeanLegLength', 'LeftKneeWidth', 'RightAnkleWidth']
	>>> data = [940.0, 105.0, 70.0]
	>>> res = createVskDataDict(labels, data)
	>>> res == {'MeanLegLength':940.0, 'LeftKneeWidth':105.0, 'RightAnkleWidth':70.0}
	True
	"""
	vsk={}
	for key,data in zip(labels,data):
		vsk[key]=data
	return vsk

def splitVskDataDict(vsk):
    """Splits a dictionary of vsk file values into labels and data arrays

    Parameters
    ----------
    vsk : dict
        dictionary of vsk file values. Dictionary keys correspond to
        names in `labels` and dictionary values correspond to values in
        `data`.

    Returns
    -------
    labels, data : tuple
        `labels` is a list of label names for vsk file values. `data`
        is a numpy array holding the corresponding values.

    Examples
    --------
    >>> from numpy import array, array_equal #used to compare numpy arrays
    >>> import sys
    >>> vsk = {'MeanLegLength':940.0, 'LeftKneeWidth':105.0, 'RightAnkleWidth':70.0}
    >>> labels, data = splitVskDataDict(vsk)
    >>> flag = True #False if any values do not match
    >>> for i in range(len(labels)):
    ...     if (vsk[labels[i]] != data[i]):
    ...         flag = False
    >>> flag
    True
    """
    if pyver == 2: return vsk.keys(),np.asarray(vsk.values())
    if pyver == 3: return list(vsk.keys()),np.asarray(list(vsk.values()))

def markerKeys():
    """A list of marker names.

    Returns
    -------
    marker_keys : array
        List of marker names.

    Examples
    --------
    >>> markerKeys() #doctest: +NORMALIZE_WHITESPACE
    ['RASI', 'LASI', 'RPSI', 'LPSI', 'RTHI', 'LTHI', 'RKNE', 'LKNE', 'RTIB',
     'LTIB', 'RANK', 'LANK', 'RTOE', 'LTOE', 'LFHD', 'RFHD', 'LBHD', 'RBHD',
     'RHEE', 'LHEE', 'CLAV', 'C7', 'STRN', 'T10', 'RSHO', 'LSHO', 'RELB', 'LELB',
     'RWRA', 'RWRB', 'LWRA', 'LWRB', 'RFIN', 'LFIN']
    """
    marker_keys = ['RASI','LASI','RPSI','LPSI','RTHI','LTHI','RKNE','LKNE','RTIB',
               'LTIB','RANK','LANK','RTOE','LTOE','LFHD','RFHD','LBHD','RBHD',
               'RHEE','LHEE','CLAV','C7','STRN','T10','RSHO','LSHO','RELB','LELB',
               'RWRA','RWRB','LWRA','LWRB','RFIN','LFIN']
    return marker_keys

def loadEZC3D(filename):
    """Use c3dez to load a c3d file.

    Parameters
    ----------
    filename : str
        Path to the c3d file to be loaded.

    Returns
    -------
    [data, None, None] : array
        `data` is the array representation of the loaded c3d file.
    """
    #Relative import mod for python 2 and 3
    try: from . import c3dez
    except: import c3dez

    dataclass = c3dez.C3DData(None, filename)
    data = dataAsArray(dataclass.Data['Markers'])
    return [data,None,None]

def loadC3D(filename):
    """Open and load a C3D file of motion capture data

    Keys in the returned data dictionaries are marker names, and
    the corresponding values are a numpy array with the associated
    value. ``data[marker] = array([x, y, z])``

    Parameters
    ----------
    filename : str
        File name of the C3D file to be loaded

    Returns
    -------
    [data, dataunlabeled, markers] : array
        `data` is a list of dict. Each dict represents one frame in
        the trial. `dataunlabeled` contains a list of dictionaries
        of the same form as in `data`, but for unlabeled points.
        `markers` is a list of marker names.

    Examples
    --------
    The files 59993_Frame_Static.c3d and RoboStatic.c3d in
    SampleData are used to test the output.

    >>> from .pyCGM_Helpers import getfilenames
    >>> from numpy import around, array
    >>> filename_59993 = getfilenames(x=1)[1]
    >>> result_59993 = loadC3D(filename_59993)
    >>> data = result_59993[0]
    >>> dataunlabeled = result_59993[1]
    >>> markers = result_59993[2]
    >>> roboFilename = getfilenames(x=3)[1]
    >>> result_roboFilename = loadC3D(roboFilename)
    >>> roboDataUnlabeled = result_roboFilename[1]

    Testing for some values from 59993_Frame_Static.c3d.

    >>> around(data[0]['RHNO'], 8) #doctest: +NORMALIZE_WHITESPACE
    array([ 555.46948242, -559.36499023, 1252.84216309])
    >>> around(data[0]['C7'], 8) #doctest: +NORMALIZE_WHITESPACE
    array([ -29.57296562, -9.34280109, 1300.86730957])
    >>> dataunlabeled[4] #doctest: +NORMALIZE_WHITESPACE
    {'*113': array([-172.66630554,  167.2040863 , 1273.71594238]),
     '*114': array([ 169.18231201, -227.13475037, 1264.34912109])}
    >>> markers
    ['LFHD', 'RFHD', 'LBHD', ...]

    Frame 0 in RoboStatic.c3d has no unlabeled data.

    >>> roboDataUnlabeled[0]
    {}
    """
    if useEZC3D == True:
        print("Using EZC3D")
        return loadEZC3D(filename)

    reader = c3d.Reader(open(filename, 'rb'))

    labels = reader.get('POINT:LABELS').string_array
    mydict = {}
    mydictunlabeled ={}
    data = []
    dataunlabeled = []
    prog_val = 1
    counter = 0
    data_length = reader.last_frame() - reader.first_frame()
    markers=[str(label.rstrip()) for label in labels]

    for frame_no, points, analog in reader.read_frames(True,True):
        for label, point in zip(markers, points):
            #Create a dictionary with format LFHDX: 123
            if label[0]=='*':
                if point[0]!=np.nan:
                    mydictunlabeled[label]=point
            else:
                mydict[label] = point

        data.append(mydict)
        dataunlabeled.append(mydictunlabeled)
        mydict = {}
        mydictunlabeled ={}
    return [data,dataunlabeled,markers]

def loadCSV(filename):
    """Open and load a CSV file of motion capture data.

    Keys in the returned data dictionaries are marker names, and
    the corresponding values are a numpy array with the associated
    value. ``data[marker] = array([x, y, z])``

    Parameters
    ----------
    filename : str
        File name of the CSV file to be loaded.

    Returns
    -------
    [motionData, unlabeledMotionData, labels] : array
        `motionData` is a list of dict. Each dict represents one frame in
        the trial. `unlabeledMotionData` contains a list of dictionaries
        of the same form as in `motionData`, but for unlabeled points.
        `labels` is a list of marker names.

    Examples
    --------
    Sample_Static.csv in SampleData is used to test the output.

    >>> filename = 'SampleData/ROM/Sample_Static.csv'
    >>> result = loadCSV(filename)
    >>> motionData = result[0]
    >>> unlabeledMotionData = result[1]
    >>> labels = result[2]

    Testing for some values from data.

    >>> motionData[0]['RHNO'] #doctest: +NORMALIZE_WHITESPACE
    array([ 811.9591064, 677.3413696, 1055.390991 ])
    >>> motionData[0]['C7'] #doctest: +NORMALIZE_WHITESPACE
    array([ 250.765976, 165.616333, 1528.094116])
    >>> unlabeledMotionData[0] #doctest: +NORMALIZE_WHITESPACE
    {'*111': array([ 692.8970947, 423.9462585, 1240.289063 ]),
     '*112': array([-225.5265198, 405.5791321, 1214.458618 ]),
     '*113': array([ -82.65164185, 232.3781891 , 1361.853638 ]),
     '*114': array([ 568.5736694, 260.4929504, 1361.799805 ])}
    >>> labels
    ['LFHD', 'RFHD', 'LBHD', ...]
    """
    if filename == '':
        self.returnedData.emit(None)
    import numpy as np
    from numpy.compat import asbytes #probably not needed

    fh = open(filename,'r')

    fh=iter(fh)
    delimiter=','

    def rowToDict(row,labels):
        """Convert a row and labels to a dictionary.

        This function is only in scope from within `loadCSV`.

        Parameters
        ----------
        row : array
            List of marker data.
        labels : array
            List of marker names.

        Returns
        -------
        dic, unlabeleddic : tuple
            `dic` is a dictionary where keys are marker names and values
            are the corresponding marker value. `unlabeleddic` holds
            all unlabeled marker values in the same format as `dic`.

        Examples
        --------
        This example uses a loop and numpy.array_equal to test the equality
        of individual dictionary elements since python does not guarantee
        the order of dictionary elements.

        >>> from numpy import array, array_equal
        >>> row = ['-1003.583618', '81.007614', '1522.236938',
        ...        '-1022.270447', '-47.190071', '1519.680420',
        ...        '-833.953979', '40.892181', '1550.325562']
        >>> labels = ['LFHD', 'RFHD', 'LBHD']
        >>> dict, unlabeleddict = rowToDict(row, labels)
        >>> expectedDict = {'LFHD': array([-1003.583618, 81.007614, 1522.236938]),
        ...                 'RFHD': array([-1022.270447, -47.190071, 1519.68042]),
        ...                 'LBHD': array([-833.953979, 40.892181, 1550.325562])}
        >>> unlabeleddict #No unlabeled values are expected for this example
        {}
        >>> flag = True #False if any values are not equal
        >>> for marker in dict:
        ...     if (not array_equal(dict[marker], expectedDict[marker])):
        ...         flag = False
        >>> flag
        True
        """
        dic={}
        unlabeleddic={}
        if pyver == 2: row=zip(row[0::3],row[1::3],row[2::3])
        if pyver == 3: row=list(zip(row[0::3],row[1::3],row[2::3]))
        empty=np.asarray([np.nan,np.nan,np.nan],dtype=np.float64)
        for coordinates,label in zip(row,labels):
            #unlabeled data goes to a different dictionary
            if label[0]=="*":
                try:
                    unlabeleddic[label]=np.float64(coordinates)
                except:
                    pass
            else:
                try:
                    dic[label]=np.float64(coordinates)
                except:
                    #Missing data from labeled marker is NaN
                    dic[label]=empty.copy()
        return dic,unlabeleddic

    def split_line(line):
        """Split a line in a csv file into an array

        This function is only in scope from within `loadCSV`.

        Parameters
        ----------
        line : str
            String form of the line to be split

        Returns
        -------
        array
            Array form of `line`, split on the predefined delimiter ','.

        Examples
        --------
        >>> line = '-772.184937, -312.352295, 589.815308'
        >>> split_line(line)
        ['-772.184937', ' -312.352295', ' 589.815308']
        """
        if pyver == 2: line = asbytes(line).strip(asbytes('\r\n'))
        elif pyver == 3: line = line.strip('\r\n')
        if line:
            return line.split(delimiter)
        else:
            return []

    def parseTrajectories(fh,framesNumber):
        r"""Converts rows of motion capture data into a dictionary

        This function is only in scope from within `loadCSV`.

        Parameters
        ----------
        fh : list iterator object
            Iterator for rows of motion capture data. The first 3 rows
            in `fh` contain the frequency, labels, and field headers
            respectively. All elements of the rows in `fh` are strings.
            See Examples.
        framesNumber : int
            Number of rows iterated over in `fh`.

        Returns
        -------
        labels, rows, rowsUnlabeled, freq : tuple
            `labels` is a list of marker names.
            `rows` is a list of dict of motion capture data.
            `rowsUnlabeled` is of the same type as `rows`, but for
            unlabeled data.
            `freq` is the frequency in Hz.

        Examples
        --------
        This example uses a loop and numpy.array_equal to test the equality
        of individual dictionary elements since python does not guarantee
        the order of dictionary elements.

        Example for 2 markers, LFHD and RFHD, and one frame of trial.
        >>> from numpy import array, array_equal

        # Rows will hold frequency, headers, fields, and one row of data
        >>> rows = [None, None, None, None]
        >>> rows[0] = '240.000000,Hz\n'
        >>> rows[1] = ',LFHD,,,RFHD\n'
        >>> rows[2] = 'Field #,X,Y,Z,X,Y,Z\n'
        >>> rows[3] = '1,-1003.583618,81.007614,1522.236938,-1022.270447,-47.190071,1519.680420\n'
        >>> fh = iter(rows)
        >>> framesNumber = 1 #Indicates one row of data
        >>> labels, rows, rowsUnlabeled, freq = parseTrajectories(fh, framesNumber)
        >>> labels
        ['LFHD', 'RFHD']
        >>> expectedRows = [{'LFHD': array([-1003.583618,  81.007614, 1522.236938]),
        ...                  'RFHD': array([-1022.270447, -47.190071, 1519.68042 ])}]

        >>> flag = True #False if any values are not equal
        >>> for i in range(len(expectedRows)):
        ...     for key in rows[i]:
        ...         if (not array_equal(rows[i][key], expectedRows[i][key])):
        ...             flag = False
        >>> flag
        True
        >>> rowsUnlabeled
        [{}]
        >>> freq
        240.0
        """
        delimiter=','
        if pyver == 2:
            freq=np.float64(split_line(fh.next())[0])
            labels=split_line(fh.next())[1::3]
            fields=split_line(fh.next())
        elif pyver == 3:
            freq=np.float64(split_line(next(fh))[0])
            labels=split_line(next(fh))[1::3]
            fields=split_line(next(fh))
        delimiter = asbytes(delimiter)
        rows=[]
        rowsUnlabeled=[]
        if pyver == 2: first_line=fh.next()
        elif pyver == 3: first_line=next(fh)
        first_elements=split_line(first_line)[1:]
        colunsNum=len(first_elements)
        first_elements,first_elements_unlabeled=rowToDict(first_elements,labels)
        rows.append(first_elements)
        rowsUnlabeled.append(first_elements_unlabeled)

        for row in fh:
            row=split_line(row)[1:]
            if len(row)!=colunsNum:
                break
            elements,unlabeled_elements=rowToDict(row,labels)
            rows.append(elements)
            rowsUnlabeled.append(unlabeled_elements)
        return labels,rows,rowsUnlabeled,freq

    ###############################################
    ### Find the trajectories
    framesNumber=0
    for i in fh:
        if i.startswith("TRAJECTORIES"):
            #First elements with freq,labels,fields
            if pyver == 2: rows=[fh.next(),fh.next(),fh.next()]
            if pyver == 3: rows=[next(fh),next(fh),next(fh)]
            for j in fh:
                if j.startswith("\r\n"):
                    break
                framesNumber=framesNumber+1
                rows.append(j)
            break
    rows=iter(rows)
    labels,motionData,unlabeledMotionData,freq=parseTrajectories(rows,framesNumber)

    return [motionData,unlabeledMotionData,labels]

def loadData(filename,rawData=True):
        """Loads motion capture data from a csv or c3d file.

        Either a csv or c3d file of motion capture data can be used.
        `loadCSV` or `loadC3D` will be called accordingly.

        Parameters
        ----------
        filename : str
            Path of the csv or c3d file to be loaded.

        Returns
        -------
        data : array
            `data` is a list of dict. Each dict represents one frame in
            the trial.

        Examples
        --------
        RoboResults.csv and RoboResults.c3d in SampleData are used to
        test the output.

        >>> csvFile = 'SampleData/Sample_2/RoboResults.csv'
        >>> c3dFile = 'SampleData/Sample_2/RoboStatic.c3d'
        >>> csvData = loadData(csvFile)
        SampleData/Sample_2/RoboResults.csv
        >>> c3dData = loadData(c3dFile)
        SampleData/Sample_2/RoboStatic.c3d

        Testing for some values from the loaded csv file.

        >>> csvData[0]['RHNO'] #doctest: +NORMALIZE_WHITESPACE
        array([-772.184937, -312.352295, 589.815308])
        >>> csvData[0]['C7'] #doctest: +NORMALIZE_WHITESPACE
        array([-1010.098999, 3.508968, 1336.794434])

        Testing for some values from the loaded c3d file.

        >>> c3dData[0]['RHNO'] #doctest: +NORMALIZE_WHITESPACE
        array([-259.45016479, -844.99560547, 1464.26330566])
        >>> c3dData[0]['C7'] #doctest: +NORMALIZE_WHITESPACE
        array([-2.20681717e+02, -1.07236075e+00, 1.45551550e+03])
        """
        print(filename)
        if str(filename).endswith('.c3d'):

                data = loadC3D(filename)[0]
                #add any missing keys
                keys = markerKeys()
                for frame in data:
                    for key in keys:
                        frame.setdefault(key,[np.nan,np.nan,np.nan])
                return data

        elif str(filename).endswith('.csv'):
                return loadCSV(filename)[0]

def dataAsArray(data):
    """Converts a dictionary of markers with xyz data to an array
    of dictionaries.

    Assumes all markers have the same length of data.

    Parameters
    ----------
    data : dict
        Dictionary of marker data. Keys are marker names. Values are
        arrays of 3 elements, each of which is an array of x, y, and z
        coordinate values respectively. ``data[marker] = array([x, y, z])``

    Returns
    -------
    dataArray : array
        List of dictionaries.

    Examples
    --------
    This example uses a loop and ``numpy.array_equal`` to test the equality
    of individual dictionary elements since python does not guarantee
    the order of dictionary elements.

    Example for motion capture data for 3 markers, each with data for
    one frame of trial.

    >>> from numpy import array, array_equal
    >>> data = {'RFHD': [array([325.82985131]), array([402.55452959]), array([1722.49816649])],
    ...         'LFHD': [array([184.55160796]), array([409.68716101]), array([1721.34289625])],
    ...         'LBHD': [array([197.8621642]) , array([251.28892152]), array([1696.90197756])]}
    >>> result = dataAsArray(data)
    >>> expected = [{'RFHD': array([325.82985131, 402.55452959, 1722.49816649]),
    ...              'LFHD': array([184.55160796, 409.68716101, 1721.34289625]),
    ...              'LBHD': array([197.8621642, 251.28892152, 1696.90197756])}]
    >>> flag = True #False if any values are not equal
    >>> for i in range(len(result)):
    ...     for key in result[i]:
    ...         if (not array_equal(result[i][key], expected[i][key])):
    ...             flag = False
    >>> flag
    True
    """
    names = list(data.keys())
    dataArray = []

    #make the marker arrays a better format
    for marker in data:
        #Turn multi array into single
        xyz = [ np.array(x) for x in zip( data[marker][0],data[marker][1],data[marker][2] ) ]
        data[marker] = xyz

    #use the first marker to get the length of frames
    datalen = len( data[names[0]] )

    for i in range(datalen):

        frameDict = {}

        for marker in data:
            frameDict[marker] = data[marker][i]

        dataArray.append(frameDict)

    return dataArray

def dataAsDict(data,npArray=False):
    """Converts the frame by frame based data to a dictionary of keys
    with all motion data as an array per key.

    Parameters
    ----------
    data : array
        List of dict. Indices of `data` correspond to frames
        in the trial. Keys in the dictionary are marker names and
        values are xyz coordinates of the corresponding marker.
    npArray : bool, optional
        False by default. If set to true, the function will return
        a numpy array for each key instead of a list.

    Returns
    -------
    dataDict : dict
        Dictionary of the motion capture data from `data`.

    Examples
    --------
    This example uses a loop and ``numpy.array_equal`` to test the equality
    of individual dictionary elements since python does not guarantee
    the order of dictionary elements.

    >>> from numpy import array, array_equal
    >>> data = [{'RFHD': array([325.82985131, 402.55452959, 1722.49816649]),
    ...          'LFHD': array([184.55160796, 409.68716101, 1721.34289625]),
    ...          'LBHD': array([197.8621642 , 251.28892152, 1696.90197756])},
    ...         {'RFHD': array([326.82985131, 403.55452959, 1723.49816649]),
    ...          'LFHD': array([185.55160796, 408.68716101, 1722.34289625]),
    ...          'LBHD': array([198.8621642 , 252.28892152, 1697.90197756])}]
    >>> result = dataAsDict(data, True) #return as numpy array
    >>> expected = {'RFHD': array([[ 325.82985131,  402.55452959, 1722.49816649],
    ...                            [ 326.82985131,  403.55452959, 1723.49816649]]),
    ...             'LFHD': array([[ 184.55160796,  409.68716101, 1721.34289625],
    ...                            [ 185.55160796,  408.68716101, 1722.34289625]]),
    ...             'LBHD': array([[ 197.8621642 ,  251.28892152, 1696.90197756],
    ...                            [ 198.8621642 ,  252.28892152, 1697.90197756]])}
    >>> flag = True #False if any values are not equal
    >>> for marker in result:
    ...     if (not array_equal(result[marker], expected[marker])):
    ...         flag = False
    >>> flag
    True
    """
    dataDict = {}

    for frame in data:
        for key in frame:
            dataDict.setdefault(key,[])
            dataDict[key].append(frame[key])

    if npArray == True:
        for key in dataDict:
            dataDict[key] = np.array(dataDict[key])

    return dataDict

def writeKinetics(CoM_output,kinetics):
    """Uses numpy.save to write kinetics data as an .npy file.

    Parameters
    ----------
    CoM_output : file, str, or Path
        Full path of the file to be saved to or a file object
        or a filename.
    kinetics : array_like
        Array data to be saved.

    Examples
    --------
    >>> import tempfile
    >>> pyver = sys.version_info.major
    >>> if pyver == 2:
    ...     tmpdirName = tempfile.mkdtemp()
    ... else:
    ...     tmpdir = tempfile.TemporaryDirectory()
    ...     tmpdirName = tmpdir.name
    >>> from numpy import load
    >>> import os
    >>> from shutil import rmtree
    >>> CoM_output = os.path.join(tmpdirName, 'CoM')
    >>> kinetics = [[246.57466721, 313.55662383, 1026.56323492],
    ...             [246.59137623, 313.6216639, 1026.56440096],
    ...             [246.60850798, 313.6856272, 1026.56531282]]
    >>> writeKinetics(CoM_output, kinetics)
    >>> load(CoM_output + '.npy') #doctest: +NORMALIZE_WHITESPACE
    array([[ 246.57466721, 313.55662383, 1026.56323492],
           [ 246.59137623, 313.6216639 , 1026.56440096],
           [ 246.60850798, 313.6856272 , 1026.56531282]])
    >>> rmtree(tmpdirName)
    """
    np.save(CoM_output,kinetics)

def writeResult(data,filename,**kargs):
        """Writes the result of the calculation into a csv file.

        Lines 0-6 of the output csv are headers. Lines 7 and onwards
        are angle or axis calculations for each frame. For example,
        line 7 of the csv is output for frame 0 of the motion capture.
        The first element of each row of ouput is the frame number.

        Parameters
        ----------
        data : array_like
            Motion capture data as a matrix of frames as rows.
            Each row is a numpy array of length 273.
            Indices 0-56 correspond to the values for angles.
            Indices 57-272 correspond to the values for axes.
            See Examples.
        filename : str
            Full path of the csv to be saved. Do not include '.csv'.
        **kargs : dict
            Dictionary of keyword arguments as follows.

            delimiter : str, optional
                String to be used as the delimiter for the csv file. The
                default delimiter is ','.
            angles : bool or array, optional
                True or false to save angles, or a list of angles to save.
                True by default.
            axis : bool or array, optional
                True or false to save axis, or a list of axis to save.
                True by default.

        Examples
        --------
        >>> from numpy import array, zeros
        >>> import os
        >>> from shutil import rmtree
        >>> import tempfile
        >>> pyver = sys.version_info.major
        >>> if pyver == 2:
        ...     tmpdirName = tempfile.mkdtemp()
        ... else:
        ...     tmpdir = tempfile.TemporaryDirectory()
        ...     tmpdirName = tmpdir.name

        Prepare a frame of data to write to csv. This example writes joint angle values
        for the first joint, the pelvis, and axis values for the pelvis origin, PELO.

        >>> frame = zeros(273)
        >>> angles = array([-0.308494914509454, -6.12129279337001, 7.57143110215171])
        >>> for i in range(len(angles)):
        ...     frame[i] = angles[i]
        >>> axis = array([-934.314880371094, -4.44443511962891, 852.837829589844])
        >>> for i in range(len(axis)):
        ...     frame[i+57] = axis[i]
        >>> data = [frame]
        >>> outfile = os.path.join(tmpdirName, 'output')

        Writing angles only.

        >>> writeResult(data, outfile, angles=True, axis=False)
        >>> with open(outfile + '.csv') as file:
        ...     lines = file.readlines()
        >>> result = lines[7].strip().split(',')
        >>> result #doctest: +NORMALIZE_WHITESPACE
        ['0.000000000000000',
         '-0.308494914509454', '-6.121292793370010', '7.571431102151710',...]

        Writing axis only.

        >>> writeResult(data, outfile, angles=False, axis=True)
        (1, 273)...
        >>> with open(outfile + '.csv') as file:
        ...     lines = file.readlines()
        >>> result = lines[7].strip().split(',')
        >>> result #doctest: +NORMALIZE_WHITESPACE
        ['0.000000000000000',
         '-934.314880371093977', '-4.444435119628910', '852.837829589843977',...]
        >>> rmtree(tmpdirName)
        """
        labelsAngs =['Pelvis','R Hip','L Hip','R Knee','L Knee','R Ankle',
                                'L Ankle','R Foot','L Foot',
                                'Head','Thorax','Neck','Spine','R Shoulder','L Shoulder',
                                'R Elbow','L Elbow','R Wrist','L Wrist']

        labelsAxis =["PELO","PELX","PELY","PELZ","HIPO","HIPX","HIPY","HIPZ","R KNEO","R KNEX","R KNEY","R KNEZ","L KNEO","L KNEX","L KNEY","L KNEZ","R ANKO","R ANKX","R ANKY","R ANKZ","L ANKO","L ANKX","L ANKY","L ANKZ","R FOOO","R FOOX","R FOOY","R FOOZ","L FOOO","L FOOX","L FOOY","L FOOZ","HEAO","HEAX","HEAY","HEAZ","THOO","THOX","THOY","THOZ","R CLAO","R CLAX","R CLAY","R CLAZ","L CLAO","L CLAX","L CLAY","L CLAZ","R HUMO","R HUMX","R HUMY","R HUMZ","L HUMO","L HUMX","L HUMY","L HUMZ","R RADO","R RADX","R RADY","R RADZ","L RADO","L RADX","L RADY","L RADZ","R HANO","R HANX","R HANY","R HANZ","L HANO","L HANX","L HANY","L HANZ"]

        outputAngs=True
        outputAxis=True
        dataFilter=None
        delimiter=","
        filterData=[]
        if 'delimiter' in kargs:
                delimiter=kargs['delimiter']
        if 'angles' in kargs:
                if kargs['angles']==True:
                        outputAngs=True
                elif kargs['angles']==False:
                        outputAngs=False
                        labelsAngs=[]
                elif isinstance(kargs['angles'], (list, tuple)):
                        filterData=[i*3 for i in range(len(labelsAngs)) if labelsAngs[i] not in kargs['angles']]
                        if len(filterData)==0:
                                outputAngs=False
                        labelsAngs=[i for i in labelsAngs if i in kargs['angles']]

        if 'axis' in kargs:
                if kargs['axis']==True:
                        outputAxis=True
                elif kargs['axis']==False:
                        outputAxis=False
                        labelsAxis=[]
                elif isinstance(kargs['axis'], (list, tuple)):
                        filteraxis=[i*3+SA for i in range(len(labelsAxis)) if labelsAxis[i] not in kargs['axis']]
                        filterData=filterData+filteraxis
                        if len(filteraxis)==0:
                                outputAxis=False
                        labelsAxis=[i for i in labelsAxis if i in kargs['axis']]

        if len(filterData)>0:
                filterData=np.repeat(filterData,3)
                filterData[1::3]=filterData[1::3]+1
                filterData[2::3]=filterData[2::3]+2

        if outputAngs==outputAxis==False:
                return
        elif outputAngs==False:
                print(np.shape(data))
                dataFilter=np.transpose(data)
                dataFilter=dataFilter[SA:EA]
                dataFilter=np.transpose(dataFilter)
                print(np.shape(dataFilter))
                print(filterData)
                filterData=[i-SA for i in filterData]
                print(filterData)
        elif outputAxis==False:
                dataFilter=np.transpose(data)
                dataFilter=dataFilter[SJA:EJA]
                dataFilter=np.transpose(dataFilter)

        if len(filterData)>0:
                if type(dataFilter)==type(None):
                        dataFilter=np.delete(data, filterData, 1)
                else:
                        dataFilter=np.delete(dataFilter, filterData, 1)
        if type(dataFilter)==type(None):
                dataFilter=data
        header=","
        headerAngs=["Joint Angle,,,",",,,x = flexion/extension angle",",,,y= abudction/adduction angle",",,,z = external/internal rotation angle",",,,"]
        headerAxis=["Joint Coordinate",",,,###O = Origin",",,,###X = X axis orientation",",,,###Y = Y axis orientation",",,,###Z = Z axis orientation"]
        for angs,axis in zip(headerAngs,headerAxis):
                if outputAngs==True:
                        header=header+angs+",,,"*(len(labelsAngs)-1)
                if outputAxis==True:
                        header=header+axis+",,,"*(len(labelsAxis)-1)
                header=header+"\n"
        labels=","
        if len(labelsAngs)>0:
                labels=labels+",,,".join(labelsAngs)+",,,"
        if len(labelsAxis)>0:
                labels=labels+",,,".join(labelsAxis)
        labels=labels+"\n"
        if pyver == 2:
            xyz="frame num,"+"X,Y,Z,"*(len(dataFilter[0])/3)
        else:
            xyz="frame num,"+"X,Y,Z,"*(len(dataFilter[0])//3)
        header=header+labels+xyz
        #Creates the frame numbers
        frames=np.arange(len(dataFilter),dtype=dataFilter[0].dtype)
        #Put the frame numbers in the first dimension of the data
        dataFilter=np.column_stack((frames,dataFilter))
        start = 1500
        end = 3600
        #dataFilter = dataFilter[start:]
        np.savetxt(filename+'.csv', dataFilter, delimiter=delimiter,header=header,fmt="%.15f")
        #np.savetxt(filename, dataFilter, delimiter=delimiter,fmt="%.15f")
        #np.savez_compressed(filename,dataFilter)

def smKeys():
    """A list of segment labels.

    Returns
    -------
    keys : array
        List of segment labels.

    Examples
    --------
    >>> smKeys() #doctest: +NORMALIZE_WHITESPACE
    ['Bodymass', 'Height', 'HeadOffset', 'InterAsisDistance', 'LeftAnkleWidth',
     'LeftAsisTrocanterDistance', 'LeftClavicleLength', 'LeftElbowWidth',
     'LeftFemurLength', 'LeftFootLength', 'LeftHandLength', 'LeftHandThickness',
     'LeftHumerusLength', 'LeftKneeWidth', 'LeftLegLength', 'LeftRadiusLength',
     'LeftShoulderOffset', 'LeftTibiaLength', 'LeftWristWidth', 'RightAnkleWidth',
     'RightClavicleLength', 'RightElbowWidth', 'RightFemurLength', 'RightFootLength',
     'RightHandLength', 'RightHandThickness', 'RightHumerusLength', 'RightKneeWidth',
     'RightLegLength', 'RightRadiusLength', 'RightShoulderOffset', 'RightTibiaLength',
     'RightWristWidth']
    """
    keys = ['Bodymass', 'Height', 'HeadOffset', 'InterAsisDistance', 'LeftAnkleWidth', 'LeftAsisTrocanterDistance',
            'LeftClavicleLength',
            'LeftElbowWidth', 'LeftFemurLength', 'LeftFootLength', 'LeftHandLength', 'LeftHandThickness',
            'LeftHumerusLength', 'LeftKneeWidth',
            'LeftLegLength', 'LeftRadiusLength', 'LeftShoulderOffset', 'LeftTibiaLength', 'LeftWristWidth',
            'RightAnkleWidth',
            'RightClavicleLength', 'RightElbowWidth', 'RightFemurLength', 'RightFootLength', 'RightHandLength',
            'RightHandThickness', 'RightHumerusLength',
            'RightKneeWidth', 'RightLegLength', 'RightRadiusLength', 'RightShoulderOffset', 'RightTibiaLength',
            'RightWristWidth',
            ]
    return keys

def loadVSK(filename,dict=True):
        """Open and load a vsk file.

        Parameters
        ----------
        filename : str
            Path to the vsk file to be loaded
        dict : bool, optional
            Returns loaded vsk file values as a dictionary if False.
            Otherwise, return as an array.

        Returns
        -------
        [vsk_keys, vsk_data] : array
            `vsk_keys` is a list of labels. `vsk_data` is a list of values
            corresponding to the labels in `vsk_keys`.

        Examples
        --------
        RoboSM.vsk in SampleData is used to test the output.

        >>> from .pyCGM_Helpers import getfilenames
        >>> filename = getfilenames(x=3)[2]
        >>> filename
        'SampleData/Sample_2/RoboSM.vsk'
        >>> result = loadVSK(filename)
        >>> vsk_keys = result[0]
        >>> vsk_data = result[1]
        >>> vsk_keys
        ['Bodymass', 'Height', 'InterAsisDistance',...]
        >>> vsk_data
        [72.0, 1730.0, 281.118011474609,...]

        Return as a dictionary.

        >>> result = loadVSK(filename, False)
        >>> type(result)
        <...'dict'>

        Testing for some dictionary values.

        >>> result['Bodymass']
        72.0
        >>> result['RightStaticPlantFlex']
        0.17637075483799
        """
        #Check if the filename is valid
        #if not, return None
        if filename == '':
                return None

        # Create Dictionary to store values from VSK file
        viconVSK = {}
        vskMarkers = []

        #Create an XML tree from file
        tree = ET.parse(filename)
        #Get the root of the file
        # <KinematicModel>
        root = tree.getroot()

        #Store the values of each parameter in a dictionary
        # the format is (NAME,VALUE)
        vsk_keys=[r.get('NAME') for r in root[0]]
        vsk_data = []
        for R in root[0]:
            val = (R.get('VALUE'))
            if val == None:
                val = 0
            vsk_data.append(float(val))

        #vsk_data=np.asarray([float(R.get('VALUE')) for R in root[0]])
        #print vsk_keys
        if dict==False: return createVskDataDict(vsk_keys,vsk_data)

        return [vsk_keys,vsk_data]

def splitDataDict(motionData):
    """Splits an array of motion capture data into separate labels and data.

    Parameters
    ----------
    motionData : array
        List of dict. Indices of `motionData` correspond to frames
        in the trial. Keys in the dictionary are marker names and
        values are xyz coordinates of the corresponding marker.

    Returns
    -------
    labels, data : tuple
        `labels` is a list of marker position names from the dictionary
        keys in `motiondata`. `data` is a list of xyz coordinate
        positions corresponding to the marker names in `labels`.
        Indices of `data` correspond to frames in the trial.

    Examples
    --------
    Example for three markers and two frames of trial.

    >>> from numpy import array
    >>> motionData = [{'RFHD': array([325.82985131, 402.55452959, 1722.49816649]),
    ...                'LFHD': array([184.55160796, 409.68716101, 1721.34289625]),
    ...                'LBHD': array([197.8621642 , 251.28892152, 1696.90197756])},
    ...               {'RFHD': array([326.82985131, 403.55452959, 1723.49816649]),
    ...                'LFHD': array([185.55160796, 408.68716101, 1722.34289625]),
    ...                'LBHD': array([198.8621642 , 252.28892152, 1697.90197756])}]
    >>> values, labels = splitDataDict(motionData)
    >>> labels
    ['RFHD', 'LFHD', 'LBHD']
    >>> values #doctest: +NORMALIZE_WHITESPACE
    [array([[ 325.82985131,  402.55452959, 1722.49816649],
            [ 184.55160796,  409.68716101, 1721.34289625],
            [ 197.8621642 ,  251.28892152, 1696.90197756]]),
     array([[ 326.82985131,  403.55452959, 1723.49816649],
            [ 185.55160796,  408.68716101, 1722.34289625],
            [ 198.8621642 ,  252.28892152, 1697.90197756]])]
    """
    if pyver == 2:
        labels = motionData[0].keys()
        values = []
        for i in range(len(motionData)):
            values.append(np.asarray(motionData[i].values()))

        return values,labels

    if pyver == 3:
        labels = list(motionData[0].keys())
        values = []
        for i in range(len(motionData)):
            values.append(np.asarray(list(motionData[i].values())))

        return values,labels

def combineDataDict(values,labels):
    """Converts two lists of values and labels to a dictionary.

    Parameters
    ----------
    values : array
        Array of motion data values. Indices of `values` correspond to
        frames in the trial. Each element is an array of xyz coordinates.
    labels : array
        List of marker names.

    Returns
    -------
    data : array
        Array of dictionaries of motion capture data. Keys are marker
        names and values are arrays of xyz values. [x, y, z]. Array
        indices correspond to frames of the trial.

    Examples
    --------
    Example for three markers and two frames of trial.

    >>> from numpy import array_equal
    >>> labels = ['RFHD', 'LFHD', 'LBHD']
    >>> values = [[[ 325.82985131,  402.55452959, 1722.49816649],
    ...            [ 184.55160796,  409.68716101, 1721.34289625],
    ...            [ 197.8621642 ,  251.28892152, 1696.90197756]],
    ...           [[ 326.82985131,  403.55452959, 1723.49816649],
    ...            [ 185.55160796,  408.68716101, 1722.34289625],
    ...            [ 198.8621642 ,  252.28892152, 1697.90197756]]]
    >>> result = combineDataDict(values, labels)
    >>> expected = [{'RFHD': [325.82985131, 402.55452959, 1722.49816649],
    ...              'LFHD': [184.55160796, 409.68716101, 1721.34289625],
    ...              'LBHD': [197.8621642, 251.28892152, 1696.90197756]},
    ...              {'RFHD': [326.82985131, 403.55452959, 1723.49816649],
    ...              'LFHD': [185.55160796, 408.68716101, 1722.34289625],
    ...              'LBHD': [198.8621642, 252.28892152, 1697.90197756]}]
    >>> flag = True #False if any values are not equal
    >>> for i in range(len(result)):
    ...     for key in result[i]:
    ...         if (not array_equal(result[i][key], expected[i][key])):
    ...             flag = False
    >>> flag
    True
    """
    data = []
    tmp_dict = {}
    for i in range (len(values)):
        for j in range (len(values[i])):
            tmp_dict[labels[j]]=values[i][j]
        data.append(tmp_dict)
        tmp_dict = {}

    return data

def make_sure_path_exists(path):
    """Creates a file path.

    Parameters
    ----------
    path : str
        String of the full file path of the directory to be created.

    Raises
    ------
    Exception
        Raised if the path was unable to be created for any reason
        other than the directory already existing.

    Examples
    --------
    >>> import os
    >>> from shutil import rmtree
    >>> import tempfile
    >>> pyver = sys.version_info.major
    >>> if pyver == 2:
    ...     tmpdirName = tempfile.mkdtemp()
    ... else:
    ...     tmpdir = tempfile.TemporaryDirectory()
    ...     tmpdirName = tmpdir.name
    >>> newDirectory = os.path.join(tmpdirName, 'newDirectory')
    >>> make_sure_path_exists(newDirectory)
    >>> os.path.isdir(newDirectory)
    True
    >>> rmtree(tmpdirName)
    """
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

max_stars_repo_name: niravkin/pyCGM

max_issues_repo_name: niravkin/pyCGM

max_forks_repo_name: niravkin/pyCGM

reason: import numpy,from numpy


text:
import numpy as np
import cv2 as cv
import torch

def numpy_to_torch(array, dtype):
    '''
    convert array to tensor, and change chanel order.
    '''
    t = torch.tensor(array, dtype= dtype)
    t = t.permute(2, 0, 1)
    return t

def torch_to_numpy(tensor):
    '''
    convert tensor to array, and change chanel order.
    '''
    tensor = tensor.permute(1, 2, 0)
    a = tensor.numpy()
    return a

def mask_to_label(mask):
    '''
    type: troch.tensor
    convert mask to label map (1 chanel)
    '''
    label =  (mask[0] > 128)*1
    label += (mask[1] > 128)*2
    label += (mask[2] > 128)*4
    return label.long()

def label_to_mask(label):
    '''
    type: torch.tensor
    convert label map to mask (3 chanels)
    '''
    w, h = label.size()
    mask = torch.zeros((3, w, h), dtype= torch.long)
    mask[0] = label%2
    label = (label - label%2)/2
    mask[1] = label%2
    label = (label - label%2)/2
    mask[2] = label%2

    mask = mask * 255
    return mask

def read_sat_image(path):
    '''
    read sat image and convert
    '''
    img = cv.imread(path)
    img = numpy_to_torch(img, torch.float)
    return img

def read_mask_to_label(path):
    '''
    read mask and convert to label
    '''
    msk = cv.imread(path)
    msk = numpy_to_torch(msk, torch.long)
    label = mask_to_label(msk)
    return label

def process(img, name):
    '''
    convert mask.png to label map then convert back,
    save with name
    '''
    label = read_mask_to_label(img)
    mask = label_to_mask(label)
    mask = torch_to_numpy(mask)
    cv.imwrite(name, mask)

def mean_iou(label_x, label_y):
    '''
    type: torch
    labels: 1, 2, 3, 5, 6, 7
    label_x : prediction
    label_y : ground truth
    '''
    labels = [1, 2, 3, 4, 6, 7]
    iou = 0
    class_num = 0
    for label in labels:
        x = torch.sum(label_x == label).item()
        y = torch.sum(label_y == label).item()
        both = torch.sum((label_x == label) * (label_y == label)).item()
        if y > 0: # Calculate mean iou only when ground truth has that class
            iou += both/(x + y - both)
            class_num = class_num + 1

    mean_iou = iou/class_num
    return mean_iou

def mean_iou_batch(batch_x, batch_y):
    num = batch_x.size(0)
    iou = 0
    for i in range(num):
        iou = iou + mean_iou(batch_x[i], batch_y[i])
    iou = iou / num
    return iou

def pixel_wise_accuracy(label_x, label_y):
    '''
    type: torch tensor
    calculate accuracy for each pixel
    '''
    w, h = label_x.size(0), label_x.size(1)
    match_map = (label_x == label_y)
    match = torch.sum(match_map).item()
    acc = match/(w*h)
    return acc

def pixel_wise_accuracy_batch(batch_x, batch_y):
    '''
    type: torch tensor
    calculate pixel-wise accuracy for two batches
    '''
    num = batch_x.size(0)
    acc = 0
    for i in range(num):
        acc = acc + pixel_wise_accuracy(batch_x[i], batch_y[i])
    acc = acc / num
    return acc

def test():
    label_1 = read_mask_to_label('0028_mask.png')
    label_2 = read_mask_to_label('0028_mask.png')
    m_iou = mean_iou(label_1, label_2)
    print('Mean IOU:', m_iou)

if __name__ == '__main__':
    process('0028_mask.png', 'test.png')


text:
import pandas as pd
import numpy as np
from chariot.transformer.formatter.base import BaseFormatter
from chariot.preprocessor import Preprocessor

class Padding(BaseFormatter):

    def __init__(self, padding=0, length=-1,
                 begin_of_sequence=False, end_of_sequence=False,
                 begin_of_sequence_index=-1, end_of_sequence_index=-1):
        super().__init__()
        self.padding = padding
        self.length = length

        self.begin_of_sequence = begin_of_sequence
        self.end_of_sequence = end_of_sequence
        self._begin_of_sequence = begin_of_sequence_index
        self._end_of_sequence = end_of_sequence_index

        if type(self.begin_of_sequence) != bool:
            self._begin_of_sequence = self.begin_of_sequence
            self.begin_of_sequence = True

        if type(self.end_of_sequence) != bool:
            self._end_of_sequence = self.end_of_sequence
            self.end_of_sequence = True

    def transfer_setting(self, vocabulary_or_preprocessor):
        vocabulary = vocabulary_or_preprocessor
        if isinstance(vocabulary_or_preprocessor, Preprocessor):
            vocabulary = vocabulary_or_preprocessor.vocabulary

        self.padding = vocabulary.pad
        if self.begin_of_sequence:
            self._begin_of_sequence = vocabulary.bos
        if self.end_of_sequence:
            self._end_of_sequence = vocabulary.eos

    def transform(self, column):
        def adjust(sequence, length):
            if self.begin_of_sequence:
                sequence = [self._begin_of_sequence] + sequence
            if self.end_of_sequence:
                sequence = sequence + [self._end_of_sequence]

            if length > 0:
                if len(sequence) < length:
                    pad_size = length - len(sequence)
                    sequence = sequence + [self.padding] * pad_size
                elif len(sequence) > length:
                    sequence = sequence[:length]

            return np.array(sequence)

        length = self.length
        if length < 0:
            length = max([len(seq) for seq in column])

        if isinstance(column, pd.Series):
            applied = column.apply(lambda x: adjust(x, length)).tolist()
            return np.array(applied)
        else:
            return np.array([adjust(x, length) for x in column])

    def inverse_transform(self, column):
        def inverse(sequence):
            excludes = [self.padding,
                        self._begin_of_sequence, self._end_of_sequence]
            inversed = [t for t in sequence if t not in excludes]
            return inversed

        if isinstance(column, pd.Series):
            return column.apply(lambda x: inverse(x))
        else:
            return [inverse(x) for x in column]

    def __reduce_ex__(self, proto):
        return type(self), (self.padding, self.length,
                            self.begin_of_sequence, self.end_of_sequence,
                            self._begin_of_sequence, self._end_of_sequence)

max_stars_repo_name: Y-Kuro-u/chariot

max_issues_repo_name: Y-Kuro-u/chariot

max_forks_repo_name: Y-Kuro-u/chariot


text:
import logging
import random
import sys

import agent_pb2
import agent_pb2_grpc
import cv2
import grpc
import numpy as np
from PIL import Image

logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

# Uncomment this for grpc debug log
# os.environ['GRPC_VERBOSITY'] = 'DEBUG'
SIZE = 512
TENSOR_SHAPE = [1, 3, 512, 512]
TENSOR_NAME = "data"
DATA_TYPE = 5

class EdgeManagerClient:
    def __init__(self):
        self.agent_socket = "unix:///tmp/aws.greengrass.SageMakerEdgeManager.sock"
        self.agent_channel = grpc.insecure_channel(
            self.agent_socket, options=(("grpc.enable_http_proxy", 0),)
        )
        self.agent_client = agent_pb2_grpc.AgentStub(self.agent_channel)

    def list_models(self):
        return self.agent_client.ListModels(agent_pb2.ListModelsRequest())

    def list_model_tensors(self, models):
        return {
            model.name: {
                "inputs": model.input_tensor_metadatas,
                "outputs": model.output_tensor_metadatas,
            }
            for model in self.list_models().models
        }

    def load_model(self, model_name, model_path):
        load_request = agent_pb2.LoadModelRequest()
        load_request.url = model_path
        load_request.name = model_name
        return self.agent_client.LoadModel(load_request)

    def unload_model(self, name):
        unload_request = agent_pb2.UnLoadModelRequest()
        unload_request.name = name
        return self.agent_client.UnLoadModel(unload_request)

    def predict_image(self, model_name, img):
        logger.info("predict_image()")
        image_tensor = agent_pb2.Tensor()
        image_tensor.byte_data = img.tobytes()
        image_tensor.tensor_metadata.name = TENSOR_NAME
        image_tensor.tensor_metadata.data_type = DATA_TYPE
        image_tensor.tensor_metadata.shape.extend(TENSOR_SHAPE)
        predict_request = agent_pb2.PredictRequest()
        predict_request.name = model_name
        predict_request.tensors.append(image_tensor)
        predict_response = self.agent_client.Predict(predict_request)
        logger.info(predict_response)
        return predict_response

    def get_prediction(self, model_name, img):
        logger.info("get_prediction()")
        # Mean and Std deviation of the RGB colors (collected from Imagenet dataset)

        try:
            mean = [123.68, 116.779, 103.939]
            std = [58.393, 57.12, 57.375]

            frame = self.resize_short_within(img, short=SIZE, max_size=SIZE * 2)
            nn_input_size = SIZE
            nn_input = cv2.resize(frame, (nn_input_size, int(nn_input_size / 4 * 3)))
            nn_input = cv2.copyMakeBorder(
                nn_input,
                int(nn_input_size / 8),
                int(nn_input_size / 8),
                0,
                0,
                cv2.BORDER_CONSTANT,
                value=(0, 0, 0),
            )
            copy_frame = nn_input[:]
            nn_input = nn_input.astype("float32")
            nn_input = nn_input.reshape((nn_input_size * nn_input_size, 3))
            scaled_frame = np.transpose(nn_input)
            scaled_frame[0, :] = scaled_frame[0, :] - mean[0]
            scaled_frame[0, :] = scaled_frame[0, :] / std[0]
            scaled_frame[1, :] = scaled_frame[1, :] - mean[1]
            scaled_frame[1, :] = scaled_frame[1, :] / std[1]
            scaled_frame[2, :] = scaled_frame[2, :] - mean[2]
            scaled_frame[2, :] = scaled_frame[2, :] / std[2]

            predict_response = self.predict_image(model_name, scaled_frame)

            i = 0
            detections = []

            for t in predict_response.tensors:
                # print("Flattened RAW Output Tensor : " + str(i + 1))
                i += 1
                deserialized_bytes = np.frombuffer(t.byte_data, dtype=np.float32)
                detections.append(np.asarray(deserialized_bytes))

            # convert the bounding boxes
            new_list = []
            for index, item in enumerate(detections[2]):
                if index % 4 == 0:
                    new_list.append(detections[2][index - 4 : index])
            detections[2] = new_list[1:]

            # get classes, scores, bboxes
            classes = detections[0]
            scores = detections[1]
            bounding_boxes = new_list[1:]

            # logger.info(bounding_boxes)
            # logger.info(scores)
            # logger.info(classes)

            return bounding_boxes, scores, classes

        except Exception as e:
            logger.error(e)

    def _get_interp_method(self, interp, sizes=()):
        """Get the interpolation method for resize functions.
        The major purpose of this function is to wrap a random interp method selection
        and a auto-estimation method.

        Parameters
        ----------
        interp : int
            interpolation method for all resizing operations

            Possible values:
            0: Nearest Neighbors Interpolation.
            1: Bilinear interpolation.
            2: Area-based (resampling using pixel area relation). It may be a
            preferred method for image decimation, as it gives moire-free
            results. But when the image is zoomed, it is similar to the Nearest
            Neighbors method. (used by default).
            3: Bicubic interpolation over 4x4 pixel neighborhood.
            4: Lanczos interpolation over 8x8 pixel neighborhood.
            9: Cubic for enlarge, area for shrink, bilinear for others
            10: Random select from interpolation method metioned above.
            Note:
            When shrinking an image, it will generally look best with AREA-based
            interpolation, whereas, when enlarging an image, it will generally look best
            with Bicubic (slow) or Bilinear (faster but still looks OK).
            More details can be found in the documentation of OpenCV, please refer to
            http://docs.opencv.org/master/da/d54/group__imgproc__transform.html.
        sizes : tuple of int
            (old_height, old_width, new_height, new_width), if None provided, auto(9)
            will return Area(2) anyway.

        Returns
        -------
        int
            interp method from 0 to 4
        """
        if interp == 9:
            if sizes:
                assert len(sizes) == 4
                oh, ow, nh, nw = sizes
                if nh > oh and nw > ow:
                    return 2
                elif nh < oh and nw < ow:
                    return 3
                else:
                    return 1
            else:
                return 2
        if interp == 10:
            return random.randint(0, 4)
        if interp not in (0, 1, 2, 3, 4):
            raise ValueError("Unknown interp method %d" % interp)

    def resize_short_within(
        self, img, short=512, max_size=1024, mult_base=32, interp=2
    ):
        """
        resizes the short side of the image so the aspect ratio remains the same AND the short
        side matches the convolutional layer for the network

        Args:
        -----
        img: np.array
            image you want to resize
        short: int
            the size to reshape the image to
        max_size: int
            the max size of the short side
        mult_base: int
            the size scale to readjust the resizer
        interp: int
            see '_get_interp_method'
        Returns:
        --------
        img: np.array
            the resized array
        """
        h, w, _ = img.shape
        im_size_min, im_size_max = (h, w) if w > h else (w, h)
        scale = float(short) / float(im_size_min)
        if np.round(scale * im_size_max / mult_base) * mult_base > max_size:
            # fit in max_size
            scale = float(np.floor(max_size / mult_base) * mult_base) / float(
                im_size_max
            )
        new_w, new_h = (
            int(np.round(w * scale / mult_base) * mult_base),
            int(np.round(h * scale / mult_base) * mult_base),
        )
        img = cv2.resize(
            img,
            (new_w, new_h),
            interpolation=self._get_interp_method(interp, (h, w, new_h, new_w)),
        )
        return img


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb  3 12:42:48 2020

@author: brycehaley
"""
import pandas as pd
import numpy as np
import os

def import_csv_from2() -> pd.DataFrame:
    path = os.path.join('..', '..', 'data', 'interim', '2_tbs_with_datetime.csv')
    df =  pd.read_csv(path, index_col = 0)
    df['trip_start_time'] = pd.to_datetime(df['trip_start_time'], format='%Y-%m-%d %H:%M:%S')
    df['trip_stop_time'] = pd.to_datetime(df['trip_stop_time'], format='%Y-%m-%d %H:%M:%S')
    return df

def move_date_fillna(df: pd.DataFrame) -> pd.DataFrame:
    df['nan_row_flag'] = ( df['to_station_name'].isnull()) & (df['user_type'].isnull())

    df_badcols = df[df['nan_row_flag'] == True]
    df_badcols.loc[:,'user_type'] = df_badcols.loc[:,'to_station_id']
    df_badcols.loc[:,'to_station_name'] = df_badcols.loc[:,'from_station_name']
    df_badcols.loc[:,'to_station_id'] = np.nan
    df_badcols.loc[:,'from_station_name'] = df_badcols.loc[:,'from_station_id']
    df_badcols.loc[:,'from_station_id'] = np.nan

    df.loc[df.index.isin(df_badcols.index), :] = df_badcols

    df_good_cols = df[df['nan_row_flag'] == False]
    df_good_cols.loc[:,'to_station_id'] = df_good_cols.loc[:,'to_station_id'].astype(int)
    df_good_cols.loc[:,'from_station_id'] = df_good_cols.loc[:,'from_station_id'].astype(int)

    df.loc[df.index.isin(df_good_cols.index), :] = df_good_cols

    return df

def is_one_station_for_name(df: pd.DataFrame) -> bool:
    # df_goodcols_big.groupby(['to_station_id','to_station_name']).size().reset_index().rename(columns={0:'count'})
    df_test_from = df.groupby(['from_station_id','from_station_name']).size().reset_index().rename(columns={0:'count'})
    from_bool = df_test_from[df_test_from['from_station_id'].duplicated(keep=False)].shape[0]  == 0

    df_test_to = df.groupby(['to_station_id','to_station_name']).size().reset_index().rename(columns={0:'count'})
    to_bool = df_test_to[df_test_to['to_station_id'].duplicated(keep=False)].shape[0]  == 0

    return to_bool & from_bool

def make_name_id(df: pd.DataFrame) -> pd.DataFrame:
    df_good_cols = df[df['nan_row_flag'] == False]

    assert is_one_station_for_name(df_good_cols)

    name_id = pd.Series(df_good_cols['from_station_id'].values, index = df_good_cols['from_station_name'])
    return name_id

def main():
    df = import_csv_from2()
    df = move_date_fillna(df)

    #name_id_df = make_name_id(df)
    #print(name_id_df)
    save_name_id_path = os.path.join('..', '..', 'data', 'processed', '3_tbs_name_id.csv')
    df.to_csv(save_name_id_path)

    df = df.drop(columns =['to_station_id', 'from_station_id', 'nan_row_flag'])

    save_path = os.path.join('..', '..', 'data', 'interim', '3_tbs_all_filled.csv')
    df.to_csv(save_path)

if __name__ == '__main__':
    main()