text:
from hoomd import *
import hoomd
import unittest
import os
import tempfile
import numpy as np
from hoomd import _hoomd, dem, md
import json

def parse_shape_spec(type_shapes):
    ret = [ json.loads(json_string) for json_string in type_shapes ];
    return ret;

class dem_shape_spec_base(unittest.TestCase):

    def setUp(self):
        hoomd.context.initialize()
        if hoomd.comm.get_rank() == 0:
            tmp = tempfile.mkstemp(suffix='.test.gsd');
            self.tmp_file = tmp[1];
        else:
            self.tmp_file = "invalid";

    def setup_system(self, cls, shape_params, expected_shapespec, filename, dim):
        if dim == 2:
            system = hoomd.init.create_lattice(unitcell=hoomd.lattice.sq(a=5.50),n=5);
        elif dim == 3:
            system = hoomd.init.create_lattice(unitcell=hoomd.lattice.sc(a=5.50),n=5);
        snapshot = system.take_snapshot(all=True)
        bindex = np.random.choice(range(5**dim),int(0.5*5**dim),replace=False)
        if comm.get_rank() == 0:
            snapshot.particles.types = ['A', 'B']
            snapshot.particles.typeid[bindex] = 1
        hoomd.context.initialize()
        system = hoomd.init.read_snapshot(snapshot);
        md.integrate.mode_standard(dt=0.001);
        obj = cls(nlist=md.nlist.cell(), radius=.5);
        obj.setParams('A', **shape_params["A"]);
        obj.setParams('B', **shape_params["B"]);
        md.integrate.nvt(group=group.all(), kT=1.0, tau=0.5)
        dumper = dump.gsd(filename=filename, group=group.all(), period=1, overwrite=True);
        dumper.dump_shape(obj);
        steps = 5
        hoomd.run(steps);
        reader = _hoomd.GSDReader(hoomd.context.exec_conf, filename, 0, False);
        if comm.get_rank() == 0:
            for i in range(steps):
                shape_spec = parse_shape_spec(reader.readTypeShapesPy(i));
                self.assertEqual(shape_spec[0], expected_shapespec[0]);
                self.assertEqual(shape_spec[1], expected_shapespec[1]);

    def test_wca_2d(self):
        sq_verts = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]];
        trg_verts = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5]];
        shape_params = dict(A=dict(vertices=sq_verts), \
                            B=dict(vertices=trg_verts));
        expected_shapespec = [dict(type='Polygon', rounding_radius=0.5, vertices=sq_verts), \
                              dict(type='Polygon', rounding_radius=0.5, vertices=trg_verts)];
        self.setup_system(cls=dem.pair.WCA, shape_params=shape_params, \
                          expected_shapespec=expected_shapespec, filename=self.tmp_file, dim=2);

    def test_wca_2d_with_sphere(self):
        sq_verts = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]];
        sph_verts = [[-0.5, -0.5]];
        shape_params = dict(A=dict(vertices=sq_verts), \
                            B=dict(vertices=sph_verts));
        expected_shapespec = [dict(type='Polygon', rounding_radius=0.5, vertices=sq_verts), \
                              dict(type='Sphere', diameter=1)];
        self.setup_system(cls=dem.pair.WCA, shape_params=shape_params, \
                          expected_shapespec=expected_shapespec, filename=self.tmp_file, dim=2);

    def test_wca_3d(self):
        cube_verts = [[-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, -0.5], [-0.5, 0.5, 0.5], \
                      [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5]];
        cube_faces = [[0,2,6], [6,4,0], [5,0,4], [5,1,0], [5,4,6], [5,6,7], [3,2,0], [3,0,1], \
                      [3,6,2], [3,7,6], [3,1,5], [3,5,7]]
        tetra_verts = [[0.5, 0.5, 0.5], [0.5, -0.5, -0.5], [-0.5, 0.5, -0.5], [-0.5, -0.5, 0.5]];
        tetra_faces = [[0,1,2], [3,0,2], [3,2,1], [3,1,0]];
        shape_params = dict(A=dict(vertices=cube_verts, faces=cube_faces), \
                            B=dict(vertices=tetra_verts, faces=tetra_faces));
        expected_shapespec = [dict(type='ConvexPolyhedron', rounding_radius=0.5, vertices=cube_verts), \
                              dict(type='ConvexPolyhedron', rounding_radius=0.5, vertices=tetra_verts)];
        self.setup_system(cls=dem.pair.WCA, shape_params=shape_params, \
                          expected_shapespec=expected_shapespec, filename=self.tmp_file, dim=3);

    def test_wca_3d_with_sphere(self):
        cube_verts = [[-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, -0.5], [-0.5, 0.5, 0.5], \
                      [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5]];
        cube_faces = [[0,2,6], [6,4,0], [5,0,4], [5,1,0], [5,4,6], [5,6,7], [3,2,0], [3,0,1], \
                      [3,6,2], [3,7,6], [3,1,5], [3,5,7]]
        sph_verts = [[-0.5, -0.5, 0.5]];
        shape_params = dict(A=dict(vertices=cube_verts, faces=cube_faces), \
                            B=dict(vertices=sph_verts, faces=[]));
        expected_shapespec = [dict(type='ConvexPolyhedron', rounding_radius=0.5, vertices=cube_verts), \
                              dict(type='Sphere', diameter=1)];
        self.setup_system(cls=dem.pair.WCA, shape_params=shape_params, \
                          expected_shapespec=expected_shapespec, filename=self.tmp_file, dim=3);

    def test_swca_2d(self):
        if comm.get_num_ranks() > 1:
            return
        sq_verts = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]];
        trg_verts = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5]];
        shape_params = dict(A=dict(vertices=sq_verts), \
                            B=dict(vertices=trg_verts));
        expected_shapespec = [dict(type='Polygon', rounding_radius=0.5, vertices=sq_verts), \
                              dict(type='Polygon', rounding_radius=0.5, vertices=trg_verts)];
        self.setup_system(cls=dem.pair.SWCA, shape_params=shape_params, \
                          expected_shapespec=expected_shapespec, filename=self.tmp_file, dim=2);

    def test_swca_2d_with_sphere(self):
        if comm.get_num_ranks() > 1:
            return
        sq_verts = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]];
        sph_verts = [[-0.5, -0.5]];
        shape_params = dict(A=dict(vertices=sq_verts), \
                            B=dict(vertices=sph_verts));
        expected_shapespec = [dict(type='Polygon', rounding_radius=0.5, vertices=sq_verts), \
                              dict(type='Sphere', diameter=1)];
        self.setup_system(cls=dem.pair.SWCA, shape_params=shape_params, \
                          expected_shapespec=expected_shapespec, filename=self.tmp_file, dim=2);

    def test_swca_3d(self):
        if comm.get_num_ranks() > 1:
            return
        cube_verts = [[-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, -0.5], [-0.5, 0.5, 0.5], \
                      [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5]];
        cube_faces = [[0,2,6], [6,4,0], [5,0,4], [5,1,0], [5,4,6], [5,6,7], [3,2,0], [3,0,1], \
                      [3,6,2], [3,7,6], [3,1,5], [3,5,7]]
        tetra_verts = [[0.5, 0.5, 0.5], [0.5, -0.5, -0.5], [-0.5, 0.5, -0.5], [-0.5, -0.5, 0.5]];
        tetra_faces = [[0,1,2], [3,0,2], [3,2,1], [3,1,0]];
        shape_params = dict(A=dict(vertices=cube_verts, faces=cube_faces), \
                            B=dict(vertices=tetra_verts, faces=tetra_faces));
        expected_shapespec = [dict(type='ConvexPolyhedron', rounding_radius=0.5, vertices=cube_verts), \
                              dict(type='ConvexPolyhedron', rounding_radius=0.5, vertices=tetra_verts)];
        self.setup_system(cls=dem.pair.SWCA, shape_params=shape_params, \
                          expected_shapespec=expected_shapespec, filename=self.tmp_file, dim=3);

    def test_swca_3d_with_sphere(self):
        if comm.get_num_ranks() > 1:
            return
        cube_verts = [[-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, -0.5], [-0.5, 0.5, 0.5], \
                      [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5]];
        cube_faces = [[0,2,6], [6,4,0], [5,0,4], [5,1,0], [5,4,6], [5,6,7], [3,2,0], [3,0,1], \
                      [3,6,2], [3,7,6], [3,1,5], [3,5,7]]
        sph_verts = [[-0.5, -0.5, 0.5]];
        shape_params = dict(A=dict(vertices=cube_verts, faces=cube_faces), \
                            B=dict(vertices=sph_verts, faces=[]));
        expected_shapespec = [dict(type='ConvexPolyhedron', rounding_radius=0.5, vertices=cube_verts), \
                              dict(type='Sphere', diameter=1)];
        self.setup_system(cls=dem.pair.SWCA, shape_params=shape_params, \
                          expected_shapespec=expected_shapespec, filename=self.tmp_file, dim=3);

    def tearDown(self):
        if comm.get_rank() == 0:
            os.remove(self.tmp_file);
        comm.barrier_all();

if __name__ == '__main__':
    unittest.main(argv = ['test.py', '-v'])

max_stars_repo_name: schwendp/hoomd-blue

max_issues_repo_name: schwendp/hoomd-blue

max_forks_repo_name: schwendp/hoomd-blue


text:
# Magnification map

# import modules
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import LogNorm
from PIL import Image
import timeit
import attempts.stage3.function_lens as lensing

# %%

# start the timer
start = timeit.default_timer()

# set up some initial parameters
rc = 1
eps = 0
dom = 6  # abs() of domain of r values (normally -1, 1 --> 1)
size = 200  # don;t set higher than 4096, cant mark uniquely in RGB

# set up an image of the source, empty, then mark each pixel uniquely in RGB
image_s = np.zeros([size, size, 3])

mark_R, mark_G, mark_B = 0, 0, 0

for i in range(size):
    for j in range(size):
        image_s[i, j, :] = mark_R, mark_G, mark_B

        # update markers in base 256, making sure to change over
        if mark_B < 255:
            mark_B += 1
        else:
            mark_B = 0
            if mark_G < 255:
                mark_G += 1
            else:
                mark_G = 0
                mark_R += 1

# plot that image
plt.imshow(image_s/255)

# now lens this image, use the imported function
image_lensed = lensing.lens(image_s, rc, eps, dom)

# start a new figure and plot it
plt.figure()
plt.imshow(image_lensed/255)

# now need to count how many of each RBG combinations appear

# set up an array to store numbers of occurances of each marker
results =  np.zeros([size, size])

# set the intial checkers
cR, cG, cB = 0, 0, 0

# set up a source pixel counter, not to have to deal with base 255 too much
countx = 0
county = 0

# set up a stopping variable
stop = False

# loop over checking each RGB combination:
while stop is False:
    # find how many values appear for current RGB checks
    number = len(np.where((image_lensed[:, :, 0] == cR) & (image_lensed[:, :, 1] == cG) & (image_lensed[:, :, 2] == cB))[0])

    # store it in the corresponding pixel
    results[countx, county] = number

    # update the checkers:
    if cB < 255:
        cB += 1
    else:
        cB = 0
        if cG < 255:
            cG += 1
        else:
            cG = 0
            cR += 1

    # update the source pixel indexes:
    if countx < size-1:
        countx += 1
    else:
        countx = 0
        county += 1

    # once all pixel combinations were checked, stop the loop
    if countx == size-1 and county == size-1:
        stop=True

