text:
import types
import pytest
import numpy as np
import discretisedfield as df

def check_mesh(mesh):
    assert isinstance(mesh.p1, tuple)
    assert len(mesh.p1) == 3
    assert isinstance(mesh.p2, tuple)
    assert len(mesh.p2) == 3
    assert isinstance(mesh.cell, tuple)
    assert len(mesh.cell) == 3
    assert isinstance(mesh.n, tuple)
    assert len(mesh.n) == 3
    assert all(isinstance(i, int) for i in mesh.n)
    assert isinstance(mesh.pmin, tuple)
    assert len(mesh.pmin) == 3
    assert isinstance(mesh.pmax, tuple)
    assert len(mesh.pmax) == 3
    assert isinstance(mesh.l, tuple)
    assert len(mesh.l) == 3
    assert isinstance(mesh.ntotal, int)
    assert isinstance(mesh.pbc, set)
    assert all(isinstance(i, str) for i in mesh.pbc)
    assert isinstance(mesh.regions, dict)
    assert isinstance(mesh.name, str)

class TestMesh:
    def setup(self):
        self.valid_args = [[(0, 0, 0), (5, 5, 5),
                            [1, 1, 1], None],
                           [(-1, 0, -3), (5, 7, 5),
                            None, (1, 1, 1)],
                           [(0, 0, 0), (5e-9, 5e-9, 5e-9),
                            None, (1e-9, 1e-9, 1e-9)],
                           [(0, 0, 0), (5e-9, 5e-9, 5e-9),
                            (5, 5, 5), None],
                           [(-1.5e-9, -5e-9, 0), (1.5e-9, -15e-9, -10e-9),
                            None, (1.5e-9, 0.5e-9, 10e-9)],
                           [(-1.5e-9, -5e-9, 0), (1.5e-9, -15e-9, -10e-9),
                            (3, 50, 2), None],
                           [(-1.5e-9, -5e-9, -5e-9), np.array((0, 0, 0)),
                            None, (0.5e-9, 1e-9, 5e-9)],
                           [(-1.5e-9, -5e-9, -5e-9), np.array((0, 0, 0)),
                            (5, 5, 7), None],
                           [[0, 5e-6, 0], (-1.5e-6, -5e-6, -5e-6),
                            None, (0.5e-6, 1e-6, 2.5e-6)],
                           [[0, 5e-6, 0], (-1.5e-6, -5e-6, -5e-6),
                            (1, 10, 100), None],
                           [(0, 125e-9, 0), (500e-9, 0, -3e-9),
                            None, (2.5e-9, 2.5e-9, 3e-9)]]

        self.invalid_args = [[(0, 0, 0), (5, 5, 5),
                              None, (-1, 1, 1)],
                             [(0, 0, 0), (5, 5, 5),
                              (-1, 1, 1), None],
                             [(0, 0, 0), (5, 5, 5),
                              'n', None],
                             [(0, 0, 0), (5, 5, 5),
                              (1, 2, 2+1j), None],
                             [(0, 0, 0), (5, 5, 5),
                              (1, 2, '2'), None],
                             [('1', 0, 0), (1, 1, 1),
                              None, (0, 0, 1e-9)],
                             [(-1.5e-9, -5e-9, 'a'), (1.5e-9, 15e-9, 16e-9),
                              None, (5, 1, -1e-9)],
                             [(-1.5e-9, -5e-9, 'a'), (1.5e-9, 15e-9, 16e-9),
                              (5, 1, -1), None],
                             [(-1.5e-9, -5e-9, 0), (1.5e-9, 16e-9),
                              None, (0.1e-9, 0.1e-9, 1e-9)],
                             [(-1.5e-9, -5e-9, 0), (1.5e-9, 15e-9, 1+2j),
                              None, (5, 1, 1e-9)],
                             ['string', (5, 1, 1e-9),
                              None, 'string'],
                             [(-1.5e-9, -5e-9, 0), (1.5e-9, 15e-9, 16e-9),
                              None, 2+2j]]

    def test_init_with_cell(self):
        p1 = (0, -4, 16.5)
        p2 = (15, 10.1, 11)
        cell = (1, 0.1, 0.5)
        pbc = 'yxz'
        name = 'test_mesh'
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell, pbc=pbc, name=name)

        check_mesh(mesh)
        assert mesh.p1 == p1
        assert mesh.p2 == p2
        assert mesh.cell == cell
        assert mesh.pbc == set(pbc)
        assert mesh.name == name
        assert mesh.pmin == (0, -4, 11)
        assert mesh.pmax == (15, 10.1, 16.5)
        assert mesh.l[0] == 15 - 0
        assert mesh.l[1] == 10.1 - (-4)
        assert mesh.l[2] == 16.5 - 11
        assert mesh.n[0] == (15 - 0) / 1.0
        assert mesh.n[1] == (10.1 - (-4)) / 0.1
        assert mesh.n[2] == (16.5 - 11) / 0.5
        assert mesh.ntotal == 23265

    def test_init_with_n(self):
        p1 = (0, -4, 16.5)
        p2 = (15, 10.1, 11)
        n = (15, 141, 11)
        pbc = 'yx'
        name = 'test_mesh'
        mesh = df.Mesh(p1=p1, p2=p2, n=n, pbc=pbc, name=name)

        check_mesh(mesh)
        assert mesh.p1 == p1
        assert mesh.p2 == p2
        assert mesh.pbc == set(pbc)
        assert mesh.name == name
        assert mesh.pmin == (0, -4, 11)
        assert mesh.pmax == (15, 10.1, 16.5)
        assert mesh.l[0] == 15 - 0
        assert mesh.l[1] == 10.1 - (-4)
        assert mesh.l[2] == 16.5 - 11
        assert mesh.cell[0] == (15 - 0) / 15
        assert mesh.cell[1] == (10.1 - (-4)) / 141
        assert mesh.cell[2] == (16.5 - 11) / 11
        assert mesh.ntotal == 15*141*11

    def test_init_with_n_and_cell(self):
        p1 = (0, -4, 16.5)
        p2 = (15, 10.1, 11)
        n = (15, 141, 11)
        cell = (1, 0.1, 0.5)
        pbc = 'x'
        name = 'test_mesh'
        with pytest.raises(ValueError) as excinfo:
            mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell, pbc=pbc, name=name)
        assert 'One and only one' in str(excinfo.value)

    def test_init_valid_args(self):
        for p1, p2, n, cell in self.valid_args:
            mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell)

            check_mesh(mesh)
            assert mesh.name == 'mesh'  # default name value
            assert mesh.pbc == set()  # default pbc value

    def test_init_invalid_args(self):
        for p1, p2, n, cell in self.invalid_args:
            with pytest.raises((TypeError, ValueError)):
                # Exceptions are raised by descriptors.
                mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell)

    def test_init_invalid_name(self):
        p1 = (0, 0, 0)
        p2 = (10, 10, 10)
        n = (10, 100, 2)
        for name in ['mesh name', '2name', ' ', 5, '-name', '+mn']:
            with pytest.raises((TypeError, ValueError)):
                # Exception is raised by the descriptor for mesh.name.
                mesh = df.Mesh(p1=p1, p2=p2, n=n, name=name)

    def test_init_valid_pbc(self):
        for p1, p2, n, cell in self.valid_args:
            for pbc in ['x', 'z', 'zx', 'yxzz', 'yz']:
                mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell, pbc=pbc)

                check_mesh(mesh)
                assert mesh.pbc == set(pbc)

    def test_init_invalid_pbc(self):
        for p1, p2, n, cell in self.valid_args:
            for pbc in ['abc', 'a', '123', 5]:
                with pytest.raises((ValueError, TypeError)):
                    # Exception is raised by the descriptor for mesh.pbc.
                    mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell, pbc=pbc)

    def test_zero_domain_edge_length(self):
        args = [[(0, 100e-9, 1e-9), (150e-9, 100e-9, 6e-9),
                 None, (1e-9, 0.01e-9, 1e-9)],
                [(0, 100e-9, 0), (150e-9, 101e-9, 0),
                 None, (2e-9, 1e-9, 0.1e-9)],
                [(0, 101e-9, -1), (150e-9, 101e-9, 0),
                 (1, 1, 1), None],
                [(10e9, 10e3, 0), (0.01e12, 11e3, 5),
                 (5, 100, 30), None]]

        for p1, p2, n, cell in args:
            with pytest.raises(ValueError) as excinfo:
                mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell)
            assert 'is zero' in str(excinfo.value)

    def test_domain_not_aggregate_of_cell(self):
        args = [[(0, 100e-9, 1e-9), (150e-9, 120e-9, 6e-9),
                 None, (4e-9, 1e-9, 1e-9)],
                [(0, 100e-9, 0), (150e-9, 104e-9, 1e-9),
                 None, (2e-9, 1.5e-9, 0.1e-9)],
                [(10e9, 10e3, 0), (11e9, 11e3, 5),
                 None, (1e9, 1e3, 1.5)]]

        for p1, p2, n, cell in args:
            with pytest.raises(ValueError) as excinfo:
                mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell)
            assert 'not an aggregate' in str(excinfo.value)

    def test_cell_greater_than_domain(self):
        p1 = (0, 0, 0)
        p2 = (1e-9, 1e-9, 1e-9)
        args = [(2e-9, 1e-9, 1e-9), (1e-9, 2e-9, 1e-9),
                (1e-9, 1e-9, 2e-9), (1e-9, 5e-9, 0.1e-9)]
        for cell in args:
            with pytest.raises(ValueError) as excinfo:
                mymesh = df.Mesh(p1=p1, p2=p2, cell=cell)
            assert 'not an aggregate' in str(excinfo.value)

    def test_regions(self):
        p1 = (0, 0, 0)
        p2 = (100, 50, 10)
        cell = (1, 1, 1)
        regions = {'r1': df.Region(p1=(0, 0, 0), p2=(50, 50, 10)),
                   'r2': df.Region(p1=(50, 0, 0), p2=(100, 50, 10))}
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell, regions=regions)

        assert (0, 0, 0) in mesh.regions['r1']
        assert (0, 0, 0) not in mesh.regions['r2']
        assert (25, 25, 5) in mesh.regions['r1']
        assert (25, 25, 5) not in mesh.regions['r2']
        assert (51, 10, 10) in mesh.regions['r2']
        assert (51, 10, 10) not in mesh.regions['r1']
        assert (100, 50, 10) in mesh.regions['r2']
        assert (100, 50, 10) not in mesh.regions['r1']

    def test_centre(self):
        p1 = (0, 0, 0)
        p2 = (100, 100, 100)
        cell = (1, 1, 1)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)
        assert mesh.centre == (50, 50, 50)
        assert mesh.centre in mesh

        p1 = (-18.5, 10, 0)
        p2 = (10, 5, 10)
        cell = (0.1, 0.25, 2)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)
        assert mesh.centre == (-4.25, 7.5, 5)
        assert mesh.centre in mesh

        p1 = (500e-9, 125e-9, 3e-9)
        p2 = (0, 0, 0)
        n = (250, 125, 3)
        mesh = df.Mesh(p1=p1, p2=p2, n=n)
        assert mesh.centre == (250e-9, 62.5e-9, 1.5e-9)
        assert mesh.centre in mesh

    def test_random_point(self):
        p1 = (-18.5, 5, 0)
        p2 = (10, -10, 10e-9)
        n = (100, 5, 10)
        mesh = df.Mesh(p1=p1, p2=p2, n=n)

        for _ in range(50):
            random_point = mesh.random_point()
            assert isinstance(random_point, tuple)
            assert random_point in mesh

    def test_repr_no_pbc(self):
        p1 = (-1, -4, 11)
        p2 = (15, 10.1, 12.5)
        cell = (1, 0.1, 0.5)
        name = 'meshname'
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell, name=name)

        rstr = ('Mesh(p1=(-1, -4, 11), p2=(15, 10.1, 12.5), '
                'cell=(1, 0.1, 0.5), pbc=set(), name=\'meshname\')')
        assert repr(mesh) == rstr

    def test_repr_pbc(self):
        p1 = (-1, -4, 11)
        p2 = (15, 10, 12.5)
        n = (16, 140, 3)
        pbc = 'x'
        name = 'meshname'
        mesh = df.Mesh(p1=p1, p2=p2, n=n, pbc=pbc, name=name)

        rstr = ('Mesh(p1=(-1, -4, 11), p2=(15, 10, 12.5), '
                'cell=(1.0, 0.1, 0.5), pbc={\'x\'}, name=\'meshname\')')
        assert repr(mesh) == rstr

    def test_index2point(self):
        p1 = (15, -4, 12.5)
        p2 = (-1, 10.1, 11)
        cell = (1, 0.1, 0.5)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)

        assert mesh.index2point((5, 10, 1)) == (4.5, -2.95, 11.75)

        # Correct minimum index
        assert isinstance(mesh.index2point((0, 0, 0)), tuple)
        assert mesh.index2point((0, 0, 0)) == (-0.5, -3.95, 11.25)
        # Below minimum index
        with pytest.raises(ValueError):
            mesh.index2point((-1, 0, 0))
        with pytest.raises(ValueError):
            mesh.index2point((0, -1, 0))
        with pytest.raises(ValueError):
            mesh.index2point((0, 0, -1))

        # Correct maximum index
        assert isinstance(mesh.index2point((15, 140, 2)), tuple)
        assert mesh.index2point((15, 140, 2)) == (14.5, 10.05, 12.25)
        # Above maximum index
        with pytest.raises(ValueError):
            mesh.index2point((16, 0, 0))
        with pytest.raises(ValueError):
            mesh.index2point((0, 141, 0))
        with pytest.raises(ValueError):
            mesh.index2point((0, 0, 3))

    def test_point2index(self):
        p1 = (-10e-9, -5e-9, 10e-9)
        p2 = (10e-9, 5e-9, 0)
        cell = (1e-9, 5e-9, 1e-9)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)

        # (0, 0, 0) cell
        assert mesh.point2index((-10e-9, -5e-9, 0)) == (0, 0, 0)
        assert mesh.point2index((-9.5e-9, -2.5e-9, 0.5e-9)) == (0, 0, 0)
        assert mesh.point2index((-9.01e-9, -0.1e-9, 0.9e-9)) == (0, 0, 0)

        # (19, 1, 9) cell
        assert mesh.point2index((10e-9, 5e-9, 10e-9)) == (19, 1, 9)
        assert mesh.point2index((9.5e-9, 2.5e-9, 9.5e-9)) == (19, 1, 9)
        assert mesh.point2index((9.1e-9, 0.1e-9, 9.1e-9)) == (19, 1, 9)

        # vicinity of (0, 0, 0) point
        assert mesh.point2index((1e-16, 1e-16, 0.99e-16)) == (10, 1, 0)
        assert mesh.point2index((-1e-16, -1e-16, 0.01e-16)) == (9, 0, 0)

        p1 = (-10, 5, 0)
        p2 = (10, -5, 10e-9)
        n = (10, 5, 5)
        mesh = df.Mesh(p1=p1, p2=p2, n=n)

        tol = 1e-12  # picometer tolerance
        with pytest.raises(ValueError):
            mesh.point2index((-10-tol, 0, 5))
        with pytest.raises(ValueError):
            mesh.point2index((-5, -5-tol, 5))
        with pytest.raises(ValueError):
            mesh.point2index((-5, 0, -tol))
        with pytest.raises(ValueError):
            mesh.point2index((10+tol, 0, 5))
        with pytest.raises(ValueError):
            mesh.point2index((6, 5+tol, 5))
        with pytest.raises(ValueError):
            mesh.point2index((0, 0, 10e-9+tol))

    def test_index2point_point2index_mutually_inverse(self):
        p1 = (15, -4, 12.5)
        p2 = (-1, 10.1, 11)
        cell = (1, 0.1, 0.5)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)

        for p in [(-0.5, -3.95, 11.25), (14.5, 10.05, 12.25)]:
            assert isinstance(mesh.index2point(mesh.point2index(p)), tuple)
            assert mesh.index2point(mesh.point2index(p)) == p

        for i in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
            assert isinstance(mesh.point2index(mesh.index2point(i)), tuple)
            assert mesh.point2index(mesh.index2point(i)) == i

    def test_indices(self):
        p1 = (0, 0, 0)
        p2 = (10, 10, 10)
        n = (10, 10, 10)
        mesh = df.Mesh(p1=p1, p2=p2, n=n)

        assert len(list(mesh.indices)) == 1000
        for index in mesh.indices:
            assert isinstance(index, tuple)
            assert len(index) == 3
            assert all([0 <= i <= 9 for i in index])

    def test_coordinates(self):
        p1 = (0, 0, 0)
        p2 = (10, 10, 10)
        cell = (1, 1, 1)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)

        assert len(list(mesh.coordinates)) == 1000
        for coord in mesh.coordinates:
            assert isinstance(coord, tuple)
            assert len(coord) == 3
            assert all([0.5 <= i <= 9.5 for i in coord])

    def test_iter(self):
        p1 = (1, 1, 1)
        p2 = (5, 5, 5)
        n = (2, 2, 2)
        mesh = df.Mesh(p1=p1, p2=p2, n=n)

        assert len(list(mesh)) == 8
        for coord in mesh:
            assert isinstance(coord, tuple)
            assert len(coord) == 3
            assert all([1.5 <= i <= 4.5 for i in coord])

    def test_contains(self):
        p1 = (0, 0, 0)
        p2 = (10e-9, 10e-9, 20e-9)
        cell = (1e-9, 1e-9, 1e-9)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)

        assert (0, 0, 0) in mesh
        assert (10e-9, 10e-9, 10e-9) in mesh
        assert (5e-9, 5e-9, 5e-9) in mesh
        assert (11e-9, 11e-9, 11e-9) not in mesh
        assert (-1e-9, -1e-9, -1e-9) not in mesh

    def test_line(self):
        p1 = (0, 0, 0)
        p2 = (10, 10, 10)
        cell = (1, 1, 1)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)

        tol = 1e-12
        line = mesh.line((0, 0, 0), (10, 10, 10), n=10)
        assert isinstance(line, types.GeneratorType)
        assert len(list(line))
        for point in line:
            assert isinstance(point, tuple)
            assert len(point) == 3
            assert all([0 <= i <= 10 for i in point])

        line = list(mesh.line((0, 0, 0), (10, 0, 0), n=30))
        assert len(line) == 30
        assert line[0] == (0, 0, 0)
        assert line[-1] == (10, 0, 0)

        with pytest.raises(ValueError):
            line = list(mesh.line((-1e-9, 0, 0), (10, 0, 0), n=30))

        with pytest.raises(ValueError):
            line = list(mesh.line((0, 0, 0), (11, 0, 0), n=30))

    def test_plane(self):
        p1 = (0, 0, 0)
        p2 = (10, 10, 10)
        cell = (1, 1, 1)
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell)

        plane = mesh.plane(z=3, n=(2, 2))
        assert isinstance(plane, df.Mesh)
        assert len(list(plane)) == 4
        for point in plane:
            assert isinstance(point, tuple)
            assert len(point) == 3
            assert point[2] == 3

        plane = mesh.plane(y=9.2, n=(3, 2))
        assert isinstance(plane, df.Mesh)
        assert len(list(plane)) == 6
        for point in plane:
            assert isinstance(point, tuple)
            assert len(point) == 3
            assert point[1] == 9.2

        plane = mesh.plane('x')
        assert isinstance(plane, df.Mesh)
        assert len(list(plane)) == 100
        for point in plane:
            assert isinstance(point, tuple)
            assert len(point) == 3
            assert point[0] == 5

        with pytest.raises(ValueError):
            plane = list(mesh.plane(x=-1))

        with pytest.raises(ValueError):
            plane = list(mesh.plane(y=11))

        with pytest.raises(ValueError):
            plane = list(mesh.plane(z=-1e-9))

        with pytest.raises(ValueError):
            plane = list(mesh.plane('x', z=-1e-9))

        with pytest.raises(ValueError):
            plane = list(mesh.plane('z', z=-1e-9))

    def test_mpl(self):
        for p1, p2, n, cell in self.valid_args:
            mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell)
            mesh.mpl()

    def test_k3d(self):
        for p1, p2, n, cell in self.valid_args:
            mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell)
            mesh.k3d()

    def test_k3d_points(self):
        for p1, p2, n, cell in self.valid_args:
            mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell)
            mesh.k3d_points()

    def test_k3d_regions(self):
        p1 = (0, 0, 0)
        p2 = (100, 80, 10)
        cell = (2, 2, 2)
        regions = {'r1': df.Region(p1=(0, 0, 0), p2=(100, 10, 10)),
                   'r2': df.Region(p1=(0, 10, 0), p2=(100, 20, 10)),
                   'r3': df.Region(p1=(0, 20, 0), p2=(100, 30, 10)),
                   'r4': df.Region(p1=(0, 30, 0), p2=(100, 40, 10)),
                   'r5': df.Region(p1=(0, 40, 0), p2=(100, 50, 10)),
                   'r6': df.Region(p1=(0, 50, 0), p2=(100, 60, 10)),
                   'r7': df.Region(p1=(0, 60, 0), p2=(100, 70, 10)),
                   'r8': df.Region(p1=(0, 70, 0), p2=(100, 80, 10))}
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell, regions=regions)
        mesh.k3d_regions()

    def test_script(self):
        for p1, p2, n, cell in self.valid_args:
            mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell)
            with pytest.raises(NotImplementedError):
                script = mesh._script

    def test_change_const_attributes(self):
        p1 = (0, 0, 0)
        p2 = (10, 10, 10)
        cell = (1, 1, 1)
        name = 'object_name'
        mesh = df.Mesh(p1=p1, p2=p2, cell=cell, name=name)

        assert mesh.p1 == p1
        assert mesh.p2 == p2
        assert mesh.cell == cell
        assert mesh.name == name
        assert mesh.l == (10, 10, 10)
        assert mesh.n == (10, 10, 10)

        # Attempt to change attribute
        with pytest.raises(AttributeError):
            mesh.p2 = (15, 15, 15)
        with pytest.raises(AttributeError):
            mesh.name = 'new_object_name'

        # Make sure the attributes have not changed.
        assert mesh.p1 == p1
        assert mesh.p2 == p2
        assert mesh.cell == cell
        assert mesh.name == name
        assert mesh.l == (10, 10, 10)
        assert mesh.n == (10, 10, 10)

