text:
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

## Grayscale
def BGR2GRAY(img):
    gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
    gray = gray.astype(np.uint8)
    return gray

# Sobel filter
def sobel_filter(img, K_size=3):
    H, W = img.shape

    # Zero padding
    pad = K_size // 2
    out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float32)
    out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float32)
    tmp = out.copy()

    outv = out.copy()
    outh = out.copy()

    # vertical kernel
    Kv = [[1., 2., 1.], [0., 0., 0.], [-1., -2., -1.]]
    # horizontal kernel
    Kh = [[1., 0., -1.], [2., 0., -2.], [1., 0., -1.]]

    ## filtering
    for y in range(H):
        for x in range(W):
           outv[pad+y, pad+x] = np.sum(tmp[y:y+K_size, x:x+K_size] * Kv)
           outh[pad+y, pad+x] = np.sum(tmp[y:y+K_size, x:x+K_size] * Kh)

    # outv = np.clip(outv, 0, 255)
    # outh = np.clip(outh, 0, 255)

    outv = outv[pad:pad+H, pad:pad+W]
    outh = outh[pad:pad+H, pad:pad+W]

    return outh, outv

# Gaussian filter
def gaussian_filter(img, K_size=3, sigma=1.3):
    if len(img.shape) == 3:
        H, W, C = img.shape
    else:
        img = np.expand_dims(img, axis=-1)
        H, W, C = img.shape

    ## Zero padding
    pad = K_size // 2
    out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float)
    out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)

    ## prepare Kernel
    K = np.zeros((K_size, K_size), dtype=np.float)
    for x in range(-pad, -pad + K_size):
        for y in range(-pad, -pad + K_size):
            K[y + pad, x + pad] = np.exp( -(x ** 2 + y ** 2) / (2 * (sigma ** 2)))
    K /= (2 * np.pi * sigma * sigma)
    K /= K.sum()

    tmp = out.copy()

    # filtering
    for y in range(H):
        for x in range(W):
            for c in range(C):
                out[pad + y, pad + x, c] = np.sum(tmp[y: y + K_size, x: x + K_size, c] * K)

    out = out[pad: pad + H, pad: pad + W]

    return out

# Harris corner detection
def Harris_corner_step1(img):
    # Step 1: Gray scaling img
    gray = BGR2GRAY(img)

    # Step 2: Get Hessian matrix
    Ix, Iy = sobel_filter(gray)
    Ix2 = Ix ** 2
    Iy2 = Iy ** 2
    Ixy = Ix * Iy

    # Step 3: Apply Gaussian filter on Hessian matrix
    Ix2_gf = gaussian_filter(Ix2, K_size=3, sigma=3)
    Iy2_gf = gaussian_filter(Iy2, K_size=3, sigma=3)
    Ixy_gf = gaussian_filter(Ixy, K_size=3, sigma=3)

    # Print and save result
    plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2)

    title = ["Ix^2", "Iy^2", "Ixy"]
    result = [Ix2_gf, Iy2_gf, Ixy_gf]
    for i in range(len(title)):
        plt.subplot(1,len(title),i+1)
        plt.imshow(result[i], cmap='gray')
        plt.title(title[i])
        plt.axis("off")

    plt.savefig("Myresult/out82.png", dpi=326)
    plt.show()

# Read image
img = cv2.imread("Jeanne.jpg")

# Process img
Harris_corner_step1(img)

max_stars_repo_path: Question_81_90/Harris Corner Detection (Step1) Sobel + Gausian.py


text:
import numpy as np

from rl.np.distributions import DiagonalGaussianProbabilityDistribution

class DiagonalGaussianProbabilityDistributionType:

    def __init__(self, shape):
        self._shape = shape

    def probability_distribution(self, mean):
        return DiagonalGaussianProbabilityDistribution(mean)

    def parameter_shape(self):
        return self._shape

    def sample_shape(self):
        return ()

    def sample_dtype(self):
        return np.int32

ext: py

max_stars_repo_name: djjh/reinforcement-learning-labs

max_issues_repo_name: djjh/reinforcement-learning-labs

max_forks_repo_name: djjh/reinforcement-learning-labs

reason: import numpy


text:
import sys
import os
from io import StringIO
from pkg_resources import resource_filename
from textwrap import dedent
from tempfile import TemporaryDirectory

from unittest import mock
import pytest

from wqio.tests import helpers

import numpy
from matplotlib import pyplot

from pybmpdb import summary

mock_figure = mock.Mock(spec=pyplot.Figure)

PYTHON2 = sys.version_info.major == 2
SKIP_DB = True  # pyodbc is None or os.name == 'posix'

def get_data_file(filename):
    return resource_filename("pybmpdb.tests._data", filename)

def get_tex_file(filename):
    return resource_filename("pybmpdb.tex", filename)

class mock_parameter(object):
    def __init__(self):
        self.name = "Carbon Dioxide"
        self.tex = r"$[\mathrm{CO}_2]$"
        self.units = "mg/L"

    def paramunit(self, *args, **kwargs):
        return "Carbon Dioxide (mg/L)"

class mock_location(object):
    def __init__(self, include):
        self.N = 25
        self.ND = 5
        self.min = 0.123456
        self.max = 123.456
        self.mean = 12.3456
        self.mean_conf_interval = numpy.array([-1, 1]) + self.mean
        self.logmean = 12.3456
        self.logmean_conf_interval = numpy.array([-1, 1]) + self.logmean
        self.geomean = 12.3456
        self.geomean_conf_interval = numpy.array([-1, 1]) + self.geomean
        self.std = 4.56123
        self.logstd = 4.56123
        self.cov = 5.61234
        self.skew = 6.12345
        self.pctl25 = 0.612345
        self.median = 1.23456
        self.median_conf_interval = numpy.array([-1, 1]) + self.median
        self.pctl75 = 2.34561
        self.include = include
        self.exclude = not self.include

class mock_dataset(object):
    def __init__(self, infl_include, effl_include):
        self.influent = mock_location(infl_include)
        self.effluent = mock_location(effl_include)

        self.n_pairs = 22
        self.wilcoxon_p = 0.0005
        self.mannwhitney_p = 0.456123
        self.definition = {"parameter": mock_parameter(), "category": "testbmp"}
        self.scenario = (infl_include, effl_include)

    def scatterplot(self, *args, **kwargs):
        return mock_figure()

    def statplot(self, *args, **kwargs):
        return mock_figure()

@pytest.fixture(params=[(True, True), (True, False), (False, False), (False, True)])
def dset_sum(request):
    ds = mock_dataset(request.param[0], request.param[1])
    return summary.DatasetSummary(ds, "Metals", "testfigpath")

def test_DatasetSummary_paramgroup(dset_sum):
    assert dset_sum.paramgroup == "Metals"

def test_DatasetSummary_ds(dset_sum):
    assert isinstance(dset_sum.ds, mock_dataset)

def test_DatasetSummary_parameter(dset_sum):
    assert dset_sum.parameter is dset_sum.ds.definition["parameter"]

def test_DatasetSummary_bmp(dset_sum):
    assert dset_sum.bmp is dset_sum.ds.definition["category"]

def test_DatasetSummary_latex_file_name(dset_sum):
    assert dset_sum.latex_file_name == "metalstestbmpcarbondioxide"

def test_DatasetSummary__tex_table_row_basic(dset_sum):
    expected = {
        (
            True,
            True,
        ): r"""
                \midrule
                The Medians & 1.23 & 1.23 \\""",
        (
            True,
            False,
        ): r"""
                \midrule
                The Medians & 1.23 & NA \\""",
        (
            False,
            True,
        ): r"""
                \midrule
                The Medians & NA & 1.23 \\""",
        (
            False,
            False,
        ): r"""
                \midrule
                The Medians & NA & NA \\""",
    }
    result_row = dset_sum._tex_table_row("The Medians", "median")
    assert result_row == expected[dset_sum.ds.scenario]

def test_DatasetSummary__tex_table_row_forceint(dset_sum):
    expected = {
        (
            True,
            True,
        ): r"""
                \midrule
                Counts & 25 & 25 \\""",
        (
            True,
            False,
        ): r"""
                \midrule
                Counts & 25 & NA \\""",
        (
            False,
            True,
        ): r"""
                \midrule
                Counts & NA & 25 \\""",
        (
            False,
            False,
        ): r"""
                \midrule
                Counts & NA & NA \\""",
    }
    result_row = dset_sum._tex_table_row("Counts", "N", forceint=True, sigfigs=1)
    assert result_row == expected[dset_sum.ds.scenario]

def test_DatasetSummary__tex_table_row_advanced(dset_sum):
    expected = {
        (
            True,
            True,
        ): r"""
                \toprule
                Mean CI & (11; 13) & (11; 13) \\""",
        (
            True,
            False,
        ): r"""
                \toprule
                Mean CI & (11; 13) & NA \\""",
        (
            False,
            True,
        ): r"""
                \toprule
                Mean CI & NA & (11; 13) \\""",
        (
            False,
            False,
        ): r"""
                \toprule
                Mean CI & NA & NA \\""",
    }
    result_row = dset_sum._tex_table_row("Mean CI", "mean_conf_interval", rule="top", twoval=True, ci=True, sigfigs=2)
    assert result_row == expected[dset_sum.ds.scenario]

def test_DatasetSummary__text_table_row_twoattrs(dset_sum):
    expected = {
        (
            True,
            True,
        ): r"""
                \midrule
                Quartiles & 0.612; 2.35 & 0.612; 2.35 \\""",
        (
            True,
            False,
        ): r"""
                \midrule
                Quartiles & 0.612; 2.35 & NA \\""",
        (
            False,
            True,
        ): r"""
                \midrule
                Quartiles & NA & 0.612; 2.35 \\""",
        (
            False,
            False,
        ): r"""
                \midrule
                Quartiles & NA & NA \\""",
    }
    result_row = dset_sum._tex_table_row("Quartiles", ["pctl25", "pctl75"], twoval=True)
    assert result_row == expected[dset_sum.ds.scenario]

def test_DatasetSummary__make_tex_figure(dset_sum):
    fig = dset_sum._make_tex_figure("testfig.png", "test caption")
    known_fig = (
        r"""
        \begin{figure}[hb]   % FIGURE
            \centering
            \includegraphics[scale=1.00]{testfig.png}
            \caption{test caption}
        \end{figure} \clearpage"""
        + "\n"
    )
    assert fig == known_fig

def test_DatasetSummary_makeTexInput(dset_sum, expected_latext_input):
    result = dset_sum.makeTexInput("test table title")
    helpers.assert_bigstring_equal(result, expected_latext_input[dset_sum.ds.scenario])