# for pcolormesh plot, set up x and y arrays:
x = np.arange(0, size, 1)
y = np.arange(0, size, 1)
xg, yg = np.meshgrid(x, y)

# now plot it as a colour map
plt.figure()
indexes = np.where(results==0)
results[indexes] = 0.01  # set 0s to 0.001 to allow for log scaling to work
plot = plt.pcolormesh(xg, yg, results, cmap=cm.jet, norm=LogNorm(0.01, results.max()))

# set a colourbar
plt.colorbar(plot)

# do the plot with imshow

plt.figure()
plt.imshow(results)

# return time to run
stop = timeit.default_timer()
print('Time to run was: {:.4f}'.format(stop - start) + ' s')

ext: py

max_stars_repo_path: attempts/stage3/magnification map.py

max_issues_repo_path: attempts/stage3/magnification map.py

max_forks_repo_path: attempts/stage3/magnification map.py

reason: import numpy


text:
from __future__ import division
import numpy as np
import matplotlib
matplotlib.use('PDF')       # or Agg (for png), SVG, PS
import matplotlib.pyplot as plt
from data_tools import get_axialLocations, extrap1d, get_inputFileParameter

#----------------------------------------------------------

def plot_RorZprof(DI, RorZ, fileType, varName, yD, expFlist, expCol, xlim, ylim, xtics, ytics, xlabel, ylabel):

    D     = get_inputFileParameter(DI, ("initParams","d_f"))
    y     = yD*D

    if RorZ=='R':
        odt  = np.loadtxt(DI['pdir']+fileType+"_"+varName+".dat")
        scaleX = D
        figName = DI['pdir']+"radial_"+varName+"_" +fileType+"_" + DI['cn'].replace(".","o")
    else:
        odt  = np.loadtxt(DI['pdir']+fileType+"_"+varName+".dat")
        scaleX = 1
        figName = DI['pdir']+"conditional_"+varName+"_" +fileType+"_" + DI['cn'].replace(".","o")
    yodt = get_axialLocations(DI, forceGetDumpTimes=False)
    npts = len(odt[:,0])
    var  = np.zeros((npts, len(yD)))

    for i in range(len(yD)):
        for k in range(npts):
            fextrap = extrap1d(yodt, odt[k,1:])
            var[k,i] = fextrap(y[i])

    plt.figure()
    plt.rc("font", size=14)

    #for i,ifile in enumerate(expFlist):
    for i in range(len(yD)):
        plt.subplot(3,2,i+1)
        plt.plot(odt[:,0]/scaleX,var[:,i],'k-')
        if expFlist != []:
            exp = np.loadtxt(expFlist[i])
            plt.plot(exp[:,0],exp[:,expCol],'r-')
        plt.xlim(xlim)
        plt.ylim(ylim)
        plt.xticks([])
        plt.yticks([])
        plt.title("y/D = %i" %yD[i])

    plt.subplot(3,2,1)
    if expFlist == []:
        plt.legend(("ODT"), frameon=False)
    else:
        plt.legend(("ODT","EXP"), frameon=False)
    plt.yticks(ytics)
    plt.ylabel(ylabel)

    plt.subplot(3,2,3)
    plt.yticks(ytics)
    plt.ylabel(ylabel)

    plt.subplot(3,2,5)
    plt.yticks(ytics)
    plt.ylabel(ylabel)
    plt.xticks(xtics)
    plt.xlabel(xlabel)

    plt.subplot(3,2,6)
    plt.xticks(xtics)
    plt.xlabel(xlabel)

    #plt.show()

    plt.savefig(figName)

max_stars_repo_name: BYUignite/ODT


text:
import os
import numpy as np
import pandas as pd
from affine import Affine
from shapely.geometry import Polygon
import skimage
import rasterio
from cw_geodata.data import data_dir
from cw_geodata.vector_label.mask import footprint_mask, boundary_mask, \
    contact_mask, df_to_px_mask

class TestFootprintMask(object):
    """Tests for cw_geodata.vector_label.mask.footprint_mask."""

    def test_make_mask(self):
        """test creating a basic mask using a csv input."""
        output_mask = footprint_mask(os.path.join(data_dir, 'sample.csv'),
                                     geom_col="PolygonWKT_Pix")
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_fp_mask.tif'))

        assert np.array_equal(output_mask, truth_mask)

    def test_make_mask_w_output_file(self):
        """test creating a basic mask and saving the output to a file."""
        output_mask = footprint_mask(
            os.path.join(data_dir, 'sample.csv'),
            geom_col="PolygonWKT_Pix",
            reference_im=os.path.join(data_dir, "sample_geotiff.tif"),
            out_file=os.path.join(data_dir, 'test_out.tif')
            )
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_fp_mask.tif'))
        saved_output_mask = skimage.io.imread(os.path.join(data_dir,
                                                           'test_out.tif'))

        assert np.array_equal(output_mask, truth_mask)
        assert np.array_equal(saved_output_mask, truth_mask)
        # clean up
        os.remove(os.path.join(data_dir, 'test_out.tif'))

    def test_make_mask_w_file_and_transform(self):
        """Test creating a mask using a geojson and an affine xform."""
        output_mask = footprint_mask(
            os.path.join(data_dir, 'geotiff_labels.geojson'),
            reference_im=os.path.join(data_dir, 'sample_geotiff.tif'),
            do_transform=True,
            out_file=os.path.join(data_dir, 'test_out.tif')
            )
        truth_mask = skimage.io.imread(
            os.path.join(data_dir, 'sample_fp_mask_from_geojson.tif')
            )
        saved_output_mask = skimage.io.imread(os.path.join(data_dir,
                                                           'test_out.tif'))

        assert np.array_equal(output_mask, truth_mask)
        assert np.array_equal(saved_output_mask, truth_mask)
        # clean up
        os.remove(os.path.join(data_dir, 'test_out.tif'))

class TestBoundaryMask(object):
    """Tests for cw_geodata.vector_label.mask.boundary_mask."""

    def test_make_inner_mask_from_fp(self):
        """test creating a boundary mask using an existing footprint mask."""
        fp_mask = skimage.io.imread(os.path.join(data_dir,
                                                 'sample_fp_mask.tif'))
        output_mask = boundary_mask(fp_mask)
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_b_mask_inner.tif'))

        assert np.array_equal(output_mask, truth_mask)

    def test_make_outer_mask_from_fp(self):
        """test creating a boundary mask using an existing footprint mask."""
        fp_mask = skimage.io.imread(os.path.join(data_dir,
                                                 'sample_fp_mask.tif'))
        output_mask = boundary_mask(fp_mask, boundary_type="outer")
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_b_mask_outer.tif'))

        assert np.array_equal(output_mask, truth_mask)

    def test_make_thick_outer_mask_from_fp(self):
        """test creating a 10-px thick boundary mask."""
        fp_mask = skimage.io.imread(os.path.join(data_dir,
                                                 'sample_fp_mask.tif'))
        output_mask = boundary_mask(fp_mask, boundary_type="outer",
                                    boundary_width=10)
        truth_mask = skimage.io.imread(
            os.path.join(data_dir, 'sample_b_mask_outer_10.tif')
            )

        assert np.array_equal(output_mask, truth_mask)

    def test_make_binary_and_fp(self):
        """test creating a boundary mask and a fp mask together."""
        output_mask = boundary_mask(df=os.path.join(data_dir, 'sample.csv'),
                                    geom_col="PolygonWKT_Pix")
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_b_mask_inner.tif'))

        assert np.array_equal(output_mask, truth_mask)

class TestContactMask(object):
    """Tests for cw_geodata.vector_label.mask.contact_mask."""

    def test_make_contact_mask_w_save(self):
        """test creating a contact point mask."""
        output_mask = contact_mask(
            os.path.join(data_dir, 'sample.csv'), geom_col="PolygonWKT_Pix",
            contact_spacing=10,
            reference_im=os.path.join(data_dir, "sample_geotiff.tif"),
            out_file=os.path.join(data_dir, 'test_out.tif')
            )
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_c_mask.tif'))
        saved_output_mask = skimage.io.imread(os.path.join(data_dir,
                                                           'test_out.tif'))

        assert np.array_equal(output_mask, truth_mask)
        assert np.array_equal(saved_output_mask, truth_mask)
        os.remove(os.path.join(data_dir, 'test_out.tif'))  # clean up after

class TestDFToPxMask(object):
    """Tests for cw_geodata.vector_label.mask.df_to_px_mask."""

    def test_basic_footprint_w_save(self):
        output_mask = df_to_px_mask(
            os.path.join(data_dir, 'sample.csv'),
            geom_col='PolygonWKT_Pix',
            reference_im=os.path.join(data_dir, "sample_geotiff.tif"),
            out_file=os.path.join(data_dir, 'test_out.tif'))
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_fp_from_df2px.tif')
                                       )
        saved_output_mask = skimage.io.imread(os.path.join(data_dir,
                                                           'test_out.tif'))

        assert np.array_equal(output_mask, truth_mask)
        assert np.array_equal(saved_output_mask, truth_mask[:, :, 0])
        os.remove(os.path.join(data_dir, 'test_out.tif'))  # clean up after

    def test_border_footprint_w_save(self):
        output_mask = df_to_px_mask(
            os.path.join(data_dir, 'sample.csv'), channels=['boundary'],
            geom_col='PolygonWKT_Pix',
            reference_im=os.path.join(data_dir, "sample_geotiff.tif"),
            out_file=os.path.join(data_dir, 'test_out.tif'))
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_b_from_df2px.tif')
                                       )
        saved_output_mask = skimage.io.imread(os.path.join(data_dir,
                                                           'test_out.tif'))

        assert np.array_equal(output_mask, truth_mask)
        assert np.array_equal(saved_output_mask, truth_mask[:, :, 0])
        os.remove(os.path.join(data_dir, 'test_out.tif'))  # clean up after

    def test_contact_footprint_w_save(self):
        output_mask = df_to_px_mask(
            os.path.join(data_dir, 'sample.csv'), channels=['contact'],
            geom_col='PolygonWKT_Pix',
            reference_im=os.path.join(data_dir, "sample_geotiff.tif"),
            out_file=os.path.join(data_dir, 'test_out.tif'))
        truth_mask = skimage.io.imread(os.path.join(data_dir,
                                                    'sample_c_from_df2px.tif')
                                       )
        saved_output_mask = skimage.io.imread(os.path.join(data_dir,
                                                           'test_out.tif'))

        assert np.array_equal(output_mask, truth_mask)
        assert np.array_equal(saved_output_mask, truth_mask[:, :, 0])
        os.remove(os.path.join(data_dir, 'test_out.tif'))  # clean up after

    def test_all_three_w_save(self):
        """Test creating a 3-channel mask."""
        output_mask = df_to_px_mask(
            os.path.join(data_dir, 'sample.csv'),
            channels=['footprint', 'boundary', 'contact'],
            boundary_type='outer', boundary_width=5, contact_spacing=15,
            geom_col='PolygonWKT_Pix',
            reference_im=os.path.join(data_dir, "sample_geotiff.tif"),
            out_file=os.path.join(data_dir, 'test_out.tif'))
        truth_mask = skimage.io.imread(
            os.path.join(data_dir, 'sample_fbc_from_df2px.tif')
            )
        saved_output_mask = skimage.io.imread(os.path.join(data_dir,
                                                           'test_out.tif'))

        assert np.array_equal(output_mask, truth_mask)
        assert np.array_equal(saved_output_mask, truth_mask)
        os.remove(os.path.join(data_dir, 'test_out.tif'))  # clean up after