max_stars_repo_name: minrk/discretisedfield

max_issues_repo_name: minrk/discretisedfield

max_forks_repo_name: minrk/discretisedfield


text:
'''
    GGNN utils
'''
import numpy as np

def compact2sparse_representation(mat, total_edge_type):
    '''
    '''
    N, _ = mat.shape

    sparse_mat = np.zeros((N, N * total_edge_type * 2))

    for i in range(N):
        for j in range(N):
            if mat[i, j] == 0: continue

            edge_type = mat[i, j]
            _from = i
            _to   = j

            in_x = j
            in_y = i + N * (edge_type - 1)
            sparse_mat[int(in_x), int(in_y)] = 1

            # fill out
            out_x = i
            out_y = total_edge_type + j + N * (edge_type - 1)
            sparse_mat[int(out_x), int(out_y)] = 1

    return sparse_mat.astype('int')

def manual_parser(env):

    def _hopper():
        graph = np.array([
                [0, 1, 0, 0],
                [1, 0, 1, 0],
                [0, 1, 0, 1],
                [0, 0, 1, 0]
            ])

        geom_meta_info = np.array([
                [0.9,     0, 0, 1.45,    0,  0, 1.05, 0.05],
                [0.9,     0, 0, 1.05,    0,  0,  0.6, 0.05],
                [0.9,     0, 0,  0.6,    0,  0,  0.1, 0.04],
                [2.0, -0.14, 0,  0.1, 0.26,  0,  0.1, 0.06]
            ])

        joint_meta_info = np.array([
                [0, 0,    0,    0,  0,   0],
                [0, 0, 1.05, -150,  0, 200],
                [0, 0,  0.6, -150,  0, 200],
                [0, 0,  0.1,  -45, 45, 200]
            ])

        meta_info = np.hstack( (geom_meta_info, joint_meta_info) )

        ob_assign = np.array([
                0, 0, 1, 2, 3,
                0, 0, 0, 1, 2, 3
            ])

        ac_assign = np.array([
                1, 2, 3
            ])

        ggnn_info = {}
        ggnn_info['n_node'] = graph.shape[0]
        ggnn_info['n_node_type'] = 1

        ggnn_info['node_anno_dim'] = meta_info.shape[1]
        ggnn_info['node_state_dim'] = 6 # observation space
        ggnn_info['node_embed_dim'] = 512 # ggnn hidden states dim - node_state_dim

        ggnn_info['n_edge_type'] = graph.max()
        ggnn_info['output_dim'] = 5 # the number of occurrences in ob_assign

        return graph, ob_assign, ac_assign, meta_info, ggnn_info

    def _half_cheetah():
        raise NotImplementedError

    def _walker():
        raise NotImplementedError

    def _ant():
        raise NotImplementedError

    if env == 'gym_hopper': return _hopper()
    else:
        raise NotImplementedError