def test_DatasetSummary__make_tex_table(dset_sum):
    if dset_sum.ds.scenario == (True, True):
        expected = (
            r"""
        \begin{table}[h!]
            \caption{test title}
            \centering
            \begin{tabular}{l l l l l}
                \toprule
                \textbf{Statistic} & \textbf{Inlet} & \textbf{Outlet} \\
                \toprule
                Count & 25 & 25 \\
                \midrule
                Number of NDs & 5 & 5 \\
                \midrule
                Min; Max & 0.123; 123 & 0.123; 123 \\
                \midrule
                Mean & 12.3 & 12.3 \\
                %%
                (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                \midrule
                Standard Deviation & 4.56 & 4.56 \\
                \midrule
                Log. Mean & 12.3 & 12.3 \\
                %%
                (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                \midrule
                Log. Standard Deviation & 4.56 & 4.56 \\
                \midrule
                Geo. Mean & 12.3 & 12.3 \\
                %%
                (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                \midrule
                Coeff. of Variation & 5.61 & 5.61 \\
                \midrule
                Skewness & 6.12 & 6.12 \\
                \midrule
                Median & 1.23 & 1.23 \\
                %%
                (95\% confidence interval) & (0.235; 2.23) & (0.235; 2.23) \\
                \midrule
                Quartiles & 0.612; 2.35 & 0.612; 2.35 \\
                \toprule
                Number of Pairs & \multicolumn{2}{c} {22} \\
                \midrule
                Wilcoxon p-value & \multicolumn{2}{c} {$<0.001$} \\
                \midrule
                Mann-Whitney p-value & \multicolumn{2}{c} {0.456} \\
                \bottomrule
            \end{tabular}
        \end{table}"""
            + "\n"
        )
        result = dset_sum._make_tex_table("test title")
        helpers.assert_bigstring_equal(result, expected)
    else:
        pass

@pytest.fixture
def expected_latext_input():
    return {
        (False, False): "",
        (True, False): "",
        (
            True,
            True,
        ): r"""\subsection{testbmp}
        \begin{table}[h!]
            \caption{test table title}
            \centering
            \begin{tabular}{l l l l l}
                \toprule
                \textbf{Statistic} & \textbf{Inlet} & \textbf{Outlet} \\
                \toprule
                Count & 25 & 25 \\
                \midrule
                Number of NDs & 5 & 5 \\
                \midrule
                Min; Max & 0.123; 123 & 0.123; 123 \\
                \midrule
                Mean & 12.3 & 12.3 \\
                %%
                (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                \midrule
                Standard Deviation & 4.56 & 4.56 \\
                \midrule
                Log. Mean & 12.3 & 12.3 \\
                %%
                (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                \midrule
                Log. Standard Deviation & 4.56 & 4.56 \\
                \midrule
                Geo. Mean & 12.3 & 12.3 \\
                %%
                (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                \midrule
                Coeff. of Variation & 5.61 & 5.61 \\
                \midrule
                Skewness & 6.12 & 6.12 \\
                \midrule
                Median & 1.23 & 1.23 \\
                %%
                (95\% confidence interval) & (0.235; 2.23) & (0.235; 2.23) \\
                \midrule
                Quartiles & 0.612; 2.35 & 0.612; 2.35 \\
                \toprule
                Number of Pairs & \multicolumn{2}{c} {22} \\
                \midrule
                Wilcoxon p-value & \multicolumn{2}{c} {$<0.001$} \\
                \midrule
                Mann-Whitney p-value & \multicolumn{2}{c} {0.456} \\
                \bottomrule
            \end{tabular}
        \end{table}

        \begin{figure}[hb]   % FIGURE
            \centering
            \includegraphics[scale=1.00]{testfigpath/statplot/metalstestbmpcarbondioxidestats.pdf}
            \caption{Box and Probability Plots of Carbon Dioxide at testbmp BMPs}
        \end{figure}

        \begin{figure}[hb]   % FIGURE
            \centering
            \includegraphics[scale=1.00]{testfigpath/scatterplot/metalstestbmpcarbondioxidescatter.pdf}
            \caption{Influent vs. Effluent Plots of Carbon Dioxide at testbmp BMPs}
        \end{figure} \clearpage"""
        + "\n",
        (
            False,
            True,
        ): r"""\subsection{testbmp}
        \begin{table}[h!]
            \caption{test table title}
            \centering
            \begin{tabular}{l l l l l}
                \toprule
                \textbf{Statistic} & \textbf{Inlet} & \textbf{Outlet} \\
                \toprule
                Count & NA & 25 \\
                \midrule
                Number of NDs & NA & 5 \\
                \midrule
                Min; Max & NA & 0.123; 123 \\
                \midrule
                Mean & NA & 12.3 \\
                %%
                (95\% confidence interval) & NA & (11.3; 13.3) \\
                \midrule
                Standard Deviation & NA & 4.56 \\
                \midrule
                Log. Mean & NA & 12.3 \\
                %%
                (95\% confidence interval) & NA & (11.3; 13.3) \\
                \midrule
                Log. Standard Deviation & NA & 4.56 \\
                \midrule
                Geo. Mean & NA & 12.3 \\
                %%
                (95\% confidence interval) & NA & (11.3; 13.3) \\
                \midrule
                Coeff. of Variation & NA & 5.61 \\
                \midrule
                Skewness & NA & 6.12 \\
                \midrule
                Median & NA & 1.23 \\
                %%
                (95\% confidence interval) & NA & (0.235; 2.23) \\
                \midrule
                Quartiles & NA & 0.612; 2.35 \\
                \toprule
                Number of Pairs & \multicolumn{2}{c} {NA} \\
                \midrule
                Wilcoxon p-value & \multicolumn{2}{c} {NA} \\
                \midrule
                Mann-Whitney p-value & \multicolumn{2}{c} {NA} \\
                \bottomrule
            \end{tabular}
        \end{table}

        \begin{figure}[hb]   % FIGURE
            \centering
            \includegraphics[scale=1.00]{testfigpath/statplot/metalstestbmpcarbondioxidestats.pdf}
            \caption{Box and Probability Plots of Carbon Dioxide at testbmp BMPs}
        \end{figure}

        \begin{figure}[hb]   % FIGURE
            \centering
            \includegraphics[scale=1.00]{testfigpath/scatterplot/metalstestbmpcarbondioxidescatter.pdf}
            \caption{Influent vs. Effluent Plots of Carbon Dioxide at testbmp BMPs}
        \end{figure} \clearpage"""
        + "\n",
    }

@pytest.fixture
def cat_sum():
    includes = [
        (True, True),
        (True, False),
        (True, True),
        (False, False),
        (False, True),
    ]
    cs = summary.CategoricalSummary([mock_dataset(*inc) for inc in includes], "Metals", "basepath", "testfigpath")
    return cs

@pytest.fixture
def expected_latex_report():
    report = dedent(
        """\
        \\begin{document}test report title
        \\input{testpath.tex}
        \\end{document}
    """
    )
    return report

@pytest.fixture
def temp_template():
    with TemporaryDirectory() as datadir:
        filename = os.path.join(datadir, "testtemplate.tex")
        with open(filename, "w") as f:
            f.write("\\begin{document}__VARTITLE")

        yield filename

def test_CategoricalSummary_datasets(cat_sum):
    for ds in cat_sum.datasets:
        assert isinstance(ds, mock_dataset)

    assert len(cat_sum.datasets) == 3

def test_CategoricalSummary_paramgroup(cat_sum):
    assert isinstance(cat_sum.paramgroup, str)
    assert cat_sum.paramgroup == "Metals"

def test_CategoricalSummary__make_input_file_IO(cat_sum, expected_latext_content):
    with StringIO() as inputIO:
        cat_sum._make_input_file_IO(inputIO)
        input_string = inputIO.getvalue()

    helpers.assert_bigstring_equal(input_string, expected_latext_content)

def test_CategoricalSummary__make_report_IO(cat_sum, expected_latex_report, temp_template):
    with StringIO() as report, open(temp_template, "r") as template:
        cat_sum._make_report_IO(template, "testpath.tex", report, "test report title")
        helpers.assert_bigstring_equal(report.getvalue(), expected_latex_report)

def test_CategoricalSummary_makeReport(cat_sum, expected_latex_report, temp_template):
    templatepath = get_tex_file("draft_template.tex")
    inputpath = get_tex_file("inputs_{}.tex".format(cat_sum.paramgroup.lower()))
    with TemporaryDirectory() as tmpdir:
        reportpath = os.path.join(tmpdir, "report_{}.tex".format(cat_sum.paramgroup.lower()))
        testpath = os.path.join(tmpdir, "testpath.tex".format(cat_sum.paramgroup.lower()))
        cat_sum.makeReport(temp_template, testpath, reportpath, "test report title", regenfigs=False)

        with open(reportpath, "r") as rp:
            helpers.assert_bigstring_equal(rp.read(), expected_latex_report)