max_stars_repo_name: nrweir/cw-geodata

max_issues_repo_name: nrweir/cw-geodata

max_forks_repo_name: nrweir/cw-geodata


text:
import torch
from torch.utils.data import Dataset, DataLoader
from torch.autograd import Variable
import pandas as pd
import numpy as np
from sklearn import preprocessing
import argparse
from Data.data_pipeline import *

def Parser(argparse):

        parser = argparse.ArgumentParser(description='VAE MNIST Example')
        parser.add_argument('--hidden-layers', type=list, default=[200,100], metavar='N',
                    help='how many layers will the encoder have')
        parser.add_argument('--encoding-dim', type=int, default=10, metavar='N',
                    help='encoding dimension')
        parser.add_argument('--learning-rate', type=int, default=0.001, metavar='N',
                    help='how many layers will the encoder have')
        parser.add_argument('--batch-size', type=int, default=20, metavar='N',
                    help='input batch size for training (default: 128)')
        parser.add_argument('--epochs', type=int, default=10, metavar='N',
                    help='number of epochs to train (default: 10)')
        parser.add_argument('--no-cuda', action='store_true', default=False,
                    help='enables CUDA training')
        parser.add_argument('--seed', type=int, default=1, metavar='S',
                    help='random seed (default: 1)')
        parser.add_argument('--log-interval', type=int, default=10, metavar='N',
                    help='how many batches to wait before logging training status')
        args = parser.parse_args(args=[])

        args.cuda = not args.no_cuda and torch.cuda.is_available()

        torch.manual_seed(args.seed)

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

        kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}

        return kwargs, device, args

def scale_data(data):
    return preprocessing.MinMaxScaler().fit_transform(data)

class ReadDataset_train_random(Dataset):
   #""" Diabetes dataset."""

   # Initialize your data, download, etc.
    def __init__(self):
        x = np.loadtxt('random.csv',
                       delimiter=',', dtype=np.float32)
        y = np.loadtxt('random.csv',
                       delimiter=',', dtype=np.float32)
        self.len = x.shape[0]
        self.x_data = torch.from_numpy(x)
        self.y_data = torch.from_numpy(y)

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len

class ReadDataset_test_random(Dataset):
   #""" Diabetes dataset."""

   # Initialize your data, download, etc.
    def __init__(self):
        x = np.loadtxt('random_test.csv',
                       delimiter=',', dtype=np.float32)
        y = np.loadtxt('random_test.csv',
                       delimiter=',', dtype=np.float32)
        self.len = x.shape[0]
        self.x_data = torch.from_numpy(x)
        self.y_data = torch.from_numpy(y)

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len

class ReadDataset_train_E_coli(Dataset):
   #""" Diabetes dataset."""

   # Initialize your data, download, etc.
    def __init__(self):
        root_gene = None
        minimum_evidence = 'weak'
        max_depth = np.inf
        r_expr, gene_symbols, sample_names = load_data(root_gene=root_gene,
                                     minimum_evidence=minimum_evidence,
                                     max_depth=max_depth)

        # Split data into train and test sets
        train_idxs, test_idxs = split_train_test(sample_names)
        expr_train = r_expr[train_idxs, :]
        expr_test = r_expr[test_idxs, :]

        x = expr_train.astype(np.float32)
        y = expr_train.astype(np.float32)
        x_n=normalize(x)
        y_n=normalize(y)
        x=scale_data(x_n)
        y=scale_data(y_n)
        self.len = x.shape[0]
        self.x_data = torch.from_numpy(x)
        self.y_data = torch.from_numpy(y)

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len

class ReadDataset_test_E_coli(Dataset):
   #""" Diabetes dataset."""

   # Initialize your data, download, etc.
    def __init__(self):
        root_gene = None
        minimum_evidence = 'weak'
        max_depth = np.inf
        r_expr, gene_symbols, sample_names = load_data(root_gene=root_gene,
                                     minimum_evidence=minimum_evidence,
                                     max_depth=max_depth)

        # Split data into train and test sets
        train_idxs, test_idxs = split_train_test(sample_names)
        expr_train = r_expr[train_idxs, :]
        expr_test = r_expr[test_idxs, :]

        x = expr_test.astype(np.float32)
        y = expr_test.astype(np.float32)
        x_n=normalize(x)
        y_n=normalize(y)
        x=scale_data(x_n)
        y=scale_data(y_n)
        self.len = x.shape[0]
        self.x_data = torch.from_numpy(x)
        self.y_data = torch.from_numpy(y)

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len

class ReadDataset_train_E_coli_genes(Dataset):
   #""" Diabetes dataset."""

   # Initialize your data, download, etc.
    def __init__(self):
        root_gene = None
        minimum_evidence = 'weak'
        max_depth = np.inf
        r_expr, gene_symbols, sample_names = load_data(root_gene=root_gene,
                                     minimum_evidence=minimum_evidence,
                                     max_depth=max_depth)
        r_expr=r_expr.T

        # Split data into train and test sets
#        train_idxs, test_idxs = split_train_test(gene_symbols)
        expr_train = r_expr[:int(r_expr.shape[0]*0.8), :]
        expr_test = r_expr[int(r_expr.shape[0]*0.8):, :]

        x = expr_train.astype(np.float32)
        y = expr_train.astype(np.float32)
        x_n=normalize(x)
        y_n=normalize(y)
        x=scale_data(x_n)
        y=scale_data(y_n)
        self.len = x.shape[0]
        self.x_data = torch.from_numpy(x)
        self.y_data = torch.from_numpy(y)

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len

class ReadDataset_test_E_coli_genes(Dataset):
   #""" Diabetes dataset."""

   # Initialize your data, download, etc.
    def __init__(self):
        root_gene = None
        minimum_evidence = 'weak'
        max_depth = np.inf
        r_expr, gene_symbols, sample_names = load_data(root_gene=root_gene,
                                     minimum_evidence=minimum_evidence,
                                     max_depth=max_depth)
        r_expr=r_expr.T

        # Split data into train and test sets
        #train_idxs, test_idxs = split_train_test(gene_symbols)
        expr_train = r_expr[:int(r_expr.shape[0]*0.8), :]
        expr_test = r_expr[int(r_expr.shape[0]*0.8):, :]

        x = expr_test.astype(np.float32)
        y = expr_test.astype(np.float32)
        x_n=normalize(x)
        y_n=normalize(y)
        x=scale_data(x_n)
        y=scale_data(y_n)
        self.len = x.shape[0]
        self.x_data = torch.from_numpy(x)
        self.y_data = torch.from_numpy(y)

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len

def normalize(expr, kappa=1):
    """
    Normalizes expressions to make each gene have mean 0 and std kappa^-1
    :param expr: matrix of gene expressions. Shape=(nb_samples, nb_genes)
    :param kappa: kappa^-1 is the gene std
    :return: normalized expressions
    """
    mean = np.mean(expr, axis=0)
    std = np.std(expr, axis=0)
    return (expr - mean) / (kappa * std)

def restore_scale(expr, mean, std):
    """
    Makes each gene j have mean_j and std_j
    :param expr: matrix of gene expressions. Shape=(nb_samples, nb_genes)
    :param mean: vector of gene means. Shape=(nb_genes,)
    :param std: vector of gene stds. Shape=(nb_genes,)
    :return: Rescaled gene expressions
    """
    return expr * std + mean

def clip_outliers(expr, r_min, r_max):
    """
    Clips expression values to make them be between r_min and r_max
    :param expr: matrix of gene expressions. Shape=(nb_samples, nb_genes)
    :param r_min: minimum expression value (float)
    :param r_max: maximum expression value (float)
    :return: Clipped expression matrix
    """
    expr_c = np.copy(expr)
    expr_c[expr_c < r_min] = r_min
    expr_c[expr_c > r_max] = r_max
    return expr_c


text:
from django.contrib.auth.models import User
import datetime as dt
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
import numpy as np
from cloudinary.models import CloudinaryField

# Create your models here.
class Profile(models.Model):
    """
    Class that contains Profile details
    """
    profile_pic = models.ImageField(upload_to = 'images/', blank=True)
    bio = models.TextField()
    contact = models.CharField(max_length = 30, blank=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True,)

    @receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)

    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.profile.save()

    post_save.connect(save_user_profile, sender=User)

    def save_profile(self):
        self.save()

    def del_profile(self):
        self.delete()

    @classmethod
    def get_by_id(cls, user_id):
            profile = Profile.objects.get(user_id = user_id)
            return profile

    def __str__(self):
                return self.bio

class Project(models.Model):
    """
    Class that contains Project details
    """
    title = models.CharField(max_length = 20)
    landing_page = models.ImageField(upload_to = 'images/', blank=True)
    description = models.TextField()
    link = models.URLField(max_length = 100)
    rating = models.TextField()
    posted_on = models.DateTimeField(auto_now_add=True)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def save_pro(self):
        self.save()

    def del_pro(self):
        self.delete()

    @classmethod
    def search_by_title(cls,search_term):
        projects = cls.objects.filter(title__icontains=search_term)
        return projects

    @classmethod
    def get_pro_by_id(cls, id):
            pro = Project.objects.get(id=id)
            return pro

    @classmethod
    def get_pro_by_user(cls, id):
            project = Project.objects.filter(user_id=id).all()
            return project

    def average_design(self):
       all_ratings = list(map(lambda x: x.rating, self.designrating_set.all()))
       return np.mean(all_ratings)

    def average_usability(self):
       all_ratings = list(map(lambda x: x.rating, self.usabilityrating_set.all()))
       return np.mean(all_ratings)

    def average_content(self):
       all_ratings = list(map(lambda x: x.rating, self.contentrating_set.all()))
       return np.mean(all_ratings)

    def __str__(self):
                return self.title

class DesignRating(models.Model):
    RATING_CHOICES = (
       (1, '1'),
       (2, '2'),
       (3, '3'),
       (4, '4'),
       (5, '5'),
       (6, '6'),
       (7, '7'),
       (8, '8'),
       (9, '9'),
       (10, '10')
    )
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    pub_date = models.DateTimeField(auto_now=True)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    rating = models.IntegerField(choices=RATING_CHOICES, default=0)

class ContentRating(models.Model):
    RATING_CHOICES = (
       (1, '1'),
       (2, '2'),
       (3, '3'),
       (4, '4'),
       (5, '5'),
       (6, '6'),
       (7, '7'),
       (8, '8'),
       (9, '9'),
       (10, '10')
    )
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    pub_date = models.DateTimeField(auto_now=True)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    rating = models.IntegerField(choices=RATING_CHOICES, default=0)