ext: py

reason: import numpy


text:
import math
from collections import Counter
from typing import Dict, List, Set, Tuple, Union

import numpy as np
from textacy.extract.basics import ngrams
from scipy.stats.mstats import gmean
from spacy.tokens import Doc, Span, SpanGroup, Token

FrequencyDist = Dict[str, float]
SpanBoundaries = Dict[str, List[str]]

def weighted_average(
    span_metrics: Dict[str, Dict[str, float]],
    frequencies: Dict[str, Counter],
) -> Dict[str, float]:
    weighted_average = {}
    for spans_key, span_type_dict in span_metrics.items():
        weighted_average[spans_key] = np.average(
            list(span_type_dict.values()),
            weights=list(frequencies[spans_key].values()),
        )

    return weighted_average

class SpanAnalyzer:
    def __init__(self, docs: List[Doc], window_size: int = 1):
        self.docs = docs
        self.p_corpus = self._get_distribution(self.docs, normalize=True)
        self.keys = self._get_all_keys()
        self.window_size = window_size

    @property
    def frequency(self) -> Dict[str, Counter]:
        """Number of spans for a span type in the dataset's training corpus.

        Frequency tends to be positively correlated with performance. However,
        architectural choices (like the use of transfer learning) can reduce
        data requirements for ML models. For such architectures, there is a
        small correlation between frequency and performance.
        """
        frequency = {}
        for doc in self.docs:
            for spans_key in list(doc.spans.keys()):
                if spans_key not in frequency:
                    frequency[spans_key] = Counter()
                for span in doc.spans[spans_key]:
                    if span.label_ is None:
                        continue
                    else:
                        frequency[spans_key][span.label_] += 1
        return frequency

    @property
    def length(self) -> Dict[str, Dict[str, float]]:
        """Geometric mean of the spans' lengths in tokens.

        Traditional CRF models tend to perform poorly at the identification of
        long spans due to their strict Markov assumption. Architectures that
        rely on such assumptions should follow the same pattern. On the other
        hand, LSTMs or Transformers should do better on long spans.
        """
        _length = {}
        for doc in self.docs:
            for spans_key in list(doc.spans.keys()):
                if spans_key not in _length:
                    _length[spans_key] = {}
                for span in doc.spans[spans_key]:
                    if span.label_ is None:
                        continue
                    elif span.label_ not in _length[spans_key]:
                        _length[spans_key][span.label_] = []
                    else:
                        _length[spans_key][span.label_].append(len(span))

        # Compute for the geometric mean for each span type
        length = {}
        for spans_key, span_type_dict in _length.items():
            length[spans_key] = {}
            for span_type, lengths in span_type_dict.items():
                length[spans_key][span_type] = gmean(lengths)

        return length

    @property
    def span_distinctiveness(self) -> Dict[str, float]:
        """Distinctiveness of the span compared to the corpus.

        Measures how distinct the text comprising the spans compared to the
        rest of the corpus. It is defined as the KL divergence D(P_span || P),
        where P is the unigram word distribution of the corpus, and P_span as
        the unigram distribution of tokens within the span.

        High values indicate that different words are used inside spans
        compared to the rest of the text, whereas low values indicate that the
        word distribution is similar inside and outside of spans.

        This property is positively correlated with model performance. Spans
        with high distinctiveness should be able to rely more heavily on local
        features, as each token carries information about span membership. Low
        span distrinctivess then calls for sequence information.
        """
        p_spans: Dict[str, Dict[str, FrequencyDist]] = {}
        for key in self.keys:
            p_spans[key] = {}
            spans_per_type = self._get_all_spans_in_key(key)
            for span_type, spans in spans_per_type.items():
                p_span = self._get_distribution(spans, normalize=True)
                p_spans[key][span_type] = p_span

        # Compute value for each spans_key
        span_distincts: Dict[str, Dict[str, float]] = {}
        for spans_key, freq_per_type in p_spans.items():
            span_distincts[spans_key] = {}
            for span_type, frequency_dist in freq_per_type.items():
                span_distincts[spans_key][span_type] = self._get_kl_divergence(
                    frequency_dist, self.p_corpus
                )
        return span_distincts

    @property
    def boundary_distinctiveness(self):
        """Distinctiveness of the boundaries compared to the corpus.

        Measures how distinctive the starts and ends of spans are. It is
        formalized as the KL-divergence D(P_bounds || P) where P is the unigram
        word distribution of the corpus, and P_bounds as the unigram
        distribution of the boundary tokens.

        This property is positively correlated with model performance. High
        values mean that the start and end points of spans are easy to spot,
        while low values indicate smooth transitions.
        """
        p_bounds: Dict[str, Dict[str, FrequencyDist]] = {}
        for key in self.keys:
            p_bounds[key] = {}
            span_boundaries_per_type = self._get_all_boundaries_in_key(
                key, window_size=self.window_size
            )
            for span_type, span_bounds in span_boundaries_per_type.items():
                bounds = span_bounds["start"] + span_bounds["end"]
                p_bound = self._get_distribution(bounds, normalize=True)
                p_bounds[key][span_type] = p_bound

        # Compute value for each spans_key
        bound_distincts: Dict[str, Dict[str, float]] = {}
        for spans_key, freq_per_type in p_bounds.items():
            bound_distincts[spans_key] = {}
            for span_type, frequency_dist in freq_per_type.items():
                bound_distincts[spans_key][span_type] = self._get_kl_divergence(
                    frequency_dist, self.p_corpus
                )

        return bound_distincts

    def _get_all_keys(self) -> Set[str]:
        """Get all spans_key in the corpus"""
        return set([key for doc in self.docs for key in list(doc.spans.keys())])

    def _get_all_spans_in_key(self, spans_key: str) -> Dict[str, List[Span]]:
        """Get all spans given a specified spans_key

        Returns a dictionary where the keys are the span types and
        the values are the list of Spans within that span type.
        """
        spans: Dict[str, List[Span]] = {}
        for doc in self.docs:
            if doc.spans.get(spans_key):
                for span in doc.spans[spans_key]:
                    if span.label_ not in spans:
                        spans[span.label_] = []
                    else:
                        spans[span.label_].append(span)

        return spans

    def _get_all_boundaries_in_key(
        self, spans_key: str, window_size: int = 1
    ) -> Dict[str, SpanBoundaries]:
        """Get the boundary tokens for all spans in a spans_key

        Returns a dictionary where the keys are the span types and
        the values are a tuple of lists for the start and end span
        boundaries.
        """
        bounds: Dict[str, SpanBoundaries] = {}
        for doc in self.docs:
            if doc.spans.get(spans_key):
                for span in doc.spans[spans_key]:
                    if span.label_ not in bounds:
                        bounds[span.label_] = {"start": [], "end": []}
                    else:
                        # Get span boundaries within a window
                        # We append Spans instead of Tokens so that it's consistent
                        # with the _get_distribution() method
                        for offset in range(window_size):
                            span_bound_start_idx = span.start - (offset + 1)
                            if span_bound_start_idx >= 0:
                                bounds[span.label_]["start"].append(
                                    doc[span_bound_start_idx : span_bound_start_idx + 1]
                                )

                            span_bound_end_idx = span.end + (offset + 1)
                            if span_bound_end_idx < len(doc):
                                bounds[span.label_]["end"].append(
                                    doc[span_bound_end_idx : span_bound_end_idx + 1]
                                )
        return bounds

    def _get_distribution(
        self,
        texts: Union[List[Doc], List[SpanGroup]],
        normalize: bool = True,
    ) -> Counter:
        """Get each word's sample frequency given a list of text"""
        word_counts = Counter()
        for text in texts:
            for token in text:
                word_counts[self._normalize_text(token.text)] += 1

        if normalize:
            total = sum(word_counts.values(), 0.0)
            word_counts = Counter({k: v / total for k, v in word_counts.items()})

        return word_counts

    def _get_kl_divergence(self, p: Counter, q: Counter) -> float:
        """Compute the Kullback-Leibler divergence

        The parameters p and q are taken as unigram word distributions
        """
        total = 0
        for word, p_word in p.items():
            total += p_word * math.log(p_word / q[word])
        return total

    def _normalize_text(self, text: str) -> str:
        # The source code from the paper converts all digits into a single
        # value, '0'. Perhaps this is a way to "normalize" all numerical value
        # I'm not sure if this is generalizable so I'll comment it out for the meantime.

        # pattern = re.compile("[0-9]")
        # text = re.sub(pattern, "0", text)
        text = text.lower().replace("``", '"').replace("''", '"')
        return text

