text:
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown Copyright 2017-2020 Met Office.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Unit tests for the InterpolateUsingDifference plugin."""

import unittest

import numpy as np
from numpy.testing import assert_array_equal

from improver.utilities.interpolation import InterpolateUsingDifference
from improver.utilities.warnings_handler import ManageWarnings

from ..set_up_test_cubes import add_coordinate, set_up_variable_cube

class Test_Setup(unittest.TestCase):

    """Set up for InterpolateUsingDifference tests."""

    def setUp(self):
        """ Set up arrays for testing."""
        snow_sleet = np.array(
            [[5.0, 5.0, 5.0], [10.0, 10.0, 10.0], [5.0, 5.0, 5.0]], dtype=np.float32
        )
        sleet_rain = np.array(
            [[4.0, 4.0, 4.0], [np.nan, np.nan, np.nan], [3.0, 3.0, 3.0]],
            dtype=np.float32,
        )
        sleet_rain = np.ma.masked_invalid(sleet_rain)
        limit_data = np.array(
            [[4.0, 4.0, 4.0], [10.0, 8.0, 6.0], [4.0, 4.0, 4.0]], dtype=np.float32
        )

        self.snow_sleet = set_up_variable_cube(
            snow_sleet,
            name="altitude_of_snow_falling_level",
            units="m",
            spatial_grid="equalarea",
        )
        self.sleet_rain = set_up_variable_cube(
            sleet_rain,
            name="altitude_of_rain_falling_level",
            units="m",
            spatial_grid="equalarea",
        )
        self.limit = set_up_variable_cube(
            limit_data, name="surface_altitude", units="m", spatial_grid="equalarea"
        )

class Test_repr(unittest.TestCase):

    """Test the InterpolateUsingDifference __repr__ method."""

    def test_basic(self):
        """Test expected string representation is returned."""
        self.assertEqual(
            str(InterpolateUsingDifference()), "<InterpolateUsingDifference>"
        )

class Test__check_inputs(Test_Setup):

    """Tests for the private _check_inputs method."""

    def test_incomplete_reference_data(self):
        """Test an exception is raised if the reference field is incomplete."""

        self.snow_sleet.data[1, 1] = np.nan
        msg = "The reference cube contains np.nan data"
        with self.assertRaisesRegex(ValueError, msg):
            InterpolateUsingDifference()._check_inputs(
                self.sleet_rain, self.snow_sleet, None
            )

    def test_incompatible_reference_cube_units(self):
        """Test an exception is raised if the reference cube has units that
        are incompatible with the input cube."""

        self.snow_sleet.units = "s"
        msg = "Reference cube and/or limit do not have units compatible"
        with self.assertRaisesRegex(ValueError, msg):
            InterpolateUsingDifference()._check_inputs(
                self.sleet_rain, self.snow_sleet, None
            )

    def test_incompatible_limit_units(self):
        """Test an exception is raised if the limit cube has units that
        are incompatible with the input cube."""

        self.limit.units = "s"
        msg = "Reference cube and/or limit do not have units compatible"
        with self.assertRaisesRegex(ValueError, msg):
            InterpolateUsingDifference()._check_inputs(
                self.sleet_rain, self.snow_sleet, limit=self.limit
            )

    def test_convert_units(self):
        """Test that a reference cube and limit cube with different but
        compatible units are converted without an exception being raised."""

        self.snow_sleet.convert_units("cm")
        self.limit.convert_units("cm")

        InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet, limit=self.limit
        )

class Test_process(Test_Setup):

    """Test the InterpolateUsingDifference process method."""

    def test_unlimited(self):
        """Test interpolation to complete an incomplete field using a reference
        field. No limit is imposed upon the returned interpolated values."""

        expected = np.array(
            [[4.0, 4.0, 4.0], [8.5, 8.5, 8.5], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(self.sleet_rain, self.snow_sleet)

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)

    def test_maximum_limited(self):
        """Test interpolation to complete an incomplete field using a reference
        field. A limit is imposed upon the returned interpolated values,
        forcing these values to the maximum limit if they exceed it."""

        expected = np.array(
            [[4.0, 4.0, 4.0], [8.5, 8.0, 6.0], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet, limit=self.limit, limit_as_maximum=True
        )

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)

    def test_minimum_limited(self):
        """Test interpolation to complete an incomplete field using a reference
        field. A limit is imposed upon the returned interpolated values,
        forcing these values to the minimum limit if they are below it."""

        expected = np.array(
            [[4.0, 4.0, 4.0], [10.0, 8.5, 8.5], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet, limit=self.limit, limit_as_maximum=False
        )

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)

    def test_multi_realization_limited(self):
        """Test interpolation to complete an incomplete field using a reference
        field. A limit is imposed upon the returned interpolated values,
        forcing these values to the minimum limit if they are below it. The
        inputs are multi-realization."""

        snow_sleet = add_coordinate(self.snow_sleet, [0, 1], "realization")
        sleet_rain = add_coordinate(self.sleet_rain, [0, 1], "realization")

        expected = np.array(
            [[4.0, 4.0, 4.0], [10.0, 8.5, 8.5], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(
            sleet_rain, snow_sleet, limit=self.limit, limit_as_maximum=False
        )

        assert_array_equal(result[0].data, expected)
        assert_array_equal(result[1].data, expected)
        self.assertEqual(result.shape, sleet_rain.shape)
        self.assertEqual(result.coords(), sleet_rain.coords())
        self.assertEqual(result.metadata, sleet_rain.metadata)

    def test_crossing_values(self):
        """Test interpolation when the reference field and field to be
        completed by interpolation cross one another. In the absence of any
        limit it should be possible to return an interpolated field of values
        that pass through the reference field in an expected way. In another
        case we apply the reference field as a lower bound to the interpolated
        values."""

        snow_sleet = np.array(
            [[15.0, 15.0, 15.0], [10.0, 10.0, 10.0], [8.0, 8.0, 8.0]], dtype=np.float32
        )

        sleet_rain = np.array(
            [[5.0, 5.0, 5.0], [np.nan, np.nan, np.nan], [15.0, 15.0, 15.0]],
            dtype=np.float32,
        )
        sleet_rain = np.ma.masked_invalid(sleet_rain)

        self.snow_sleet.data = snow_sleet
        self.sleet_rain.data = sleet_rain

        expected_unlimited = np.array(
            [[5.0, 5.0, 5.0], [8.5, 8.5, 8.5], [15.0, 15.0, 15.0]], dtype=np.float32
        )
        expected_limited = np.array(
            [[5.0, 5.0, 5.0], [10.0, 10.0, 10.0], [15.0, 15.0, 15.0]], dtype=np.float32
        )

        result_unlimited = InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet
        )

        result_limited = InterpolateUsingDifference().process(
            self.sleet_rain,
            self.snow_sleet,
            limit=self.snow_sleet,
            limit_as_maximum=False,
        )

        assert_array_equal(result_unlimited.data, expected_unlimited)
        assert_array_equal(result_limited.data, expected_limited)

    def test_linear_failure(self):
        """Test that if the use of linear interpolation does not result in a
        complete difference field, and thus a complete field of interest, the
        secondary use of nearest neighbour interpolation completes the
        field."""

        sleet_rain = np.array(
            [[np.nan, np.nan, 4.0], [np.nan, np.nan, np.nan], [3.0, 3.0, 3.0]],
            dtype=np.float32,
        )
        sleet_rain = np.ma.masked_invalid(sleet_rain)
        self.sleet_rain.data = sleet_rain

        expected = np.array(
            [[3.5, 4.0, 4.0], [8.5, 8.5, 8.5], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(self.sleet_rain, self.snow_sleet)

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)

    @ManageWarnings(record=True)
    def test_unmasked_input_cube(self, warning_list=None):
        """Test a warning is raised if the input cube is not masked and that
        the input cube is returned unchanged."""

        self.sleet_rain.data = np.ones((3, 3), dtype=np.float32)
        expected = self.sleet_rain.copy()
        warning_msg = "Input cube unmasked, no data to fill in, returning"

        result = InterpolateUsingDifference().process(self.sleet_rain, self.snow_sleet)

        self.assertEqual(result, expected)
        self.assertTrue(any(item.category == UserWarning for item in warning_list))
        self.assertTrue(any(warning_msg in str(item) for item in warning_list))

    def test_convert_units(self):
        """Test that a reference cube and limit cube with different but
        compatible units are converted for use and return the expected
        result."""

        expected = np.array(
            [[4.0, 4.0, 4.0], [8.5, 8.0, 6.0], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        self.snow_sleet.convert_units("cm")
        self.limit.convert_units("cm")

        result = InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet, limit=self.limit
        )

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)

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

max_stars_repo_name: pnijhara/improver

max_issues_repo_name: pnijhara/improver

max_forks_repo_name: pnijhara/improver


text:
import os,sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

_n=115.131 # factor de correción para llevar a 10^4 hab

#datos vacunacion
df1=pd.read_csv('vacunas/datos/primera.csv',sep=',').sort_values(by='fecha').set_index('fecha').fillna(0)
df2=pd.read_csv('vacunas/datos/segunda.csv',sep=',').sort_values(by='fecha').set_index('fecha').fillna(0)

#ambas concentraciones de datos tienen distintos etiquetados.
#se replican los ordenes en distintos vectores.
#bdep_v=['Beni','Chuquisaca','Cochabamba','La Paz','Oruro','Pando','Potosi','Santa Cruz','Tarija']
c_dep = _n*(1/10)*np.array([4.19,5.78,17.52,27.03,4.92,1.10,8.23,26.42,4.81])  #el porcentaje

y=df1.index.values       #Se recuperaron los datos de la fuente

v_v1 = df1.rolling(7,min_periods=1).mean()
v_v2 = df2.rolling(7,min_periods=1).mean()
mv_v1 = v_v1.iloc[:,:].values.T
mv_v2 = v_v2.iloc[:,:].values.T

mm_v1=np.zeros((9,len(mv_v1[0])))
mm_v2=np.zeros((9,len(mv_v2[0])))

for j in range(9):
    mm_v1[j,0]=mv_v1[j,0]/c_dep[j]
    mm_v2[j,0]=mv_v2[j,0]/c_dep[j]
    for i in range(1,len(mv_v1[0])):
        mm_v1[j,i]=(mv_v1[j,i]-mv_v1[j,i-1])/c_dep[j]
        mm_v2[j,i]=(mv_v2[j,i]-mv_v2[j,i-1])/c_dep[j]

from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
import matplotlib.image as mpimg
from matplotlib import font_manager as fm, rcParams

fpath = os.path.join(r'MonoLisaSimpson-Regular.ttf')
prop = fm.FontProperties(fname=fpath)
fname = os.path.split(fpath)[1]

# These are the "Tableau 20" colors as RGB.
tableau20 = [(48,48,48), (240,240,240), (59,170,6), (61,167,249),
             (230,0,0)]

# Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.
for i in range(len(tableau20)):
    r, g, b = tableau20[i]
    tableau20[i] = (r / 255., g / 255., b / 255.)

vac1 = mm_v1[0]+mm_v1[1]+mm_v1[2]+mm_v1[3]+mm_v1[4]+mm_v1[5]+mm_v1[6]+mm_v1[7]+mm_v1[8]
vac2 = mm_v2[0]+mm_v2[1]+mm_v2[2]+mm_v2[3]+mm_v2[4]+mm_v2[5]+mm_v2[6]+mm_v2[7]+mm_v2[8]

bol = mpimg.imread('bol.jpg')
imagebox = OffsetImage(bol,zoom=1)
firma = AnnotationBbox(imagebox,(len(y)/2,280))

fig = plt.figure(figsize=(50,30))

#Color del fondo
fig.patch.set_facecolor(tableau20[0])
plt.axes().patch.set_facecolor(tableau20[0])

plt.subplots_adjust(top=0.80)
plt.title('\nTasa de vacunación cada 10\'000 Hab a nivel Nacional:'+'\n(último reporte en fuente: '+y[-1]+')\n',fontsize=75,fontproperties=prop,color=tableau20[1])
plt.plot(y,np.abs(vac1),label='Promedio 7 días',linewidth=4,color=tableau20[3],linestyle='-')
plt.plot(y,vac2,label='Promedio 7 días',linewidth=4,color=tableau20[2],linestyle='-')

plt.yticks(fontsize=60,fontproperties=prop,color=tableau20[1])
plt.xticks(y[::7],fontsize=50,rotation=45,fontproperties=prop,color=tableau20[1])
plt.ylabel('\nVacunados/día\n',fontsize=60,fontproperties=prop,color=tableau20[1])
plt.gca().yaxis.grid(linestyle='--',linewidth=1,dashes=(5,15))
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["bottom"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.gca().spines["left"].set_visible(False)
plt.gca().get_xaxis().tick_bottom()
plt.gca().get_yaxis().tick_left()
plt.gca().add_artist(firma)
plt.subplots_adjust(bottom=0.2)
plt.text(len(mv_v1[0]), vac1[-1],'1er\nDosis',fontsize=30,color=tableau20[3])
plt.text(len(mv_v2[0]), vac2[-1],'2da\nDosis',fontsize=30,color=tableau20[2])

plt.text(0,-1.35*np.max(vac2),"Data source: https://www.udape.gob.bo/index.php?option=com_wrapper&view=wrapper&Itemid=104"
       "\nAutor: Telegram Bot: @Bolivian_Bot"
       "\nNota: Curva de vacunas/día ajustada a 10 000 hab",fontsize=35,fontproperties=prop,color=tableau20[1]);
plt.savefig('imagenes/ratevacnac.png')

ext: py

reason: import numpy


text:
#!/usr/bin/env python
# Copyright 2014-2020 The PySCF Developers. All Rights Reserved.
#
# 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 ctypes
import unittest
from functools import reduce
import tempfile
import numpy
import h5py
from pyscf import lib
from pyscf import gto
from pyscf import ao2mo
from pyscf.ao2mo import semi_incore

mol = gto.Mole()
mol.atom = [
    ['H' , (0. , 0. , .917)],
    ['F' , (0. , 0. , 0.)], ]
mol.basis = '6311g'
mol.build()
nao = mol.nao_nr()
eri = mol.intor('int2e_sph', aosym='s8')

def tearDownModule():
    global mol, eri
    del mol, eri

class KnowValues(unittest.TestCase):
    def test_general(self):
        numpy.random.seed(15)
        nmo = 12
        mo = numpy.random.random((nao,nmo))
        eriref = ao2mo.incore.full(eri, mo)

        tmpfile = tempfile.NamedTemporaryFile(dir=lib.param.TMPDIR)
        io_size = nao**2*4e-5

        semi_incore.general(eri, [mo]*4, tmpfile.name, ioblk_size=io_size)
        with ao2mo.load(tmpfile) as eri_mo:
            self.assertAlmostEqual(abs(eriref - eri_mo[:]).max(), 0, 9)

        semi_incore.general(eri, [mo]*4, tmpfile.name, ioblk_size=io_size,
                            compact=False)
        with ao2mo.load(tmpfile) as eri_mo:
            eriref = ao2mo.restore(1, eriref, nmo).reshape(nmo**2,nmo**2)
            self.assertAlmostEqual(abs(eriref - eri_mo[:]).max(), 0, 9)

    def test_general_complex(self):
        numpy.random.seed(15)
        nmo = 12
        mo = numpy.random.random((nao,nmo)) + numpy.random.random((nao,nmo))*1j
        eriref = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', ao2mo.restore(1, eri, nao),
                            mo.conj(), mo, mo.conj(), mo)
        eriref = eriref.reshape(12**2,12**2)

        tmpfile = tempfile.NamedTemporaryFile(dir=lib.param.TMPDIR)
        io_size = nao**2*4e-5

        semi_incore.general(eri, [mo]*4, tmpfile.name, ioblk_size=io_size)
        with ao2mo.load(tmpfile) as eri_mo:
            self.assertAlmostEqual(abs(eriref - eri_mo[:]).max(), 0, 9)

        io_size = nao**2*4e-5
        eri_4fold = ao2mo.restore(4, eri, nao)
        semi_incore.general(eri_4fold, [mo]*4, tmpfile.name, ioblk_size=io_size)
        with ao2mo.load(tmpfile) as eri_mo:
            self.assertAlmostEqual(abs(eriref - eri_mo[:]).max(), 0, 9)

if __name__ == '__main__':
    print('Full Tests for ao2mo.semi_incore')
    unittest.main()

max_stars_repo_name: QuESt-Calculator/pyscf

max_issues_repo_name: QuESt-Calculator/pyscf

max_forks_repo_name: QuESt-Calculator/pyscf


text:
""" Class for f(N) constraints
"""

from __future__ import print_function, absolute_import, division, unicode_literals

import numpy as np
import imp
import pdb

from pyigm.fN.fnmodel import FNModel
from pyigm.fN.constraints import FNConstraint
from pyigm.fN import tau_eff

# Path for pyigm
pyigm_path = imp.find_module('pyigm')[1]

def tst_fn_data(fN_model=None, model_two=None, data_list=None, outfil=None):
    """ Make a matplotlib plot like the final figure from P14
    See the Notebook for Bokeh plots
    Taken from xastropy without actually trying to run

    Parameters
    ----------
    fN_model
    model_two
    data_list
    outfil
    """
    import matplotlib as mpl
    mpl.rcParams['font.family'] = 'STIXGeneral-Regular' # Not for PDF
    mpl.rcParams['lines.linewidth'] = 2

    from matplotlib import pyplot as plt
    #from matplotlib.backends.backend_pdf import PdfPages

    # fN data
    all_fN_cs = FNConstraint.load_defaults()

    # Remove K12
    if data_list is None:
        fN_cs = [fN_c for fN_c in all_fN_cs if ((fN_c.ref != 'K02') & (fN_c.ref != 'PW09'))]
    else:
        fN_cs = [fN_c for fN_c in all_fN_cs if fN_c.ref in data_list]
    fN_dtype = [fc.fN_dtype for fc in fN_cs]

    fig = plt.figure(figsize=(8, 5))
    fig.clf()
    main = fig.add_axes( [0.1, 0.1, 0.8, 0.8] ) # xypos, xy-size

    # f(N) data
    main.set_ylabel(r'$\log f(N_{\rm HI})$')
    main.set_xlabel(r'$\log N_{\rm HI}$')
    main.set_ylim(-25., -9)

    for fN_c in fN_cs:
        if fN_c.fN_dtype == 'fN':
            # Length
            ip = range(fN_c.data['NPT'])
            val = np.where(fN_c.data['FN'][ip] > -90)[0]
            if len(val) > 0:
                ipv = np.array(ip)[val]
                xval = np.median(fN_c.data['BINS'][:,ipv],0)
                xerror = [ fN_c.data['BINS'][1,ipv]-xval, xval-fN_c.data['BINS'][0,ipv] ]
                yerror = [ fN_c.data['SIG_FN'][1,ipv], fN_c.data['SIG_FN'][0,ipv] ] # Inverted!
                main.errorbar(xval, fN_c.data['FN'][ipv], xerr=xerror, yerr=yerror, fmt='o',
                              label=fN_c.ref,capthick=2)
    main.legend(loc='lower left', numpoints=1)

    # Model?
    if fN_model is not None:
        xplt = 12.01 + 0.01*np.arange(1100)
        yplt = fN_model.evaluate(xplt, 2.4)
        main.plot(xplt,yplt,'-',color='black')
        print(xplt[0],yplt[0])
    if model_two is not None:
        xplt = 12.01 + 0.01*np.arange(1100)
        yplt = model_two.evaluate(xplt, 2.4)
        main.plot(xplt,yplt,'-',color='gray')

    # Extras
    #mpl.r['lines.capthick'] = 2

    inset = fig.add_axes( [0.55, 0.6, 0.25, 0.25] ) # xypos, xy-size
    inset.set_ylabel('Value') # LHS
    inset.xaxis.set_major_locator(plt.FixedLocator(range(5)))
    inset.xaxis.set_major_formatter(plt.FixedFormatter(['',r'$\tau_{\rm eff}^{\rm Ly\alpha}$',
                                                        r'$\ell(X)_{\rm LLS}$',
                                                        r'$\lambda_{\rm mfp}^{912}$', '']))
    inset.set_ylim(0., 0.6)

    # tau_eff
    flg_teff = 1
    try:
        itau = fN_dtype.index('teff') # Passes back the first one
    except:
        flg_teff = 0

    if flg_teff:
        teff=float(fN_cs[itau].data['TEFF'])
        D_A = 1. - np.exp(-1. * teff)
        SIGDA_LIMIT = 0.1  # Allows for systemtics and b-value uncertainty
        sig_teff = np.max([fN_cs[itau].data['SIG_TEFF'], (SIGDA_LIMIT*teff)])
        # Plot
        inset.errorbar(1, teff, sig_teff, fmt='_', capthick=2)
        # Model
        if fN_model is not None:
            model_teff = tau_eff.lyman_ew(1215.6701*(1+fN_cs[itau].zeval), fN_cs[itau].zeval+0.1,
                                          fN_model, NHI_MIN=fN_cs[itau].data['NHI_MNX'][0],
                                          NHI_MAX=fN_cs[itau].data['NHI_MNX'][1])
            inset.plot(1, model_teff, 'ko')
            #xdb.set_trace()

    ## #######
    # LLS constraint
    flg_LLS = 1
    try:
        iLLS = fN_dtype.index('LLS') # Passes back the first one
    except:
        #raise ValueError('fN.data: Missing LLS type')
        flg_LLS = 0
    if flg_LLS:
        inset.errorbar(2, fN_cs[iLLS].data['LX'], yerr=fN_cs[iLLS].data['SIG_LX'],
                       fmt='_', capthick=2)
        # Model
        if fN_model != None:
            lX = fN_model.calculate_lox(fN_cs[iLLS].zeval,
                                        17.19+np.log10(fN_cs[iLLS].data['TAU_LIM']), 22.)
            inset.plot(2, lX, 'ko')

    ## #######
    # MFP constraint
    flg_MFP = 1
    try:
        iMFP = fN_dtype.index('MFP') # Passes back the first one
    except:
        #raise ValueError('fN.data: Missing MFP type')
        flg_MFP = 0

    if flg_MFP:
        inset2 = inset.twinx()
        inset2.errorbar(3, fN_cs[iMFP].data['MFP'], yerr=fN_cs[iMFP].data['SIG_MFP'],
                        fmt='_', capthick=2)
        inset2.set_xlim(0,4)
        inset2.set_ylim(0,350)
        inset2.set_ylabel('(Mpc)')

        # Model
        if fN_model is not None:
            #fN_model.zmnx = (0.1, 20.) # Reset for MFP calculation
            mfp = fN_model.mfp(fN_cs[iMFP].zeval)
            inset2.plot(3, mfp, 'ko')

    # Show
    if outfil != None:
        plt.savefig(outfil,bbox_inches='tight')
    else:
        plt.show()

if __name__ == '__main__':

    flg_fig = 0
    #flg_fig += 2**0   # Without model
    flg_fig += 2**1   # With model

    # Without model
    if (flg_fig % 2**1) >= 2**0:
        tst_fn_data()

    # With model
    if (flg_fig % 2**2) >= 2**1:
        fN_default = FNModel.default_model()
        tst_fn_data(fN_model=fN_default)

max_stars_repo_name: pyigm/pyigm

max_issues_repo_name: pyigm/pyigm

max_forks_repo_name: pyigm/pyigm


text:
import argparse
import os
import urllib.parse as urlparse

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from spectrum_utils import plot
from spectrum_utils.spectrum import PeptideFragmentAnnotation

from ann_solo import reader
from ann_solo import spectrum_match
from ann_solo.config import config
from ann_solo.spectrum import process_spectrum

def set_matching_peaks(library_spectrum, query_spectrum):
    peak_matches = spectrum_match.get_best_match(
        query_spectrum, [library_spectrum],
        config.fragment_mz_tolerance, config.allow_peak_shifts)[2]
    query_spectrum.annotation = np.full_like(query_spectrum.mz, None, object)
    for peak_match in peak_matches:
        library_annotation = library_spectrum.annotation[peak_match[1]]
        if library_annotation is not None:
            query_spectrum.annotation[peak_match[0]] = library_annotation
        else:
            fragment_annotation = PeptideFragmentAnnotation(1, 1, 'z', 0)
            fragment_annotation.ion_type = 'unknown'
            query_spectrum.annotation[peak_match[0]] =\
                library_spectrum.annotation[peak_match[1]] =\
                fragment_annotation

def main():
    # Load the cmd arguments.
    parser = argparse.ArgumentParser(
        description='Visualize spectrum–spectrum matches from your '
                    'ANN-SoLo identification results')
    parser.add_argument(
        'mztab_filename', help='Identifications in mzTab format')
    parser.add_argument(
        'query_id', help='The identifier of the query to visualize')
    args = parser.parse_args()

    # Read the mzTab file.
    metadata = {}
    with open(args.mztab_filename) as f_mztab:
        for line in f_mztab:
            line_split = line.strip().split('\t')
            if line_split[0] == 'MTD':
                metadata[line_split[1]] = line_split[2]
            else:
                break   # Metadata lines should be on top.
    ssms = reader.read_mztab_ssms(args.mztab_filename)
    # make sure the SSM ids are strings.
    ssms.index = ssms.index.map(str)

    # Recreate the search configuration.
    settings = []
    # Search settings.
    for key in metadata:
        if 'software[1]-setting' in key:
            param = metadata[key][: metadata[key].find(' ')]
            value = metadata[key][metadata[key].rfind(' ') + 1:]
            if value != 'None':
                if value != 'False':
                    settings.append('--{}'.format(param))
                if value not in ('False', 'True'):
                    settings.append(value)
    # File names.
    settings.append('dummy_spectral_library_filename')
    settings.append('dummy_query_filename')
    settings.append('dummy_output_filename')
    config.parse(' '.join(settings))

    # Retrieve information on the requested query.
    query_id = args.query_id
    query_uri = urlparse.urlparse(urlparse.unquote(
        metadata['ms_run[1]-location']))
    query_filename = os.path.abspath(os.path.join(
        query_uri.netloc, query_uri.path))
    ssm = ssms.loc[query_id]
    library_id = ssm['opt_ms_run[1]_cv_MS:1003062_spectrum_index']
    library_uri = urlparse.urlparse(urlparse.unquote(ssm['database']))
    library_filename = os.path.abspath(os.path.join(
        library_uri.netloc, library_uri.path))
    score = ssm['search_engine_score[1]']

    # Read library and query spectrum.
    with reader.SpectralLibraryReader(library_filename) as lib_reader:
        library_spectrum = lib_reader.get_spectrum(library_id, True)
    query_spectrum = None
    for spec in reader.read_mgf(query_filename):
        if spec.identifier == query_id:
            query_spectrum = process_spectrum(spec, False)
            # Make sure that the precursor charge is set for query spectra
            # with a undefined precursor charge.
            query_spectrum.precursor_charge = library_spectrum.precursor_charge
            break
    # verify that the query spectrum was found
    if query_spectrum is None:
        raise ValueError('Could not find the specified query spectrum')

    # Set the matching peaks in the query spectrum to correctly color them.
    set_matching_peaks(library_spectrum, query_spectrum)
    # Modify the colors to differentiate non-matching peaks.
    plot.colors[None] = '#757575'

    # Plot the match.
    fig, ax = plt.subplots(figsize=(20, 10))

    # Plot without annotations.
    plot.mirror(query_spectrum, library_spectrum,
                {'color_ions': True, 'annotate_ions': False}, ax)
    # Add annotations to the library spectrum.
    max_intensity = library_spectrum.intensity.max()
    for i, annotation in enumerate(library_spectrum.annotation):
        if annotation is not None and annotation.ion_type != 'unknown':
            x = library_spectrum.mz[i]
            y = -library_spectrum.intensity[i] / max_intensity
            ax.text(x, y, str(annotation),
                    color=plot.colors[annotation.ion_type], zorder=5,
                    horizontalalignment='right', verticalalignment='center',
                    rotation=90, rotation_mode='anchor')

    ax.set_ylim(-1.1, 1.05)

    ax.text(0.5, 1.06, f'{library_spectrum.peptide}, Score: {score:.3f}',
            horizontalalignment='center', verticalalignment='bottom',
            fontsize='x-large', fontweight='bold', transform=ax.transAxes)
    ax.text(0.5, 1.02, f'File: {os.path.basename(query_filename)}, '
                       f'Scan: {query_spectrum.identifier}, '
                       f'Precursor m/z: {query_spectrum.precursor_mz:.4f}, '
                       f'Library m/z: {library_spectrum.precursor_mz:.4f}, '
                       f'Charge: {query_spectrum.precursor_charge}',
            horizontalalignment='center', verticalalignment='bottom',
            fontsize='large', transform=ax.transAxes)

    plt.savefig(f'{query_id}.png', dpi=300, bbox_inches='tight')
    plt.close()

if __name__ == '__main__':
    main()

max_stars_repo_name: tycheyoung/ANN-SoLo

max_issues_repo_name: tycheyoung/ANN-SoLo

max_forks_repo_name: tycheyoung/ANN-SoLo


text:
#[Super SloMo]
##High Quality Estimation of Multiple Intermediate Frames for Video Interpolation

import argparse

import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn

from model import superslomo_half as superslomo
from model.extraction import center, ends, MPRNet
from utils import meanshift, load_image

from copy import deepcopy
import os, glob
import numpy as np

# For parsing commandline arguments
parser = argparse.ArgumentParser()
parser.add_argument("--input", type=str, default='demo_input', help='path to dataset folder containing train-test-validation folders')
parser.add_argument("--output", type=str, default='demo_output', help='path to folder for saving checkpoints')
parser.add_argument("--checkpoint", type=str, help='path of checkpoint for pretrained model')
parser.add_argument("--add_blur", action='store_true', help='Add blurry image')
parser.add_argument("--seq_len", type=int, default=7, help='number of frames that composes a sequence.')

args = parser.parse_args()

### For visualizing loss and interpolated frames
###Initialize flow computation and arbitrary-time flow interpolation CNNs.

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
flowComp = superslomo.UNet(6, 4)
flowComp.to(device)
if args.add_blur:
    ArbTimeFlowIntrp = superslomo.UNet(23, 5)
else:
    ArbTimeFlowIntrp = superslomo.UNet(20, 5)
ArbTimeFlowIntrp.to(device)

### Load Pretrained Extraction Models

# center_estimation = center.Center()
center_estimation = MPRNet.MPRNet()
border_estimation = ends.Ends()
center_estimation = center_estimation.to(device)
border_estimation = border_estimation.to(device)

### Load Datasets
# Channel wise mean calculated on adobe240-fps training dataset
mean = [0.429, 0.431, 0.397]
std  = [1, 1, 1]

seq_len = args.seq_len
ctr_idx = seq_len // 2

def test(blurred_img, fname):
    with torch.no_grad():
        blurred_img = blurred_img.to(device)
        batch_size = blurred_img.shape[0]

        h, w = blurred_img.shape[-2:]

        ###Initialze backward warpers for train and validation datasets
        validationFlowBackWarp = superslomo.backWarp(w, h, device)
        validationFlowBackWarp = validationFlowBackWarp.to(device)

        blurred_img = meanshift(blurred_img, mean, std, device, False)
        center = center_estimation(blurred_img)
        start, end = border_estimation(blurred_img, center)

        start = meanshift(start, mean, std, device, True)
        end = meanshift(end, mean, std, device, True)
        center = meanshift(center, mean, std, device, True)
        blurred_img = meanshift(blurred_img, mean, std, device, True)

        I0 = torch.zeros_like(blurred_img)
        I1 = center

        for vindex in range(seq_len):
            if vindex == 0:
                Ft_p = torch.zeros_like(blurred_img)
                for b in range(batch_size):
                    Ft_p[b] = start[b].clone()

            elif vindex == seq_len-1:
                Ft_p = torch.zeros_like(blurred_img)
                for b in range(batch_size):
                    Ft_p[b] = end[b].clone()

            elif vindex == ctr_idx:
                Ft_p = center.clone()

            else:
                validationIndex = torch.ones(batch_size) * (vindex - 1)
                validationIndex = validationIndex.long()
                if vindex > ctr_idx:
                    validationIndex = seq_len - 3 - validationIndex

                for b in range(batch_size):
                    if vindex < ctr_idx:
                        I0[b] = start[b]
                    else:
                        I0[b] = end[b]

                flowOut = flowComp(torch.cat((I0, I1), dim=1))
                F_0_1 = flowOut[:,:2,:,:]
                F_1_0 = flowOut[:,2:,:,:]

                fCoeff = superslomo.getFlowCoeff(validationIndex, device, seq_len)

                F_t_0 = fCoeff[0] * F_0_1 + fCoeff[1] * F_1_0
                F_t_1 = fCoeff[2] * F_0_1 + fCoeff[3] * F_1_0

                g_I0_F_t_0 = validationFlowBackWarp(I0, F_t_0)
                g_I1_F_t_1 = validationFlowBackWarp(I1, F_t_1)

                if args.add_blur:
                    intrpOut = ArbTimeFlowIntrp(torch.cat((I0, I1, F_0_1, F_1_0, F_t_1, F_t_0, g_I1_F_t_1, g_I0_F_t_0, blurred_img), dim=1))
                else:
                    intrpOut = ArbTimeFlowIntrp(torch.cat((I0, I1, F_0_1, F_1_0, F_t_1, F_t_0, g_I1_F_t_1, g_I0_F_t_0), dim=1))

                F_t_0_f = intrpOut[:, :2, :, :] + F_t_0
                F_t_1_f = intrpOut[:, 2:4, :, :] + F_t_1
                V_t_0   = torch.sigmoid(intrpOut[:, 4:5, :, :])
                V_t_1   = 1 - V_t_0

                g_I0_F_t_0_f = validationFlowBackWarp(I0, F_t_0_f)
                g_I1_F_t_1_f = validationFlowBackWarp(I1, F_t_1_f)

                wCoeff = superslomo.getWarpCoeff(validationIndex, device, seq_len)

                Ft_p = (wCoeff[0] * V_t_0 * g_I0_F_t_0_f + wCoeff[1] * V_t_1 * g_I1_F_t_1_f) / (wCoeff[0] * V_t_0 + wCoeff[1] * V_t_1)

            Ft_p = meanshift(Ft_p, mean, std, device, False)
            out_img = Ft_p.cpu().clone()

            filename = fname + '_est{:02d}'.format(vindex+1) + '.png'
            # Comment two lines below if you want to save images
            if not os.path.exists(os.path.join(args.output, fname)):
                os.mkdir(os.path.join(args.output, fname))
            torchvision.utils.save_image(out_img[0], os.path.join(args.output, fname, filename))

### Initialization
dict1 = torch.load(args.checkpoint)
ArbTimeFlowIntrp.load_state_dict(dict1['state_dictAT'])
flowComp.load_state_dict(dict1['state_dictFC'])
center_estimation.load_state_dict(dict1['state_dictCT'])
border_estimation.load_state_dict(dict1['state_dictBD'])
print('Load model from: ', args.checkpoint)

inputfolder = args.input
img_list = sorted(glob.glob(os.path.join(inputfolder, '*.png')))

args.output = os.path.join(args.output, 'Len{:02d}'.format(args.seq_len))
if not os.path.exists(args.output):
    os.makedirs(args.output)

for inputFile in img_list:
    print(inputFile)
    inputs = load_image(inputFile)
    fname = os.path.splitext(os.path.basename(inputFile))[0]
    width, height= inputs.size
    inputs = inputs.crop((0,0, width-width%64, height-height%64))

    normalize = transforms.Normalize(mean=mean, std=std)
    input_transform = transforms.Compose([transforms.ToTensor(), normalize])
    inputs = input_transform(inputs)
    inputs = inputs.unsqueeze(0)

    test(inputs, fname)


text:
import argparse
import os, sys
import time
import math
import numpy as np
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn

import data
import model
import time
import genotypes
from utils import batchify, get_batch, repackage_hidden, create_exp_dir, save_checkpoint

parser = argparse.ArgumentParser(description='PyTorch PennTreeBank/WikiText2 Language Model')
parser.add_argument('--data', type=str, default='ptb\\',
                    help='location of the data corpus')
parser.add_argument('--emsize', type=int, default=850,
                    help='size of word embeddings')
parser.add_argument('--nhid', type=int, default=850,
                    help='number of hidden units per layer')
parser.add_argument('--nhidlast', type=int, default=850,
                    help='number of hidden units for the last rnn layer')
parser.add_argument('--lr', type=float, default=20,
                    help='initial learning rate')
parser.add_argument('--clip', type=float, default=0.25,
                    help='gradient clipping')
parser.add_argument('--epochs', type=int, default=8000,
                    help='upper epoch limit')
parser.add_argument('--batch_size', type=int, default=64, metavar='N',
                    help='batch size')
parser.add_argument('--bptt', type=int, default=35,
                    help='sequence length')
parser.add_argument('--dropout', type=float, default=0.75,
                    help='dropout applied to layers (0 = no dropout)')
parser.add_argument('--dropouth', type=float, default=0.3,
                    help='dropout for rnn layers (0 = no dropout)')
parser.add_argument('--dropouti', type=float, default=0.2,
                    help='dropout for input embedding layers (0 = no dropout)')
parser.add_argument('--dropoute', type=float, default=0.2,
                    help='dropout to remove words from embedding layer (0 = no dropout)')
parser.add_argument('--seed', type=int, default=1267,
                    help='random seed')
parser.add_argument('--nonmono', type=int, default=5,
                    help='random seed')
parser.add_argument('--cuda', action='store_false',default=False,
                    help='use CUDA')
parser.add_argument('--log-interval', type=int, default=200, metavar='N',
                    help='report interval')
parser.add_argument('--model_path', type=str,  default='weight\\model.pt',
                    help='path to load the pretrained model')
parser.add_argument('--alpha', type=float, default=0,
                    help='alpha L2 regularization on RNN activation (alpha = 0 means no regularization)')
parser.add_argument('--beta', type=float, default=1e-3,
                    help='beta slowness regularization applied on RNN activiation (beta = 0 means no regularization)')
parser.add_argument('--wdecay', type=float, default=5e-7,
                    help='weight decay applied to all weights')
parser.add_argument('--continue_train', action='store_true',
                    help='continue train from a checkpoint')
parser.add_argument('--n_experts', type=int, default=1,
                    help='number of experts')
parser.add_argument('--max_seq_len_delta', type=int, default=20,
                    help='max sequence length')
parser.add_argument('--gpu', type=int, default=0, help='GPU device to use')
parser.add_argument('--arch', type=str, default='DARTS_A2', help='which architecture to use')
parser.add_argument('--dropoutx', type=float, default=0.75,
                    help='dropout for input nodes rnn layers (0 = no dropout)')
args = parser.parse_args()

def logging(s, print_=True, log_=True):
    print(s)
# Set the random seed manually for reproducibility.
np.random.seed(args.seed)
torch.manual_seed(args.seed)
# if torch.cuda.is_available():
#     if not args.cuda:
#         print("WARNING: You have a CUDA device, so you should probably run with --cuda")
#     else:
#         torch.cuda.set_device(args.gpu)
#         cudnn.benchmark = True
#         cudnn.enabled=True
#         torch.cuda.manual_seed_all(args.seed)

corpus = data.Corpus(args.data)
#word_2_id
word = corpus.dictionary.word2idx
word_2_id= dict(zip(word.values(), word.keys()))

test_batch_size = 1
test_data = batchify(corpus.test, test_batch_size, args)
########################################################
my_data = batchify(corpus.mytxt, test_batch_size, args)
ntokens = len(corpus.dictionary)

def evaluate_my(data_source, batch_size=10):
    # Turn on evaluation mode which disables dropout.
    model.eval()
    total_loss = 0
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(batch_size)
    for i in range(0, data_source.size(0) - 1, args.bptt):
        # print(i, data_source.size(0)-1)
        data, targets = get_batch(data_source, i, args, evaluation=True)
        targets = targets.view(-1)

        log_prob, hidden = parallel_model(data, hidden)
        ########################################
        data_ = data.data.cpu().numpy()
        txt_data = []
        for t in data_:
            tt = word_2_id[t[0]]
            txt_data.append(tt)
        # print(txt_data)
        ########################################
        target = targets.data.cpu().numpy()
        txt_tar = []
        for t in target:
            tt = word_2_id[t]
            txt_tar.append(tt)
        # print(txt_tar)
        out = log_prob.view(-1, log_prob.size(2))
        output = []
        for i in range(out.shape[0]):#i表示第i个单词的索引
            i_vetor = out[i,:].data.cpu().numpy()#ii表示第i个单词的词向量
            i_vetor_list = []#i_vetor_list
            for n in i_vetor:
                i_vetor_list.append(n)
            max_i = sorted(i_vetor_list)[-1]
            index = i_vetor_list.index(max_i)
            index_word = word_2_id[index]
            output.append(index_word)
            #输出最后一位的十种可能情况
            if i == int(out.shape[0])-1:
                word_pre_end = []
                possible_end_word = sorted(i_vetor_list,reverse=True)[:10]
                for end_word in possible_end_word:
                    end_word_index = i_vetor_list.index(end_word)
                    possible_word = word_2_id[end_word_index]
                    word_pre_end.append(possible_word)
        word_pre = [i for i in output]
        # word_pre_1 = [output[i] for i in range(len(output))]
        return word_pre_end

        ##################################

# Load the best saved model.
load_start_time = time.time()
# genotype = eval("genotypes.%s" % args.arch)
# model = model.RNNModel(ntokens, args.emsize, args.nhid, args.nhidlast,
#                     args.dropout, args.dropouth, args.dropoutx, args.dropouti, args.dropoute,
#                     cell_cls=model.DARTSCell, genotype=genotype)
# model.load_state_dict(torch.load(args.model_path, map_location='cpu'),False)
model = torch.load(args.model_path, map_location='cpu')

#################################################################
parallel_model = model
print(my_data)
for i in my_data:
    id = i.data.cpu().numpy()[0]
    print(id)
    word = word_2_id[id]
    print(word)
pre_word = evaluate_my(my_data, test_batch_size)
logging('next word = {}'.format(pre_word))
logging('=' * 89)
logging('prediction time = {}s'.format(time.time()-load_start_time))
logging('=' * 89)

max_stars_repo_name: xinglong-zh/nas-cnn-rnn

max_issues_repo_name: xinglong-zh/nas-cnn-rnn

max_forks_repo_name: xinglong-zh/nas-cnn-rnn


text:
#Purpose: derive the NEI ionic fraction in the case of dusty SNR

# Import used modules
import dustnei_physcalc
import pickle, os
import numpy as np
from datetime import datetime
import multiprocessing as mp

def derive_dustnei_ionfrac_module(log=False):
  # System parameters
  rootpath = os.getcwd() + '/'
  if log:
    rootpath += 'log/'
  else:
    rootpath += 'linear/'
  Zlist = [26]

  # Define the condition dictionary, based on Zehao's setting
  kTlist  = np.array([1.,2.,4.,10.]) # in keV
  if log:
    taulist = np.zeros(1201, dtype=float) + 1e10 # in cm-3 s
    taulist[0:301] = 10**(np.linspace(9,10,301))
  else:
    taulist = np.zeros(1001, dtype=float) + 1e10 # in cm-3 s
  # taulist = 10**(np.linspace(9,11,1001)) # in cm-3 s
  # taulist = np.array([10**(np.linspace(9,10,301)), \
  #                    np.zeros(900) + 1e10]) # in cm-3 s
  accum_tau = np.cumsum(taulist) - taulist[0]
  n_kT  = len(kTlist)
  n_tau = len(taulist)
  nelist = np.zeros(n_tau, dtype=float) + 1.0  # in cm-3
  v_list = np.zeros(n_tau, dtype=float) + 1e10 # in cm/s
  r_list = accum_tau / nelist * v_list # in cm

  conditions = {}
  conditions['R'] = r_list
  conditions['velo'] = v_list
  conditions['dens'] = nelist
  conditions['kT'] = np.zeros(n_tau, dtype=float) + kTlist[0]

  # Derive the change of the neutral iron
  dustZele  = 0.95
  tau_13 = accum_tau/1e13
  f_tau=1.0-3.2*tau_13**0.5+3.0*tau_13-tau_13**2+0.2*tau_13**3
  init_ionfrac = np.zeros([27, n_tau], dtype=float)
  init_ionfrac[0,:] = (1-dustZele) + dustZele * (1-f_tau)
  init_file = {}
  for Z in Zlist:
    init_file[Z] = init_ionfrac

  now1 = datetime.now().hour*3600. + datetime.now().minute*60. + \
         datetime.now().second + datetime.now().microsecond/1e6
  pool = mp.Pool(mp.cpu_count())
  res  = pool.starmap(dustnei_physcalc.calc_nei_ionfrac, \
           [(Zlist, conditions, 'tionfrac_Fe_%04.1fkeV.pkl'%kTlist[M], \
            init_file, kTlist[M]) for M in range(0,n_kT)])
  # for M in range(0,n_kT):
  #   condition['kT'] = np.zeros(n_tau, dtype=float) + kTlist[M] # in keV
  #   dustnei_physcalc.calc_nei_ionfrac(Zlist, condifile=condition, \
  #     outfilename='tionfrac_Fe_%fkeV.pkl'%kTlist[M], \
  #     init_file=init_file, rootpath=rootpath)
  now2 = datetime.now().hour*3600. + datetime.now().minute*60. + \
         datetime.now().second + datetime.now().microsecond/1e6
  print("# Time Consuming:%7.2f sec." % (now2-now1))

  # Combine derived ionic fraction
  ionfrac = np.zeros([n_kT, 28, n_tau], dtype=float)
  for M in range(0,n_kT):
    res = pickle.load(open(rootpath+'tionfrac_Fe_%04.1fkeV.pkl'%kTlist[M], 'rb'))
    ionfrac[M,0,:] = dustZele * f_tau
    for iZ in range(0,Z+1):
      ionfrac[M,iZ+1,:] = res[Z][iZ,:] * init_ionfrac[0,:]

  dat={}
  dat['telist']  = kTlist
  dat['taulist'] = accum_tau
  dat['ionfrac'] = ionfrac
  tmp = open(rootpath+'dustsnr_ionfrac_Fe.pkl', 'wb')
  pickle.dump(dat, tmp)
  tmp.close()

if __name__ == '__main__':
  derive_dustnei_ionfrac_module(log=True)
  derive_dustnei_ionfrac_module(log=False)


text:
from os.path import dirname, join as pjoin
from scipy.io import wavfile
import scipy.io

samplerate, data = wavfile.read('output.wav')

Diff = 1000*(data[:, 1] - data[:, 0])

wavfile.write("key.wav", samplerate, Diff)

max_stars_repo_path: Journey of the 06 Keys Challenge/Key_06/key6.py

reason: import scipy,from scipy


text:
import numpy as np
from sklearn import cross_validation
from sklearn import svm
from sklearn.svm import LinearSVC
from sklearn.datasets import load_svmlight_file
from sklearn.pipeline import make_pipeline
from sklearn.feature_selection import SelectFromModel
from sklearn.feature_selection import RFE
from sklearn.cross_validation import StratifiedKFold
from sklearn.feature_selection import RFECV
import matplotlib.pyplot as plt
from scipy.sparse import csr_matrix
import csv
import random
from numpy import matrix
from scipy import sparse

#data
#data = load_svmlight_file("leu")

f = open("arcene_train.data") # read the data file
f1 = open("arcene_train.labels") # read the lable file
try:
    a = []
    for row in csv.reader(f):
        row = [int(i) for i in row[0].split()]
        a.append(row)   # data matrix
    b = []
    for row in csv.reader(f1):
        row = map(int,row)
        b.append(row[0])   # labels matrix
        #print(golub(a,b))
finally:
    f.close   # close the files
    f1.close

a =csr_matrix(a) # convert into sparse matrix
b = np.asarray(b)

data =  (a,b)

c = 1.9 #SVM soft-margin constant

print("SVM soft-margin constant %d", c)

# subSampling
l =len(data[1])
start = int(round(l*0.70,0))

N = random.sample(range(start,l), 1)
#print(N)
i = np.random.choice(np.arange(data[0].shape[0]), N, replace=False)

sub_data = data[0][i.tolist()]
sub_sample = data[1][i.tolist()] # check for this step
#test_size= 0.4 # selecting number of samples

'''
X_train, X_test, y_train, y_test = cross_validation.train_test_split(sub_data,sub_sample, test_size=0.4, random_state=1)

X_1 = X_train.todense().tolist()  # samples 72 features above 7129
y_1 = map(int,y_train)   # classes 2
'''

#print(sub_data.shape)
X_1 = sub_data.todense().tolist()
y_1 = map(int,sub_sample)
#print(len(y_1))

#L2 SVM trained on the features selected by the L1 SVM subSampling
clf = LinearSVC(penalty='l1', dual=False,C=c).fit(X_1, y_1)
model = SelectFromModel(clf, prefit=True)
X = model.transform(X_1)

print("number of featuer selected %d", X.shape[1])
clf = LinearSVC(penalty='l2',dual=False,C=c)
scores = cross_validation.cross_val_score(clf, X, y_1, cv=10)

print(scores)
print("L2 SVM trained on the features selected by the L1 SVM subsampling. \n  Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

#L2 SVM that uses RFE (with an L2-SVM) to select relevant features
clf = LinearSVC(penalty='l2',dual=False,C=c)
rfe = RFE(estimator=clf, n_features_to_select=10, step=1)
rfe.fit(data[0], data[1])
scores = cross_validation.cross_val_score(rfe.estimator_ , data[0], data[1], cv=10)
print(scores)
print("L2 SVM that uses RFE (with an L2-SVM) to select relevant features. \n Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

# featuer selection using The Golub score

def golub(data, labels):
    c = []
    for i in range(0,len(data)):
        c.append([labels[i],data[i]]) # append labels with data matrix

    negative = []
    positive = []

    for i in range(0,len(c)):
        if c[i][0] == [-1]:          # for negative condition
            negative.append(c[i][1])
        if c[i][0] == [1]:           # for positive condition
            positive.append(c[i][1])
    negMean = np.mean(np.asarray(negative),axis=0).tolist()  # calculate mean
    negStd = np.std(np.asarray(negative),axis=0).tolist()    # calculate standard deviation
    posMean = np.mean(np.asarray(positive),axis=0).tolist()  # calculating mean
    posStd = np.std(np.asarray(positive),axis=0).tolist()    # calculating Standard Deviation

    score =[]
    for i in range(0,len(negMean)):
        if((posStd[i]+negStd[i]) != 0):
            score.append((posMean[i]-negMean[i])/(posStd[i]+negStd[i]))
        else:
            score.append(0)    # zero if denominator is zero
    #print(len(score))
    return (score, score)  # return score as score , score as asked

#data = load_svmlight_file("leu")
X_1 = data[0].todense().tolist()
y_1 = map(int,data[1])
#print(len(y_1))

lables= []
for i in range(0,len(y_1)):
    lables = lables + [[y_1[i]]]

#print X_1[0]
    #print(y_1)
    #print(lables)
filterData = np.zeros((len(y_1),1))
filterData = filterData[:,:0]

score = golub(X_1,lables)[0]
print score
print len(score)
for i in range(0,len(score)):
    if(score[i] >0): # filter value
        col = np.array(X_1)[:,i]
        filterData = np.c_[filterData,col]
        #col = matrix(X_1).transpose()[i].getA()[0]
        #filterData = np.append(filterData, col, 1)
#print filterData.shape

clf = LinearSVC(penalty='l2',dual=False,C=c)
#print data[0]
print("Number of Filtred (non zero golub score)featuers %d", sparse.csr_matrix(filterData).shape[1])
scores = cross_validation.cross_val_score(clf, sparse.csr_matrix(filterData), map(int,data[1]), cv=10)

print(scores)
print("L2 SVM trained on filtered features using golub score \n Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

reason: import numpy,from numpy,from scipy


text:
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir))
#--------------------------------------------------------------------------------
import networkx as nx
import matplotlib.pyplot as plt

from code.static_network.generate.pa_homophily_network import GenerateSyntheticNetwork

if __name__ == "__main__":
    syn_network = GenerateSyntheticNetwork(None)
    syn_network.save_info("total_nodes", 20)
    syn_network.save_info("edges_per_iteration", 2)
    syn_network.save_info("initial_nodes", 2)
    syn_network.save_info("minority_fraction", 0.2)
    syn_network.save_info("homophily_among_minorities", 0.8)
    syn_network.save_info("homophily_among_majorities", 0.8)
    syn_network.generate_network()

    G = syn_network.get_network()
    node_colors = []
    node_sizes = []
    SIZE_RANGE_MAX, SIZE_RANGE_MIN = 1000, 100
    deg_centrality_dict = nx.degree_centrality(G)
    for n, v in deg_centrality_dict.items():
        node_sizes.append(v)
        node_colors.append("red" if G.nodes[n]['type'] == "minority" else "blue")

    new_range = SIZE_RANGE_MAX - SIZE_RANGE_MIN
    old_range_min = min(node_sizes)
    old_range_max = max(node_sizes)
    old_range = old_range_max - old_range_min
    node_sizes = [(SIZE_RANGE_MIN + (((v - old_range_min) / old_range) * new_range)) for v in node_sizes]

    plt.figure(figsize=(10, 6))
    nx.draw_networkx(G=G,
                     pos=nx.spring_layout(G, k=0.15, iterations=30),
                     node_color=node_colors, # Color the Nodes
                     node_size=node_sizes, # Size of the nodes according to degree centrality
                     with_labels=False, # Do not show the node labels
                     edgecolors='black'
                     )
    plt.axis('off')
    plt.show()

reason: import networkx


text:
import serial
import pickle
import os
from datetime import datetime
import numpy as np
import sys
import time

class serialstream:
	def __init__(self,port = '/dev/ttyS0',baudrate = 9600,flight_category = None):
		self.port = port
		self.baudrate = baudrate
		self.flight_category = flight_category
		self.connection = serial.Serial(self.port,baudrate = self.baudrate)
		self.data = None
		self.data_len = None
		if self.flight_category is None: self.record_file_base_name = 'flight_data/UAV_episode'
		else:self.record_file_base_name = 'flight_data/'+self.flight_category+'/UAV_episode'
		self.episode_record = [] #list()
		self.keys = keys = list(np.zeros([1,34])[0]) #init keys
		keys[0] = 'time'
		keys[1] = 'dt'
		keys[2] = 'travel_x'
		keys[3] = 'travel_y'
		keys[4] = 'travel_z'
		keys[5] = 'accel_ms_x'
		keys[6] = 'accel_ms_y'
		keys[7] = 'accel_ms_z'
		keys[8] = 'accel_ang_x'
		keys[9] = 'accel_ang_y'
		keys[10] = 'gyro_deg_x'
		keys[11] = 'gyro_deg_y'
		keys[12] = 'gyro_deg_z'
		keys[13] = 'gyro_ang_x'
		keys[14] = 'gyro_ang_y'
		keys[15] = 'gyro_ang_z'
		keys[16] = 'com_pitch'
		keys[17] = 'com_roll'
		keys[18] = 'com_yaw'
		keys[19] = 'IMU_vel_x'
		keys[20] = 'IMU_vel_y'
		keys[21] = 'IMU_vel_z'
		keys[22] = 'IMU_dist_x'
		keys[23] = 'IMU_dist_y'
		keys[24] = 'IMU_dist_z'
		keys[25] = 'chng_velx'
		keys[26] = 'chng_vely'
		keys[27] = 'chng_velz'
		keys[28] = 'chng_distx'
		keys[29] = 'chng_disty'
		keys[30] = 'chng_distz'
		keys[31] = 'chng_axp'
		keys[32] = 'chng_ayp'
		keys[33] = 'chng_azp'

		#	while True:
		#	if self.c.in_waiting>0:print(self.c.readline())

	def stream(self,format = False,format_type = None, keys = None,expected_feature_size = None,record = False):
		'''if format <bool>: format data and return type and ensure right number of features are reurned because serial is asynchronous and can lose some.Else return decoded utf-8 data
		   if format type is []: if format is true, return data as  decoded utf-8 with each item's eval. d
		   if format_type is {}: if format is true, return data as decoded utf-8 as dict with keys as keys and values as []. Each itm assigned to key
		   expected_size <int>: effective when format is True. return only on expected feature size because serial might lose some'''
		try: #try to get data
			if self.connection.in_waiting>0: #if data is available
				self.data = self.connection.readline() #read data
				self.data = self.data.decode().split() #separate/split features by single whitespeace
				self.data_len = len(self.data)	#get number of features in this read
				# format
				#allow the option to format without right feature size, Not recommended. if [],feeatures may not be at right indices when because serial may lose some
				#if {}, keys may not be assigned to right features
				if expected_feature_size is None:expected_feature_size = self.data_len

				if format: #ensure the right number of features is returned
						if type(format_type) is list:
							self.data =  [eval(feature) for feature in self.data]
						elif type(format_type) is dict:
							try:
								self.data = dict(zip(keys,[eval(feature) for feature in self.data]))
							except TypeError:
								raise ValueError('Keys is None, not provided')

				if record: self.episode_record.append(self.data)

				if expected_feature_size == self.data_len: # return only the formatted right sized features
					return self.data

				elif not format: return self.data #return unformatted option
		#if exception, try to handle
		except serial.serialutil.SerialException:
			del self.connection
			self.connection = serial.Serial(self.port,baudrate = self.baudrate)
			print('serial_exc')
			pass

		except OSError:
			#try:
			#	#self.connection.flush()
			del self.connection
			self.connection = serial.Serial(self.port,baudrate = self.baudrate)
			print('OSerror - Handled')
			#
			#	#print('reinit')
			#except:
			#	#self.connection.flush()
			#	#self.connection = serial.Serial(self.port,baudrate = self.baudrate)
			#	#print('reinit2')
		except SyntaxError:
			pass
			print('Syntax Error - Handled')

	def save_episode(self):
		'''save recorded data -> state variables of UAV flight'''
		with open(self.record_file_base_name+str(datetime.today())+'.pickle','wb') as record:
			pickle.dump(self.episode_record,record)
			record.close()

	def save_interract(self,flight_category):
		print('Saving flight episode...')
		save = input('Save this flight episode [y/[n]]?:')
		if save.lower() == 'y':
			S.save_episode()
			if flight_category is None: flight_category = 'No specified flight_category'
			print('['+flight_category+']','Done!, recorded',len(self.episode_record),'samples under',self.flight_category)
			print('flight data saved at',os.path.join(os.getcwd(),self.record_file_base_name.rstrip('UAV_episode')))
		else:
			print('discarded episode.')

if __name__ =='__main__':
	flight_category = None
	samples = None
	try:
		flight_category = sys.argv[1]
		samples = sys.argv[2] #no of samples to collect

	except IndexError: #if no parameters are given in shell
		print('collecting infinite samples to',os.path.join(os.getcwd(),'flight_data'))
		time.sleep(3)
		pass
	S = serialstream(baudrate = 115200,flight_category  = flight_category)
	expected_feature_size = 34
	while True:
		try:
			data = S.stream(format = True,format_type = {},keys = S.keys,expected_feature_size = expected_feature_size,record = True)
			if data is not None:
				print(data,len(data))
		except KeyboardInterrupt:
			S.save_interract(flight_category)
			break

		if samples is not None:
			if len(S.episode_record) >= eval(samples):
				S.save_interract(flight_category)
				break


text:
'''
PrintTopics.py

Prints the top topics

Usage
-------
python PrintTopics.py dataName allocModelName obsModelName algName [options]

Saves topics as top_words.txt within the job directory that the script draws from.

Options
--------
--topW : an integer representing how many of the top words you wish to show (must be less than the size of vocabulary)
--taskids : ids of the tasks (individual runs) of the given job to plot.
             Ex: "1" or "3" or "1,2,3" or "1-6"
'''
from matplotlib import pylab
import numpy as np
import argparse
import os
import sys
import bnpy
import PlotELBO

def loadData(jobpath):
  ''' Load in bnpy Data obj associated with given learning task.
      TODO: make dataseed work
  '''
  bnpyoutdir = os.environ['BNPYOUTDIR']
  subdirpath = jobpath[len(bnpyoutdir):]
  fields = subdirpath.split(os.path.sep)
  dataname = fields[0]
  sys.path.append(os.environ['BNPYDATADIR'])
  datamodulepath = os.path.join(os.environ['BNPYDATADIR'], dataname+".py")
  if not os.path.exists(datamodulepath):
    raise ValueError("Could not find data %s" % (dataname))
  datamod = __import__(dataname, fromlist=[])
  return datamod.get_data()

def plotData(Data, nObsPlot=5000):
  ''' Plot data items, at most nObsPlot distinct points (for quick rendering)
  '''
  if type(Data) == bnpy.data.XData:
    PRNG = np.random.RandomState(nObsPlot)
    pIDs = PRNG.permutation(Data.nObs)[:nObsPlot]
    pylab.plot(Data.X[pIDs,0], Data.X[pIDs,1], 'k.')

def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('dataName', type=str,
        help='name of python script that produces data to analyze.')
  parser.add_argument('allocModelName', type=str,
        help='name of allocation model. {MixModel, DPMixModel}')
  parser.add_argument('obsModelName', type=str,
        help='name of observation model. {Gauss, ZMGauss}')
  parser.add_argument('algName', type=str,
        help='name of learning algorithms to consider, {EM, VB, moVB, soVB}.')
  parser.add_argument('--jobname', type=str, default='defaultjob',
        help='name of experiment whose results should be plotted')
  parser.add_argument('--topW', type=int, default=10,
        help='the number of top words printed for a given topic')
  parser.add_argument('--taskids', type=str, default=None,
        help="int ids for tasks (individual runs) of the given job to plot." + \
              'Ex: "1" or "3" or "1,2,3" or "1-6"')
  parser.add_argument('--savefilename', type=str, default=None,
        help="absolute path to directory to save figure")
  parser.add_argument('--iterid', type=int, default=None)
  args = parser.parse_args()

  rootpath = os.path.join(os.environ['BNPYOUTDIR'], args.dataName, \
                              args.allocModelName, args.obsModelName)
  jobpath = os.path.join(rootpath, args.algName, args.jobname)

  if not os.path.exists(jobpath):
    raise ValueError("No such path: %s" % (jobpath))

  taskids = PlotELBO.parse_task_ids(jobpath, args.taskids)

  Data = loadData(jobpath)

  if args.savefilename is not None and len(taskids) > 0:
    try:
      args.savefilename % ('1')
    except TypeError:
      raise ValueError("Missing or bad format string in savefilename %s" %
                        (args.savefilename)
                      )

  for taskid in taskids:
    taskpath = os.path.join(jobpath, taskid)
    if args.iterid is None:
      prefix = "Best"
    else:
      prefix = "Iter%05d" % (args.iterid)
    hmodel = bnpy.ioutil.ModelReader.load_model(taskpath, prefix)
    # Print top words across all topics
    learnedK = hmodel.allocModel.K
    savefid = taskpath + "/top_words.txt"
    fid = open(savefid,'w+')
    for k in xrange(learnedK):
        lamvec = hmodel.obsModel.comp[k].lamvec
        elamvec = lamvec / lamvec.sum()
        topW_ind = np.argsort(elamvec)[-args.topW:]
        for w in xrange(args.topW):
            word = str(Data.vocab_dict[topW_ind[w]])
            fid.write( word + ", " )
        fid.write("...\n")
    fid.close()

if __name__ == "__main__":
  main()


text:
# -*- coding: utf-8 -*-
"""Critic, Generator, optimizers and hyper params definitions."""

import numpy as np
from chainer import Chain, Variable, cuda, initializers, optimizers
from chainer import functions as F
from chainer import links as L

Z_VECTOR_DIM = 128
BASE_CH = 64

HEIGHT = 64
WIDTH = 64
CHANNEL = 3

FLIP_H = True

BATCH_SIZE = 64
UPDATE_MAX = 40000
UPDATE_SAVE_PARAMS = 1000
UPDATE_CRI_PER_GEN = 1

LAMBDA = 10

OPTIMIZER_GEN = optimizers.Adam(alpha=1e-4, beta1=0., beta2=0.9)
OPTIMIZER_CRI = optimizers.Adam(alpha=1e-4, beta1=0., beta2=0.9)

class Generator(Chain):
    """Generator definition."""

    def __init__(self):
        """Prepare learnable layers."""
        super(Generator, self).__init__()
        with self.init_scope():
            self.fc0 = L.Linear(
                Z_VECTOR_DIM, 8 * BASE_CH * 4 * 4, nobias=False)
            self.deconv1 = L.Deconvolution2D(
                8 * BASE_CH, 4 * BASE_CH, 5, stride=2, pad=2, nobias=True)
            self.deconv2 = L.Deconvolution2D(
                4 * BASE_CH, 2 * BASE_CH, 5, stride=2, pad=2, nobias=True)
            self.deconv3 = L.Deconvolution2D(
                2 * BASE_CH, BASE_CH, 5, stride=2, pad=2, nobias=True)
            self.deconv4 = L.Deconvolution2D(
                BASE_CH, 3, 5, stride=2, pad=2, nobias=False)
            self.bn0 = L.BatchNormalization(8 * BASE_CH)
            self.bn1 = L.BatchNormalization(4 * BASE_CH)
            self.bn2 = L.BatchNormalization(2 * BASE_CH)
            self.bn3 = L.BatchNormalization(BASE_CH)

        for param in self.params():
            if param.name == 'W':
                p_norm = np.sqrt(np.sum(np.square(param.data)))
                param.data /= p_norm

    def __call__(self, z):
        """Neural net architecture, Define-by-RUN!"""
        x = F.reshape(self.fc0(z), (-1, 8 * BASE_CH, 4, 4))
        x = F.relu(self.bn0(x))
        height, width = x.data.shape[2:]

        height *= 2
        width *= 2
        self.deconv1.outsize = (height, width)
        x = F.relu(self.bn1(self.deconv1(x)))

        height *= 2
        width *= 2
        self.deconv2.outsize = (height, width)
        x = F.relu(self.bn2(self.deconv2(x)))

        height *= 2
        width *= 2
        self.deconv3.outsize = (height, width)
        x = F.relu(self.bn3(self.deconv3(x)))

        height *= 2
        width *= 2
        self.deconv4.outsize = (height, width)
        x = F.tanh(self.deconv4(x))

        return x

class Critic(Chain):
    """Critic definition."""

    def __init__(self):
        """Prepare learnable layers."""
        super(Critic, self).__init__()
        with self.init_scope():
            self.conv0 = L.Convolution2D(
                3, BASE_CH, 5, stride=2, pad=2, nobias=False)
            self.conv1 = L.Convolution2D(
                BASE_CH, 2 * BASE_CH, 5, stride=2, pad=2, nobias=True)
            self.conv2 = L.Convolution2D(
                2 * BASE_CH, 4 * BASE_CH, 5, stride=2, pad=2, nobias=True)
            self.conv3 = L.Convolution2D(
                4 * BASE_CH, 8 * BASE_CH, 5, stride=2, pad=2, nobias=True)
            self.fc4 = L.Linear(8 * BASE_CH * 4 * 4, 256, nobias=True)

        for param in self.params():
            if param.name == 'W':
                p_norm = np.sqrt(np.sum(np.square(param.data)))
                param.data /= p_norm

    def __call__(self, x):
        """Neural net architecture, Define-by-RUN!"""
        y = F.leaky_relu(self.conv0(x))
        y = F.leaky_relu(self.conv1(y))
        y = F.leaky_relu(self.conv2(y))
        y = F.leaky_relu(self.conv3(y))
        y = F.reshape(y, (-1, 8 * BASE_CH * 4 * 4))
        y = self.fc4(y)

        return y


text:
#Copyright (c) 2020 Ocado. All Rights Reserved.

import matplotlib, os, sys
import matplotlib.pyplot as plt
from matplotlib.cbook import get_sample_data
import numpy as np

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)))
from amrrt.space import StateSpace
from amrrt.grid_graph import GridGraph
from amrrt.metrics import GeodesicMetric

matplotlib.use("pgf")
matplotlib.rcParams.update({
    "pgf.texsystem": "pdflatex",
    'font.family': 'serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
})
'''
fig, ax = plt.subplots(1, 3, sharex='col', sharey='row', subplot_kw={'xmargin': 0.05, 'ymargin':0.05}, gridspec_kw={'wspace':0.7, 'hspace':0.1, 'left':0, 'right':1, 'top':1, 'bottom':0}, figsize=(12, 2.7))
start_position = np.array([6.78531739, 91.99546438])*10
goal_position = np.array([83.0, 33.0])*10
for i in range(3):
    ax[i].imshow(plt.imread(get_sample_data('/Users/danarmstrong/Desktop/Path Planning/rewire_' + ['rtrrt_euclidean', 'amrrt_euclidean', 'amrrt_diffusion'][i] + '.png')), zorder=-1)
    ax[i].text(start_position[0], start_position[1], "A", size=8, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,1)))
    ax[i].text(goal_position[0], goal_position[1], "G", size=8, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,1)))
    ax[i].axis('off')
plt.savefig('rewire_visuals.pgf')
plt.show()
'''

distances = np.load("distances.npy")
times = np.load("times.npy")

images = ["empty", "bug_trap", "maze", "office"]
spaces = [StateSpace.from_image(os.path.join("demos/resources", image + ".png")) for image in images]
grid_graphs = [GridGraph.from_saved(space, os.path.join("demos/resources", image)) for space, image in zip(spaces, images)]
distance_matrices = [np.load(os.path.join("demos/resources", image, "distance_matrix.npy")) for image in images]
geodesic_metrics = [GeodesicMetric(grid_graph, distance_matrix) for grid_graph, distance_matrix in zip(grid_graphs, distance_matrices)]

waypoints = np.array([[[50.0, 50.0], [13.0, 33.2], [15.0, 78.0], [92.4, 8.2], [92.6, 93.6], [32.1, 7.0], [4.9, 56.0]],
                      [[35.0, 33.0], [11.2, 49.2], [35.0, 70.0], [90.0, 59.2], [8.4, 8.2], [53.0, 90.0], [90.0, 47.0]],
                      [[63.0, 51.0], [7.2, 6.6], [28.0, 81.0], [35.0, 37.0], [93.0, 91.0], [89.2,  7.0], [16.2, 37.2]],
                      [[79.0, 65.0], [125.6, 141.6], [162.6, 13.4], [170.8, 128.0], [60.0, 185.8], [23.8, 48.6], [185.0, 178.2]]])

shortest_paths = [[np.linalg.norm(waypoints[0,i] - waypoints[0,i+1]) for i in range(distances.shape[2])],
                  [61.484680484261155, 63.12353620072288, 172.19053767418228, 116.21909186775824, 86.46336558701273, 128.02185403146035],
                  [74.61747343047412, 84.61575781480384, 64.40995862204083, 98.07522464033143, 127.19815498031583, 95.70905438785839],
                  [108.02716559200414, 138.82642333275155, 116.63626014222743, 164.60327397998313, 159.42910328956557, 233.444084954655]]

for i in range(distances.shape[0]):
    for j in range(distances.shape[2]):
        distances[i,:,j] /= shortest_paths[i][j]

start_positions = [np.array([50.0, 50.0]), np.array([35.0, 33.0]), np.array([63.0, 51.0]), np.array([79.0, 65.0])]
goal_positions = np.array([[[13.0, 33.2], [15.0, 78.0], [92.4, 9.2], [92.6, 91.6], [32.1, 9.0], [6.9, 56.0]],
                           [[11.2, 49.2], [35.0, 70.0], [90.0, 59.2], [8.4, 8.2], [27.0, 90.0], [90.0, 10.0]],
                           [[7.2, 7.6], [28.0, 83.0], [37.0, 37.0], [93.0, 91.0], [89.2,  7.5], [14.2, 39.2]],
                           [[125.6, 141.6], [162.6, 13.4], [170.8, 122.0], [65.0, 185.8], [24.8, 50.6], [185.0, 178.2]]])

fig, ax = plt.subplots(3, 4, figsize=(12, 5.4), subplot_kw={'xmargin': 0.04, 'ymargin':0.08}, gridspec_kw={'wspace':0.1, 'hspace':0.15, 'left':0.05, 'right':0.95, 'top':0.99, 'bottom':0.075, 'height_ratios': [0.6,1,1]})

labels = ['G1', 'G2', 'G3', 'G4', 'G5', 'G6']
avgs = np.array([np.nanmean(distances, axis=3), np.nanmean(times, axis=3)])
bar_width = 0.12
for i in range(3):
    for j in range(4):
        if i > 0:
            x = np.arange(len(labels))
            bars1 = ax[i,j].bar(x - 2*bar_width*1.3, avgs[i-1,j,0], bar_width, color='#00bdaa')
            bars2 = ax[i,j].bar(x - 1*bar_width*1.3, avgs[i-1,j,1], bar_width, color='#400082')
            bars3 = ax[i,j].bar(x, avgs[i-1,j,2], bar_width, color='#ff0000')
            bars4 = ax[i,j].bar(x + 1*bar_width*1.3, avgs[i-1,j,3], bar_width, color='#00c800')
            bars5 = ax[i,j].bar(x + 2*bar_width*1.3, avgs[i-1,j,4], bar_width, color='#ffa600')
            ax[i,j].set_xticks(x)
            ax[i,j].spines['top'].set_visible(False)
            ax[i,j].spines['right'].set_visible(False)
            if i == 1:
                ax[i,j].set_xticklabels([])
                ax[i,j].set_ylim(0.75,1.75)
                ax[i,j].set_yticks([0.75,1,1.25,1.5,1.75])
                ax[i,j].text(2.5, 1.82, ['Empty', 'Bug Trap', 'Maze', 'Office'][j], ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)))
            else:
                ax[i,j].set_xticklabels(labels)
                ax[i,j].set_ylim(0.01,1000)
                ax[i,j].set_yticks([0.01, 0.1, 1, 10, 100, 1000])
                ax[i,j].set_yscale("log")
            if j > 0 : ax[i,j].set_yticklabels([])
        else:
            ax[i,j].axis('off')
            ax[i,j].imshow(plt.imread(get_sample_data('/Users/danarmstrong/Desktop/Path Planning/demos/resources/' + images[j] + '.png')), zorder=-1)
            ax[i,j].text(start_positions[j][0], start_positions[j][1], "S", size=9, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
            for k, (x, y) in enumerate(goal_positions[j]):
                if j == 3 : x, y = x * 1, y * 1
                ax[i,j].text(x, y, ""+str(k+1), size=9, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
"""for i in range(4):
    imax = fig.add_axes([0.787, 0.765 - 0.227*i, 0.19, 0.19], anchor='NE', zorder=-1)
    imax.imshow(plt.imread(get_sample_data('/Users/danarmstrong/Desktop/Path Planning/demos/resources/' + images[i] + '.png')), zorder=-1)
    imax.axis('off')
    imax.text(start_positions[i][0], start_positions[i][1], "S", size=9, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
    for k, (x, y) in enumerate(goal_positions[i]):
        imax.text(x, y, ""+str(k+1), size=9, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
    if i < 3 : imax.text(110, 50, ['Empty', 'Bug Trap', 'Maze', 'Office'][i], ha="center", va="center", rotation=-90)
    else : imax.text(220, 100, ['Empty', 'Bug Trap', 'Maze', 'Office'][i], ha="center", va="center", rotation=-90)
"""
fig.text(0.5, 0.013, "Goal Number", ha='center', va='center', size=11)
fig.text(0.0105, 0.596, "Path length ratio", ha='center', va='center', rotation='vertical', size=11)
fig.text(0.0105, 0.238, "Search time (s)", ha='center', va='center', rotation='vertical', size=11)
fig.legend((matplotlib.lines.Line2D([], [], color='#00bdaa'), matplotlib.lines.Line2D([], [], color='#400082'),
            matplotlib.lines.Line2D([], [], color='#ff0000'), matplotlib.lines.Line2D([], [], color='#00c800'), matplotlib.lines.Line2D([], [], color='#ffa600')),
            ('RT-RRT*', 'RT-RRT*(D)', 'AM-RRT*(E)', 'AM-RRT*(D)', 'AM-RRT*(G)'), loc=(0.057,0.61), markerfirst=True, labelspacing=0.2, prop={'size': 8}, markerscale=1.4)

for i in range(avgs.shape[2]):
    print(i,j,np.sum(avgs[0,:,i]),(np.sum(avgs[0,:,0])-np.sum(avgs[0,:,i])) / np.sum(avgs[0,:,0]) * 100)
    print(i,j,np.sum(avgs[1,:,i]),(np.sum(avgs[1,:,0])-np.sum(avgs[1,:,i])) / np.sum(avgs[1,:,0]) * 100)
    print(i,j,np.sum(avgs[0,3,i]),(np.sum(avgs[0,3,0])-np.sum(avgs[0,3,i])) / np.sum(avgs[0,3,0]) * 100)
    print(i,j,np.sum(avgs[1,3,i]),(np.sum(avgs[1,3,0])-np.sum(avgs[1,3,i])) / np.sum(avgs[1,3,0]) * 100)
    print()

print(np.sum(avgs[1,3,1]) / np.sum(avgs[1,3,3]))
print(np.sum(avgs[1,:,0]) / (26.1 + np.sum(avgs[1,:,3])))
print(np.mean(avgs[0,:,3]))

plt.show()
plt.savefig('all.pgf')

"""
fig, ax = plt.subplots(2, 5, figsize=(13, 4.7), subplot_kw={'xmargin': 0.08, 'ymargin':0.08}, gridspec_kw={'wspace':0.14, 'hspace':0.15, 'left':0.05, 'right':0.95, 'top':0.955, 'bottom':0.085, 'width_ratios': [2.3,2.3,2.3,2.3,0.5]})

labels = ['G1', 'G2', 'G3', 'G4', 'G5', 'G6']
avgs = np.array([np.nanmean(distances, axis=3), np.nanmean(times, axis=3)])
bar_width = 0.1
for i in range(2):
    for j in range(5):
        if j < 4:
            x = np.arange(len(labels))
            bars1 = ax[i,j].bar(x - 2*bar_width, avgs[i,j,0], bar_width, color='#400082')
            bars2 = ax[i,j].bar(x - 1*bar_width, avgs[i,j,1], bar_width, color='#00bdaa')
            bars3 = ax[i,j].bar(x, avgs[i,j,2], bar_width, color='#ff0000')
            bars4 = ax[i,j].bar(x + 1*bar_width, avgs[i,j,3], bar_width, color='#ffc800')
            bars5 = ax[i,j].bar(x + 2*bar_width, avgs[i,j,4], bar_width, color='#00c800')
            ax[i,j].set_xticks(x)
            ax[i,j].spines['top'].set_visible(False)
            ax[i,j].spines['right'].set_visible(False)
            if i == 0:
                ax[i,j].set_xticklabels([])
                ax[i,j].set_ylim(0.75,1.75)
                ax[i,j].set_yticks([0.75,1,1.25,1.5,1.75])
                ax[i,j].text(2.5, 2.09, ['Empty', 'Bug Trap', 'Maze', 'Office'][j], ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)))
            else:
                ax[i,j].set_xticklabels(labels)
                ax[i,j].set_ylim(0.01,1000)
                ax[i,j].set_yticks([0.01, 0.1, 1, 10, 100, 1000])
                ax[i,j].set_yscale("log")
            if j > 0 : ax[i,j].set_yticklabels([])
        else:
            ax[i,j].axis('off')