class UsabilityRating(models.Model):
    RATING_CHOICES = (
       (1, '1'),
       (2, '2'),
       (3, '3'),
       (4, '4'),
       (5, '5'),
       (6, '6'),
       (7, '7'),
       (8, '8'),
       (9, '9'),
       (10, '10')
    )
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    pub_date = models.DateTimeField(auto_now=True)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    rating = models.IntegerField(choices=RATING_CHOICES, default=0)

max_stars_repo_name: Denis-leparteleg/Awards

max_issues_repo_name: Denis-leparteleg/Awards

max_forks_repo_name: Denis-leparteleg/Awards


text:
import numpy as np

_bins = 500
_bin_size = 2 * np.pi / _bins
_d = np.linspace(0, 2 * np.pi, _bins, endpoint=False)

def _circfuncs_common(samples, high, low):
    r"""
    :param samples: np.ndarray
    :param high: np.ndarray
    :param low: np.ndarray
    :return: np.ndarray
    """
    samples = np.asarray(samples)
    if samples.size == 0:
        return np.nan
    return (samples - low) * 2. * np.pi / (high - low)

def _circ_mean(ang, axis=None, mass=1):
    r"""Circular mean of angles in [0, 2\pi).
    :param ang: np.ndarray, angular values in [0, 2\pi).
    :param axis: int, about which axis
    :param mass: np.ndarray or float, mass of particles.
    :return: np.ndarray
    """
    s = (mass * np.sin(ang)).sum(axis=axis)
    c = (mass * np.cos(ang)).sum(axis=axis)
    res = np.arctan2(-s, -c)
    return res + np.pi

def _circ_midpoint(x, _bin_size, _bins):
    r"""Deduplicate using histogram then calculate circular median.
    :param x: np.ndarray.
    :param _bin_size: float, bin size.
    :param _bins: int, how many bins.
    :return: np.ndarray
    """
    p = np.bincount((x / _bin_size).astype(np.int), minlength=_bins)
    return _circ_mean(_d[p > 0])

def com(x, hi, lo, axis=0, mass=1, midpoint=False):
    r"""Compute COM or median of PBC datas.
    :param x: np.ndarray, input coordinates.
    :param hi: np.ndarray, box.high
    :param lo: np.ndarray, box.low
    :param axis: int, mean along given axis.
    :param mass: np.ndarray or float, mass of particles.
    :param midpoint: bool, calculate midpoint or COM
    :return: np.ndarray, midpoint or COM
    """
    hi = np.asarray(hi)
    lo = np.asarray(lo)
    box = hi - lo
    x_ang = _circfuncs_common(x, hi, lo)
    if not midpoint:
        mean_ang = _circ_mean(x_ang, axis=axis, mass=mass)
    else:
        mean_ang = np.apply_along_axis(_circ_midpoint, axis,
                                       x_ang, _bin_size, _bins)
    return mean_ang / 2 / np.pi * box + lo

max_stars_repo_name: atomrq/simulab

max_issues_repo_name: atomrq/simulab

max_forks_repo_name: atomrq/simulab


text:
import json
import os
import re
from datetime import datetime
import numpy as np
import operator
from collections import OrderedDict
from typing import Iterator, List, Dict, Union, Tuple

def tweet_features_main(reaction_status_json, source_tweet_user_screen_name, source_text) -> List:
    num_retweets = reaction_status_json["retweet_count"]
    num_favorites = reaction_status_json["favorite_count"] if reaction_status_json["favorite_count"] is not None else 0  # indicates approximately how many times this Tweet has been liked by Twitter users

    if 'full_text' in reaction_status_json:
        context_text = reaction_status_json['full_text']
    elif 'text' in reaction_status_json:
        context_text = reaction_status_json['text']
    else:
        raise ValueError

    has_question = 1 if re.findall(r"\?", context_text) else 0
    is_duplicate = 1 if context_text.strip() == source_text.strip() else 0
    has_img = 1 if reaction_status_json['user']['profile_use_background_image'] else 0
    has_urls = 1 if reaction_status_json['entities']['urls'] else 0  # if the tweet has urls
    if has_urls ==1:
        num_urls = len(reaction_status_json['entities']['urls'])
    else:
        num_urls = 0
    ## if the tweet contains native media (shared with the Tweet user-interface as opposed via a link to elsewhere)
    ## e.g., https://twitter.com/RT_com/status/500350777844457473/photo/1
    has_native_media = 1 if 'extended_entities' in reaction_status_json else 0
    # native_media_type_map = {'photo':0, 'video':1, 'animated_gif':2}
    # if has_native_media == 1:
    #     media_type = native_media_type_map[reaction_status_json['extended_entities']['media'][0]['type']]
    # else:
        # media_type = 3
    context_len = len(re.findall(r"[\w']+", re.sub(r"(?:{})\s+| http\S+".format(source_tweet_user_screen_name),
                                "", context_text)))  # tweet length after removing the source author's screen name and urls
    tweet_features = [
        num_retweets,
        num_favorites,
        int(has_question),
        int(is_duplicate),
        int(has_img),
        int(has_urls),
        num_urls,
        int(has_native_media),
        context_len
    ]
    # print("Number of tweet features: ", len(tweet_features))
    return tweet_features

max_stars_repo_name: jerrygaoLondon/RPDNN


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

"""
%prog mcscan.txt all.bed layout.csv

Illustrate MCscan multiple collinearity alignments. Use layout.csv to indicate
the positions of tracks. For example:

#x, y, rotation, ha, va, color, ratio
0.5, 0.6, 0, left, center, g
0.25, 0.7, 45, top, center, m

With the row ordering corresponding to the column ordering in the MCscan output.
"""

import sys
import logging
import numpy as np

from jcvi.compara.synteny import BlockFile
from jcvi.formats.bed import Bed
from jcvi.formats.base import DictFile
from jcvi.utils.cbook import human_size
from jcvi.apps.base import OptionParser

from jcvi.graphics.glyph import Glyph, RoundLabel
from jcvi.graphics.base import mpl, plt, savefig, markup, \
            Path, PathPatch, AbstractLayout

forward, backward = 'b', 'g'  # Genes with different orientations

class LayoutLine (object):

    def __init__(self, row, delimiter=','):
        self.hidden = row[0] == '*'
        if self.hidden:
            row = row[1:]
        args = row.rstrip().split(delimiter)
        args = [x.strip() for x in args]
        self.x = float(args[0])
        self.y = float(args[1])
        self.rotation = int(args[2])
        self.ha = args[3]
        self.va = args[4]
        self.color = args[5]
        self.ratio = 1
        if len(args) > 6:
            self.ratio = float(args[6])
        if len(args) > 7:
            self.label = args[7].strip()
        else:
            self.label = None

class Layout (AbstractLayout):

    def __init__(self, filename, delimiter=','):
        super(Layout, self).__init__(filename)
        fp = open(filename)
        self.edges = []
        for row in fp:
            if row[0] == '#':
                continue
            if row[0] == 'e':
                args = row.rstrip().split(delimiter)
                args = [x.strip() for x in args]
                a, b = args[1:3]
                a, b = int(a), int(b)
                assert args[0] == 'e'
                self.edges.append((a, b))
            else:
                self.append(LayoutLine(row, delimiter=delimiter))

        if 3 <= len(self) <= 8:
            self.assign_colors()

class Shade (object):

    def __init__(self, ax, a, b, ymid, highlight=False, ec="k", fc="k",
                    alpha=.2, lw=1, zorder=1):
        a1, a2 = a
        b1, b2 = b
        ax1, ay1 = a1
        ax2, ay2 = a2
        bx1, by1 = b1
        bx2, by2 = b2
        M, C4, L, CP = Path.MOVETO, Path.CURVE4, Path.LINETO, Path.CLOSEPOLY
        pathdata = \
        [
            (M, a1),
            (C4, (ax1, ymid)),
            (C4, (bx1, ymid)),
            (C4, b1),
            (L, b2),
            (C4, (bx2, ymid)),
            (C4, (ax2, ymid)),
            (C4, a2),
            (CP, a1)
        ]
        codes, verts = zip(*pathdata)
        path = Path(verts, codes)
        if highlight:
            ec = fc = highlight

        pp = PathPatch(path, ec=ec, fc=fc, alpha=alpha,
                     lw=lw, zorder=zorder)
        ax.add_patch(pp)

class Region (object):

    def __init__(self, ax, ext, layout, bed, scale, switch=None,
                 chr_label=True, loc_label=True,
                 pad=.05, vpad=.015, extra_features=None):
        x, y = layout.x, layout.y
        ratio = layout.ratio
        scale /= ratio
        self.y = y
        lr = layout.rotation
        tr = mpl.transforms.Affine2D().\
                    rotate_deg_around(x, y, lr) + ax.transAxes
        inv = ax.transAxes.inverted()

        start, end, si, ei, chr, orientation, span = ext
        flank = span / scale / 2
        xstart, xend = x - flank, x + flank
        self.xstart, self.xend = xstart, xend

        cv = lambda t: xstart + abs(t - startbp) / scale
        hidden = layout.hidden

        # Chromosome
        if not hidden:
            ax.plot((xstart, xend), (y, y), color="gray", transform=tr, \
                    lw=2, zorder=1)

        self.genes = genes = bed[si: ei + 1]
        startbp, endbp = start.start, end.end
        if orientation == '-':
            startbp, endbp = endbp, startbp

        if switch:
            chr = switch.get(chr, chr)
        if layout.label:
            chr = layout.label

        label = "-".join((human_size(startbp, target="Mb", precision=2)[:-2],
                          human_size(endbp, target="Mb", precision=2)))

        height = .012
        self.gg = {}
        # Genes
        for g in genes:
            gstart, gend = g.start, g.end
            strand = g.strand
            if strand == '-':
                gstart, gend = gend, gstart
            if orientation == '-':
                strand = "+" if strand == "-" else "-"

            x1, x2, a, b = self.get_coordinates(gstart, gend, y, cv, tr, inv)
            self.gg[g.accn] = (a, b)

            color = forward if strand == "+" else backward
            if not hidden:
                gp = Glyph(ax, x1, x2, y, height, gradient=False, fc=color, zorder=3)
                gp.set_transform(tr)

        # Extra features (like repeats)
        if extra_features:
            for g in extra_features:
                gstart, gend = g.start, g.end
                x1, x2, a, b = self.get_coordinates(gstart, gend, y, cv, tr, inv)
                gp = Glyph(ax, x1, x2, y, height * 3 / 4, gradient=False,
                           fc='#ff7f00', zorder=2)
                gp.set_transform(tr)

        ha, va = layout.ha, layout.va

        hpad = .02
        if ha == "left":
            xx = xstart - hpad
            ha = "right"
        elif ha == "right":
            xx = xend + hpad
            ha = "left"
        else:
            xx = x
            ha = "center"

        # Tentative solution to labels stick into glyph
        magic = 40.
        cc = abs(lr) / magic if abs(lr) > magic else 1
        if va == "top":
            yy = y + cc * pad
        elif va == "bottom":
            yy = y - cc * pad
        else:
            yy = y

        l = np.array((xx, yy))
        trans_angle = ax.transAxes.transform_angles(np.array((lr, )),
                                                    l.reshape((1, 2)))[0]
        lx, ly = l
        if not hidden:
            bbox = dict(boxstyle="round", fc='w', ec='w', alpha=.5)
            kwargs = dict(ha=ha, va="center",
                          rotation=trans_angle, bbox=bbox, zorder=10)

            # TODO: I spent several hours on trying to make this work - with no
            # good solutions. To generate labels on multiple lines, each line
            # with a different style is difficult in matplotlib. The only way,
            # if you can tolerate an extra dot (.), is to use the recipe below.
            #chr_label = r"\noindent " + markup(chr) + r" \\ ." if chr_label else None
            #loc_label = r"\noindent . \\ " + label if loc_label else None

            chr_label = markup(chr) if chr_label else None
            loc_label = label if loc_label else None
            if chr_label:
                if loc_label:
                    ax.text(lx, ly + vpad, chr_label, color=layout.color, **kwargs)
                    ax.text(lx, ly - vpad, loc_label, color="lightslategrey",
                            size=10, **kwargs)
                else:
                    ax.text(lx, ly, chr_label, color=layout.color, **kwargs)

    def get_coordinates(self, gstart, gend, y, cv, tr, inv):
        x1, x2 = cv(gstart), cv(gend)
        a, b = tr.transform((x1, y)), tr.transform((x2, y))
        a, b = inv.transform(a), inv.transform(b)
        return x1, x2, a, b