reason: import numpy,from scipy


text:
import hydra
import hydra.utils as utils

import json
from pathlib import Path
import torch
import numpy as np
import librosa
from tqdm import tqdm
import pyloudnorm

from preprocess import preemphasis
from model import Encoder, Vocoder
from config import load_conf, ConfGlobal

def convert(cfg: ConfGlobal):
    dataset_path = Path(utils.to_absolute_path("datasets")) / cfg.dataset.path
    with open(dataset_path / "speakers.json") as file:
        speakers = sorted(json.load(file))

    synthesis_list_path = Path(utils.to_absolute_path(cfg.synthesis_list))
    with open(synthesis_list_path) as file:
        synthesis_list = json.load(file)

    in_dir = Path(utils.to_absolute_path(cfg.in_dir))
    out_dir = Path(utils.to_absolute_path(cfg.out_dir))
    out_dir.mkdir(exist_ok=True, parents=True)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    encoder = Encoder(**cfg.model.encoder)
    vocoder = Vocoder(**cfg.model.vocoder)
    encoder.to(device)
    vocoder.to(device)

    print("Load checkpoint from: {}:".format(cfg.cpc_checkpoint))
    checkpoint_path = utils.to_absolute_path(cfg.cpc_checkpoint)
    checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
    encoder.load_state_dict(checkpoint["encoder"])

    print("Load checkpoint from: {}:".format(cfg.vocoder_checkpoint))
    checkpoint_path = utils.to_absolute_path(cfg.vocoder_checkpoint)
    checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
    vocoder.load_state_dict(checkpoint["vocoder"])

    encoder.eval()
    vocoder.eval()

    meter = pyloudnorm.Meter(cfg.preprocessing.sr)

    for wav_path, speaker_id, out_filename in tqdm(synthesis_list):
        wav_path = in_dir / wav_path
        wav, _ = librosa.load(
            wav_path.with_suffix(".wav"),
            sr=cfg.preprocessing.sr)
        ref_loudness = meter.integrated_loudness(wav)
        wav = wav / np.abs(wav).max() * 0.999

        mel = librosa.feature.melspectrogram(
            preemphasis(wav, cfg.preprocessing.preemph),
            sr=cfg.preprocessing.sr,
            n_fft=cfg.preprocessing.n_fft,
            n_mels=cfg.preprocessing.n_mels,
            hop_length=cfg.preprocessing.hop_length,
            win_length=cfg.preprocessing.win_length,
            fmin=cfg.preprocessing.fmin,
            power=1)
        logmel = librosa.amplitude_to_db(mel, top_db=cfg.preprocessing.top_db)
        logmel = logmel / cfg.preprocessing.top_db + 1

        mel = torch.FloatTensor(logmel).unsqueeze(0).to(device)
        speaker = torch.LongTensor([speakers.index(speaker_id)]).to(device)

        with torch.no_grad():
            _, _, indices = encoder.encode(mel)
            output = vocoder.generate(indices, speaker)

        output_loudness = meter.integrated_loudness(output)
        output = pyloudnorm.normalize.loudness(output, output_loudness, ref_loudness)
        path = out_dir / out_filename
        # [todo]: .write_wav was deprecated.
        librosa.output.write_wav(path.with_suffix(".wav"), output.astype(np.float32), sr=cfg.preprocessing.sr)

if __name__ == "__main__":
    conf = load_conf()
    convert(conf)

max_stars_repo_name: tarepan/VectorQuantizedCPC

max_issues_repo_name: tarepan/VectorQuantizedCPC

max_forks_repo_name: tarepan/VectorQuantizedCPC


text:
import numpy as np
import inferno.utils.python_utils as pyu
from inferno.io.transform import Transform
from ..criteria.multi_scale_loss import Downsampler
from .segmentation import DtypeMapping

try:
    from affogato.affinities import compute_multiscale_affinities, compute_affinities
except ImportError:
    compute_affinities, compute_multiscale_affinities = None, None

# TODO add more options (membrane prediction)
# helper function that returns affinity transformation from config
def affinity_config_to_transform(**affinity_config):
    assert ('offsets' in affinity_config) != ('block_shapes' in affinity_config), \
        "Need either 'offsets' or 'block_shapes' parameter in config"

    if 'offsets' in affinity_config:
        # whether to calculating affinities on 2D or 3D
        if len(affinity_config['offsets'][0]) == 2:
            return Segmentation2Affinities2D(**affinity_config)
        else:
            return Segmentation2Affinities(**affinity_config)
    else:
        return Segmentation2MultiscaleAffinities(**affinity_config)