@pytest.fixture
def expected_latext_content():
    content = dedent(
        r"""        \section{Carbon Dioxide}
        \subsection{testbmp}
                \begin{table}[h!]
                    \caption{Statistics for Carbon Dioxide (mg/L) at testbmp BMPs}
                    \centering
                    \begin{tabular}{l l l l l}
                        \toprule
                        \textbf{Statistic} & \textbf{Inlet} & \textbf{Outlet} \\
                        \toprule
                        Count & 25 & 25 \\
                        \midrule
                        Number of NDs & 5 & 5 \\
                        \midrule
                        Min; Max & 0.123; 123 & 0.123; 123 \\
                        \midrule
                        Mean & 12.3 & 12.3 \\
                        %%
                        (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                        \midrule
                        Standard Deviation & 4.56 & 4.56 \\
                        \midrule
                        Log. Mean & 12.3 & 12.3 \\
                        %%
                        (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                        \midrule
                        Log. Standard Deviation & 4.56 & 4.56 \\
                        \midrule
                        Geo. Mean & 12.3 & 12.3 \\
                        %%
                        (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                        \midrule
                        Coeff. of Variation & 5.61 & 5.61 \\
                        \midrule
                        Skewness & 6.12 & 6.12 \\
                        \midrule
                        Median & 1.23 & 1.23 \\
                        %%
                        (95\% confidence interval) & (0.235; 2.23) & (0.235; 2.23) \\
                        \midrule
                        Quartiles & 0.612; 2.35 & 0.612; 2.35 \\
                        \toprule
                        Number of Pairs & \multicolumn{2}{c} {22} \\
                        \midrule
                        Wilcoxon p-value & \multicolumn{2}{c} {$<0.001$} \\
                        \midrule
                        Mann-Whitney p-value & \multicolumn{2}{c} {0.456} \\
                        \bottomrule
                    \end{tabular}
                \end{table}

                \begin{figure}[hb]   % FIGURE
                    \centering
                    \includegraphics[scale=1.00]{testfigpath/statplot/metalstestbmpcarbondioxidestats.pdf}
                    \caption{Box and Probability Plots of Carbon Dioxide at testbmp BMPs}
                \end{figure}

                \begin{figure}[hb]   % FIGURE
                    \centering
                    \includegraphics[scale=1.00]{testfigpath/scatterplot/metalstestbmpcarbondioxidescatter.pdf}
                    \caption{Influent vs. Effluent Plots of Carbon Dioxide at testbmp BMPs}
                \end{figure} \clearpage
        \clearpage
        \subsection{testbmp}
                \begin{table}[h!]
                    \caption{Statistics for Carbon Dioxide (mg/L) at testbmp BMPs}
                    \centering
                    \begin{tabular}{l l l l l}
                        \toprule
                        \textbf{Statistic} & \textbf{Inlet} & \textbf{Outlet} \\
                        \toprule
                        Count & 25 & 25 \\
                        \midrule
                        Number of NDs & 5 & 5 \\
                        \midrule
                        Min; Max & 0.123; 123 & 0.123; 123 \\
                        \midrule
                        Mean & 12.3 & 12.3 \\
                        %%
                        (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                        \midrule
                        Standard Deviation & 4.56 & 4.56 \\
                        \midrule
                        Log. Mean & 12.3 & 12.3 \\
                        %%
                        (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                        \midrule
                        Log. Standard Deviation & 4.56 & 4.56 \\
                        \midrule
                        Geo. Mean & 12.3 & 12.3 \\
                        %%
                        (95\% confidence interval) & (11.3; 13.3) & (11.3; 13.3) \\
                        \midrule
                        Coeff. of Variation & 5.61 & 5.61 \\
                        \midrule
                        Skewness & 6.12 & 6.12 \\
                        \midrule
                        Median & 1.23 & 1.23 \\
                        %%
                        (95\% confidence interval) & (0.235; 2.23) & (0.235; 2.23) \\
                        \midrule
                        Quartiles & 0.612; 2.35 & 0.612; 2.35 \\
                        \toprule
                        Number of Pairs & \multicolumn{2}{c} {22} \\
                        \midrule
                        Wilcoxon p-value & \multicolumn{2}{c} {$<0.001$} \\
                        \midrule
                        Mann-Whitney p-value & \multicolumn{2}{c} {0.456} \\
                        \bottomrule
                    \end{tabular}
                \end{table}

                \begin{figure}[hb]   % FIGURE
                    \centering
                    \includegraphics[scale=1.00]{testfigpath/statplot/metalstestbmpcarbondioxidestats.pdf}
                    \caption{Box and Probability Plots of Carbon Dioxide at testbmp BMPs}
                \end{figure}

                \begin{figure}[hb]   % FIGURE
                    \centering
                    \includegraphics[scale=1.00]{testfigpath/scatterplot/metalstestbmpcarbondioxidescatter.pdf}
                    \caption{Influent vs. Effluent Plots of Carbon Dioxide at testbmp BMPs}
                \end{figure} \clearpage
        \clearpage
        \subsection{testbmp}
                \begin{table}[h!]
                    \caption{Statistics for Carbon Dioxide (mg/L) at testbmp BMPs}
                    \centering
                    \begin{tabular}{l l l l l}
                        \toprule
                        \textbf{Statistic} & \textbf{Inlet} & \textbf{Outlet} \\
                        \toprule
                        Count & NA & 25 \\
                        \midrule
                        Number of NDs & NA & 5 \\
                        \midrule
                        Min; Max & NA & 0.123; 123 \\
                        \midrule
                        Mean & NA & 12.3 \\
                        %%
                        (95\% confidence interval) & NA & (11.3; 13.3) \\
                        \midrule
                        Standard Deviation & NA & 4.56 \\
                        \midrule
                        Log. Mean & NA & 12.3 \\
                        %%
                        (95\% confidence interval) & NA & (11.3; 13.3) \\
                        \midrule
                        Log. Standard Deviation & NA & 4.56 \\
                        \midrule
                        Geo. Mean & NA & 12.3 \\
                        %%
                        (95\% confidence interval) & NA & (11.3; 13.3) \\
                        \midrule
                        Coeff. of Variation & NA & 5.61 \\
                        \midrule
                        Skewness & NA & 6.12 \\
                        \midrule
                        Median & NA & 1.23 \\
                        %%
                        (95\% confidence interval) & NA & (0.235; 2.23) \\
                        \midrule
                        Quartiles & NA & 0.612; 2.35 \\
                        \toprule
                        Number of Pairs & \multicolumn{2}{c} {NA} \\
                        \midrule
                        Wilcoxon p-value & \multicolumn{2}{c} {NA} \\
                        \midrule
                        Mann-Whitney p-value & \multicolumn{2}{c} {NA} \\
                        \bottomrule
                    \end{tabular}
                \end{table}

                \begin{figure}[hb]   % FIGURE
                    \centering
                    \includegraphics[scale=1.00]{testfigpath/statplot/metalstestbmpcarbondioxidestats.pdf}
                    \caption{Box and Probability Plots of Carbon Dioxide at testbmp BMPs}
                \end{figure}

                \begin{figure}[hb]   % FIGURE
                    \centering
                    \includegraphics[scale=1.00]{testfigpath/scatterplot/metalstestbmpcarbondioxidescatter.pdf}
                    \caption{Influent vs. Effluent Plots of Carbon Dioxide at testbmp BMPs}
                \end{figure} \clearpage
        \clearpage
    """
    )
    return content

max_stars_repo_name: Geosyntec/pybmp

max_issues_repo_name: Geosyntec/pybmp

max_forks_repo_name: Geosyntec/pybmp


text:
from logging import getLogger
from shutil import rmtree
from numpy import array, median

from bidso import Electrodes
from bidso.find import find_in_bids
from bidso.utils import replace_underscore, read_tsv

from wonambi.attr import Channels, Freesurfer
# from wonambi.viz import Viz3

from ..bidso import read_channels

lg = getLogger(__name__)

ELECSURF_DIR = 'electrodes_on_surface'

def xmain(bids_dir, analysis_dir, freesurfer_dir, output_dir,
         acquisition='clinical', measure_modality="", measure_column=""):
    """
    plot electrodes onto the brain surface,

    Parameters
    ----------
    bids_dir : path

    analysis_dir : path

    freesurfer_dir : path

    output_dir : path

    acquisition : str
        acquisition type of the electrode files
    measure_modality : str
        modality
    measure_column : str
        column
    """
    img_dir = output_dir / ELECSURF_DIR
    rmtree(img_dir, ignore_errors=True)
    img_dir.mkdir(exist_ok=True, parents=True)

    for electrode_path in find_in_bids(bids_dir, generator=True, acquisition=acquisition, modality='electrodes', extension='.tsv'):
        lg.debug(f'Reading electrodes from {electrode_path}')
        elec = Electrodes(electrode_path)
        fs = Freesurfer(freesurfer_dir / ('sub-' + elec.subject))

        labels = None
        if measure_modality != "":
            try:
                ecog_file = find_in_bids(
                    analysis_dir,
                    subject=elec.subject,
                    modality=measure_modality,
                    extension='.tsv')
            except FileNotFoundError as err:
                lg.warning(err)
                continue

            lg.debug(f'Reading {measure_column} from {ecog_file}')
            ecog_tsv = read_tsv(ecog_file)

            labels = [x['name'] for x in elec.electrodes.tsv]
            labels, vals = read_channels(ecog_tsv, labels, measure_column)

        else:
            vals = None

        v = plot_electrodes(elec, fs, labels, vals)

        png_file = img_dir / replace_underscore(elec.get_filename(), 'surfaceplot.png')
        lg.debug(f'Saving electrode plot on {png_file}')
        v.save(png_file)
        v.close()

def plot_electrodes(elec, freesurfer, labels=None, values=None):
    if labels is None:
        labels = [x['name'] for x in elec.electrodes.tsv]
    xyz = array(elec.get_xyz(labels))

    # convert from RAS to tkRAS
    xyz -= freesurfer.surface_ras_shift

    chan = Channels(labels, xyz)
    if median(chan.return_xyz()[:, 0]) > 0:
        surf = freesurfer.read_brain().rh
    else:
        surf = freesurfer.read_brain().lh
    v = Viz3()
    v.add_chan(chan, values=values)
    v.add_surf(surf, alpha=0.5)

    return v

max_stars_repo_name: gpiantoni/boavus

max_issues_repo_name: gpiantoni/boavus

max_forks_repo_name: gpiantoni/boavus

reason: from numpy


text:
from pycovid19xray.utils import configure_logging, set_gpu
configure_logging()
# set_gpu(1)

import torch
from pycovid19xray.fastai import FastaiModel, show_metrics
from pycovid19xray.tests import DATA_DIR

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import fastai.vision as fv
from PIL import Image
import numpy as np

fastaimodel = FastaiModel(modelname="rs50-dataaug-covidxv3",
                          bs=64)

data, test = fastaimodel.create_covidx_databunch()
test_data = test.databunch()

# show a grid
plt.close("all")
data.show_batch(rows=4, figsize=(12,9))
fastaimodel.save_fig("covidx_grid.jpg")

plt.close("all")
data.show_batch(rows=4, figsize=(12,9), ds_type=fv.DatasetType.Valid)
fastaimodel.save_fig("covidx_grid_valid.jpg")

plt.close("all")
test_data.show_batch(rows=3, figsize=(12,9))
fastaimodel.save_fig("covidx_grid_test.jpg")

#######
# model
# w = torch.cuda.FloatTensor([1., 1., 6.])
# learn = fastaimodel.create_learner(data, loss_func=torch.nn.CrossEntropyLoss(weight=w))
learn = fastaimodel.create_learner(data, loss_func=torch.nn.CrossEntropyLoss())
learn.lr_find()
plt.close("all")
learn.recorder.plot()
fastaimodel.save_fig("train_resnet_covidx_find_lr.jpg")

lr = 1e-2
learn.fit_one_cycle(5, slice(lr))
fastaimodel.save_learner(learn, 'stage-1')