class Synteny (object):

    def __init__(self, fig, root, datafile, bedfile, layoutfile,
                 switch=None, tree=None, extra_features=None,
                 chr_label=True, loc_label=True, pad=.05, vpad=.015,
                 scalebar=False):

        w, h = fig.get_figwidth(), fig.get_figheight()
        bed = Bed(bedfile)
        order = bed.order
        bf = BlockFile(datafile)
        self.layout = lo = Layout(layoutfile)
        switch = DictFile(switch, delimiter="\t") if switch else None
        if extra_features:
            extra_features = Bed(extra_features)

        exts = []
        extras = []
        for i in xrange(bf.ncols):
            ext = bf.get_extent(i, order)
            exts.append(ext)
            if extra_features:
                start, end, si, ei, chr, orientation, span = ext
                start, end = start.start, end.end  # start, end coordinates
                ef = list(extra_features.extract(chr, start, end))

                # Pruning removes minor features with < 0.1% of the region
                ef_pruned = [x for x in ef if x.span >= span / 1000]
                print >> sys.stderr, "Extracted {0} features "\
                        "({1} after pruning)".format(len(ef), len(ef_pruned))
                extras.append(ef_pruned)

        maxspan = max(exts, key=lambda x: x[-1])[-1]
        scale = maxspan / .65

        self.gg = gg = {}
        self.rr = []
        ymids = []
        #vpad = .012 * w / h
        for i in xrange(bf.ncols):
            ext = exts[i]
            ef = extras[i] if extras else None
            r = Region(root, ext, lo[i], bed, scale, switch,
                       chr_label=chr_label, loc_label=loc_label,
                       vpad=vpad, extra_features=ef)
            self.rr.append(r)
            # Use tid and accn to store gene positions
            gg.update(dict(((i, k), v) for k, v in r.gg.items()))
            ymids.append(r.y)

        for i, j in lo.edges:
            for ga, gb, h in bf.iter_pairs(i, j):
                a, b = gg[(i, ga)], gg[(j, gb)]
                ymid = (ymids[i] + ymids[j]) / 2
                Shade(root, a, b, ymid, fc="gainsboro", lw=0, alpha=1)

            for ga, gb, h in bf.iter_pairs(i, j, highlight=True):
                a, b = gg[(i, ga)], gg[(j, gb)]
                ymid = (ymids[i] + ymids[j]) / 2
                Shade(root, a, b, ymid, alpha=1, highlight=h, zorder=2)

        if scalebar:
            print >> sys.stderr, "Build scalebar (scale={})".format(scale)
            # Find the best length of the scalebar
            ar = [1, 2, 5]
            candidates = [1000 * x for x in ar] + [10000 * x for x in ar] + \
                         [100000 * x for x in ar]
            # Find the one that's close to an optimal canvas size
            dists = [(abs(x / scale - .12), x) for x in candidates]
            dist, candidate = min(dists)
            dist = candidate / scale
            x, y, yp = .2, .96, .005
            a, b = x - dist / 2, x + dist / 2
            lsg = "lightslategrey"
            root.plot([a, a], [y - yp, y + yp], "-", lw=2, color=lsg)
            root.plot([b, b], [y - yp, y + yp], "-", lw=2, color=lsg)
            root.plot([a, b], [y, y], "-", lw=2, color=lsg)
            root.text(x, y + .02, human_size(candidate, precision=0),
                      ha="center", va="center")

        if tree:
            from jcvi.graphics.tree import draw_tree, read_trees

            trees = read_trees(tree)
            ntrees = len(trees)
            logging.debug("A total of {0} trees imported.".format(ntrees))
            xiv = 1. / ntrees
            yiv = .3
            xstart = 0
            ystart = min(ymids) - .4
            for i in xrange(ntrees):
                ax = fig.add_axes([xstart, ystart, xiv, yiv])
                label, outgroup, tx = trees[i]
                draw_tree(ax, tx, outgroup=outgroup, rmargin=.4, leaffont=11)
                xstart += xiv
                RoundLabel(ax, .5, .3, label, fill=True, fc="lavender", color="r")

def draw_gene_legend(ax, x1, x2, ytop, d=.04, text=False, repeat=False):
    ax.plot([x1, x1 + d], [ytop, ytop], ":", color=forward, lw=2)
    ax.plot([x1 + d], [ytop], ">", color=forward, mec=forward)
    ax.plot([x2, x2 + d], [ytop, ytop], ":", color=backward, lw=2)
    ax.plot([x2], [ytop], "<", color=backward, mec="g")
    if text:
        ax.text(x1 + d / 2, ytop + d / 2, "gene (+)", ha="center")
        ax.text(x2 + d / 2, ytop + d / 2, "gene (-)", ha="center")
    if repeat:
        xr = (x1 + x2 + d) / 2
        Glyph(ax, xr - d / 2, xr + d / 2, ytop, .012 * 3 / 4, gradient=False,
              fc='#ff7f00', zorder=2)
        ax.text(xr, ytop + d / 2, "repeat", ha="center")

def main():
    p = OptionParser(__doc__)
    p.add_option("--switch",
                 help="Rename the seqid with two-column file [default: %default]")
    p.add_option("--tree",
                 help="Display trees on the bottom of the figure [default: %default]")
    p.add_option("--extra", help="Extra features in BED format")
    p.add_option("--scalebar", default=False, action="store_true",
                 help="Add scale bar to the plot")
    opts, args, iopts = p.set_image_options(figsize="8x7")

    if len(args) != 3:
        sys.exit(not p.print_help())

    datafile, bedfile, layoutfile = args
    switch = opts.switch
    tree = opts.tree

    pf = datafile.rsplit(".", 1)[0]
    fig = plt.figure(1, (iopts.w, iopts.h))
    root = fig.add_axes([0, 0, 1, 1])

    Synteny(fig, root, datafile, bedfile, layoutfile,
            switch=switch, tree=tree, extra_features=opts.extra,
            scalebar=opts.scalebar)

    root.set_xlim(0, 1)
    root.set_ylim(0, 1)
    root.set_axis_off()

    image_name = pf + "." + iopts.format
    savefig(image_name, dpi=iopts.dpi, iopts=iopts)

if __name__ == '__main__':
    main()


text:
# Copyright 2017 The TensorFlow Authors. 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.
# ==============================================================================

"""Tests for object_detection.utils.per_image_evaluation."""

import numpy as np
import tensorflow as tf

from retail_demo.object_detection.utils import per_image_evaluation