class Segmentation2Affinities2or3D(Transform, DtypeMapping):
    def __init__(self, offsets, dtype='float32',
                 retain_mask=False, ignore_label=None,
                 retain_segmentation=False, segmentation_to_binary=False,
                 map_to_foreground=True, learn_ignore_transitions=False,
                 **super_kwargs):
        assert compute_affinities is not None,\
            "Couldn't find 'affogato' module, affinity calculation is not available"
        assert pyu.is_listlike(offsets), "`offsets` must be a list or a tuple."
        super(Segmentation2Affinities2or3D, self).__init__(**super_kwargs)
        self.dim = len(offsets[0])
        assert self.dim in (2, 3), str(self.dim)
        assert all(len(off) == self.dim for off in offsets[1:])
        self.offsets = offsets
        self.dtype = dtype
        self.retain_mask = retain_mask
        self.ignore_label = ignore_label
        self.retain_segmentation = retain_segmentation
        self.segmentation_to_binary = segmentation_to_binary
        assert not (self.retain_segmentation and self.segmentation_to_binary),\
            "Currently not supported"
        self.map_to_foreground = map_to_foreground
        self.learn_ignore_transitions = learn_ignore_transitions

    def to_binary_segmentation(self, tensor):
        assert self.ignore_label != 0, "We assume 0 is background, not ignore label"
        if self.map_to_foreground:
            return (tensor == 0).astype(self.dtype)
        else:
            return (tensor != 0).astype(self.dtype)

    def include_ignore_transitions(self, affs, mask, seg):
        ignore_seg = (seg == self.ignore_label).astype(seg.dtype)
        ignore_transitions, invalid_mask = compute_affinities(ignore_seg, self.offsets)
        invalid_mask = np.logical_not(invalid_mask)
        # NOTE affinity convention returned by affogato:
        # transitions are marked by 0
        ignore_transitions = ignore_transitions == 0
        ignore_transitions[invalid_mask] = 0
        affs[ignore_transitions] = 0
        mask[ignore_transitions] = 1
        return affs, mask

    def input_function(self, tensor):
        # print("affs: in shape", tensor.shape)
        if self.ignore_label is not None:
            # output.shape = (C, Z, Y, X)
            output, mask = compute_affinities(tensor, self.offsets,
                                              ignore_label=self.ignore_label,
                                              have_ignore_label=True)
            if self.learn_ignore_transitions:
                output, mask = self.include_ignore_transitions(output, mask, tensor)
        else:
            output, mask = compute_affinities(tensor, self.offsets)

        # FIXME what does this do, need to refactor !
        # hack for platyneris data
        platy_hack = False
        if platy_hack:
            chan_mask = mask[1].astype('bool')
            output[0][chan_mask] = np.min(output[:2], axis=0)[chan_mask]

            chan_mask = mask[2].astype('bool')
            output[0][chan_mask] = np.minimum(output[0], output[2])[chan_mask]

        # Cast to be sure
        if not output.dtype == self.dtype:
            output = output.astype(self.dtype)
        #
        # print("affs: shape before binary", output.shape)
        if self.segmentation_to_binary:
            output = np.concatenate((self.to_binary_segmentation(tensor)[None],
                                     output), axis=0)
        # print("affs: shape after binary", output.shape)

        # print("affs: shape before mask", output.shape)
        # We might want to carry the mask along.
        # If this is the case, we insert it after the targets.
        if self.retain_mask:
            mask = mask.astype(self.dtype, copy=False)
            if self.segmentation_to_binary:
                if self.ignore_label is None:
                    additional_mask = np.ones((1,) + tensor.shape, dtype=self.dtype)
                else:
                    additional_mask = (tensor[None] != self.ignore_label).astype(self.dtype)
                mask = np.concatenate([additional_mask, mask], axis=0)
            output = np.concatenate((output, mask), axis=0)
        # print("affs: shape after mask", output.shape)

        # We might want to carry the segmentation along for validation.
        # If this is the case, we insert it before the targets.
        if self.retain_segmentation:
            # Add a channel axis to tensor to make it (C, Z, Y, X) before cating to output
            output = np.concatenate((tensor[None].astype(self.dtype, copy=False), output),
                                    axis=0)

        # print("affs: out shape", output.shape)
        return output

class Segmentation2Affinities(Segmentation2Affinities2or3D):
    def __init__(self, **super_kwargs):
        super(Segmentation2Affinities, self).__init__(**super_kwargs)

    def volume_function(self, tensor):
        assert tensor.ndim == 3
        output = self.input_function(tensor)
        return output

class Segmentation2Affinities2D(Segmentation2Affinities2or3D):
    def __init__(self, **super_kwargs):
        super(Segmentation2Affinities2D, self).__init__(**super_kwargs)

    def image_function(self, tensor):
        assert tensor.ndim == 2
        output = self.input_function(tensor)
        return output

class Segmentation2MultiscaleAffinities(Transform, DtypeMapping):
    def __init__(self, block_shapes, dtype='float32', ignore_label=None,
                 retain_mask=False, retain_segmentation=False,
                 original_scale_offsets=None, **super_kwargs):
        super(Segmentation2MultiscaleAffinities, self).__init__(**super_kwargs)
        assert compute_multiscale_affinities is not None,\
            "Couldn't find 'affogato' module, affinity calculation is not available"
        assert pyu.is_listlike(block_shapes)
        self.block_shapes = block_shapes
        self.dim = len(block_shapes[0])
        assert self.dim in (2, 3), str(self.dim)
        assert all(len(bs) == self.dim for bs in block_shapes[1:])

        self.dtype = dtype
        self.ignore_label = ignore_label
        self.retain_mask = retain_mask
        self.retain_segmentation = retain_segmentation
        self.original_scale_offsets = original_scale_offsets
        if self.retain_segmentation:
            self.downsamplers = [Downsampler(bs) for bs in self.block_shapes]

    def tensor_function(self, tensor):
        # for 2 d input, we need singleton input
        if self.dim == 2:
            assert tensor.shape[0] == 1
            tensor = tensor[0]

        outputs = []
        for ii, bs in enumerate(self.block_shapes):
            # if the block shape is all ones, we can compute normal affinities
            # with nearest neighbor offsets. This should yield the same result,
            # but should be more efficient.
            original_scale = all(s == 1 for s in bs)
            if original_scale:
                if self.original_scale_offsets is None:
                    offsets = [[0 if i != d else -1 for i in range(self.dim)]
                               for d in range(self.dim)]
                else:
                    offsets = self.original_scale_offsets
                output, mask = compute_affinities(tensor.squeeze().astype('uint64'), offsets,
                                                  ignore_label=0 if self.ignore_label is None else self.ignore_label,
                                                  have_ignore_label=False if self.ignore_label is None else True)
            else:
                output, mask = compute_multiscale_affinities(tensor.squeeze().astype('uint64'), bs,
                                                             ignore_label=0 if self.ignore_label is None
                                                             else self.ignore_label,
                                                             have_ignore_label=False if self.ignore_label is None
                                                             else True)

            # Cast to be sure
            if not output.dtype == self.dtype:
                output = output.astype(self.dtype)

            # We might want to carry the mask along.
            # If this is the case, we insert it after the targets.
            if self.retain_mask:
                output = np.concatenate((output, mask.astype(self.dtype, copy=False)), axis=0)
            # We might want to carry the segmentation along for validation.
            # If this is the case, we insert it before the targets for the original scale.
            if self.retain_segmentation:
                ds_target = self.downsamplers[ii](tensor.astype(self.dtype, copy=False))
                if ds_target.ndim != output.ndim:
                    assert ds_target.ndim == output.ndim - 1
                    ds_target = ds_target[None]
                output = np.concatenate((ds_target, output), axis=0)
            outputs.append(output)

        return outputs

max_stars_repo_name: nasimrahaman/neurofire

max_issues_repo_name: nasimrahaman/neurofire

max_forks_repo_name: nasimrahaman/neurofire


text:
import numpy as np
import tensorflow as tf
import edward as ed
import _pickle

# TODO figure out seed
# TODO hyperparam search

def save_graph_parameters(file):
    sess = ed.get_session()
    trained_vars = []
    for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
        trained_vars.append(sess.run(var))
    _pickle.dump(trained_vars, open(file, 'wb'))
    return file

def load_graph_parameters(file):
    sess = ed.get_session()
    trained_vars = _pickle.load(open(file, "rb"))
    i=0
    for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
        sess.run(var.assign(trained_vars[i]))
        i += 1
    return True

def get_nn_weights(nn_layer_dims, mean_W, stddev_W, mean_b, stddev_b):
    """
    :param nn_layer_dims <list int>: dimensions of each layer of nn.
        eg:
            [255, 50, 50, 1]
            means an input layer of dim 255,
                  followed by a hidden layer of dim 50,
                  followed by a hidden layer of dim 50,
                  followed by an output layer of dim 1.
    """
    params = []

    prv_dim = nn_layer_dims[0]
    for layer in range(1, len(nn_layer_dims)):
        cur_dim = nn_layer_dims[layer]

        W = tf.get_variable(initializer=tf.random_normal([prv_dim, cur_dim], mean=mean_W, stddev=stddev_W),
                            name="W{}".format(layer))
        b = tf.get_variable(initializer=tf.random_normal([1      , cur_dim], mean=mean_b, stddev=stddev_b),
                            name="b{}".format(layer))
        params.append(W)
        params.append(b)

        prv_dim = cur_dim

    return params

    ## For reference, if we were to code up a 5 layer NN, the params look like this:
    #
    #    W0 = tf.get_variable(initializer=tf.random_normal([D*2+Dp, nn_units_1]), name="W0")
    #    b0 = tf.get_variable(initializer=tf.random_normal([1     , nn_units_1]), name="b0")
    #    W1 = tf.get_variable(initializer=tf.random_normal([nn_units_1, nn_units_2]), name="W1")
    #    b1 = tf.get_variable(initializer=tf.random_normal([1         , nn_units_2]), name="b1")
    #    W2 = tf.get_variable(initializer=tf.random_normal([nn_units_2, nn_units_3]), name="W2")
    #    b2 = tf.get_variable(initializer=tf.random_normal([1         , nn_units_3]), name="b2")
    #    W3 = tf.get_variable(initializer=tf.random_normal([nn_units_3, 1]), name="W3")
    #    b3 = tf.get_variable(initializer=tf.random_normal([1         , 1]), name="b3")
    #    return W0, b0, W1, b1, W2, b2, W3, b3

def neural_net(x, nn_layer_dims, *args):
    """
    @param x <(None, 2*D+Dp) float>: input to the neural net
    """
    params = get_nn_weights(nn_layer_dims, *args)

    sofar = x
    for i in range(0, len(params), 2):
        W, b = params[i:i+2]
        sofar = tf.matmul(sofar, W) + b
        if i < len(params)-2: # if not last layer
            sofar = tf.nn.sigmoid(sofar)

    return sofar

    ## For reference, if we were to code up a 5 layer NN, the params look like this:
    #
    #    W0, b0, W1, b1, W2, b2, W3, b3 = get_nn_weights()
    #    z0 = tf.nn.sigmoid(tf.matmul(x , W0) + b0)
    #    z1 = tf.nn.sigmoid(tf.matmul(z0, W1) + b1)
    #    z2 = tf.nn.sigmoid(tf.matmul(z1, W2) + b2)
    #    z3 = tf.matmul(z2, W3) + b3
    #    return z3