# eval on evaluation dataset
interp = fv.ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix(figsize=(12,12), dpi=60)
fastaimodel.save_fig("val_confmat.jpg")

# show metrics
show_metrics(learn, test)

# unfreeze and fine tune
learn.unfreeze()
learn.lr_find()
plt.close("all")
learn.recorder.plot()
fastaimodel.save_fig("rain_resnet_covidx_find_lr_unfreeze.jpg")

learn.fit_one_cycle(15, slice(1e-5, 1e-4))
fastaimodel.save_learner(learn, 'stage-2')

# eval on evaluation dataset
interp = fv.ClassificationInterpretation.from_learner(learn)
plt.close("all")
interp.plot_confusion_matrix(figsize=(12,12), dpi=60)
fastaimodel.save_fig("eval_confmat_2.jpg")

show_metrics(learn, test)


text:
import torch.nn as nn
import math
import torch
import numpy as np
import torch.nn.functional as F

def get_upsampling_weight(in_channels, out_channels, kernel_size):
    """Make a 2D bilinear kernel suitable for upsampling"""
    factor = (kernel_size + 1) // 2
    if kernel_size % 2 == 1:
        center = factor - 1
    else:
        center = factor - 0.5
    og = np.ogrid[:kernel_size, :kernel_size]
    filt = (1 - abs(og[0] - center) / factor) * \
           (1 - abs(og[1] - center) / factor)
    weight = np.zeros((in_channels, out_channels, kernel_size, kernel_size),
                      dtype=np.float64)
    weight[range(in_channels), range(out_channels), :, :] = filt
    return torch.from_numpy(weight).float()