for i in range(4):
    imax = fig.add_axes([0.787, 0.765 - 0.227*i, 0.19, 0.19], anchor='NE', zorder=-1)
    imax.imshow(plt.imread(get_sample_data('/Users/danarmstrong/Desktop/Path Planning/demos/resources/' + images[i] + '.png')), zorder=-1)
    imax.axis('off')
    imax.text(start_positions[i][0], start_positions[i][1], "S", size=9, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
    for k, (x, y) in enumerate(goal_positions[i]):
        imax.text(x, y, ""+str(k+1), size=9, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
    if i < 3 : imax.text(110, 50, ['Empty', 'Bug Trap', 'Maze', 'Office'][i], ha="center", va="center", rotation=-90)
    else : imax.text(220, 100, ['Empty', 'Bug Trap', 'Maze', 'Office'][i], ha="center", va="center", rotation=-90)

fig.text(0.5, 0.013, "Goal Number", ha='center', va='center', size=11)
fig.text(0.015, 0.76, "Path length ratio", ha='center', va='center', rotation='vertical', size=11)
fig.text(0.015, 0.29, "Search time", ha='center', va='center', rotation='vertical', size=11)
fig.legend((matplotlib.lines.Line2D([], [], color='#400082'), matplotlib.lines.Line2D([], [], color='#00bdaa'),
            matplotlib.lines.Line2D([], [], color='#ff0000'), matplotlib.lines.Line2D([], [], color='#ffc800'), matplotlib.lines.Line2D([], [], color='#00c800')),
            ('RT-RRT*', 'RT-RRT*(D)', 'AM-RRT*(E)', 'AM-RRT*(D)', 'AM-RRT*(G)'), loc=(0.054,0.79), markerfirst=True, labelspacing=0.2, prop={'size': 8})

print(np.nonzero(times > 15000*0.15))
for i in range(avgs.shape[2]):
    print(i,j,np.sum(avgs[0,:,i]),(np.sum(avgs[0,:,0])-np.sum(avgs[0,:,i])) / np.sum(avgs[0,:,0]) * 100)
    print(i,j,np.sum(avgs[1,:,i]),(np.sum(avgs[1,:,0])-np.sum(avgs[1,:,i])) / np.sum(avgs[1,:,0]) * 100)
    print(i,j,np.sum(avgs[0,3,i]),(np.sum(avgs[0,3,0])-np.sum(avgs[0,3,i])) / np.sum(avgs[0,3,0]) * 100)
    print(i,j,np.sum(avgs[1,3,i]),(np.sum(avgs[1,3,0])-np.sum(avgs[1,3,i])) / np.sum(avgs[1,3,0]) * 100)
    print()

print(np.sum(avgs[1,3,1]) / np.sum(avgs[1,3,3]))
print(np.mean(avgs[0,:,3]))
plt.show()
plt.savefig('all.pgf')
"""
'''fig, ax = plt.subplots(1, 4, sharex='col', sharey='row', subplot_kw={'xmargin': 0.05, 'ymargin':0.05}, gridspec_kw={'wspace':0.08, 'hspace':0.08, 'left':0.001, 'right':0.999, 'top':0.9, 'bottom':0.001}, figsize=(12, 3.2))
for i in range(4):
    ax[i].imshow(plt.imread(get_sample_data('/Users/danarmstrong/Desktop/Path Planning/bm/' + images[i] + '.png')), zorder=-1)
    ax[i].text(50, -6, ['Empty', 'Bug Trap', 'Maze', 'Office'][i], size=20, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)))
    ax[i].text(start_positions[i][0], start_positions[i][1], "S", size=20, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
    for k, (x, y) in enumerate(goal_positions[i]):
        ax[i].text(x, y, ""+str(k+1), size=20, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
    ax[i].axis('off')
plt.savefig('images.pgf')
'''

"""
        if i == 0:
            ax[i,j].imshow(plt.imread(get_sample_data('/Users/danarmstrong/Desktop/Path Planning/bm/' + images[j] + '.png')), zorder=-1)
            if j == 3 : ax[i,j].text(100, -16, ['Empty', 'Bug Trap', 'Maze', 'Office'][j], ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)))
            else : ax[i,j].text(50, -8, ['Empty', 'Bug Trap', 'Maze', 'Office'][j], ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)))
            ax[i,j].text(start_positions[j][0], start_positions[j][1], "S", size=9, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
            for k, (x, y) in enumerate(goal_positions[j]):
                ax[i,j].text(x, y, ""+str(k+1), size=8, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
            ax[i,j].axis('off')
"""

#np.save("iterations.npy", iterations)

'''
fig, ax = plt.subplots(2, 2, sharex='col', sharey='row', subplot_kw={'xmargin': 0.05, 'ymargin':0.05}, gridspec_kw={'wspace':0.08, 'hspace':0.105, 'left':0.08, 'right':0.92, 'top':0.994, 'bottom':0.08}, figsize=(7, 4.8))
for i in range(2):
    for j in range(2):
        ax[i, j].plot(range(coverages.shape[2]), coverages[2*i+j,0], '#400082')
        ax[i, j].plot(range(coverages.shape[2]), coverages[2*i+j,1], '#00bdaa')
        ax[i, j].plot(range(coverages.shape[2]), coverages[2*i+j,3], '#ffc800')
        if i == 0 and j == 0 : ax[i, j].plot(range(coverages.shape[2]), coverages[2*i+j,2], '#ff0000', linestyle=":")
        else : ax[i, j].plot(range(coverages.shape[2]), coverages[2*i+j,2], '#ff0000')
        ax[i, j].set_xticks([0,50,100,150,200,250])
        ax[i, j].set_yticks([0.0,0.2,0.4,0.6,0.8,1.0])
        ax[i, j].text(0.1, 0.964, "ABCD"[2*i+j], size=8.5, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)))
fig.text(0.5, 0.011, 'Number of iterations', ha='center', va='center')
fig.text(0.015, 0.5, 'Fraction of map reachable', ha='center', va='center', rotation='vertical')
fig.legend((matplotlib.lines.Line2D([], [], color='#400082'), matplotlib.lines.Line2D([], [], color='#00bdaa'), matplotlib.lines.Line2D([], [], color='#ff0000'), matplotlib.lines.Line2D([], [], color='#FFE100')),
    ('RT-RRT*', 'RT-RRT*(D)', 'LM-RRT*(E)', 'LM-RRT*(D)'), loc=(0.755,0.08), framealpha=0, markerfirst=False, labelspacing=0.2, fontsize=8)
plt.savefig('coverages.pgf')
plt.show()
'''
'''
labels = ['G1', 'G2', 'G3', 'G4', 'G5', 'G6']
x = np.arange(len(labels))  # the label locations
width = 0.1  # the width of the bars
fig, ax = plt.subplots(2, 2, sharex='col', sharey='row', subplot_kw={'xmargin': 0.05, 'ymargin':0.05}, gridspec_kw={'wspace':0.08, 'hspace':0.105, 'left':0.08, 'right':0.92, 'top':0.994, 'bottom':0.08}, figsize=(7, 4.8))
for i in range(2):
    for j in range(2):
        bars1 = ax[i, j].bar(x - 1.5*width, distances[2*i+j,0], width, label='rtrrt', color='#400082')
        bars2 = ax[i, j].bar(x - 0.5*width, distances[2*i+j,1], width, label='dmrrt', color='#00bdaa')
        bars3 = ax[i, j].bar(x + 0.5*width, distances[2*i+j,2], width, label='emrrt', color='#ff0000')
        bars4 = ax[i, j].bar(x + 1.5*width, distances[2*i+j,3], width, label='emrrt', color='#ffc800')
        ax[i, j].set_xticks(x)
        ax[i, j].set_xticklabels(labels)
        ax[i, j].autoscale(False)
        if i == 0:
            ax[i, j].set_ylim(0,200*1.05)
            ax[i, j].text(5.3, 200*0.99, "ABCD"[2*i+j], size=8.5, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)), fontsize=10)
        else:
            ax[i, j].set_ylim(0,300*1.05)
            ax[i, j].text(5.3, 300*0.99, "ABCD"[2*i+j], size=8.5, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)), fontsize=10)
fig.text(0.5, 0.013, 'Goal number', ha='center', va='center', fontsize=12)
fig.text(0.015, 0.5, 'Distance of path to goal', ha='center', va='center', rotation='vertical', fontsize=12)
fig.legend((matplotlib.lines.Line2D([], [], color='#400082'), matplotlib.lines.Line2D([], [], color='#00bdaa'), matplotlib.lines.Line2D([], [], color='#ff0000'), matplotlib.lines.Line2D([], [], color='#FFE100')),
    ('RT-RRT*', 'RT-RRT*(D)', 'LM-RRT*(E)', 'LM-RRT*(D)'), loc=(0.087,0.838), framealpha=0, markerfirst=True, labelspacing=0.2, fontsize=10)
plt.savefig('distances.pgf')
plt.show()

labels = ['G1', 'G2', 'G3', 'G4', 'G5', 'G6']
x = np.arange(len(labels))  # the label locations
width = 0.1  # the width of the bars
fig, ax = plt.subplots(2, 2, sharex='col', sharey='row', subplot_kw={'xmargin': 0.05, 'ymargin':0.05}, gridspec_kw={'wspace':0.08, 'hspace':0.105, 'left':0.08, 'right':0.92, 'top':0.994, 'bottom':0.08}, figsize=(7, 4.8))
for i in range(2):
    for j in range(2):
        bars1 = ax[i, j].bar(x - 1.5*width, iterations[2*i+j,0], width, label='rtrrt', color='#400082')
        bars2 = ax[i, j].bar(x - 0.5*width, iterations[2*i+j,1], width, label='dmrrt', color='#00bdaa')
        bars3 = ax[i, j].bar(x + 0.5*width, iterations[2*i+j,2], width, label='emrrt', color='#ff0000')
        bars4 = ax[i, j].bar(x + 1.5*width, iterations[2*i+j,3], width, label='emrrt', color='#ffc800')
        ax[i, j].set_xticks(x)
        ax[i, j].set_xticklabels(labels)
        ax[i, j].set_yscale("log")
        ax[i, j].autoscale(False)
        if i == 0:
            ax[i, j].set_ylim(0.1,100*1.4)
            ax[i, j].set_yticks([0.1, 1, 10, 100])
            ax[i, j].text(5.3, 97, "ABCD"[2*i+j], size=8.5, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)), fontsize=10)
        else:
            ax[i, j].set_ylim(0.1,1000*1.75)
            ax[i, j].text(5.3, 1090, "ABCD"[2*i+j], size=8.5, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)), fontsize=10)
fig.text(0.5, 0.013, 'Goal number', ha='center', va='center', fontsize=12)
fig.text(0.015, 0.5, 'Number of iterations to find path to goal', ha='center', va='center', rotation='vertical', fontsize=12)
fig.legend((matplotlib.lines.Line2D([], [], color='#400082'), matplotlib.lines.Line2D([], [], color='#00bdaa'), matplotlib.lines.Line2D([], [], color='#ff0000'), matplotlib.lines.Line2D([], [], color='#FFE100')),
    ('RT-RRT*', 'RT-RRT*(D)', 'LM-RRT*(E)', 'LM-RRT*(D)'), loc=(0.087,0.838), framealpha=0, markerfirst=True, labelspacing=0.2, fontsize=10)
plt.savefig('iterations.pgf')
plt.show()
'''
'''
fig, ax = plt.subplots(1, 4, sharex='col', sharey='row', subplot_kw={'xmargin': 0.05, 'ymargin':0.05}, gridspec_kw={'wspace':0.08, 'hspace':0.08, 'left':0.001, 'right':0.999, 'top':0.9, 'bottom':0.001}, figsize=(12, 3.2))
start_positions = [np.array([50.0, 50.0]), np.array([63.0, 51.0]), np.array([35.0, 33.0]), np.array([79.0/2, 65.0/2])]
goal_positions = np.array([[[13.0, 33.2], [15.0, 78.0], [92.4, 8.2], [92.6, 93.6], [32.1, 7.0], [4.9, 56.0]],
                           [[7.2, 6.6], [28.0, 81.0], [35.0, 37.0], [93.0, 91.0], [89.2,  7.0], [16.2, 37.2]],
                           [[11.2, 49.2], [35.0, 70.0], [90.0, 59.2], [8.4, 8.2], [53.0, 90.0], [90.0, 47.0]],
                           [[125.6/2, 141.6/2], [162.6/2, 13.4/2], [170.8/2, 122.0/2], [60.0/2, 185.8/2], [23.8/2, 48.6/2], [185.0/2, 178.2/2]]])
for i in range(4):
    ax[i].imshow(plt.imread(get_sample_data('/Users/danarmstrong/Desktop/Path Planning/bm/' + ['empty', 'maze', 'bug_trap', 'office'][i] + '.png')), zorder=-1)
    ax[i].text(50, -6, ['Empty (A)', 'Maze (B)', 'Bug Trap (C)', 'Office (D)'][i], size=20, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,0,0,0)))
    ax[i].text(start_positions[i][0], start_positions[i][1], "S", size=20, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
    for k, (x, y) in enumerate(goal_positions[i]):
        ax[i].text(x, y, ""+str(k+1), size=20, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(1,200/255,0,0)))
    ax[i].axis('off')
plt.savefig('images.pgf')
plt.show()
'''

"""fig, ax = plt.subplots(1, 3, sharex='col', sharey='row', subplot_kw={'xmargin': 0.05, 'ymargin':0.05}, gridspec_kw={'wspace':0.3, 'hspace':0.1, 'left':0, 'right':1, 'top':1, 'bottom':0}, figsize=(12.4, 2.7))
start_position = np.array([25.0, 85.0])*10
goal_position = np.array([83.0, 20.0])*10
for i in range(3):
    ax[i].imshow(plt.imread(get_sample_data('/Users/danarmstrong/Desktop/AM-RRT*/' + ['rtrrt_e', 'amrrt_e', 'amrrt_d'][i] + '.png')), zorder=-1)
    ax[i].text(start_position[0], start_position[1], "A", size=8, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,189/255,170/255)))
    ax[i].text(goal_position[0], goal_position[1], "G", size=8, ha="center", va="center", bbox=dict(boxstyle="round", ec=(0,0,0,0), fc=(0,189/255,170/255)))
    ax[i].axis('off')