def neural_net_tensor(x, N_LOOKUP, nn_layer_dims, *args):
    """
    @param x <(None, None, 2*D+Dp) float>: input to the neural net as a tensor.
    TODO Does this create a new graph every time it's ran? If so, change N_LOOKUP to a constant equal to number of items.
    TODO get N_LOOKUP by second dim of x
    TODO If I just use 1 function to handle tensor and non-tensor, what is the impact on speed?
    """
    # Tensor x Matrix multiplication, see:
    # Note: We can use tf.matmul if the pre- dimensions of the tensors are the same. In other words, matmul doesn't broadcast.
    # https://stackoverflow.com/questions/38235555/tensorflow-matmul-of-input-matrix-with-batch-data
    # https://groups.google.com/a/tensorflow.org/forum/#!topic/discuss/4tgsOSxwtkY
    # https://rdipietro.github.io/tensorflow-scan-examples/
    # TODO why doesn't scan work??
    # Error: TensorArray was passed element_shape [?,11] which does not match the Tensor at index 0: [3,7]
    #self.bar = tf.scan(lambda a, x: tf.matmul(x, foo), test_nn_input)

    params = get_nn_weights(nn_layer_dims, *args)

    prv_dim = nn_layer_dims[0]
    sofar = x
    for layer in range(1, len(nn_layer_dims)):
        cur_dim = nn_layer_dims[layer]
        W, b = params[2*(layer-1):2*(layer-1)+2]

        inp = tf.reshape(sofar, [-1, prv_dim])
        sofar = tf.reshape(tf.matmul(inp, W), [-1, N_LOOKUP, cur_dim]) + b

        if layer < len(nn_layer_dims)-1: # if not last layer
            sofar = tf.nn.sigmoid(sofar)

        prv_dim = cur_dim

    return sofar

    ## For reference, if we were to code up a 5 layer NN, the params look like this:

    #    W0, b0, W1, b1, W2, b2, W3, b3 = get_nn_weights()

    #    x_ = tf.reshape(x, [-1, D*2+Dp])
    #    z0 = tf.reshape(tf.matmul(x_, W0), [-1, N_LOOKUP, nn_units_1]) + b0
    #    z0 = tf.nn.sigmoid(z0)

    #    z0_ = tf.reshape(z0, [-1, nn_units_1])
    #    z1 = tf.reshape(tf.matmul(z0_, W1), [-1, N_LOOKUP, nn_units_2]) + b1
    #    z1 = tf.nn.sigmoid(z1)

    #    z1_ = tf.reshape(z1, [-1, nn_units_2])
    #    z2 = tf.reshape(tf.matmul(z1_, W2), [-1, N_LOOKUP, nn_units_3]) + b2
    #    z2 = tf.nn.sigmoid(z2)

    #    z2_ = tf.reshape(z2, [-1, nn_units_3])
    #    z3 = tf.reshape(tf.matmul(z2_, W3), [-1, N_LOOKUP, 1]) + b3

    #    return z3

def create_mat_and_qmat(shape, pZ_prior_stddev=1.):
    """
    :param pZ_prior_stddev: to scale the prior distribution's prior.
    Note we only parameterize p(Z)'s stddev because it acts as a regularizer.
    """
    mat = ed.models.Normal(loc=tf.zeros(shape), scale=pZ_prior_stddev*tf.ones(shape))
    qmat = ed.models.Normal(loc=tf.Variable(tf.random_normal(shape)),
                            scale=tf.nn.softplus(tf.Variable(tf.random_normal(shape))))
    ## For reference, this is what I had before:
    #mat = ed.models.Normal(loc=tf.zeros(shape), scale=tf.ones(shape))
    #qmat = ed.models.Normal(loc=tf.Variable(tf.zeros(shape)),
    #                        scale=tf.Variable(tf.ones(shape)))
    return mat, qmat

def create_optimizer(optimizer, lr_init, lr_decay_steps, lr_decay_rate):
    """
    Ripped from: https://github.com/blei-lab/edward/blob/master/edward/inferences/variational_inference.py
    Also see: https://www.tensorflow.org/api_docs/python/tf/train/exponential_decay

    :return
        optimizer: the optimizer
        global_step: variable used to count training step & decrease the learning rate
    """
    global_step = tf.Variable(0, trainable=False, name="global_step")

    learning_rate = tf.train.exponential_decay(lr_init, global_step, lr_decay_steps, lr_decay_rate, staircase=True)

    if optimizer == 'gradientdescent':
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    elif optimizer == 'adadelta':
        optimizer = tf.train.AdadeltaOptimizer(learning_rate)
    elif optimizer == 'adagrad':
        optimizer = tf.train.AdagradOptimizer(learning_rate)
    elif optimizer == 'momentum':
        optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)
    elif optimizer == 'adam':
        optimizer = tf.train.AdamOptimizer(learning_rate)
    elif optimizer == 'ftrl':
        optimizer = tf.train.FtrlOptimizer(learning_rate)
    elif optimizer == 'rmsprop':
        optimizer = tf.train.RMSPropOptimizer(learning_rate)
    else:
        raise ValueError('Optimizer class not found:', optimizer)

    return optimizer, global_step

class NNMF:
    def __init__(self, ratings_matrix,
                 D=30, Dp=50, pZ_prior_stddev=1., pR_stddev=1.,
                 nn_hidden_layer_dims=None, nn_W_init_mean=0., nn_W_init_stddev=1., nn_b_init_mean=0., nn_b_init_stddev=1.,
                 batch_size=200, n_samples=1,
                 optimizer='adam', lr_init=0.1, lr_decay_steps=100, lr_decay_rate=0.9):
        """
        Computes R = UV' with SVI.

        :param ratings_matrix: The full ratings matrix.
        :param D: Hidden dim size for for U and V.
        :param Dp: Hidden dim size for for Up and Vp.
        :param pZ_prior_stddev: The prior stddev for p(Z). This effectively acts as a regularizer.
        :param pR_stddev: The model's stddev, ie. for p(r_{ij}|U,V,U',V').
        :param nn_hidden_layer_dims <list int>: A list of hidden layer dimensions.
            eg. [50, 25] -> 2 hidden layers with 50 units and 25 units.
        :param nn_W_init_mean: for the neural network weights
        :param nn_W_init_stddev: -
        :param nn_b_init_mean: -
        :param nn_b_init_stddev: -
        :param batch_size: For each itration of SVI, how many samples from ratings matrix to use.
            Bigger batch => more stable gradients.
        :param n_samples: For each iteration of SVI, how many latent samples to draw to estimate the gradient.
            Higher n_samples => more stable gradients. But note it might result in biased gradients.
        :param optimizer <str>: One of:
            'gradientdescent'
            'adadelta'
            'adagrad'
            'momentum'
            'adam'
            'ftrl'
            'rmsprop'
        :param lr_init: Initial learning rate (before decay)
        :param lr_decay_steps: -
        :param lr_decay_rate: -

        Learning rate decay is done with tf.train.exponential_decay:
            decayed_learning_rate = lr_init * lr_decay_rate ^ (global_step / lr_decay_steps)

        Note: It's recommended to run this in a wrapped session. For example:

        ```
        tf.reset_default_graph()
        sess = tf.Session()
        with sess.as_default():
            model = NNMF(...)
            model.train(...)
        sess.close()
        ```

        TODO the following are not parameterized yet, becaus they don't seem important:
        - q: mu, var
        - latent priors: mu
        """

        # Require an optimizer (don't use Edward's default)
        assert optimizer

        # Require at least 1 hidden layer.
        assert nn_hidden_layer_dims is not None
        # Prepend input size, append output size.
        nn_layer_dims = [D*2+Dp] + nn_hidden_layer_dims + [1]

        self.batch_size = batch_size
        self.R_ = ratings_matrix
        N, M = self.R_.shape; self.N = N; self.M = M

        ############
        # TRAINING #
        ############
        # We use r_ph to feed in only the elements in R_ that idx_i and idx_j correspond to.
        self.r_ph  = tf.placeholder(tf.float32, name="batch_r")
        self.idx_i = tf.placeholder(tf.int32, name="idx_i")
        self.idx_j = tf.placeholder(tf.int32, name="idx_j")

        # "Priors" p(Z) and the variational approximating dist q(Z)
        self.U , self.qU  = create_mat_and_qmat([N, D] , pZ_prior_stddev)
        self.V , self.qV  = create_mat_and_qmat([M, D] , pZ_prior_stddev)
        self.Up, self.qUp = create_mat_and_qmat([N, Dp], pZ_prior_stddev)
        self.Vp, self.qVp = create_mat_and_qmat([M, Dp], pZ_prior_stddev)

        # P(X|Z)
        U_selected  = tf.gather(self.U , self.idx_i)
        V_selected  = tf.gather(self.V , self.idx_j)
        Up_selected = tf.gather(self.Up, self.idx_i)
        Vp_selected = tf.gather(self.Vp, self.idx_j)

        nn_input = tf.concat([U_selected, V_selected, tf.multiply(Up_selected, Vp_selected)], axis=1)

        with tf.variable_scope('nn') as nn_scope:
            means = neural_net(nn_input, nn_layer_dims, nn_W_init_mean, nn_W_init_stddev, nn_b_init_mean, nn_b_init_stddev)

        self.R = ed.models.Normal(loc=tf.squeeze(means), scale=pR_stddev*tf.ones(self.batch_size))

        #############
        # Inference #
        #############
        self.inference = ed.KLqp({self.U: self.qU, self.V: self.qV,
                                  self.Up: self.qUp, self.Vp: self.qVp}, data={self.R: self.r_ph})

        _optimizer, global_step = create_optimizer(optimizer, lr_init, lr_decay_steps, lr_decay_rate)

        self.inference.initialize(scale={self.R: N*M/self.batch_size}, n_samples=n_samples,
                                  optimizer=_optimizer, global_step=global_step)

        # Note: global_variables_initializer has to be run after creating inference.
        ed.get_session().run(tf.global_variables_initializer())

        ###########
        # Testing #
        ###########
        self.test_idx_i = tf.placeholder(tf.int32, name="test_idx_i")
        self.test_idx_j = tf.placeholder(tf.int32, name="test_idx_j")
        self.n_test_samples = tf.placeholder(tf.int32, name="n_test_samples")
        N_LOOKUP = tf.size(self.test_idx_i)
        # TODO assert tf.size(self.test_idx_i) == tf.size(self.test_idx_j)

        # after gather => (self.n_test_samples, N_LOOKUP, D or Dp)
        qU_samples  = tf.gather(self.qU.sample(self.n_test_samples) , self.test_idx_i, axis=1)
        qV_samples  = tf.gather(self.qV.sample(self.n_test_samples) , self.test_idx_j, axis=1)
        qUp_samples = tf.gather(self.qUp.sample(self.n_test_samples), self.test_idx_i, axis=1)
        qVp_samples = tf.gather(self.qVp.sample(self.n_test_samples), self.test_idx_j, axis=1)

        # shape (self.n_test_samples, N_LOOKUP, 2*D+Dp)
        test_nn_input = tf.concat([qU_samples, qV_samples, tf.multiply(qUp_samples, qVp_samples)], axis=-1)

        with tf.variable_scope(nn_scope, reuse=True):
            self.sample_rhats = neural_net_tensor(test_nn_input, N_LOOKUP,
                                                  nn_layer_dims, nn_W_init_mean, nn_W_init_stddev, nn_b_init_mean, nn_b_init_stddev)

        ##############
        # Validation #
        ##############
        self.r_map_estimates = tf.reduce_mean(tf.squeeze(self.sample_rhats), axis=0)
        self.mse = tf.reduce_mean(tf.square(self.r_map_estimates - self.r_ph))
        self.loss = self.inference.loss

        ########
        # Misc #
        ########
        self.saver = tf.train.Saver(max_to_keep=0)

    def sample_user_ratings(self, user_index, n_samples=100):
        idx_i = [user_index] * self.M
        idx_j = list(range(self.M))
        feed_dict = {
            self.test_idx_i: idx_i,
            self.test_idx_j: idx_j,
            self.n_test_samples: n_samples
        }
        return np.squeeze(ed.get_session().run(self.sample_rhats, feed_dict))

    def train(self, mask, valid_mask=None, n_iter=1000, verbose=False):
        """
        :param mask: Same size as ratings_matrix.
            A 0 indicates to not use this rating, else use this rating.
            If None given, ratings_matrix will be used as the mask.
        :param valid_mask: Same size as ratings_matrix.
            Used to access when the model has trained to convergence.
        :param n_iter: How many iterations of SVI to run.
        """
        info_dicts = []
        seen_indices = np.array(np.where(mask))

        if valid_mask is not None:
            valid_losses = []
            valid_indices = np.array(np.where(valid_mask))

            valid_mses = []
            valid_mse_idx_i = valid_indices[0, :]
            valid_mse_idx_j = valid_indices[1, :]
            valid_mse_feed_dict = {
                self.test_idx_i: valid_mse_idx_i,
                self.test_idx_j: valid_mse_idx_j,
                self.n_test_samples: 20,
                self.r_ph: self.R_[valid_mse_idx_i, valid_mse_idx_j]
            }

        for ii in range(n_iter):
            # TODO is there a problem training with random batches? Look into lit more.
            # Train on a batch of self.batch_size random elements each iteration.
            rand_idx = np.random.choice(seen_indices.shape[1], self.batch_size, replace=True) # TODO trying it out...
            #rand_idx = np.random.choice(seen_indices.shape[1], self.batch_size, replace=False)
            idx_i = seen_indices[0, rand_idx]
            idx_j = seen_indices[1, rand_idx]
            feed_dict = {
                self.idx_i: idx_i,
                self.idx_j: idx_j,
                self.r_ph: self.R_[idx_i, idx_j]
            }
            info_dict = self.inference.update(feed_dict=feed_dict)
            info_dicts.append(info_dict)

            # TODO print out progress without using edward
            if verbose:
                self.inference.print_progress(info_dict)
                #if ii % 100 == 0:
                #    print(ii, info_dict['loss'])

            if valid_mask is not None:
                # Validation
                # TODO either we use self.mse
                # Or we need to make the batch size bigger
                # Or we need to make the run this multiple times
                valid_idx = np.random.choice(valid_indices.shape[1], self.batch_size, replace=False)

                valid_idx_i = valid_indices[0, valid_idx]
                valid_idx_j = valid_indices[1, valid_idx]
                valid_feed_dict = {
                    self.idx_i: valid_idx_i,
                    self.idx_j: valid_idx_j,
                    self.r_ph: self.R_[valid_idx_i, valid_idx_j]
                }

                valid_loss = ed.get_session().run(self.loss, valid_feed_dict)
                valid_losses.append(valid_loss)

                valid_mse = ed.get_session().run(self.mse, valid_mse_feed_dict)
                valid_mses.append(valid_mse)

        losses = [x['loss'] for x in info_dicts]

        if valid_mask is not None:
            return losses, valid_losses, valid_mses
        else:
            return losses