class SingleClassTpFpWithDifficultBoxesTest(tf.test.TestCase):

  def setUp(self):
    num_groundtruth_classes = 1
    matching_iou_threshold = 0.5
    nms_iou_threshold = 1.0
    nms_max_output_boxes = 10000
    self.eval = per_image_evaluation.PerImageEvaluation(
        num_groundtruth_classes, matching_iou_threshold, nms_iou_threshold,
        nms_max_output_boxes)

    self.detected_boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3]],
                                   dtype=float)
    self.detected_scores = np.array([0.6, 0.8, 0.5], dtype=float)
    detected_masks_0 = np.array([[0, 1, 1, 0],
                                 [0, 0, 1, 0],
                                 [0, 0, 0, 0]], dtype=np.uint8)
    detected_masks_1 = np.array([[1, 0, 0, 0],
                                 [1, 1, 0, 0],
                                 [0, 0, 0, 0]], dtype=np.uint8)
    detected_masks_2 = np.array([[0, 0, 0, 0],
                                 [0, 1, 1, 0],
                                 [0, 1, 0, 0]], dtype=np.uint8)
    self.detected_masks = np.stack(
        [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)
    self.groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 10, 10]],
                                      dtype=float)
    groundtruth_masks_0 = np.array([[1, 1, 0, 0],
                                    [1, 1, 0, 0],
                                    [0, 0, 0, 0]], dtype=np.uint8)
    groundtruth_masks_1 = np.array([[0, 0, 0, 1],
                                    [0, 0, 0, 1],
                                    [0, 0, 0, 1]], dtype=np.uint8)
    self.groundtruth_masks = np.stack(
        [groundtruth_masks_0, groundtruth_masks_1], axis=0)

  def test_match_to_gt_box_0(self):
    groundtruth_groundtruth_is_difficult_list = np.array([False, True],
                                                         dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [False, False], dtype=bool)
    scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
        self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([False, True, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_mask_match_to_gt_mask_0(self):
    groundtruth_groundtruth_is_difficult_list = np.array([False, True],
                                                         dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [False, False], dtype=bool)
    scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
        self.detected_boxes,
        self.detected_scores,
        self.groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list,
        detected_masks=self.detected_masks,
        groundtruth_masks=self.groundtruth_masks)
    expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([True, False, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_match_to_gt_box_1(self):
    groundtruth_groundtruth_is_difficult_list = np.array([True, False],
                                                         dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [False, False], dtype=bool)
    scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
        self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    expected_scores = np.array([0.8, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([False, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_mask_match_to_gt_mask_1(self):
    groundtruth_groundtruth_is_difficult_list = np.array([True, False],
                                                         dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [False, False], dtype=bool)
    scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
        self.detected_boxes,
        self.detected_scores,
        self.groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list,
        detected_masks=self.detected_masks,
        groundtruth_masks=self.groundtruth_masks)
    expected_scores = np.array([0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([False, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

class SingleClassTpFpWithGroupOfBoxesTest(tf.test.TestCase):

  def setUp(self):
    num_groundtruth_classes = 1
    matching_iou_threshold = 0.5
    nms_iou_threshold = 1.0
    nms_max_output_boxes = 10000
    self.eval = per_image_evaluation.PerImageEvaluation(
        num_groundtruth_classes, matching_iou_threshold, nms_iou_threshold,
        nms_max_output_boxes)

    self.detected_boxes = np.array(
        [[0, 0, 1, 1], [0, 0, 2, 1], [0, 0, 3, 1]], dtype=float)
    self.detected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    detected_masks_0 = np.array([[0, 1, 1, 0],
                                 [0, 0, 1, 0],
                                 [0, 0, 0, 0]], dtype=np.uint8)
    detected_masks_1 = np.array([[1, 0, 0, 0],
                                 [1, 1, 0, 0],
                                 [0, 0, 0, 0]], dtype=np.uint8)
    detected_masks_2 = np.array([[0, 0, 0, 0],
                                 [0, 1, 1, 0],
                                 [0, 1, 0, 0]], dtype=np.uint8)
    self.detected_masks = np.stack(
        [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)

    self.groundtruth_boxes = np.array(
        [[0, 0, 1, 1], [0, 0, 5, 5], [10, 10, 20, 20]], dtype=float)
    groundtruth_masks_0 = np.array([[1, 0, 0, 0],
                                    [1, 0, 0, 0],
                                    [1, 0, 0, 0]], dtype=np.uint8)
    groundtruth_masks_1 = np.array([[0, 0, 1, 0],
                                    [0, 0, 1, 0],
                                    [0, 0, 1, 0]], dtype=np.uint8)
    groundtruth_masks_2 = np.array([[0, 1, 0, 0],
                                    [0, 1, 0, 0],
                                    [0, 1, 0, 0]], dtype=np.uint8)
    self.groundtruth_masks = np.stack(
        [groundtruth_masks_0, groundtruth_masks_1, groundtruth_masks_2], axis=0)

  def test_match_to_non_group_of_and_group_of_box(self):
    groundtruth_groundtruth_is_difficult_list = np.array(
        [False, False, False], dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [False, True, True], dtype=bool)
    expected_scores = np.array([0.8], dtype=float)
    expected_tp_fp_labels = np.array([True], dtype=bool)
    scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
        self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_mask_match_to_non_group_of_and_group_of_box(self):
    groundtruth_groundtruth_is_difficult_list = np.array(
        [False, False, False], dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [False, True, True], dtype=bool)
    expected_scores = np.array([0.6], dtype=float)
    expected_tp_fp_labels = np.array([True], dtype=bool)
    scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
        self.detected_boxes,
        self.detected_scores,
        self.groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list,
        detected_masks=self.detected_masks,
        groundtruth_masks=self.groundtruth_masks)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_match_two_to_group_of_box(self):
    groundtruth_groundtruth_is_difficult_list = np.array(
        [False, False, False], dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [True, False, True], dtype=bool)
    expected_scores = np.array([0.5], dtype=float)
    expected_tp_fp_labels = np.array([False], dtype=bool)
    scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
        self.detected_boxes, self.detected_scores, self.groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_mask_match_two_to_group_of_box(self):
    groundtruth_groundtruth_is_difficult_list = np.array(
        [False, False, False], dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [True, False, True], dtype=bool)
    expected_scores = np.array([0.8], dtype=float)
    expected_tp_fp_labels = np.array([True], dtype=bool)
    scores, tp_fp_labels = self.eval._compute_tp_fp_for_single_class(
        self.detected_boxes,
        self.detected_scores,
        self.groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list,
        detected_masks=self.detected_masks,
        groundtruth_masks=self.groundtruth_masks)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

class SingleClassTpFpNoDifficultBoxesTest(tf.test.TestCase):

  def setUp(self):
    num_groundtruth_classes = 1
    matching_iou_threshold_high_iou = 0.5
    matching_iou_threshold_low_iou = 0.1
    nms_iou_threshold = 1.0
    nms_max_output_boxes = 10000
    self.eval_high_iou = per_image_evaluation.PerImageEvaluation(
        num_groundtruth_classes, matching_iou_threshold_high_iou,
        nms_iou_threshold, nms_max_output_boxes)

    self.eval_low_iou = per_image_evaluation.PerImageEvaluation(
        num_groundtruth_classes, matching_iou_threshold_low_iou,
        nms_iou_threshold, nms_max_output_boxes)

    self.detected_boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3]],
                                   dtype=float)
    self.detected_scores = np.array([0.6, 0.8, 0.5], dtype=float)
    detected_masks_0 = np.array([[0, 1, 1, 0],
                                 [0, 0, 1, 0],
                                 [0, 0, 0, 0]], dtype=np.uint8)
    detected_masks_1 = np.array([[1, 0, 0, 0],
                                 [1, 1, 0, 0],
                                 [0, 0, 0, 0]], dtype=np.uint8)
    detected_masks_2 = np.array([[0, 0, 0, 0],
                                 [0, 1, 1, 0],
                                 [0, 1, 0, 0]], dtype=np.uint8)
    self.detected_masks = np.stack(
        [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)

  def test_no_true_positives(self):
    groundtruth_boxes = np.array([[100, 100, 105, 105]], dtype=float)
    groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
    scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
        self.detected_boxes, self.detected_scores, groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([False, False, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_mask_no_true_positives(self):
    groundtruth_boxes = np.array([[100, 100, 105, 105]], dtype=float)
    groundtruth_masks_0 = np.array([[1, 1, 1, 1],
                                    [1, 1, 1, 1],
                                    [1, 1, 1, 1]], dtype=np.uint8)
    groundtruth_masks = np.stack([groundtruth_masks_0], axis=0)
    groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
    scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
        self.detected_boxes,
        self.detected_scores,
        groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list,
        detected_masks=self.detected_masks,
        groundtruth_masks=groundtruth_masks)
    expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([False, False, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_one_true_positives_with_large_iou_threshold(self):
    groundtruth_boxes = np.array([[0, 0, 1, 1]], dtype=float)
    groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
    scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
        self.detected_boxes, self.detected_scores, groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([False, True, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_mask_one_true_positives_with_large_iou_threshold(self):
    groundtruth_boxes = np.array([[0, 0, 1, 1]], dtype=float)
    groundtruth_masks_0 = np.array([[1, 0, 0, 0],
                                    [1, 1, 0, 0],
                                    [0, 0, 0, 0]], dtype=np.uint8)
    groundtruth_masks = np.stack([groundtruth_masks_0], axis=0)
    groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
    scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
        self.detected_boxes,
        self.detected_scores,
        groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list,
        detected_masks=self.detected_masks,
        groundtruth_masks=groundtruth_masks)
    expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([True, False, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_one_true_positives_with_very_small_iou_threshold(self):
    groundtruth_boxes = np.array([[0, 0, 1, 1]], dtype=float)
    groundtruth_groundtruth_is_difficult_list = np.zeros(1, dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array([False], dtype=bool)
    scores, tp_fp_labels = self.eval_low_iou._compute_tp_fp_for_single_class(
        self.detected_boxes, self.detected_scores, groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([True, False, False], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

  def test_two_true_positives_with_large_iou_threshold(self):
    groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3.5, 3.5]], dtype=float)
    groundtruth_groundtruth_is_difficult_list = np.zeros(2, dtype=bool)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [False, False], dtype=bool)
    scores, tp_fp_labels = self.eval_high_iou._compute_tp_fp_for_single_class(
        self.detected_boxes, self.detected_scores, groundtruth_boxes,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    expected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
    expected_tp_fp_labels = np.array([False, True, True], dtype=bool)
    self.assertTrue(np.allclose(expected_scores, scores))
    self.assertTrue(np.allclose(expected_tp_fp_labels, tp_fp_labels))

class MultiClassesTpFpTest(tf.test.TestCase):

  def test_tp_fp(self):
    num_groundtruth_classes = 3
    matching_iou_threshold = 0.5
    nms_iou_threshold = 1.0
    nms_max_output_boxes = 10000
    eval1 = per_image_evaluation.PerImageEvaluation(num_groundtruth_classes,
                                                    matching_iou_threshold,
                                                    nms_iou_threshold,
                                                    nms_max_output_boxes)
    detected_boxes = np.array([[0, 0, 1, 1], [10, 10, 5, 5], [0, 0, 2, 2],
                               [5, 10, 10, 5], [10, 5, 5, 10], [0, 0, 3, 3]],
                              dtype=float)
    detected_scores = np.array([0.8, 0.1, 0.8, 0.9, 0.7, 0.8], dtype=float)
    detected_class_labels = np.array([0, 1, 1, 2, 0, 2], dtype=int)
    groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3.5, 3.5]], dtype=float)
    groundtruth_class_labels = np.array([0, 2], dtype=int)
    groundtruth_groundtruth_is_difficult_list = np.zeros(2, dtype=float)
    groundtruth_groundtruth_is_group_of_list = np.array(
        [False, False], dtype=bool)
    scores, tp_fp_labels, _ = eval1.compute_object_detection_metrics(
        detected_boxes, detected_scores, detected_class_labels,
        groundtruth_boxes, groundtruth_class_labels,
        groundtruth_groundtruth_is_difficult_list,
        groundtruth_groundtruth_is_group_of_list)
    expected_scores = [np.array([0.8], dtype=float)] * 3
    expected_tp_fp_labels = [np.array([True]), np.array([False]), np.array([True
                                                                           ])]
    for i in range(len(expected_scores)):
      self.assertTrue(np.allclose(expected_scores[i], scores[i]))
      self.assertTrue(np.array_equal(expected_tp_fp_labels[i], tp_fp_labels[i]))

class CorLocTest(tf.test.TestCase):

  def test_compute_corloc_with_normal_iou_threshold(self):
    num_groundtruth_classes = 3
    matching_iou_threshold = 0.5
    nms_iou_threshold = 1.0
    nms_max_output_boxes = 10000
    eval1 = per_image_evaluation.PerImageEvaluation(num_groundtruth_classes,
                                                    matching_iou_threshold,
                                                    nms_iou_threshold,
                                                    nms_max_output_boxes)
    detected_boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3],
                               [0, 0, 5, 5]], dtype=float)
    detected_scores = np.array([0.9, 0.9, 0.1, 0.9], dtype=float)
    detected_class_labels = np.array([0, 1, 0, 2], dtype=int)
    groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3, 3], [0, 0, 6, 6]],
                                 dtype=float)
    groundtruth_class_labels = np.array([0, 0, 2], dtype=int)

    is_class_correctly_detected_in_image = eval1._compute_cor_loc(
        detected_boxes, detected_scores, detected_class_labels,
        groundtruth_boxes, groundtruth_class_labels)
    expected_result = np.array([1, 0, 1], dtype=int)
    self.assertTrue(np.array_equal(expected_result,
                                   is_class_correctly_detected_in_image))

  def test_compute_corloc_with_very_large_iou_threshold(self):
    num_groundtruth_classes = 3
    matching_iou_threshold = 0.9
    nms_iou_threshold = 1.0
    nms_max_output_boxes = 10000
    eval1 = per_image_evaluation.PerImageEvaluation(num_groundtruth_classes,
                                                    matching_iou_threshold,
                                                    nms_iou_threshold,
                                                    nms_max_output_boxes)
    detected_boxes = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3],
                               [0, 0, 5, 5]], dtype=float)
    detected_scores = np.array([0.9, 0.9, 0.1, 0.9], dtype=float)
    detected_class_labels = np.array([0, 1, 0, 2], dtype=int)
    groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3, 3], [0, 0, 6, 6]],
                                 dtype=float)
    groundtruth_class_labels = np.array([0, 0, 2], dtype=int)

    is_class_correctly_detected_in_image = eval1._compute_cor_loc(
        detected_boxes, detected_scores, detected_class_labels,
        groundtruth_boxes, groundtruth_class_labels)
    expected_result = np.array([1, 0, 0], dtype=int)
    self.assertTrue(np.array_equal(expected_result,
                                   is_class_correctly_detected_in_image))