def conv3x3(in_planes, out_planes, stride=1):
    "3x3 convolution with padding"
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=False)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=False)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, stride=stride, bias=False) # change
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, # change
                               padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * 4)
        self.relu = nn.ReLU(inplace=False)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class ResNet(nn.Module):
    def __init__(self, block, layers, num_classes):
        self.inplanes = 64
        super(ResNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=False)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=0, ceil_mode=True) # change
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AvgPool2d(18, ceil_mode=True)
        #self.avgpool = nn.AvgPool2d(7)
        #self.fc_1 = nn.Linear(512 * block.expansion, 256)
        #self.fc = nn.Linear(512 * block.expansion, num_classes)
        self.fc = nn.Linear(512, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        #x = x.view(-1, 512 * 4)
        #feat = self.fc_1(x)
        x = self.fc(x)

        return x

def discriminator(n_class):
    # Constructs a ResNet-18 model.

    model = ResNet(BasicBlock, [2, 2, 2, 2], num_classes=n_class)

    return model

class depth_estimator(nn.Module):
    def __init__(self):
        super(depth_estimator, self).__init__()

        self.RGB_CNN = nn.Sequential(
            nn.Conv2d(3, 64, 3, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU(),
            nn.Conv2d(64, 64, 3, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU(),
            nn.AvgPool2d(2, stride=2),
            nn.Conv2d(64, 128, 3, padding=1),
            nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU(),
            nn.Conv2d(128, 128, 3, padding=1),
            nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU(),
            nn.AvgPool2d(2, stride=2),
            nn.Conv2d(128, 64, 3, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU(),
            nn.Conv2d(64, 64, 3, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU(),
            nn.AvgPool2d(2, stride=2),
            nn.Conv2d(64, 32, 3, padding=1),
            nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU()
        )

        self.D_CNN = nn.Sequential(
            nn.Conv2d(32, 32, 3, padding=1),
            nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU(),
            nn.Conv2d(32, 32, 3, padding=1),
            nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU(),
            nn.Conv2d(32, 32, 3, padding=1),
            nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True),
            nn.PReLU()
        )

        self.pred2_depth = nn.Sequential(
            nn.Conv2d(64, 32, 1, padding=0),
            nn.Conv2d(32, 1, 1, padding=0),
        )
        self.up2 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
        self.upsample = nn.Upsample(scale_factor=8, mode='bilinear', align_corners=True)

        self._initialize_weights()

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                # m.weight.data.zero_()
                nn.init.normal_(m.weight.data, std=0.01)
                if m.bias is not None:
                    m.bias.data.zero_()
            if isinstance(m, nn.ConvTranspose2d):
                assert m.kernel_size[0] == m.kernel_size[1]
                initial_weight = get_upsampling_weight(m.in_channels, m.out_channels, m.kernel_size[0])
                m.weight.data.copy_(initial_weight)

    def forward(self, images, x3, x4, x5):

        RGB_fea = self.RGB_CNN(images)

        # Depth CNN
        x3 = x3 + self.up2(x4) +self.up2(self.up2(x5))
        Depth_fea = self.D_CNN(x3)

        Combined_fea = torch.cat([RGB_fea,Depth_fea],dim=1)

        pred_depth = self.pred2_depth(Combined_fea)
        pred_depth = self.upsample(pred_depth)
        return pred_depth


text:
import torch
import requests
import sys
import os
import logging
import torchvision.transforms as T
import torch.nn as nn
import numpy as np
from torch import nn
from dataclasses import dataclass
from functools import partial
from typing import Dict
from torch import Tensor
from .ModuleTransfer import ModuleTransfer
from tqdm.autonotebook import tqdm
from pathlib import Path
from PIL import Image
from typing import Tuple
from typing import Callable
from functools import wraps
logging.basicConfig(level=logging.INFO)

IMAGENET_DEFAULT_MEAN = torch.Tensor([0.485, 0.456, 0.406])
IMAGENET_DEFAULT_STD = torch.Tensor([0.229, 0.224, 0.225])

@dataclass
class Config:
    """Describe one configuration for a pretrained model.

    Returns:
        [type]: [description]
    """
    input_size: int = 224
    resize: int = 256
    mean: Tuple[float] = IMAGENET_DEFAULT_MEAN
    std: Tuple[float] = IMAGENET_DEFAULT_STD
    interpolation: str = 'bilinear'

    @property
    def transform(self):
        interpolations = {
            'bilinear': Image.BILINEAR,
            'bicubic': Image.BICUBIC
        }
        tr = T.Compose([
            T.Resize(self.resize, interpolations[self.interpolation]),
            T.CenterCrop(self.input_size),
            T.ToTensor(),
        ])

        if self.mean != None or self.std != None:
            tr.transforms.append(T.Normalize(mean=self.mean, std=self.std))

        return tr

StateDict = Dict[str, Tensor]

def pretrained(name: str = None) -> Callable:
    _name = name

    def decorator(func: Callable) -> Callable:
        """Decorator to fetch the pretrained model.

        Args:
            func ([Callable]): [description]

        Returns:
            [Callable]: [description]
        """
        name = func.__name__ if _name is None else _name
        provider = PretrainedWeightsProvider()
        @wraps(func)
        def wrapper(*args,  pretrained: bool = False, **kwargs) -> Callable:
            model = func(*args, **kwargs)
            if pretrained:
                model.load_state_dict(provider[name])
                model.eval()
            return model
        return wrapper
    return decorator

@dataclass
class BasicUrlHandler:
    url: str

    def get_response(self) -> requests.Request:
        r = requests.get(self.url, stream=True)
        return r

    def __call__(self, save_path: Path, chunk_size: int = 1024) -> Path:
        r = self.get_response()

        with open(save_path, 'wb') as f:
            total_length = sys.getsizeof(r.content)
            bar = tqdm(r.iter_content(chunk_size=chunk_size),
                       total=total_length // chunk_size)
            for chunk in bar:
                if chunk:
                    f.write(chunk)
                    f.flush()

class GoogleDriveUrlHandler(BasicUrlHandler):

    def __init__(self, url: str, file_id: str):
        super().__init__(url)
        self.file_id = file_id

    def get_confirm_token(self, response: requests.Request) -> object:
        for key, value in response.cookies.items():
            if key.startswith('download_warning'):
                return value
        return None

    def get_response(self) -> requests.Request:
        session = requests.Session()

        response = session.get(
            self.url, params={'id': self.file_id}, stream=True)
        token = self.get_confirm_token(response)

        if token:
            params = {'id': self.file_id, 'confirm': token}
            response = session.get(self.url, params=params, stream=True)

        return response

@dataclass
class PretrainedWeightsProvider:
    """
    This class allows to retrieve pretrained models weights (state dict).

    Example:
        >>> provider = PretrainedWeightsProvider()
        >>> provider['resnet18'] # get a pre-trained resnet18 model
        # see all the outputs
        >>> provider = PretrainedWeightsProvider(verbose=1)
        # change save dir
        >>> provider = PretrainedWeightsProvider(save_dir=Path('./awesome/'))
        # override model even if already downloaded
        >>> provider = PretrainedWeightsProvider(override=True)
    """

    BASE_URL: str = 'https://cv-glasses.s3.eu-central-1.amazonaws.com'
    BASE_DIR: Path = Path(torch.hub.get_dir()) / Path('glasses')
    save_dir: Path = BASE_DIR
    chunk_size: int = 1024 * 1
    verbose: int = 0
    override: bool = False

    weights_zoo = {
        'densenet121': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/densenet121.pth?raw=true'),
        'densenet169': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/densenet169.pth?raw=true'),
        'densenet201': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/densenet201.pth?raw=true'),
        'dummy': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/dummy.pth?raw=true'),
        'efficientnet_b0': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/efficientnet_b0.pth?raw=true'),
        'efficientnet_b1': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/efficientnet_b1.pth?raw=true'),
        'efficientnet_b2': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/efficientnet_b2.pth?raw=true'),
        'efficientnet_b3': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/efficientnet_b3.pth?raw=true'),
        'regnetx_002': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnetx_002.pth?raw=true'),
        'regnetx_004': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnetx_004.pth?raw=true'),
        'regnetx_006': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnetx_006.pth?raw=true'),
        'regnetx_008': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnetx_008.pth?raw=true'),
        'regnetx_016': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnetx_016.pth?raw=true'),
        'regnetx_032': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnetx_032.pth?raw=true'),
        'regnety_002': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnety_002.pth?raw=true'),
        'regnety_004': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnety_004.pth?raw=true'),
        'regnety_006': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnety_006.pth?raw=true'),
        'regnety_008': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnety_008.pth?raw=true'),
        'regnety_016': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnety_016.pth?raw=true'),
        'regnety_032': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/regnety_032.pth?raw=true'),
        'resnet18': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/resnet18.pth?raw=true'),
        'resnet26': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/resnet26.pth?raw=true'),
        'resnet26d': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/resnet26d.pth?raw=true'),
        'resnet34': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/resnet34.pth?raw=true'),
        'resnet34d': BasicUrlHandler('https://github.com/FrancescoSaverioZuppichini/glasses-weights/blob/main/resnet34d.pth?raw=true'),
        # aws stored
        'cse_resnet50': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/cse_resnet50.pth'),
        'densenet161': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/densenet161.pth'),
        'resnet101': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/resnet101.pth'),
        'resnet152': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/resnet152.pth'),
        'resnet50': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/resnet50.pth'),
        'resnet50d': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/resnet50d.pth'),
        'resnext101_32x8d': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/resnext101_32x8d.pth'),
        'resnext50_32x4d': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/resnext50_32x4d.pth'),
        'vgg11': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/vgg11.pth'),
        'vgg11_bn': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/vgg11_bn.pth'),
        'vgg13': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/vgg13.pth'),
        'vgg13_bn': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/vgg13_bn.pth'),
        'vgg16': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/vgg16.pth'),
        'vgg16_bn': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/vgg16_bn.pth'),
        'vgg19': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/vgg19.pth'),
        'vgg19_bn': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/vgg19_bn.pth'),
        'wide_resnet101_2': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/wide_resnet101_2.pth'),
        'wide_resnet50_2': BasicUrlHandler('https://glasses-weights.s3.eu-central-1.amazonaws.com/wide_resnet50_2.pth'),

    }

    def __post_init__(self):
        try:
            self.save_dir.mkdir(exist_ok=True)
        except FileNotFoundError:
            default_dir = str(Path(__file__).resolve().parent)
            self.save_dir = Path(os.environ.get(
                'HOME', default_dir)) / Path('.glasses/')
            self.save_dir.mkdir(exist_ok=True)
        os.environ['GLASSES_HOME'] = str(self.save_dir)

    def __getitem__(self, key: str) -> dict:
        if key not in self.weights_zoo:
            raise KeyError(
                f'No weights for model "{key}". Available models are {",".join(list(self.weights_zoo.keys()))}')

        save_path = self.save_dir / f'{key}.pth'

        should_download = not save_path.exists()

        if should_download or self.override:
            handler = self.weights_zoo[key]
            handler(save_path, self.chunk_size)

        weights = torch.load(save_path)
        logging.info(f'Loaded {key} pretrained weights.')
        return weights

    # def __contains__(self, key: str) -> bool:
    #     return key in self.zoo.keys()

max_stars_repo_name: abhishekvermasg/glasses

max_issues_repo_name: abhishekvermasg/glasses

max_forks_repo_name: abhishekvermasg/glasses


text:
from numpy import zeros
from scipy.optimize import fsolve

class integrators:
    def __init__(self,integrator_str,counter):
        self.integrator_str=integrator_str
        self.counter=counter
    def __call__(self):
        if self.integrator_str=='Forward Euler':
            return self.forward_euler
        elif self.integrator_str=='Backward Euler':
            return  self.backward_euler
        elif self.integrator_str=='Crank Nicholson':
            return self.crank_nicholson

    def forward_euler(self,rhsfun, phi0, times, bcfun):
        """
        foward_euler - implements the forward euler algorithm
        :param rhsfun: the function that computes the RHS of the PDE(s) that we are solving.  Should take one arguments: the value of the solution variables at which the RHS should be evaluated.
        :param phi0: the solution vector at the first element in "times"
        :param times: the vector of times at which we should evaluate the RHS and obtain the solution
        :param bcfun: the boundary condition function
        :return: the solution variables at each of the requested times as a matrix with each time in a row and each variable in a column
        """

        size = phi0.size +2 # +2 is for the ghost cells
        phi = zeros((times.size,size))  # allocate storage for the results
        phi[0, 1:-1] = phi0  # store the initial condition in the results

        bcfun(phi[0, :]) # apply bcs for the initial condition

        for i in range(times.size - 1):
            self.counter()
            dt = times[i + 1] - times[i]

            phi[i+1,:] = phi[i,:] + dt * rhsfun(phi[i,:]) # time advance

            bcfun(phi[i+1,:]) # apply bcs

        return phi

    def backward_euler(self,rhsfun, phi0, times, bcfun):
      """
      backward_euler - implements the backward euler algorithm
      :param rhsFun: the function that computes the RHS of the PDE(s) that we are solving.  Should take one arguments: the value of the solution variables at which the RHS should be evaluated.
      :param phi0: the solution vector at the first element in "times"
      :param times: the vector of times at which we should evaluate the RHS and obtain the solution
      :param bcfun: the boundary condition function
      :return: the solution variables at each of the requested times as a matrix with each time in a row and each variable in a column
      """

      size = phi0.size + 2  # +2 is for the ghost cells
      phi = zeros((times.size, size))  # allocate storage for the results
      phi[0, 1:-1] = phi0  # store the initial condition in the results

      bcfun(phi[0, :]) # apply bcs for the initial condition

      for i in range(times.size - 1):
          self.counter()
          dt = times[i + 1] - times[i]

          def resfun(phinew):
              return phinew - phi[i, :] - dt * rhsfun(phinew)

          phi[i + 1, :] = fsolve(resfun, phi[i, :]) # time advance

          bcfun(phi[i + 1,:])

      return phi

    def crank_nicholson(self,rhsfun, phi0, times,bcfun):
        """
        crank_nicholson - implements the Crank-Nicholson algorithm
        :param rhsFun: the function that computes the RHS of the PDE(s) that we are solving.  Should take one arguments: the value of the solution variables at which the RHS should be evaluated.
        :param phi0: the solution vector at the first element in "times"
        :param times: the vector of times at which we should evaluate the RHS and obtain the solution
        :param bcfun: the boundary condition function
        :return: the solution variables at each of the requested times as a matrix with each time in a row and each variable in a column
        """

        size = phi0.size + 2  # +2 is for the ghost cells
        phi = zeros((times.size, size))  # allocate storage for the results
        phi[0, 1:-1] = phi0  # store the initial condition in the results

        bcfun(phi[0, :])  # apply bcs for the initial condition

        for i in range(times.size - 1):
            self.counter()
            dt = times[i + 1] - times[i]
            rhsold = rhsfun(phi[i,:])
            def resfun(phinew):
                return (phinew - phi[i, :])/dt - 0.5 * ( rhsold + rhsfun(phinew) )

            phi[i + 1, :] = fsolve(resfun, phi[i, :])

            bcfun(phi[i + 1, :])

        return phi

reason: from numpy,from scipy


text:
import os
import os.path as path
import logging
from common.config import DATA_PATH, DEFAULT_TABLE
from common.const import UPLOAD_PATH
from common.const import input_shape
from common.const import default_cache_dir
from service.train import do_train
from service.search import do_search
from service.count import do_count
from service.delete import do_delete
from service.theardpool import thread_runner
# from preprocessor.vggnet import vgg_extract_feat
from indexer.index import milvus_client, create_table, insert_vectors, delete_table, search_vectors, create_index
from service.search import query_name_from_ids
from flask_cors import CORS
from flask import Flask, request, send_file, jsonify
from flask_restful import reqparse
from werkzeug.utils import secure_filename
from keras.preprocessing import image
import numpy as np
import tensorflow as tf
from numpy import linalg as LA
from resnet50_encoder.encode import CustomOperator
from diskcache import Cache
import shutil
from itertools import groupby
from operator import itemgetter

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.5
image_encoder = CustomOperator()
# global sess
# sess = tf.Session(config=config)
# set_session(sess)

app = Flask(__name__)
ALLOWED_EXTENSIONS = set(['jpg', 'png'])
app.config['UPLOAD_FOLDER'] = UPLOAD_PATH
app.config['JSON_SORT_KEYS'] = False
CORS(app)

@app.route('/api/v1/train', methods=['POST'])
def do_train_api():
    args = reqparse.RequestParser(). \
        add_argument('Table', type=str). \
        add_argument('File', type=str). \
        parse_args()
    table_name = args['Table']
    file_path = args['File']
    try:
        thread_runner(1, do_train, table_name, file_path)
        #print(file_path)
        filenames = os.listdir(file_path)
        if not os.path.exists(DATA_PATH):
            os.mkdir(DATA_PATH)
        for filename in filenames:
            shutil.copy(file_path + '/' + filename, DATA_PATH)
        return "Start"
    except Exception as e:
        return "Error with {}".format(e)

@app.route('/api/v1/delete', methods=['POST'])
def do_delete_api():
    args = reqparse.RequestParser(). \
        add_argument('Table', type=str). \
        parse_args()
    table_name = args['Table']
    print("delete table.")
    status = do_delete(table_name)
    try:
        shutil.rmtree(DATA_PATH)
    except:
        print("cannot remove", DATA_PATH)
    return "{}".format(status)

@app.route('/api/v1/count', methods=['POST'])
def do_count_api():
    args = reqparse.RequestParser(). \
        add_argument('Table', type=str). \
        parse_args()
    table_name = args['Table']
    rows = do_count(table_name)
    return "{}".format(rows)

@app.route('/api/v1/process')
def thread_status_api():
    cache = Cache(default_cache_dir)
    return "current: {}, total: {}".format(cache['current'], cache['total'])

@app.route('/data/<image_name>')
def image_path(image_name):
    file_name = DATA_PATH + '/' + image_name
    if path.exists(file_name):
        return send_file(file_name)
    return "file not exist"

@app.route('/api/v1/search', methods=['POST'])
def do_search_api():
    args = reqparse.RequestParser(). \
        add_argument("Table", type=str). \
        add_argument("Num", type=int, default=1). \
        parse_args()

    table_name = args['Table']
    if not table_name:
        table_name = DEFAULT_TABLE
    top_k = args['Num']
    file = request.files.get('file', "")
    if not file:
        return "no file data", 400
    if not file.name:
        return "need file name", 400
    if file:
        filename = secure_filename(file.filename)
        img_path = app.config['UPLOAD_FOLDER']+'/'+filename.split('.')[0]
        if not os.path.exists(img_path):
            os.mkdir(img_path)
        file_path = os.path.join(img_path, filename)
        file.save(file_path)
        res_id,res_distance = do_search(image_encoder,table_name, img_path, top_k)
        # print(res_id,res_distance)
        # if isinstance(res_id, str):
        #     return res_id
        res ={}
        for img, dis in zip(res_id,res_distance):
           # print("[[[[[[[[[[[[[[", img, dis)
            key_img = request.url_root +"data/" + img
            if key_img not in res.keys() or res[key_img] > dis:
                res[key_img] = dis
        #print("dic res:", res)
        # res = dict(zip(res_img,res_distance))

        #res = [list(g)[0] for k, g in groupby(sorted(res), key=itemgetter(0))]
       # print(",,,,,,,",res)
        res = sorted(res.items(),key=lambda item:item[1])
        # myslice = slice(top_k)
        # res=res[myslice]
        return jsonify(res), 200
    return "not found", 400

if __name__ == "__main__":
    app.run(host="0.0.0.0")

max_stars_repo_name: xiaogaozi/bootcamp

max_issues_repo_name: xiaogaozi/bootcamp

max_forks_repo_name: xiaogaozi/bootcamp

reason: import numpy,from numpy


text:
# Copyright 2021 Adobe
# All Rights Reserved.

# NOTICE: Adobe permits you to use, modify, and distribute this file in
# accordance with the terms of the Adobe license agreement accompanying
# it.
'''
>> python benchmark.py
'''
import glob
import os
import matplotlib.pyplot as plt
from skimage import io
import time
from PIL import Image
import yaml
import beacon_aug as BA
import beacon_aug.generator.custom as custom
from beacon_aug.generator.operator_generator import DEFAULT_LIBRARIES
import pandas as pd
import imgaug.augmenters as iaa
import numpy as np

def benchmark(BA_operator, image_data,  batch_size=128,  property="runtime", save_individual_fig_path=None, **kwargs):
    '''    benchmark test
    input:
    @ BA_operator: operator class
    @ image: numpy array input
    @ batch_size: the number of images to run in one data loader
    @ property: "runtime", "differientable"
    @ save_individual_fig_path

    Output:
        dictonary[{property}][{library}: {propterty value}]

    '''
    avail_libraries = BA_operator(**kwargs).avail_libraries

    numOfLibraries = len(avail_libraries)
    attributes_result = {}

    mask_required_ops = ["CropNonEmptyMaskIfExists","MaskDropout"]

    cropping_bbox_required_ops = ["RandomCropNearBBox"]

    for i, library in enumerate(avail_libraries):
        print("Testing for",BA_operator.__name__, " in ",library)
        t_before = time.time()
        op = BA_operator(p=1, library=library, **kwargs)
        for j in range(batch_size):
            if BA_operator.__name__  in mask_required_ops:
                image_auged = op(image=image_data, mask=(image_data[:,:,0] > 128))["image"]
            elif BA_operator.__name__  in cropping_bbox_required_ops:
                image_auged = op(image=image_data, cropping_bbox = [0,64,0,64])["image"]
            else:
                image_auged = op(image=image_data)["image"]

        t_after = time.time()
        runtime = (t_after - t_before)/batch_size

        if property == "runtime":
            attributes_result[library] = runtime
        elif property == "differentiable":
            attributes_result[library] = BA.properties.isOpDifferentiable(op)

        if save_individual_fig_path:
            img_name = os.path.join(save_individual_fig_path,
                                    BA_operator.__name__+"-" + library+".jpeg")
            if os.path.isfile(img_name):
                print("\n...screenshot individual figure already existed as : ", img_name)
            else:
                if image_auged.min() < 0:   # normalzied case, need to
                    image_auged = image_auged - image_auged.min()
                    image_auged = image_auged/image_auged.max()
                    print("@@@@@@@", image_auged.min())

                plt.imsave(img_name, image_auged)
                print("\n...screenshot individual figure save as : ", img_name)

    return attributes_result

if __name__ == "__main__":

    image = io.imread("../data/example.png")
    SAVE_PATH = "../docs/_images/"
    SAVE_PATH = None

    all_ops = []
    '''Extract all operator names '''
    # standard libs
    ym_fnames = glob.glob("../beacon_aug/generator/standard/*.yaml")

    for ym_fname in ym_fnames:
        ym_f = open(ym_fname)
        OP_para = yaml.safe_load(ym_f)
        for op_name in OP_para:     # each operator
            all_ops.append(op_name)
    # Customize
    custom_doc_path = "../beacon_aug/generator/custom/"
    for op_name in custom.__all__:
        if op_name not in all_ops:
            all_ops.append(op_name)

    '''Set necessary default parameters '''
    points_sampler = iaa.DropoutPointsSampler(
                        iaa.RelativeRegularGridPointsSampler(
                            n_cols_frac=(0.05, 0.2),
                            n_rows_frac=0.1),
                        0.2)
    extra_paras = { # converesion
                   "Rotate": {"limit": (30, 30)},
                   "Autocontrast": {"limit": (0.5, 0.5)},
                   "Brightness": {"limit": (0.5, 0.5)},
                   "Saturation": {"saturation": (0.5, 0.5)},
                   "Contrast": {"limit": (0.5, 0.5)},
                   "Posterize": {"num_bits": 2},
                   "Solarize": {"threshold": 64},
                   "Sharpen": {"alpha": (0.5, 0.5)},

                   # remain
                    "DropoutPointsSampler":{"other_points_sampler": iaa.RegularGridPointsSampler(10, 20), "p_drop":0.2},
                    "KeepSizeByResize":{"children": iaa.Crop((20, 40), keep_size=False)},
                    "WithPolarWarping":{"children": iaa.Crop((20, 40), keep_size=False)},
                    "Voronoi":{"points_sampler": points_sampler},

                    # augly
                    "MaskedComposite":{"mask": (image[:,:,0] > 128), "transform_function": None},
                    "OverlayImage":{"overlay":  Image.fromarray(np.uint8(image)).convert('RGB')},
                   # custom
                   "OverlayText": {"size": 50},
                   "TextFlow": {"size": 50},
                   }

    height_width_required_ops = ["RandomCrop", "CenterCrop", "RandomResizedCrop","Resize",
                                "CropNonEmptyMaskIfExists","RandomSizedBBoxSafeCrop",
                                "CenterCropToFixedSize","CenterPadToFixedSize", "CenterCropToFixedSize","PadToFixedSize"]
    for op_name in height_width_required_ops:
        extra_paras[op_name] = {"height":64, "width":64}
    for op_name in all_ops:
        if "BlendAlpha" in op_name:
            extra_paras[op_name] = {"foreground": "iaa.Add(100)"}

    '''Test all operators on different batch sizes '''
    for batch_size in [128, 1024]:
        D = {}   # runtime table dictionary
        for i, op_name in enumerate(all_ops):
        # for i , op_name in enumerate( ["AveragePooling",] ):   # bebug specific operator
                print (i, op_name)
                if op_name in extra_paras:      # parse default paras
                    D[op_name] = benchmark(getattr(BA, op_name), image_data=image.copy(), batch_size=batch_size, property="runtime",
                                        save_individual_fig_path=SAVE_PATH, **extra_paras[op_name])
                else:                           # no need to parse
                    D[op_name] = benchmark(getattr(BA, op_name), image_data=image.copy(),  batch_size=batch_size, property="runtime",
                                        save_individual_fig_path=SAVE_PATH)
        # output
        runtime = pd.DataFrame(D).T

        # change precision
        # runtime = pd.read_csv("runtime_batch128.csv")
        for v in runtime.columns.values[1:]:
            runtime[v] *=100
        runtime.to_csv("benchmark_results/runtime_batch"+str(batch_size)+"(ms).csv",float_format='%.3f')

max_stars_repo_name: adobe-research/beacon-aug

max_issues_repo_name: adobe-research/beacon-aug

max_forks_repo_name: adobe-research/beacon-aug


text:
import random
import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from instance_loader import InstanceLoader
from sklearn.decomposition import PCA
import itertools, sys, os, model, util

if __name__ == "__main__":
  embedding_size = 64
  time_steps = [32,64,96,128]#[2,12,22,32]
  centralities = sorted( sys.argv[1:] )

  if len( centralities ) <= 0:
    raise ValueError( "No centrality passed" )
  #end if
  for centrality in centralities:
    if centrality not in [ "betweenness", "closeness", "degree", "eigenvector" ]:
      raise ValueError( "Centrality {c} not one of the accepted ones".format( c = centrality ) )
    #end if
  #end for

  fname = "centrality-" + "-".join( centralities )
  # Build model
  print( "Building model" )
  GNN = model.model_builder( embedding_size, centralities )
  # Load instances
  random_seed = 1477
  instance_loader_random_generator = random.Random( random_seed )

  instance_loader = InstanceLoader( "instances-pca", rng = instance_loader_random_generator )

  # Disallow GPU and multicore
  config = tf.ConfigProto(
    device_count = {"GPU": 0},
    intra_op_parallelism_threads = 1,
    inter_op_parallelism_threads = 1
  )
  with tf.Session(config=config) as sess:
    print( "Initializing global variables ... " )
    sess.run( tf.global_variables_initializer() )
    print( "Loading weights ... " )
    util.load_weights( sess, fname )
    print( "Loading an instance ..." )
    for instance in instance_loader.get_batches( 1 ):
      print( "Building feed dict ..." )
      feed_dict = {}
      feed_dict[ GNN["gnn"].matrix_placeholders["M"] ] = util.sparse_to_dense( instance["matrix"] )
      feed_dict[ GNN["nodes_n"] ]                      = instance["problem_n"]
      for centrality in centralities:
        clabels  = "{c}_labels" .format( c = centrality )
        ccompare = "{c}_compare".format( c = centrality )
        feed_dict[ GNN[clabels] ]                      = util.sparse_to_dense( instance[ccompare] )
      #end for

      print( instance["fnames"][0].split("/")[-1].split(".")[0] )

      ncols =  len( time_steps )
      for centrality in centralities:
        print( centrality )
        fig, ax = plt.subplots( nrows = 1, ncols = ncols, sharey = "all", figsize=(ncols*2, 2.5) )
        axes = list( ax.flat )
        cmin = min( filter( lambda x: x>0, instance[centrality][0] ) ) # Filter zeros to avoid problems with log scale
        cmax = max( filter( lambda x: x>0,  instance[centrality][0] ) )
        for i, axis in enumerate( axes ):
          if i+1 == ncols//2:
           axis.set_xlabel( "Normalized PCA value".format( c = centrality ) )
          #end if
          axis.set_xlim( 0, 1 )
          num_xticks = 5
          axis.set_xticks( np.linspace( 0, 1, num_xticks ) )
          axis.set_xticklabels( "{:.1f}".format(x) if i==0 or i==2 or i==4 else None for i, x in enumerate( list( np.linspace( 0, 1, num_xticks ) ) ) )
          if i == 0:
            axis.set_ylabel( "log scale {c} value".format( c = centrality ) )
          else:
            axis.set_yticklabels( [] )
          #end if
          axis.set_yscale( "log" )
          axis.set_ylim( cmin, cmax )
          #end if
        #end for
        if centrality == "betweenness":
          color = "#E85D75"
        elif centrality == "closeness":
          color = "#729EA1"
        elif centrality == "degree":
          color = "#B5BD89"
        elif centrality == "eigenvector":
          color = "#F7EF81"
        else:
          color = "#8A4F7D"
        scatterplots = [
          axis.scatter(
            np.zeros( instance["problem_n"][0] ),
            np.zeros( instance["problem_n"][0] ),
            c = color,
            edgecolor= "#000000"#"#8A4F7D"
          ) for axis in list( axes )
        ]

        for i, t, scatterplot, axis in zip( range(len(time_steps)), time_steps, scatterplots, axes ):

          feed_dict[ GNN["gnn"].time_steps ] = t

          print( "Timestep {t:02d}, ".format( t = t ), end = "" )
          embeddings, acc = sess.run(
            [ GNN["gnn"].last_states["N"].h, GNN["{c}_accuracy".format(c=centrality)] ],
            feed_dict = feed_dict
          )
          print( "accuracy {acc:04.2f}".format( acc = acc * 100 ) )

          pca = PCA( n_components = 1 ).fit_transform( embeddings )
          pca[:,0] = (pca[:,0]-min(pca[:,0])) / (max(pca[:,0])-min(pca[:,0]))

          scatterplot.set_offsets( [ (x,y) for x,y in zip( pca, instance[centrality][0] ) ] )
          if True:#i%2==1:
            axis.set_title(
              "t {t:02d}, acc {a:04.2f}".format(
                t = t,
                a = acc * 100
              )
            )
          #end if

          fig.savefig(
            "pca-1d-{m}-{c}-{g}.eps".format(
              m = "".join( map( lambda x: x[0], centralities ) ),
              c = centrality,
              g = instance["fnames"][0].split("/")[-1].split(".")[0]
            ),
            bbox_inches = "tight"
          )
          plt.close(fig)
        #end for
    #end for instances
  #end session
#EOF

max_stars_repo_name: phcavelar/centrality-multitask

max_issues_repo_name: phcavelar/centrality-multitask

max_forks_repo_name: phcavelar/centrality-multitask


text:
from scipy import io
from scipy.spatial.distance import squareform, directed_hausdorff
from itertools import combinations
from sklearn.cluster import DBSCAN, AgglomerativeClustering
import numpy as np

import cv2
from tqdm import tqdm
from matplotlib import pyplot as plt
import matlab.engine
import argparse
import os
import csv
import yaml
from au_functions import *

__all__ = ['Controller', 'AU']

class Controller():
    def __init__(self, args):
        self.input = args.input
        self.Init(self.input)
        self.tkBoxes = []
        self.tkIDs = []

        self.auFifo = args.auFifo
        self.auNum = args.auNum
        self.dAdd = args.dAdd
        self.folder = args.outfolder
        self.name = args.name

        self.tDel = args.tDel * self.tFrame
        self.areaDel = args.areaDel
        self.tLive = args.tLive * self.tFrame
        self.areaLive = args.areaLive
        self.numLive = args.numLive

        self.t = self.tFrame

        self.split = args.split
        self.epsDiv = args.epsDiv
        self.minptsDiv = 1

        self.merge = args.merge
        self.iomMer = args.iomMer
        self.dsMer = args.dsMer
        self.minptsMer = 1
        self.epsMer = 1

        if "inbound" in self.input:
            self.bdspawn1 = 40
            self.bdspawn2 = 40
        else:
            self.bdspawn1 = -1
            self.bdspawn2 = -1

        if "outbound" in self.input:
            self.bdkill = 20
        else:
            self.bdkill = -1

        self.globalID = -1
        self.AUs = []

        self.eng = matlab.engine.start_matlab()

    def Init(self, file):
        if "bound" in self.input:
            self.tFrame = 40000
        else:
            self.tFrame = 44065

        self.cmap = io.loadmat('cmap.mat')['cmap'] * 255
        self.events = io.loadmat(file)['events']
        self.lx, self.ly = self.events[:, :2].max(0) + 1
        self.pFrame = -1
        self.nFrame = self.events[:, 4][-1] + 1
        self.frame0 = np.zeros((self.ly, self.lx, 3), 'uint8') + 255
        self.iFrame = self.frame0.copy()
        self.frames = np.tile(self.frame0, (self.nFrame, 1, 1, 1))

    def Update(self):
        for au in self.AUs:
            idxFade = np.argwhere(
                au.auEvents[:, 2] < au.auEvents[-1, 2] - self.tFrame).flatten()

            if idxFade.size == 0:
                continue

            for ievt in idxFade:
                au.shrink(au.auEvents[ievt, 0], au.auEvents[ievt, 1])

            au.auEvents = np.delete(au.auEvents, idxFade, axis=0)

        for au in self.AUs:
            au.auBox = bbox(au.auEvents[:, 0], au.auEvents[:, 1])

    def Split(self):
        idxDel = []

        if len(self.AUs) >= self.auNum:
            return

        for j, au in enumerate(self.AUs):
            if self.split == 'DBSCAN':
                idxGroup = DBSCAN(eps=self.epsDiv, min_samples=self.minptsDiv).fit_predict(
                    au.auEvents[:, :2])
                idxGroup[idxGroup < 0] = 0
            elif self.split == 'HAC':
                if au.auEvents.shape[0] <= 1:
                    continue
                clustering = AgglomerativeClustering(
                    linkage='average', affinity='euclidean').fit(au.auEvents[:, :2])
                idxGroup = clustering.labels_
                idxGroup[idxGroup < 0] = 0
                if max(directed_hausdorff(au.auEvents[idxGroup == 0, :2], au.auEvents[idxGroup == 1, :2])[0],
                       directed_hausdorff(au.auEvents[idxGroup == 1, :2], au.auEvents[idxGroup == 0, :2])[0]) < self.dsMer:
                    continue
            else:
                raise Exception('Split algorithm error!')

            if max(idxGroup) <= 0:
                continue
            else:
                idxDel.append(j)

            idxTk = np.argmax([sum(idxGroup == idx)
                              for idx in np.unique(idxGroup)])

            for k in range(max(idxGroup) + 1):
                idxEvents = np.argwhere(idxGroup == k).flatten()

                newau = AU(au.auEvents[idxEvents], self.lx,
                           self.ly, self.dAdd, self.auFifo)
                newau.auBox = bbox(newau.auEvents[:, 0], newau.auEvents[:, 1])

                if k == idxTk:
                    newau.auNumber = au.auNumber
                else:
                    newau.auNumber = [0, min(newau.auEvents[:, 2])]

                self.AUs.append(newau)

        if len(idxDel) > 0:
            for idx in sorted(idxDel, reverse=True):
                self.AUs.pop(idx)

    def Merge(self):
        idxDel = []

        if len(self.AUs) < 2:
            return

        idxnk = list(combinations(range(len(self.AUs)), 2))
        if self.merge == 'IOM':
            idxGroup = clusterAu(np.array([self.AUs[idx].auBox for idx in range(len(self.AUs))]), self.iomMer)
        elif self.merge == 'DIST':
            auPoints = [bbox2points(au.auBox) for au in self.AUs]
            dist = [max(directed_hausdorff(auPoints[idx[0]], auPoints[idx[1]])[0],
                        directed_hausdorff(auPoints[idx[1]], auPoints[idx[0]])[0]) > self.dsMer for idx in idxnk]
            idxGroup = DBSCAN(eps=self.epsMer, min_samples=self.minptsMer).fit_predict(
                squareform(dist))
        else:
            raise Exception('Merge algorithm error!')

        for j in range(max(idxGroup) + 1):
            idxAU = np.argwhere(idxGroup == j).flatten()
            if idxAU.size < 2:
                continue
            idxDel.extend(idxAU)

            events = np.concatenate(
                [self.AUs[idx].auEvents for idx in idxAU], axis=0)
            events = events[np.argsort(events[:, 2])]

            newau = AU(events, self.lx, self.ly, self.dAdd, self.auFifo)
            newau.auBox = bbox(newau.auEvents[:, 0], newau.auEvents[:, 1])

            if any([self.AUs[idx].auNumber[0] > 0 for idx in idxAU]):
                idxAU = idxAU[[self.AUs[idx].auNumber[0] > 0 for idx in idxAU]]
            idxNum = idxAU[np.argmin(
                [self.AUs[idx].auNumber[1] for idx in idxAU])]
            newau.auNumber = self.AUs[idxNum].auNumber

            self.AUs.append(newau)

        if len(idxDel) > 0:
            for idx in sorted(idxDel, reverse=True):
                self.AUs.pop(idx)

    def Kill(self, ts):
        idxDel = np.argwhere([ts - au.auEvents[-1, 2] > self.tDel or bbArea(
            au.auBox) < self.areaDel or (au.auBox[1] + au.auBox[3]) / 2 < self.bdkill for au in self.AUs]).flatten()

        if idxDel.size > 0:
            for idx in sorted(idxDel, reverse=True):
                self.AUs.pop(idx)

        for au in self.AUs:
            if not au.auNumber[0] and ts - au.auNumber[1] > self.tLive and bbArea(au.auBox) > self.areaLive and au.auEvents.shape[0] > self.numLive and au.auBox[2] / 2 > self.bdspawn1 and au.auBox[3] / 2 > self.bdspawn2:
                self.globalID += 1
                au.auNumber[0] = self.globalID

    def Animation(self, x, y, p, ts, f):
        if f > self.pFrame and self.pFrame >= 0:
            if len(self.AUs) >= 1:
                idxC = np.array([au.auNumber[0] %
                                7 for au in self.AUs], 'int32')
                auColors = self.cmap[idxC]
                idxW = np.array([au.auNumber[0] == 0 for au in self.AUs])
                auColors[idxW] = auColors[idxW] * 0 + 1

                idxVis = []
                boxes = []
                IDs = []

                for j, au in enumerate(self.AUs):
                    if au.auNumber[0] > 0:
                        idxEvt = au.auEvents[:, 2] >= ts - self.tFrame
                        if any(idxEvt):
                            idxVis.append(j)
                            boxes.append(
                                bbox(au.auEvents[idxEvt, 0], au.auEvents[idxEvt, 1]))
                            IDs.append(au.auNumber[0])

                self.tkBoxes.append(boxes)
                self.tkIDs.append(IDs)

                if len(idxVis) > 0:
                    for j, k in enumerate(idxVis):
                        self.iFrame = cv2.rectangle(
                            self.iFrame, (boxes[j][0], boxes[j][1]), (boxes[j][2], boxes[j][3]), auColors[k].tolist(), 1)

                if len(idxVis) > 0:
                    for j, k in enumerate(idxVis):
                        self.iFrame = cv2.putText(self.iFrame, '{}'.format(
                            IDs[j]), (boxes[j][0], boxes[j][1]), cv2.FONT_HERSHEY_PLAIN, 1., auColors[k].tolist(), 1)

            self.iFrame = cv2.putText(self.iFrame, 'Frame:{}/{}, MaxID:{}'.format(
                self.pFrame, self.nFrame - 1, self.globalID), (0, 15), cv2.FONT_HERSHEY_PLAIN, 1., [255, 255, 255], 1)

            self.frames[self.pFrame] = self.iFrame

        if f > self.pFrame:
            self.pFrame = f
            self.iFrame = self.frame0.copy()

        if p > 0:
            self.iFrame[y, x] = [255, 0, 0]
        else:
            self.iFrame[y, x] = [0, 0, 255]

    def SaveResults(self):
        if not os.path.exists(self.folder):
            os.mkdir(self.folder)

        tkbox_dir = os.path.join(self.folder, self.name + 'tkBoxes.mat')
        tkid_dir = os.path.join(self.folder, self.name + 'tkIDs.mat')
        gt_dir = self.input.replace('.mat', 'GT.mat')
        video_dir = os.path.join(self.folder, self.name + 'output.avi')

        for boxes in self.tkBoxes:
            if len(boxes) == 0:
                continue
            for box in boxes:
                box[2] = box[2] - box[0]
                box[3] = box[3] - box[1]

        self.tkBoxes = np.array(self.tkBoxes, dtype='object')
        self.tkIDs = np.array(self.tkIDs, dtype='object')
        io.savemat(tkbox_dir, {'tkBoxes': self.tkBoxes})
        io.savemat(tkid_dir, {'tkIDs': self.tkIDs})

        HOTA, DETA, ASSA, hacc, dacc, aacc = self.eng.evalhota(
            tkbox_dir, tkid_dir, gt_dir, nargout=6)

        print('Name={}, HOTA={:4f}, DETA={:4f}, ASSA={:4f}\n'.format(self.name, HOTA, DETA, ASSA))
        plt.plot(hacc, color='red', label='HOTA')
        plt.plot(dacc, color='green', label='Detection Accuracy')
        plt.plot(aacc, color='blue', label='Association Accuracy')
        plt.legend()

        result_csv_name = "result.csv"
        with open(os.path.join(self.folder, result_csv_name), 'a') as csv_file:
            writer = csv.writer(csv_file)
            writer.writerow([tkbox_dir, HOTA])

        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        videoWriter = cv2.VideoWriter(
            video_dir, fourcc, 5, (self.lx, self.ly), True)
        for i in range(self.pFrame):
            videoWriter.write(self.frames[i])
        videoWriter.release()

    def Process(self, animation=True):
        print("Split: ", self.split)
        print("Merge: ", self.merge)

        for i in tqdm(range(self.events.shape[0])):
            x = self.events[i, 0]
            y = self.events[i, 1]
            p = self.events[i, 2]
            ts = self.events[i, 3]
            f = self.events[i, 4]

            if ts > self.t and len(self.AUs) >= 1:
                self.t = self.t + self.tFrame

                # self.Shrink()
                self.Update()
                self.Split()
                self.Merge()
                self.Kill(ts)

            if animation:
                self.animation = True
                self.Animation(x, y, p, ts, f)

            exist_interested = False

            if len(self.AUs) >= 1:
                for au in self.AUs:
                    if au.interested(x, y):
                        exist_interested = True
                        au.process(x, y, ts)

            if not exist_interested and len(self.AUs) < self.auNum:
                newau = AU(np.array([[x, y, ts]]), self.lx,
                           self.ly, self.dAdd, self.auFifo)
                newau.auBox = [x, y, x + 1, y + 1]
                newau.auNumber = [0, ts]
                self.AUs.append(newau)

class AU():
    def __init__(self, events, lx, ly, dAdd, auFifo):
        self.lx = lx
        self.ly = ly
        self.dAdd = dAdd
        self.auFifo = auFifo

        self.auEvents = events
        self.auMap = np.zeros((self.ly, self.lx), 'int32')
        self.auBox = [0, 0, 0, 0]
        self.auNumber = [0, 0]
        for i in range(events.shape[0]):
            self.expand(events[i, 0], events[i, 1])

    def interested(self, x, y):
        return self.auMap[y, x] > 0

    def expand(self, x, y):
        x = int(x)
        y = int(y)
        idxx = np.arange(max(x - self.dAdd, 0),
                         min(x + self.dAdd + 1, self.lx))
        idxy = np.arange(max(y - self.dAdd, 0),
                         min(y + self.dAdd + 1, self.ly))
        idxxx, idxyy = np.meshgrid(idxx, idxy)
        self.auMap[idxyy, idxxx] += 1

    def shrink(self, x, y):
        x = int(x)
        y = int(y)
        idxx = np.arange(max(x - self.dAdd, 0),
                         min(x + self.dAdd + 1, self.lx))
        idxy = np.arange(max(y - self.dAdd, 0),
                         min(y + self.dAdd + 1, self.ly))
        idxxx, idxyy = np.meshgrid(idxx, idxy)
        self.auMap[idxyy, idxxx] -= 1

    def process(self, x, y, ts):
        self.expand(x, y)

        self.auEvents = np.insert(
            self.auEvents, self.auEvents.shape[0], [x, y, ts], axis=0)

        if self.auEvents.shape[0] > self.auFifo:
            idxFade = range(self.auEvents.shape[0] - self.auFifo)
            for i in idxFade:
                self.shrink(self.auEvents[i, 0], self.auEvents[i, 1])
            self.auEvents = np.delete(self.auEvents, idxFade, axis=0)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--config",   default='./config/shape_6dof.yml')
    parser.add_argument("--auFifo", type=int)
    parser.add_argument("--auNum", type=int)
    parser.add_argument("--dAdd", type=int)
    parser.add_argument("--tDel", type=int)
    parser.add_argument("--areaDel", type=int)
    parser.add_argument("--tLive", type=int)
    parser.add_argument("--areaLive", type=int)
    parser.add_argument("--numLive", type=int)
    parser.add_argument("--epsDiv", type=int)
    parser.add_argument("--iomMer", type=float)
    parser.add_argument("--dsMer", type=int)
    parser.add_argument("--split", choices=['DBSCAN', 'HAC'])
    parser.add_argument("--merge", choices=['IOM', 'DIST'])
    parser.add_argument("--input")
    parser.add_argument("--name")
    parser.add_argument("--outfolder")

    args = parser.parse_args()
    with open(args.config, "r") as file:
        config = yaml.safe_load(file)

    args = vars(args)
    config.update({k: v for k, v in args.items() if v is not None})

    config = ARGS(config)
    controller = Controller(config)
    controller.Process()
    controller.SaveResults()

if __name__ == "__main__":
    main()

reason: import numpy,from scipy


text:
#encoding:utf-8

from PIL import Image
import os
import cv2
from numpy import *
from pylab import *

def read_features_from_file(imgname):
    im = cv2.imread(imgname)
    im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    sift = cv2.SIFT()

    kp, desc = sift.detectAndCompute(im,None)
    return kp, desc

def plot_features(im,kp,circle=False):
    """ show image with features. input: im (image as array),
        locs (row, col, scale, orientation of each feature) """
    if circle:
        img=cv2.drawKeypoints(im,kp,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    else:
        img=cv2.drawKeypoints(im,kp)

    imshow(img)
    axis('off')

def match(desc1,desc2):
    """ 对第一副图像的每个描述子,找到在第二幅图像中的匹配 OpenCV 版"""

    dist_ratio = 0.6
    desc1_size = desc1.shape

    # BFMatcher with default params
    bf = cv2.BFMatcher()
    matches = bf.knnMatch(desc1,desc2, k=2)

    # Apply ratio test
    good = []
    for m,n in matches:
        if m.distance < dist_ratio*n.distance:
            good.append(m)

    # 仿照原书的输出
    matchscores = zeros((desc1_size[0],1))
    for m in good:
        matchscores[m.queryIdx] = m.trainIdx

    return matchscores

def appendimages(im1,im2):
    """ return a new image that appends the two images side-by-side."""

    #select the image with the fewest rows and fill in enough empty rows
    rows1 = im1.shape[0]
    rows2 = im2.shape[0]

    if rows1 < rows2:
        im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1]))), axis=0)
    elif rows1 > rows2:
        im2 = concatenate((im2,zeros((rows1-rows2,im2.shape[1]))), axis=0)
    #if none of these cases they are equal, no filling needed.

    return concatenate((im1,im2), axis=1)

def plot_matches(im1,im2,kp1,kp2,matchscores,show_below=True):
    """ OpwnCV 版:
        show a figure with lines joining the accepted matches
        input: im1,im2 (images as arrays), locs1,locs2 (location of features),
        matchscores (as output from 'match'), show_below (if images should be shown below). """

    im3 = appendimages(im1,im2)
    if show_below:
        im3 = vstack((im3,im3))

    # show image
    imshow(im3)

    # draw lines for matches
    cols1 = im1.shape[1]
    for i in range(len(matchscores)):
        if matchscores[i] > 0:
            pt1 = kp1[i].pt
            pt2 = kp2[int(matchscores[i,0])].pt
            plot([pt1[0],pt2[0]+cols1], [pt1[1], pt2[1]], 'c')
    axis('off')

def match_twosided(desc1,desc2):
    """ two-sided symmetric version of match(). """

    matches_12 = match(desc1,desc2)
    matches_21 = match(desc2,desc1)

    ndx_12 = matches_12.nonzero()[0]

    #remove matches that are not symmetric
    for n in ndx_12:
        if matches_21[int(matches_12[n])] != n:
            matches_12[n] = 0

    return matches_12

if __name__ == "__main__":

    process_image('box.pgm','tmp.sift')
    l,d = read_features_from_file('tmp.sift')

    im = array(Image.open('box.pgm'))
    figure()
    plot_features(im,l,True)
    gray()

    process_image('scene.pgm','tmp2.sift')
    l2,d2 = read_features_from_file('tmp2.sift')
    im2 = array(Image.open('scene.pgm'))

    m = match_twosided(d,d2)
    figure()
    plot_matches(im,im2,l,l2,m)

    gray()
    show()

max_stars_repo_name: LMCallMe/PYCVReading

max_issues_repo_name: LMCallMe/PYCVReading

max_forks_repo_name: LMCallMe/PYCVReading