text:
# --------------
#Importing header files

import pandas as pd
from sklearn.model_selection import train_test_split

# Code starts here
data = pd.read_csv(path)

X = data.drop(columns=['customer.id','paid.back.loan'])

y = data['paid.back.loan']

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.3, random_state = 0)
# Code ends here

# --------------
#Importing header files
import matplotlib.pyplot as plt

# Code starts here
fully_paid = y_train.value_counts()
print(fully_paid)
fully_paid.plot(kind='bar')
# Code ends here

# --------------
#Importing header files
import numpy as np
from sklearn.preprocessing import LabelEncoder

# Code starts here

X_train['int.rate'] = X_train['int.rate'].str.replace('%','')

X_train['int.rate'] = X_train['int.rate'].astype(float)/100

X_test['int.rate'] = X_test['int.rate'].str.replace('%','')

X_test['int.rate'] = X_test['int.rate'].astype(float)/100

num_df = X_train.select_dtypes(include='number')
cat_df = X_train.select_dtypes(include='object')
# Code ends here

# --------------
#Importing header files
import seaborn as sns

# Code starts here
cols = num_df.columns

fig ,axes =  plt.subplots(nrows = 9 , ncols = 1)

for i in range(1,8):
    sns.boxplot(x=y_train, y=num_df[cols[i]], ax=axes[i])
# Code ends here

# --------------
# Code starts here
cols = list(cat_df.columns)

fig,axes = plt.subplots(nrows = 2 , ncols = 2, figsize=(10,20))

for i in range(2):
    for j in range(2):
      sns.countplot(x=X_train[cols[i*2+j]], hue=y_train, ax=axes[i,j])
      fig.tight_layout()

# Code ends here

# --------------
#Importing header files
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelBinarizer

# Code starts here
for i in cols:
    X_train[i].fillna('NA')
    X_test[i].fillna('NA')
    le = LabelEncoder ()
    X_train[i]=le.fit_transform(X_train[i])
    X_test[i]=le.transform(X_test[i])
    x_train = pd.get_dummies(X_train[i])

    x_test = pd.get_dummies(X_test[i])

encoder = LabelBinarizer()
y_train=encoder.fit_transform(y_train)
y_test=encoder.transform(y_test)

model = DecisionTreeClassifier(random_state=0)
model.fit(X_train,y_train)
acc = model.score(X_test,y_test)

print(acc)
# Code ends here

# --------------
#Importing header files
from sklearn.model_selection import GridSearchCV

#Parameter grid
parameter_grid = {'max_depth': np.arange(3,10), 'min_samples_leaf': range(10,50,10)}

# Code starts here
model_2 = DecisionTreeClassifier(random_state=0)

p_tree = GridSearchCV(estimator=model_2, param_grid=parameter_grid, cv=5)

p_tree.fit(X_train,y_train)

acc_2 = p_tree.score(X_test,y_test)

print(acc_2)
# Code ends here

# --------------
#Importing header files

from io import StringIO
from sklearn.tree import export_graphviz
from sklearn import tree
from sklearn import metrics
from IPython.display import Image
import pydotplus

# Code starts here
dot_data = export_graphviz(decision_tree=p_tree.best_estimator_, out_file=None, feature_names=X.columns, filled = True , class_names=['loan_paid_back_yes','loan_paid_back_no'])

graph_big = pydotplus.graph_from_dot_data(dot_data)

# show graph - do not delete/modify the code below this line
img_path = user_data_dir+'/file.png'
graph_big.write_png(img_path)

plt.figure(figsize=(20,15))
plt.imshow(plt.imread(img_path))
plt.axis('off')
plt.show()

# Code ends here

max_stars_repo_name: NishthaShukla/ga-learner-dsmp-repo

max_issues_repo_name: NishthaShukla/ga-learner-dsmp-repo

max_forks_repo_name: NishthaShukla/ga-learner-dsmp-repo


text:
import numpy as np
from SpheroLib.shared_resources import SharedResources

class SpheroLibrary:
    def __init__(self, sphero_config):
        self.config = sphero_config
        self.shared_resources = SharedResources(sphero_config)
        self.shared_resources.get_numpy_resources()

    def get_sphero_states(self):
        rgb_state = np.random.randint(0, 255, self.shared_resources.resources["np_array_rgb"].shape).astype(np.uint8)
        depth_state = np.random.randint(0, 255, self.shared_resources.resources["np_array_depth"].shape).astype(np.uint16)
        audio_state = np.random.uniform(0, 1, self.shared_resources.resources["np_array_audio"].shape)
        rgb_state = rgb_state[::-self.shared_resources.sphero_config["CAMERA_OUTPUT_SKIP"]]
        depth_state = depth_state[::-self.shared_resources.sphero_config["CAMERA_OUTPUT_SKIP"]]
        state = {
            "rgb": rgb_state,
            "depth": depth_state,
            "audio": audio_state}
        if self.shared_resources.sphero_config["SIMULTANEOUS_SPHEROS"] > 0:
            state["spheros"] = np.random.uniform(0, 1, self.shared_resources.resources["np_array_sphero_states"].shape)
        timestamps = self.shared_resources.resources["np_array_timestamps"].copy()
        return state, timestamps

    def set_sphero_action(self, spheroNum, spheroHeading, spheroSpeed):
        return True


text:
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image

st.set_option('deprecation.showfileUploaderEncoding', False)

@st.cache(allow_output_mutation=True)
def load_model():
	model = tf.keras.models.load_model('./flower_model_trained.hdf5')
	return model

def predict_class(image, model):

	image = tf.cast(image, tf.float32)
	image = tf.image.resize(image, [180, 180])

	image = np.expand_dims(image, axis = 0)

	prediction = model.predict(image)

	return prediction

model = load_model()
st.title('Flower Classifier')

file = st.file_uploader("Upload an image of a flower", type=["jpg", "png"])

if file is None:
	st.text('Waiting for upload....')