if __name__ == '__main__':
  tf.test.main()


text:
import numpy as np
import scipy.io as sio

def generate_mmc_center(var, dim_dense, num_class):

    mmc_centers = np.zeros((num_class, dim_dense))
    mmc_centers[0][0] = 1
    for i in range(1,num_class):
        for j in range(i):
            mmc_centers[i][j] = - (1/(num_class-1) + np.dot(mmc_centers[i],mmc_centers[j])) / mmc_centers[j][j]
        mmc_centers[i][i] = np.sqrt(np.absolute(1 - np.linalg.norm(mmc_centers[i]))**2)
    for k in range(num_class):
        mmc_centers[k] = var * mmc_centers[k]

    return mmc_centers

var, dim_dense, num_class = 10, 256, 20
mmc_centers_global = generate_mmc_center(var, dim_dense, num_class)
print("mmc_centers global shape- ", mmc_centers_global.shape)
print("mmc_centers global:",mmc_centers_global)

final_centers=np.zeros(shape=(100,256))

var_2, dim_dense_l, sub_class = 1, 256, 5
mmc_centers_local = generate_mmc_center(var_2, dim_dense_l, sub_class)
print("mmc center local shape- ", mmc_centers_local.shape)
print("mmc_centers local:",mmc_centers_local)