plt.savefig('rewires.pgf')
plt.show()

"""

'''print(((distances[:,0] - distances[:,3]) / distances[:,3]).mean()*100)
print(((distances[:,0] - distances[:,2]) / distances[:,2]).mean()*100)
print(((distances[:,0] - distances[:,1]) / distances[:,1]).mean()*100)
print(((iterations[:,0] - iterations[:,3]) / iterations[:,3]).mean()*100)
print(((iterations[:,0] - iterations[:,2]) / iterations[:,2]).mean()*100)
print(((iterations[:,0] - iterations[:,1]) / iterations[:,1]).mean()*100)'''

'''rtd_dist = ((distances[:,0] - distances[:,1]) / distances[:,1]).mean()
lme_dist = ((distances[:,0] - distances[:,2]) / distances[:,2]).mean()
lmd_dist = ((distances[:,0] - distances[:,3]) / distances[:,3]).mean()
lmd_dist_office = ((distances[3,0] - distances[3,3]) / distances[3,3]).mean()
rtd_iter = ((iterations[:,0] - iterations[:,1]) / iterations[:,1]).mean()
lme_iter = ((iterations[:,0] - iterations[:,2]) / iterations[:,2]).mean()
lmd_iter = ((iterations[:,0] - iterations[:,3]) / iterations[:,3]).mean()
lmd_iter_office = ((iterations[3,0] - iterations[3,3]) / iterations[3,3]).mean()

print((distances[:,1] / distances[:,0]).mean())
print((distances[:,2] / distances[:,0]).mean())
print((iterations[:,2] / iterations[:,0]).mean())

print()

print(((iterations[:,0] - iterations[:,2]) / iterations[:,2]).mean())

print(rtd_dist/(rtd_dist+1)*100)
print(lme_dist/(lme_dist+1)*100)
print(lmd_dist/(lmd_dist+1)*100)
print(rtd_iter/(rtd_iter+1)*100)
print(lme_iter/(lme_iter+1)*100)
print(lmd_iter/(lmd_iter+1)*100)
'''

max_stars_repo_name: dan-armstrong/amrrt

max_issues_repo_name: dan-armstrong/amrrt

max_forks_repo_name: dan-armstrong/amrrt


text:
from typing import Optional
from pathlib import Path

import wandb
import joblib
import pandas as pd
import numpy as np
from plotly.graph_objs._figure import Figure
from sklearn.base import BaseEstimator
from sklearn.utils.validation import check_is_fitted
from sklearn.exceptions import NotFittedError
from sklearn.pipeline import Pipeline

from poniard.plugins.core import BasePlugin

class WandBPlugin(BasePlugin):
    """Weights and Biases plugin. Kwargs from the constructor are passed to `wandb.init()`.

    Parameters
    ----------
    project :
        Name of the Weights and Biases project.
    entity :
        Name of the Weights and Biases entity (username).
    """

    def __init__(
        self, project: Optional[str] = None, entity: Optional[str] = None, **kwargs
    ):
        self.project = project
        self.entity = entity
        wandb.init(project=project, entity=entity, **kwargs)

    def build_config(self) -> dict:
        """Helper method that builds a config dict from the poniard instance."""
        return {
            "estimators": self._poniard.estimators_,
            "metrics": self._poniard.metrics_,
            "cv": self._poniard.cv_,
            "preprocess": self._poniard.preprocess,
            "preprocessor": self._poniard.preprocessor_,
            "custom_preprocessor": self._poniard.custom_preprocessor,
            "numeric_imputer": self._poniard.numeric_imputer,
            "scaler": self._poniard.scaler,
            "numeric_threshold": self._poniard.numeric_threshold,
            "cardinality_threshold": self._poniard.cardinality_threshold,
            "verbosity": self._poniard.verbose,
            "n_jobs": self._poniard.n_jobs,
            "random_state": self._poniard.random_state,
        }

    def on_setup_end(self) -> None:
        """Log config and dataset."""
        config = self.build_config()
        wandb.config.update(config)

        X, y = self._poniard.X, self._poniard.y
        try:
            dataset = pd.concat([X, y], axis=1)
        except TypeError:
            if y.ndim == 1:
                dataset = np.concatenate([X, np.expand_dims(y, 1)], axis=1)
            else:
                dataset = np.concatenate([X, y], axis=1)
            dataset = pd.DataFrame(dataset)
        table = wandb.Table(dataframe=dataset)
        wandb.log({"dataset": table})
        return

    def on_plot(self, figure: Figure, name: str):
        """Log plots."""
        wandb.log({name: figure})
        return

    def on_fit_end(self):
        """Log results table."""
        results = self._poniard.show_results().reset_index()
        results.rename(columns={"index": "Estimator"}, inplace=True)
        table = wandb.Table(dataframe=results)
        wandb.log({"results": table})
        return

    def on_get_estimator(self, estimator: BaseEstimator, name: str):
        """Log fitted estimator on full data and log multiple estimator plots provided by WandB."""
        X, y = self._poniard.X, self._poniard.y
        saved_model_path = Path(wandb.run.dir, f"{name}.joblib")
        # This works fine for sklearn and sklearn-like models only.
        try:
            check_is_fitted(estimator)
        except NotFittedError:
            estimator.fit(X, y)
        joblib.dump(estimator, saved_model_path)
        artifact = wandb.Artifact(name=f"saved_estimators", type="model")
        artifact.add_file(saved_model_path.as_posix(), name=name)
        wandb.log_artifact(artifact)

        (
            X_train,
            X_test,
            y_train,
            y_test,
        ) = self._poniard._train_test_split_from_cv()
        estimator.fit(X_train, y_train)
        y_pred = estimator.predict(X_test)
        estimator_type = self._poniard._check_estimator_type()
        if estimator_type == "classifier":
            labels = y_test.unique()
            wandb.sklearn.plot_confusion_matrix(y_test, y_pred, labels)
            if hasattr(estimator, "predict_proba"):
                y_probas = estimator.predict_proba(X_test)
                wandb.sklearn.plot_roc(y_test, y_probas, labels)
                wandb.sklearn.plot_precision_recall(y_test, y_probas, labels)
            wandb.sklearn.plot_calibration_curve(estimator, X_train, y_train, name)
            if isinstance(estimator, Pipeline):
                estimator = estimator[-1]
            wandb.sklearn.plot_feature_importances(estimator, pd.DataFrame(X).columns)
        else:
            wandb.sklearn.plot_residuals(estimator, X_train, y_train)
            wandb.sklearn.plot_outlier_candidates(estimator, X_train, y_train)
        return

    def on_remove_estimators(self):
        """Log config and results table."""
        self.on_fit_end()
        config = self.build_config()
        wandb.config.update(config)
        return

max_stars_repo_name: rxavier/poniard

max_issues_repo_name: rxavier/poniard

max_forks_repo_name: rxavier/poniard


text:
from elegy.module import Deferable
from elegy import hooks
import typing as tp

import haiku as hk
import numpy as np

class Sequential(hk.Sequential, Deferable):
    """
    Sequentially calls the given list of layers.

    Note that `Sequential` is limited in the range of possible architectures
    it can handle. This is a deliberate design decision; `Sequential` is only
    meant to be used for the simple case of fusing together modules/ops where
    the input of a particular module/op is the output of the previous one.

    Another restriction is that it is not possible to have extra arguments in the
    `__call__` method that are passed to the constituents of the module - for
    example, if there is a `BatchNorm` module in `Sequential` and the user
    wishes to switch the `is_training` flag. If this is the desired use case,
    the recommended solution is to subclass :class:`Module` and implement
    `__call__`:

    ```python
    class CustomModule(elegy.Module):
        def __call__(self, x, is_training):
            x = elegy.Conv2D(32, 4, 2)(x)
            x = elegy.BatchNorm(True, True, 0.9)(x, is_training)
            x = jax.nn.relu(x)
            return x
    ```
    """

    def __init__(
        self,
        layers: tp.Iterable[tp.Callable[..., tp.Any]],
        name: tp.Optional[str] = None,
    ):
        """
        Creates a Sequential instance.

        Arguments:
            layers: A list of moules or functions that take a single input.
            name: An optional string name for the class. Must be a valid Python
                identifier. If `name` is not provided then the class name for the
                current instance is converted to `lower_snake_case` and used instead.
        """
        super().__init__(layers=layers, name=name)

    def __call__(self, inputs: np.ndarray, *args, **kwargs):
        """
        Arguments:
            inputs: Input array.
            args: Additional positional input arguments for the first layer.
            kwargs: Additional keyword arguments for the first layer.
        """
        outputs = super().__call__(inputs, *args, **kwargs)

        hooks.add_summary(None, self.__class__.__name__, outputs)

        return outputs

max_stars_repo_name: Ciroye/elegy

max_issues_repo_name: Ciroye/elegy

max_forks_repo_name: Ciroye/elegy