else:
	slot = st.empty()
	slot.text('Running inference....')

	test_image = Image.open(file)

	st.image(test_image, caption="Input Image", width = 400)

	pred = predict_class(np.asarray(test_image), model)

	class_names = ['daisy', 'dandelion', 'rose', 'sunflower', 'tulip']

	result = class_names[np.argmax(pred)]

	output = 'The image is a ' + result

	slot.text('Done')

	st.success(output)


text:
import robin_stocks as r
import pandas as pd
from datetime import datetime
import numpy as np
pd.set_option('display.max_columns', None)
pd.set_option('display.max_columns', None)

r.login('rh_username','rh_password')
my_stocks = r.build_holdings()

df = pd.DataFrame(my_stocks)
print(df)

df = df.T
df['ticker'] = df.index
df = df.reset_index(drop=True)

cols = df.columns.drop(['id','type','name','pe_ratio','ticker'])
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')

df_buy = df[(df['average_buy_price'] <= 25.000) & (df['quantity'] == 1.000000) & (df['percent_change'] <= -.50)]
df_sell = df[(df['quantity'] == 5.000000) & (df['percent_change'] >= .50)]

print(df_buy)
print(df_sell)

tkr_buy_list  = df_buy['ticker'].tolist()
tkr_sell_list = df_sell['ticker'].tolist()

print(f"{len(r.orders.get_all_open_orders())} open order")

r.orders.cancel_all_open_orders()

if len(tkr_sell_list) > 0:
    for i in tkr_sell_list:
        print(i)
        print(r.orders.order_sell_market(i,4,timeInForce= 'gfd'))
else:
    print('Nothing to sell right now!')

if len(tkr_buy_list) > 0:
    for i in tkr_buy_list:
        test = r.orders.order_buy_market(i,4,timeInForce= 'gfd')
        print(i)
        print(test)
        print(type(test))
else:
    print('Nothing to buy right now!')

r.get_crypto_currency_pairs()

r.stocks.get_earnings('TSLA',info='report')

max_issues_repo_name: happydasch/Finance

max_forks_repo_name: happydasch/Finance


text:
import time
import cv2
import numpy as np
import keyboard
from PIL import ImageGrab
from win32api import GetSystemMetrics

# display screen resolution, get it from your OS settings
Width = GetSystemMetrics(0)
Height = GetSystemMetrics(1)
SCREEN_SIZE = (Width, Height)
# define the codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# create the video write object
out = cv2.VideoWriter("output.avi", fourcc, 24.0, (SCREEN_SIZE))

last = time.time()
while True:
    # make a screenshot
    frame = np.array(ImageGrab.grab() )
    # convert colors from BGR to RGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # write the frame
    out.write(frame)
    # show the
    print("The screen printed at {}".format(time.time()-last))
    #cv2.imshow("screenshot", frame)
    # if the user clicks q, it exits
    last = time.time()
    if keyboard.is_pressed("q"):
        print("You pressed q")
        break
out.release()

max_stars_repo_path: Screen Recorder/screenrecorder.py

max_issues_repo_path: Screen Recorder/screenrecorder.py

max_forks_repo_path: Screen Recorder/screenrecorder.py


text:
#!/usr/bin/env python
## Copyright 2002-2010 by PyMMLib Development Group (see AUTHORS file)
## This code is part of the PyMMLib distribution and governed by
## its license.  Please see the LICENSE file that should have been
## included as part of this package.

import sys
import re
import string
import numpy

from mmLib import Constants, FileIO, Structure, PDB, TLS

def usage():
    print "tlsanl.py <xxx.pdb> [REFMAC TLS File]"
    print
    print "description:"
    print "    Compute anisotropic ADP records from the given TLS"
    print "    description.  The TLS description is taken from the"
    print "    REMARK fields in the PDB file, or from the TLSOUT file"
    print "    written by REFMAC."
    print

def astr(a):
    a0 = "%.3f" % (a[0])
    a1 = "%.3f" % (a[1])
    a2 = "%.3f" % (a[2])

    return a0.rjust(7) + a1.rjust(7) + a2.rjust(7)

def print_TLS(text, Tt, T, Lt, L, St, S):
    print text

    s0 = "%s TENSOR" % (Tt)
    s1 = "%s TENSOR" % (Lt)
    s2 = "%s TENSOR" % (St)

    print "".ljust(5) + s0.ljust(24) + s1.ljust(24) + s2
    print "     (A^2)                   (DEG^2)                 (A DEG)"

    L = L * Constants.RAD2DEG2
    S = S * Constants.RAD2DEG

    for i in range(3):
        print "   %s   %s   %s" % (astr(T[i]), astr(L[i]), astr(S[i]))

def print_TLSGroup(tls):
    print "TLS GROUP NAME: %s" % (tls.name)

    calcs = tls.calc_COR()

    print_TLS(
        "INPUT TENSOR MATRICES WRT ORTHOGONAL AXES USING ORIGIN "\
        "OF CALCULATIONS",
        "T", tls.T,
        "L", tls.L,
        "S", tls.S)

    print
    print "TRACE OF TRANSLATION TENSOR               %.3f" % (
        numpy.trace(tls.T))
    print "MEAN TRANSLATION (TRACE/3)                %.3f" % (
        numpy.trace(tls.T)/3.0)
    print "MEAN LIBRATION   (TRACE/3)                %.3f" % (
        numpy.trace(tls.L * Constants.RAD2DEG2)/3.0)
    print

    print_TLS(
        "TENSOR MATRICES WRT LIBRATION AXES USING ORIGIN OF CALCULATIONS",
        "T^", calcs["T^"],
        "L^", calcs["L^"],
        "S^", calcs["S^"])

    print
    print "ORIGIN SHIFT RHO(O)^ TO CENTRE WRT LIBRATION AXES (A): "+\
          astr(calcs["RHO^"])
    print "ORIGIN SHIFT TO CENTRE WRT ORTHOGONAL AXES        (A): "+\
          astr(calcs["RHO"])
    print "TLS CENTRE OF REACTION WRT ORTHOGONAL AXES        (A): "+\
          astr(calcs["COR"])
    print

    print_TLS(
        "TENSOR MATRICES WRT LIBRATION AXES USING CENTRE OF REACTION",
        "T'^", calcs["T'^"],
        "L'^", calcs["L'^"],
        "S'^", calcs["S'^"])

    print

    print_TLS(
        "TENSOR MATRICES WRT ORTHOGONAL AXES USING CENTRE OF REACTION",
        "T'", calcs["T'"],
        "L'", calcs["L'"],
        "S'", calcs["S'"])

    print
    print "TRACE(T')/3.0   (A^2): %.3f" % (numpy.trace(calcs["T'"])/3.0)
    print "TRACE(L')/3.0 (DEG^2): %.3f" % (numpy.trace(calcs["L'"])/3.0*Constants.RAD2DEG2)
    print "TRACE(S')/3.0 (A*DEG): %.3f" % (numpy.trace(calcs["S'"])/3.0*Constants.RAD2DEG)
    print

    print
    print "SHIFT OF LIBRATION AXES TO DIAGNOLIZE S WRT ORTHOGONAL AXES USING"
    print "FROM THE ORIGIN OF CALCULATION (A): "
    print "L1 (A): " + astr(calcs["L1_rho"] + calcs["COR"])
    print "L2 (A): " + astr(calcs["L2_rho"] + calcs["COR"])
    print "L3 (A): " + astr(calcs["L3_rho"] + calcs["COR"])
    print

    print
    print "SHIFT OF LIBRATION AXES TO DIAGNOLIZE S WRT ORTHOGONAL AXES"
    print "FROM THE CENTER OF REACTION (A): "
    print "L1 (A): " + astr(calcs["L1_rho"])
    print "L2 (A): " + astr(calcs["L2_rho"])
    print "L3 (A): " + astr(calcs["L3_rho"])
    print

    print "SCREW PITCH OF THE 3 NON-INTERSECTING LIBRATION AXES"

    for Lx, Lx_pitch in [
        ("L1", "L1_pitch"),
        ("L2", "L2_pitch"),
        ("L3", "L3_pitch")]:

        print "%s PITCH (A/DEG): %10.3f" % (Lx, calcs[Lx_pitch]/Constants.RAD2DEG)

def main(pdb_path, tls_out_path, calc_tls):

    struct = FileIO.LoadStructure(file = pdb_path)

    ## calculate one set of TLS tensors from all the amino acid atoms
    if calc_tls == True:
        tls = TLS.TLSGroup()

        for res in struct.iter_amino_acids():
            for atm in res.iter_atoms():
                tls.append(atm)

        tls.origin = tls.calc_centroid()
        tls.calc_tls_tensors()
        print_TLSGroup(tls)

    else:
        tls_file = TLS.TLSFile()

        ## get TLS groups from REMARK statments in PDB file
        if tls_out_path == None:
            tls_file.set_file_format(TLS.TLSFileFormatPDB())
            try:
                tls_file.load(open(pdb_path, "r"), pdb_path)
            except IOError, e:
                print "[Error] %s: %s" % (str(e), pdb_path)

        ## or get TLS groups from REFMAC TLSOUT file
        else:
            tls_file.set_file_format(TLS.TLSFileFormatTLSOUT())
            try:
                tls_file.load(open(tls_out_path, "r"), tls_out_path)
            except IOError, e:
                print "[Error] %s: %s" % (str(e), tls_out_path)

        ## print the TLS groups
        tls_group_list = tls_file.construct_tls_groups_with_atoms(struct)

        for tls_group in tls_group_list:
            print_TLSGroup(tls_group)
            print

if __name__ == "__main__":

    ## calculate TLS tensors -- do not read from file
    if "-c" in sys.argv:
        sys.argv.remove("-c")
        calc_tls = True
    else:
        calc_tls = False

    try:
        pdb_path = sys.argv[1]
    except IndexError:
        usage()
        sys.exit(1)

    try:
        tls_out_path = sys.argv[2]
    except IndexError:
        tls_out_path = None

    main(pdb_path, tls_out_path, calc_tls)

max_stars_repo_name: hokinus/mmLib

max_issues_repo_name: hokinus/mmLib

max_forks_repo_name: hokinus/mmLib