for i in range(100):
    final_centers[i]=mmc_centers_global[i//5]+mmc_centers_local[i%5]

print(final_centers)

sio.savemat('./cifar100_hmmc_tree_featuredim'+str(dim_dense)+'_class'+str(num_class)+'.mat', {'mean_logits': final_centers})

reason: import numpy,import scipy


text:
#  Copyright 2021 Institute of Advanced Research in Artificial Intelligence (IARAI) GmbH.
#  IARAI licenses this file to You under the Apache License, Version 2.0
#  (the "License"); you may not use this file except in compliance with
#  the License. You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
import numpy as np
import pytest
import torch

from baselines.naive_weighted_average import NaiveWeightedAverage
from baselines.naive_weighted_average_with_sparsity_cutoff import NaiveWeightedAverageWithSparsityCutoff
from data.data_layout import volume_channel_indices

@pytest.mark.parametrize(
    "num_zero_volume_slots,num_zeroize_cells,num_slots_all_volumes_zero_cutoff,expected_same",
    [(3, 0, 2, True), (3, 20, 2, False), (2, 20, 2, False), (4, 20, 5, True), (5, 20, 5, False), (8, 20, 5, False)],
)
def test_test_naive_weighted_average_with_zero_sparsity_cutoff_is_same_as_naive_weighted_average(
    num_zero_volume_slots: int, num_zeroize_cells: int, num_slots_all_volumes_zero_cutoff: int, expected_same: bool
):
    x = torch.randint(low=0, high=256, size=(8, 12, 495, 436, 8))
    for _ in range(num_zeroize_cells):
        batch_idx = np.random.choice(8)
        r = np.random.choice(495)
        c = np.random.choice(436)
        time_indices = np.random.choice(12, size=num_zero_volume_slots, replace=False)
        for time_idx in time_indices:
            for ch in volume_channel_indices:
                x[batch_idx, time_idx, r, c, ch] = 0

    m = NaiveWeightedAverageWithSparsityCutoff(num_slots_all_volumes_zero_cutoff=num_slots_all_volumes_zero_cutoff)
    y = m.forward(x)
    y_baseline = NaiveWeightedAverage().forward(x)
    expected_shape = (8, 6, 495, 436, 8)
    assert y.shape == expected_shape, f"expected shape {expected_shape}, actual {y.shape}"
    assert np.allclose(y, y_baseline) == expected_same


text:
from raymon.profiling.components import DataType
import pytest
import json
from raymon import ModelProfile
from raymon import InputComponent
from raymon import IntStats, FloatStats, CategoricStats
from raymon.profiling.extractors.vision.similarity import FixedSubpatchSimilarity
from raymon.profiling.extractors.structured.scoring import ClassificationErrorType
from raymon.profiling.extractors.vision import DN2AnomalyScorer, AvgIntensity, Sharpness
from raymon.profiling.extractors.structured import ElementExtractor
import numpy as np
import pandas as pd

def test_schema_jcr():
    extractor = FixedSubpatchSimilarity(
        patch={"x0": 0, "y0": 0, "x1": 64, "y1": 64}, refs=["adf8d224cb8786cc"], nrefs=1
    )
    stats = FloatStats(min=0, max=1, mean=0.5, std=0.02, percentiles=range(101))
    component = InputComponent(name="testcomponent", extractor=extractor, stats=stats, dtype=DataType.FLOAT)
    component2 = InputComponent(name="testcomponent2", extractor=extractor, stats=stats, dtype=DataType.FLOAT)

    schema = ModelProfile(name="Testing", version="1.0.0", components=[component, component2])
    schema_jcr = schema.to_jcr()
    assert len(schema_jcr["components"]) == 2
    jsonstr = json.dumps(schema_jcr)  # Should not throw an error
    assert len(jsonstr) > 0
    schema_restored = ModelProfile.from_jcr(schema_jcr)

    assert schema.name == schema_restored.name
    assert schema.version == schema_restored.version
    assert all([c1 == c2 for (c1, c2) in zip(schema.components.keys(), schema_restored.components.keys())])

def test_score_json():
    extractor = ClassificationErrorType(positive=1)
    jcr = extractor.to_jcr()
    dumped = json.dumps(jcr)
    assert isinstance(dumped, str)

def test_profile_with_vision_data(images, tmp_path):
    profile = ModelProfile(
        name="retinopathy",
        version="2.0.0",
        components=[
            InputComponent(
                name="sharpness",
                extractor=Sharpness(),
                dtype=DataType.FLOAT,
            ),
            InputComponent(
                name="intensity",
                extractor=AvgIntensity(),
                dtype=DataType.FLOAT,
            ),
            InputComponent(
                name="outlierscore",
                extractor=DN2AnomalyScorer(k=5),
                dtype=DataType.FLOAT,
            ),
        ],
    )
    # Build profile: profile
    profile.build(input=images)
    # Save profile
    profile.save(str(tmp_path))
    # Load profile: loaded_profile
    loaded_profile = ModelProfile().load(str(tmp_path) + f"/{profile.group_idfr}.json")
    # Create the jcr of the profile
    loaded_profile_jcr = loaded_profile.to_jcr()
    # jcr checks
    assert len(loaded_profile_jcr["components"]) == 3
    jsonstr = json.dumps(loaded_profile_jcr)  # Should not throw an error
    assert len(jsonstr) > 0
    assert (
        loaded_profile_jcr["components"]["intensity"]["state"]["extractor"]["class"]
        == "raymon.profiling.extractors.vision.intensity.AvgIntensity"
    )
    assert (
        loaded_profile_jcr["components"]["sharpness"]["state"]["extractor"]["class"]
        == "raymon.profiling.extractors.vision.sharpness.Sharpness"
    )
    assert loaded_profile_jcr["components"]["outlierscore"]["state"]["extractor"]["state"]["k"] == 5
    # from_jcr checks
    profile_restored = profile.from_jcr(loaded_profile_jcr)
    assert loaded_profile.name == profile_restored.name
    assert loaded_profile.version == profile_restored.version
    assert all([c1 == c2 for (c1, c2) in zip(loaded_profile.components.keys(), profile_restored.components.keys())])

def test_profile_with_structured_data(tmp_path):
    profile = ModelProfile(
        name="vector",
        version="1.0.0",
        components=[InputComponent(name="actual", extractor=ElementExtractor(element="num1"), dtype=DataType.INT)],
    )
    # Sample data: df
    cols = {
        "num1": list(range(10)),
        "cat1": ["a"] * 5 + ["b"] * 5,
        "cat2": ["c"] * 5 + ["d"] * 5,
        "num2": [0.2] * 10,
    }
    df = pd.DataFrame(data=cols)
    # Build profile: profile
    profile.build(input=df)
    # Save profile
    profile.save(str(tmp_path))
    # Load profile: loaded_profile
    loaded_profile = ModelProfile().load(str(tmp_path) + f"/{profile.group_idfr}.json")
    # Create the jcr of the profile
    loaded_profile_jcr = loaded_profile.to_jcr()
    # jcr checks
    assert len(loaded_profile_jcr["components"]) == 1
    jsonstr = json.dumps(loaded_profile_jcr)  # Should not throw an error
    assert len(jsonstr) > 0
    assert loaded_profile_jcr["components"]["actual"]["state"]["extractor"]["state"]["element"] == "num1"
    # from_jcr checks
    profile_restored = profile.from_jcr(loaded_profile_jcr)
    assert loaded_profile.name == profile_restored.name
    assert loaded_profile.version == profile_restored.version
    assert all([c1 == c2 for (c1, c2) in zip(loaded_profile.components.keys(), profile_restored.components.keys())])

max_stars_repo_name: raymon-ai/raymon

max_issues_repo_name: raymon-ai/raymon

max_forks_repo_name: raymon-ai/raymon


text:
#Variational Monte Carlo program for the Helium atom which utilizes the code in MC.cpp - parallelized with pypar
#This implementation is based on a master-slave strategy for distributing work, instead of splitting up
#each integral on all the nodes.
#Written by Magnar K. Bugge

from math import sqrt
from numpy import linspace
import pypar
import MC

nprocs = pypar.size() #Number of processes
myid = pypar.rank() #Id of this process

class result:
    alpha = 0.0
    E = 0.0
    sigma = 0.0
    error = 0.0
    acceptance = 0.0
    id = 0 #id of the process who did this job

MCcycles = 100000000 #Number of MC cycles
MCcycles2 = 10000 #Number of MC cycles for determination of optimal delta
delta_min = .01 #Minimum length of Metropolis step
delta_max = 2.0 #Maximum length of Metropolis step
tolerance = .01
idum = MC.seed() * (myid+1) #Seed for random number generator (different for each process)

#Function which should be close to zero for optimal delta
def difference(delta):
    x = MC.runMC(MCcycles2,delta,idum,alpha)
    return x.accepted*1.0/MCcycles2 - .5 #We want 50% accepted moves

#Function for sorting the results from small to large alpha
def sort(results):
    sorted_results = []

    for i in xrange(len(values)):
        sorted_results.append(result())
        sorted_results[i].alpha = values[i]
        j = 0
        while results[j].alpha != sorted_results[i].alpha:
            j += 1
        sorted_results[i].E = results[j].E
        sorted_results[i].sigma = results[j].sigma
        sorted_results[i].error = results[j].error
        sorted_results[i].acceptance = results[j].acceptance
        #add a line here if you also want to keep information about who (id) did what

    return sorted_results

#Array of alpha values
values = linspace(1.4,2.5,23) #(alpha values)

#Code for the master
if myid == 0:

    results = [] #List of results
    ndistributed = 0 #Number of distributed jobs
    nreceived = 0 #Number of received jobs

    #The master distributes jobs to the slaves. First, he sends one job to each slave.
    for i in xrange(1,nprocs):
        pypar.send(values[ndistributed],destination=i)
        ndistributed += 1

    #Then he goes in a loop, waiting for results and sending out new jobs
    while nreceived < len(values):

        #Receive a result
        results.append(pypar.receive(source=pypar.any_source))
        nreceived += 1

        #If there is more jobs to do, send the next one, if not, send a termination signal (alpha = 0)
        if ndistributed < len(values):
            pypar.send(values[ndistributed],destination=results[-1].id)
            ndistributed += 1
        else:
            pypar.send(0.0,destination=results[-1].id)

    #As the results are generally received out of order, they need to be sorted
    results = sort(results)

    #Finally, master writes the results to file
    outfile = open('data','w')
    for i in xrange(len(results)):
        outfile.write('%f %f %f %f %f\n' %(results[i].alpha,results[i].E,results[i].sigma,results[i].error,results[i].acceptance))
    outfile.close()

#Code for the slaves
if myid != 0:

    #Run in a loop receiving and returning jobs as long as the termination signal alpha=0 is not given
    while True:

        #Receive a job (or possibly the termination signal)
        alpha = pypar.receive(source=0)

        #If it isn't the termination signal, do the job and return the result
        if alpha != 0.0:

            #Determination of optimal delta value (for each alpha), i.e.
            #finding the zero-point of the difference function by the bisection method
            minimum = delta_min
            maximum = delta_max
            while maximum - minimum > tolerance:
                if difference(minimum)*difference((minimum+maximum)/2) < 0:
                    maximum = (minimum + maximum) / 2
                else:
                    minimum = (minimum + maximum) / 2
            delta = (minimum + maximum) / 2

            #Run MC calculation (store results in x)
            x = MC.runMC(MCcycles,delta,idum,alpha)
            idum = x.idum

            #Calculate statistics and store them in a result object
            thisresult = result()
            thisresult.alpha = alpha
            thisresult.id = myid

            thisresult.E = x.sum / x.N
            E2 = x.squaresum / x.N
            thisresult.sigma = sqrt(E2 - thisresult.E**2)
            thisresult.acceptance = x.accepted*1.0/MCcycles
            thisresult.error = thisresult.sigma / sqrt(x.N)

            #Send to master
            pypar.send(thisresult,destination=0)

        else:
            break

pypar.finalize()

max_stars_repo_name: kimrojas/ComputationalPhysicsMSU

reason: from numpy


text:
import numpy as np

from glue.core import Data
from jdaviz import Application
from spectral_cube import SpectralCube

from ..gaussian_smooth import GaussianSmooth

def test_linking_after_spectral_smooth(spectral_cube_wcs):

    app = Application(configuration="cubeviz")
    dc = app.data_collection
    dc.append(Data(x=np.ones((3, 4, 5)), label='test', coords=spectral_cube_wcs))

    gs = GaussianSmooth(app=app)

    gs._on_data_selected({'new': 'test'})
    gs.stddev = '3.2'
    gs.vue_spectral_smooth()

    assert len(dc) == 2
    assert dc[1].label == 'Smoothed test'
    assert len(dc.external_links) == 1

    assert dc.external_links[0].cids1[0] is dc[0].world_component_ids[0]
    assert dc.external_links[0].cids2[0] is dc[1].world_component_ids[0]

def test_spatial_convolution(spectral_cube_wcs):

    app = Application()
    dc = app.data_collection
    dc.append(Data(x=np.ones((3, 4, 5)), label='test', coords=spectral_cube_wcs))

    gs = GaussianSmooth(app=app)
    gs._on_data_selected({'new': 'test'})
    gs.stddev = '3'
    gs.vue_spatial_convolution()

    assert len(dc) == 2
    assert dc[1].label == "Smoothed test"
    assert dc["Smoothed test"].get_object(cls=SpectralCube).shape == (3,4,5)

max_stars_repo_name: maartenbreddels/jdaviz

max_issues_repo_name: maartenbreddels/jdaviz

max_forks_repo_name: maartenbreddels/jdaviz


text:
import kmerexpr.multinomial_simplex_model as mm
import kmerexpr.transcriptome_reader as tr
import numpy as np
import os
import pytest
from scipy import optimize

from test.conftest import check_gradient
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
DATA_PATH = os.path.join(HERE, "data")

def test1():
    ISO_FILE = os.path.join(DATA_PATH, "test4.fsa")
    X_FILE = os.path.join(DATA_PATH, "x4_csr.npz")
    K = 2
    tr.transcriptome_to_x(K, ISO_FILE, X_FILE)
    y_test = np.random.poisson(5, 4 ** K)
    model = mm.multinomial_simplex_model(X_FILE, y_test)
    os.remove(X_FILE)
    assert model.T == 3
    assert model.M == 16
    alpha = 0.5 * np.ones(model.T)
    theta_test = np.random.dirichlet(alpha)
    check_gradient(model, theta_test)

@pytest.mark.slow
def test_human_transcriptome():
    ISO_FILE = os.path.join(ROOT, "data", "GRCh38_latest_rna.fna")
    X_FILE = os.path.join(DATA_PATH, "xgrch38_csr.npz")
    K = 3  # make = 10 for full test
    M = 4 ** K
    T = 81456  # Rob: It's coming out at 81456 in my test. It was 80791 before
    tr.transcriptome_to_x(K, ISO_FILE, X_FILE)
    print("finished writing x to file")
    y_test = np.random.poisson(20, 4 ** K)
    print("reading in model")
    model = mm.multinomial_simplex_model(X_FILE, y_test)
    os.remove(X_FILE)
    assert model.T == T
    assert model.M == M
    alpha = 0.1 * np.ones(model.T)
    theta_test = np.random.dirichlet(alpha)
    print("checking gradient")
    check_gradient(model, theta_test)

def test_optimizer():
    ISO_FILE = os.path.join(DATA_PATH, "test4.fsa")
    X_FILE = os.path.join(DATA_PATH, "x4_csr.npz")
    print(X_FILE)
    K = 2
    tr.transcriptome_to_x(K, ISO_FILE, X_FILE)
    y_test = np.random.poisson(5, 4 ** K)
    model = mm.multinomial_simplex_model(X_FILE, y_test)
    theta0 = np.random.dirichlet( np.ones(model.T))
    # Get high precision solution
    dict_sol = model.fit(theta0, n_iters=2000)
    # Compare against softmax+lbfgs solution
    import kmerexpr.multinomial_model as mm_softmax
    model_softmax = mm_softmax.multinomial_model(X_FILE, y_test)
    theta0_softmax = np.random.normal(0, 1, model.T)
    dict_sol_softmax = model_softmax.fit(theta0_softmax, factr=0.01, gtol=1e-17, n_iters=4000)
    assert model_softmax.logp_grad(dict_sol['x'])[0]+1e-06 > model_softmax.logp_grad(dict_sol_softmax['x'])[0]
    os.remove(X_FILE)

max_stars_repo_name: bob-carpenter/kmers

max_issues_repo_name: bob-carpenter/kmers

max_forks_repo_name: bob-carpenter/kmers

reason: import numpy,from scipy


text:
import io
import numpy as np
from PIL import Image
from PyQt5 import QtGui, QtWidgets, QtCore, uic

class Painter(QtWidgets.QWidget):
    def __init__(self, color_picker):
        super().__init__()
        self.color_picker = color_picker
        self.chosen_point = []
        self._hint = QtGui.QPixmap(500, 500)
        self._hint.fill(QtCore.Qt.white)

        dummy = np.require(
            np.zeros(shape=[500, 500, 3],
                     dtype=np.uint8), np.uint8, 'C')
        self.line_np = dummy
        dummy = QtGui.QImage(dummy, 500, 500, QtGui.QImage.Format_RGB888)
        self._line = QtGui.QPixmap(dummy)

        self.setMinimumSize(520, 520)
        self.last_x, self.last_y = None, None

        self.pen = QtGui.QPen()
        self.pen.setWidth(4)
        self.pen.setColor(QtCore.Qt.red)

    def QImageToCvMat(self, incomingImage):
        '''  Converts a QImage into an opencv MAT format  '''

        incomingImage = incomingImage.convertToFormat(
            QtGui.QImage.Format.Format_RGBA8888)

        width = incomingImage.width()
        height = incomingImage.height()

        ptr = incomingImage.constBits()
        ptr.setsize(height * width * 4)
        arr = np.frombuffer(ptr, np.uint8).reshape((height, width, 4))
        return arr

    def QImageToImage(self, image: QtGui.QImage):
        buf = QtCore.QBuffer()
        image.save(buf, 'png')
        return Image.open(io.BytesIO(buf.data()))

    def get_image(self):
        size = self._line.size()
        hint_map = QtGui.QPixmap(size)
        hint_map.fill(QtCore.Qt.transparent)
        painter = QtGui.QPainter(hint_map)

        for pos in self.chosen_point:
            self.pen.setColor(pos['color'])
            self.pen.setWidth(pos['width'])
            painter.setPen(self.pen)
            painter.drawPoint(pos['pos'])

        painter.end()
        hint = self.QImageToImage(hint_map.toImage())
        hint = np.array(hint)
        return self.line_np, hint

    @staticmethod
    def create_pixmap(image: np.ndarray):
        image = QtGui.QImage(image, image.shape[1],
                             image.shape[0], image.shape[1] * 3,
                             QtGui.QImage.Format_RGB888)
        return QtGui.QPixmap(image)

    def set_line(self, image: Image.Image, parent) -> None:
        image = image.convert('RGB')
        w, h = image.size
        image = np.array(image)

        self.line_np = image
        self._line = self.create_pixmap(image)
        self.setFixedHeight(h)
        self.setFixedWidth(w)
        parent.resize(parent.minimumSize())

    def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
        painter = QtGui.QPainter(self)
        painter.drawPixmap(self.rect(), self._line)
        color = self.pen.color()
        size = self.pen.width()
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)

        for pos in self.chosen_point:
            self.pen.setColor(pos['color'])
            self.pen.setWidth(pos['width'])
            painter.setPen(self.pen)
            painter.drawPoint(pos['pos'])

        self.pen.setColor(color)
        self.pen.setWidth(size)

    def mouseReleaseEvent(self, e: QtGui.QMouseEvent) -> None:
        if e.button() == QtCore.Qt.LeftButton:
            data = {
                'pos': e.pos(),
                'color': self.pen.color(),
                'width': self.pen.width()
            }
            self.chosen_point.append(data)
        self.update()

    def mousePressEvent(self, e):
        if e.button() == QtCore.Qt.RightButton:
            self.remove()
        self.update()

    def remove(self):
        if len(self.chosen_point) > 0:
            self.chosen_point.pop()
        self.update()

max_stars_repo_name: System-SW/SketchColorization

max_issues_repo_name: System-SW/SketchColorization