text:
from typing import Any, Tuple, Union

import numpy as np
import xarray as xr

from .. import conversion
from ..typing import BoolArray, FloatArray, FloatDType, IntArray, IntDType
from . import ugrid_io
from .ugridbase import AbstractUgrid

class Ugrid1d(AbstractUgrid):
    """
    This class stores the topological data of a "1-D unstructured grid": a
    collection of connected line elements, such as a river network.

    Parameters
    ----------
    node_x: ndarray of floats
    node_y: ndarray of floats
    fill_value: int
    edge_node_connectivity: ndarray of integers
    dataset: xr.Dataset, optional
    name: string, optional
        Network name. Defaults to "network1d" in dataset.
    crs: Any, optional
        Coordinate Reference System of the geometry objects. Can be anything accepted by
        :meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
        such as an authority string (eg "EPSG:4326") or a WKT string.
    """

    def __init__(
        self,
        node_x: FloatArray,
        node_y: FloatArray,
        fill_value: int,
        edge_node_connectivity: IntArray = None,
        dataset: xr.Dataset = None,
        name: str = None,
        crs: Any = None,
    ):
        self.node_x = np.ascontiguousarray(node_x)
        self.node_y = np.ascontiguousarray(node_y)
        self.fill_value = fill_value
        self.edge_node_connectivity = edge_node_connectivity
        if name is None:
            name = ugrid_io.UGRID1D_DEFAULT_NAME
        self.name = name
        self.topology_attrs = None

        # Optional attributes, deferred initialization
        # Meshkernel
        self._mesh = None
        self._meshkernel = None
        # Celltree
        self._celltree = None
        # Bounds
        self._xmin = None
        self._xmax = None
        self._ymin = None
        self._ymax = None
        # Edges
        self._edge_x = None
        self._edge_y = None
        # Connectivity
        self._node_edge_connectivity = None
        # crs
        if crs is None:
            self.crs = None
        else:
            import pyproj

            self.crs = pyproj.CRS.from_user_input(crs)
        # Store dataset
        if dataset is None:
            dataset = self.topology_dataset()
        self.dataset = dataset
        self.topology_attrs = dataset[self.name].attrs

    @staticmethod
    def from_dataset(dataset: xr.Dataset):
        """
        Extract the 1D UGRID topology information from an xarray Dataset.

        Parameters
        ----------
        dataset: xr.Dataset
            Dataset containing topology information stored according to UGRID conventions.

        Returns
        -------
        grid: Ugrid1d
        """
        ds = dataset
        if not isinstance(ds, xr.Dataset):
            raise TypeError(
                "Ugrid should be initialized with xarray.Dataset. "
                f"Received instead: {type(ds)}"
            )

        # Collect names
        mesh_topology = ugrid_io.get_topology_variable(ds)
        ugrid_roles = ugrid_io.get_ugrid2d_variables(dataset, mesh_topology)
        # Rename the arrays to their standard UGRID roles
        topology_ds = ds[set(ugrid_roles.keys())]
        ugrid_ds = topology_ds.rename(ugrid_roles)
        # Coerce type and fill value
        ugrid_ds["node_x"] = ugrid_ds["node_x"].astype(FloatDType)
        ugrid_ds["node_y"] = ugrid_ds["node_y"].astype(FloatDType)
        fill_value = -1
        ugrid_ds["edge_node_connectivity"] = ugrid_ds["edge_node_connectivity"].astype(
            IntDType
        )

        return Ugrid1d(
            ugrid_ds["node_x"].values,
            ugrid_ds["node_y"].values,
            fill_value,
            ugrid_ds["edge_node_connectivity"].values,
            topology_ds,
            mesh_topology.name,
        )

    def remove_topology(
        self, obj: Union[xr.DataArray, xr.Dataset]
    ) -> Union[xr.DataArray, xr.Dataset]:
        """
        Remove UGRID specific variables from the object.

        Parameters
        ----------
        obj: Union[xr.DataArray, xr.Dataset]

        Returns
        -------
        sanitized: Union[xr.DataArray, xr.Dataset]
        """
        return self._remove_topology(obj, ugrid_io.UGRID1D_TOPOLOGY_VARIABLES)

    def topology_coords(self, obj: Union[xr.DataArray, xr.Dataset]) -> dict:
        """
        Return a dictionary with the coordinates required for the dimension of
        the object.

        Parameters
        ----------
        obj: Union[xr.DataArray, xr.Dataset]

        Returns
        -------
        coords: dict
        """
        coords = {}
        dims = obj.dims
        attrs = self.topology_attrs
        edgedim = self.edge_dimension
        nodedim = self.node_dimension
        if edgedim in dims:
            # It's not a required attribute.
            if "edge_coordinates" in attrs:
                name_x, name_y = attrs["edge_coordinates"].split()
            else:
                name_x = f"{self.name}_edge_x"
                name_y = f"{self.name}_edge_y"
            coords[name_x] = (edgedim, self.edge_x)
            coords[name_y] = (edgedim, self.edge_y)
            attrs["edge_coordinates"] = f"{name_x} {name_y}"
        if nodedim in dims:
            name_x, name_y = attrs["node_coordinates"].split()
            coords[name_x] = (nodedim, self.node_x)
            coords[name_y] = (nodedim, self.node_y)
        return coords

    def topology_dataset(self):
        """
        Store the UGRID topology information in an xarray Dataset according to
        the UGRID conventions.

        Returns
        -------
        ugrid_topology: xr.Dataset
        """
        return ugrid_io.ugrid1d_dataset(
            self.node_x,
            self.node_y,
            self.fill_value,
            self.edge_node_connectivity,
            self.name,
            self.topology_attrs,
        )

    def _clear_geometry_properties(self):
        """Clear all properties that may have been invalidated"""
        # Meshkernel
        self._mesh = None
        self._meshkernel = None
        # Celltree
        self._celltree = None
        # Bounds
        self._xmin = None
        self._xmax = None
        self._ymin = None
        self._ymax = None
        # Edges
        self._edge_x = None
        self._edge_y = None

    # These are all optional UGRID attributes. They are not computed by
    # default, only when called upon.
    @property
    def topology_dimension(self):
        """Highest dimensionality of the geometric elements: 1"""
        return 1

    def _get_dimension(self, dim):
        key = f"{dim}_dimension"
        if key not in self.topology_attrs:
            self.topology_attrs[key] = ugrid_io.UGrid.mesh1d_get_attributes(self.name)[
                key
            ]
        return self.topology_attrs[key]

    @property
    def mesh(self) -> "mk.Mesh1d":  # type: ignore # noqa
        """
        Create if needed, and return meshkernel Mesh1d object.

        Returns
        -------
        mesh: meshkernel.Mesh1d
        """
        import meshkernel as mk

        edge_nodes = self.edge_node_connectivity.ravel().astype(np.int32)

        if self._mesh is None:
            self._mesh = mk.Mesh1d(
                node_x=self.node_x,
                node_y=self.node_y,
                edge_nodes=edge_nodes,
            )
        return self._mesh

    @property
    def meshkernel(self) -> "mk.MeshKernel":  # type: ignore # noqa
        """
        Create if needed, and return meshkernel MeshKernel instance.

        Returns
        -------
        meshkernel: meshkernel.MeshKernel
        """
        import meshkernel as mk

        if self._meshkernel is None:
            self._meshkernel = mk.MeshKernel(is_geographic=False)
            self._meshkernel.mesh1d_set(self.mesh)
        return self._meshkernel

    @staticmethod
    def from_geodataframe(geodataframe: "geopandas.GeoDataFrame") -> "Ugrid1d":  # type: ignore # noqa
        """
        Convert geodataframe of linestrings into a UGRID1D topology.

        Parameters
        ----------
        geodataframe: gpd.GeoDataFrame

        Returns
        -------
        topology: Ugrid1d
        """
        x, y, edge_node_connectivity = conversion.linestrings_to_edges(
            geodataframe.geometry
        )
        fill_value = -1
        return Ugrid1d(x, y, fill_value, edge_node_connectivity)

    def to_pygeos(self, dim):
        """
        Convert UGRID topology to pygeos objects.

        * nodes: points
        * edges: linestrings

        Parameters
        ----------
        dim: str
            Node or edge dimension.

        Returns
        -------
        geometry: ndarray of pygeos.Geometry
        """
        if dim == self.node_dimension:
            return conversion.nodes_to_points(
                self.node_x,
                self.node_y,
            )
        elif dim == self.edge_dimension:
            return conversion.edges_to_linestrings(
                self.node_x,
                self.node_y,
                self.edge_node_connectivity,
            )
        else:
            raise ValueError(
                f"Dimension {dim} is not a node or edge dimension of the"
                " Ugrid1d topology."
            )

    def _validate_indexer(self, indexer) -> Tuple[float, float]:
        if isinstance(indexer, slice):
            if indexer.step is not None:
                raise ValueError("Ugrid1d does not support steps in slices")
            if indexer.start >= indexer.stop:
                raise ValueError("slice start should be smaller than slice stop")
        else:
            raise ValueError("Ugrid1d only supports slice indexing")
        return indexer.start, indexer.stop

    def sel(self, x, y) -> Tuple[str, bool, IntArray, dict]:
        """
        Select a selection of edges, based on edge centroids.

        Parameters
        ----------
        x: slice
        y: slice

        Returns
        -------
        dimension: str
        as_ugrid: bool
        index: 1d array of integers
        coords: dict
        """
        xmin, xmax = self._validate_indexer(x)
        ymin, ymax = self._validate_indexer(y)
        index = np.nonzero(
            (self.edge_x >= xmin)
            & (self.edge_x < xmax)
            & (self.edge_y >= ymin)
            & (self.edge_y < ymax)
        )[0]
        return self.edge_dimension, True, index, {}

    def topology_subset(self, edge_index: Union[BoolArray, IntArray]):
        """
        Create a new UGRID1D topology for a subset of this topology.

        Parameters
        ----------
        edge_index: 1d array of integers or bool
            Edges of the subset.

        Returns
        -------
        subset: Ugrid1d
        """
        edge_index = np.atleast_1d(edge_index)
        return self._topology_subset(edge_index, self.edge_node_connectivity)

max_stars_repo_name: Deltares/xugrid

max_issues_repo_name: Deltares/xugrid

max_forks_repo_name: Deltares/xugrid


text:
#py_numpy_descriptives.py
import numpy as np
import pandas as pd
data=np.random.gumbel(size=1000)  #used for 'fat tailed' distributions

df=pd.DataFrame({'a':data, 'b':data*2})

#basic descriptives with describe()
d= df.describe()
print()
print(d)
print()
print(d['a']['mean'])
print()

#... or use min(), max(), mean(), std()
print(df.mean()['a'])
print()
print(df.min())
print()
print(df.kurtosis())
print()

from scipy import stats
dstats=stats.describe(df)
print(dstats)
print()
dstats.minmax[1][0]
print()
print(dstats.mean[0])
print()

#some tests
sn = stats.normaltest(df['a'])
print(sn)
print()
print(sn.statistic)
print()

#t-test for independent samples (are they really ind. based on distros?)
ttind= stats.ttest_ind(df['a'], df['b'])
print(ttind)
print()

#help on the t-test for related samples
help(stats.ttest_rel)

x=1

ext: py

reason: import numpy,from scipy


text:
import numpy as np
import pandas as pd
from stockstats import StockDataFrame as Sdf
from finrl.config import config

class FeatureEngineer:
    """Provides methods for preprocessing the stock price data

    Attributes
    ----------
        df: DataFrame
            data downloaded from Yahoo API
            7 columns: A date, open, high, low, close, volume and tick symbol
            for the specified stock ticker
        use_technical_indicator : boolean
            we technical indicator or not
        tech_indicator_list : list
            a list of technical indicator names (modified from config.py)
        use_turbulence : boolean
            use turbulence index or not
        user_defined_feature:boolean
            user user defined features or not

    Methods
    -------
    preprocess_data()
        main method to do the feature engineering

    """
    def __init__(self,
        df,
        use_technical_indicator=True,
        tech_indicator_list = config.TECHNICAL_INDICATORS_LIST,
        use_turbulence=False,
        user_defined_feature=False):

        self.df = df
        self.use_technical_indicator = use_technical_indicator
        self.tech_indicator_list = tech_indicator_list
        self.use_turbulence=use_turbulence
        self.user_defined_feature=user_defined_feature

        #type_list = self._get_type_list(5)
        #self.__features = type_list
        #self.__data_columns = config.DEFAULT_DATA_COLUMNS + self.__features

    def preprocess_data(self):
        """main method to do the feature engineering
        @:param config: source dataframe
        @:return: a DataMatrices object
        """
        df = self.df.copy()

        # add technical indicators
        # stockstats require all 5 columns
        if (self.use_technical_indicator==True):
            # add technical indicators using stockstats
            df=self.add_technical_indicator(df)
            print("Successfully added technical indicators")

        # add turbulence index for multiple stock
        if self.use_turbulence==True:
            df = self.add_turbulence(df)
            print("Successfully added turbulence index")

        # add user defined feature
        if self.user_defined_feature == True:
            df = self.add_user_defined_feature(df)
            print("Successfully added user defined features")

        # fill the missing values at the beginning and the end
        df=df.fillna(method='bfill').fillna(method="ffill")
        return df

    def add_technical_indicator(self, data):
        """
        calcualte technical indicators
        use stockstats package to add technical inidactors
        :param data: (df) pandas dataframe
        :return: (df) pandas dataframe
        """
        df = data.copy()
        stock = Sdf.retype(df.copy())
        unique_ticker = stock.tic.unique()

        for indicator in self.tech_indicator_list:
            indicator_df = pd.DataFrame()
            for i in range(len(unique_ticker)):
                try:
                    temp_indicator = stock[stock.tic == unique_ticker[i]][indicator]
                    temp_indicator= pd.DataFrame(temp_indicator)
                    indicator_df = indicator_df.append(temp_indicator, ignore_index=True)
                except Exception as e:
                    print(e)
            df[indicator] = indicator_df
        return df

    def add_user_defined_feature(self, data):
        """
         add user defined features
        :param data: (df) pandas dataframe
        :return: (df) pandas dataframe
        """
        df = data.copy()
        df['daily_return']=df.close.pct_change(1)
        #df['return_lag_1']=df.close.pct_change(2)
        #df['return_lag_2']=df.close.pct_change(3)
        #df['return_lag_3']=df.close.pct_change(4)
        #df['return_lag_4']=df.close.pct_change(5)
        return df

    def add_turbulence(self, data):
        """
        add turbulence index from a precalcualted dataframe
        :param data: (df) pandas dataframe
        :return: (df) pandas dataframe
        """
        df = data.copy()
        turbulence_index = self.calcualte_turbulence(df)
        df = df.merge(turbulence_index, on='date')
        df = df.sort_values(['date','tic']).reset_index(drop=True)
        return df

    def calcualte_turbulence(self, data):
        """calculate turbulence index based on dow 30"""
        # can add other market assets
        df = data.copy()
        df_price_pivot=df.pivot(index='date', columns='tic', values='close')
        unique_date = df.date.unique()
        # start after a year
        start = 252
        turbulence_index = [0]*start
        #turbulence_index = [0]
        count=0
        for i in range(start,len(unique_date)):
            current_price = df_price_pivot[df_price_pivot.index == unique_date[i]]
            hist_price = df_price_pivot[[n in unique_date[0:i] for n in df_price_pivot.index ]]
            cov_temp = hist_price.cov()
            current_temp=(current_price - np.mean(hist_price,axis=0))
            temp = current_temp.values.dot(np.linalg.inv(cov_temp)).dot(current_temp.values.T)
            if temp>0:
                count+=1
                if count>2:
                    turbulence_temp = temp[0][0]
                else:
                    #avoid large outlier because of the calculation just begins
                    turbulence_temp=0
            else:
                turbulence_temp=0
            turbulence_index.append(turbulence_temp)

        turbulence_index = pd.DataFrame({'date':df_price_pivot.index,
                                         'turbulence':turbulence_index})
        return turbulence_index

    def _get_type_list(self, feature_number):
        """
        :param feature_number: an int indicates the number of features
        :return: a list of features n
        """
        if feature_number == 1:
            type_list = ["close"]
        elif feature_number == 2:
            type_list = ["close", "volume"]
            #raise NotImplementedError("the feature volume is not supported currently")
        elif feature_number == 3:
            type_list = ["close", "high", "low"]
        elif feature_number == 4:
            type_list = ["close", "high", "low", "open"]
        elif feature_number == 5:
            type_list = ["close", "high", "low", "open","volume"]
        else:
            raise ValueError("feature number could not be %s" % feature_number)
        return type_list

max_stars_repo_name: navinkrishna-tb/FinRL-Library

max_issues_repo_name: navinkrishna-tb/FinRL-Library

max_forks_repo_name: navinkrishna-tb/FinRL-Library

reason: import numpy


text:
# coding: UTF-8
"""
Tests for class Ahp
"""

import pytest
import numpy as np
import numpy.testing as npt

from yeahp.ahp import Ahp
from yeahp.criterion import Criterion

pytestmark = [pytest.mark.yeahp_compute]

@pytest.fixture(scope="module", name="ahp_obj")
def ahp_obj_fixture():
    goal = "goal"

    tree = {
        "Criterion #1": None,
        "Criterion #2": {
            "Sub-criterion #21": None,
            "Sub-criterion #22": None,
            "Sub-criterion #23": None
        },
        "Criterion #3": None
    }

    alternatives = ["alternative #2", "alternative #3", "alternative #1"]

    return Ahp(goal=goal, tree=tree, alternatives=alternatives)

def test_create(ahp_obj):
    """
    **Success testing**
    """

    assert ahp_obj.goal.description == "goal"
    assert ahp_obj.goal.priority == 1.

    assert ahp_obj.criteria_table[0].description == "goal"
    assert ahp_obj.criteria_table[1].description == "Criterion #1"
    assert ahp_obj.criteria_table[2].description == "Criterion #2"
    assert ahp_obj.criteria_table[3].description == "Criterion #3"
    assert ahp_obj.criteria_table[4].description == "Sub-criterion #21"
    assert ahp_obj.criteria_table[5].description == "Sub-criterion #22"
    assert ahp_obj.criteria_table[6].description == "Sub-criterion #23"

    assert set(ahp_obj.criteria_hash) == {"goal", "Criterion #1", "Criterion #2", "Criterion #3", "Sub-criterion #21",
                                          "Sub-criterion #22", "Sub-criterion #23"}

    assert [crit.is_covering() for crit in ahp_obj.criteria_table] == [False, True, False, True, True, True, True]

    assert [alt.description for alt in ahp_obj.alternatives] == ["alternative #1", "alternative #2", "alternative #3"]

# TODO: Check that criteria have distinct names

def test_compare_criteria(ahp_obj):
    """
    **Success testing**
       Cross-comparison of a set of child criteria against their parent

    :param ahp_obj:
    :type ahp_obj: Ahp
    """
    parent = ahp_obj.criteria_table[0]

    comp = [
        ["Criterion #1", "Criterion #2", 3],
        ["Criterion #1", "Criterion #3", 7],
        ["Criterion #2", "Criterion #3", 3]
    ]

    ahp_obj._compare_criteria(parent=parent, comparisons=comp)

    # Test the computation of priorities assigned to criteria
    ref_matrix = np.array(
        [
            [1, 3, 7],
            [1 / 3, 1, 3],
            [1 / 7, 1 / 3, 1]
        ]
    )
    eig_val, eig_vect = np.linalg.eig(ref_matrix)
    eig_max_ind = eig_val.argmax()
    priorities = eig_vect[:, eig_max_ind]
    ref_priorities = np.real(priorities / priorities.sum())  # Normalization
    crit1 = ahp_obj.criteria_table[1]  # type: Criterion
    crit2 = ahp_obj.criteria_table[2]  # type: Criterion
    crit3 = ahp_obj.criteria_table[3]  # type: Criterion
    npt.assert_array_almost_equal_nulp(ref_priorities[0], crit1.priority)
    npt.assert_array_almost_equal_nulp(ref_priorities[1], crit2.priority)
    npt.assert_array_almost_equal_nulp(ref_priorities[2], crit3.priority)

def test_hierarchical_comparison(ahp_obj):
    """
    **Success testing**
       Complete hierarchical cross-comparison of criteria against their parent and hierarchical weighting of priorities.

    :param ahp_obj:
    :type ahp_obj: Ahp
    """

    comparisons = {
        "goal": [
            ["Criterion #1", "Criterion #2", 3],
            ["Criterion #1", "Criterion #3", 7],
            ["Criterion #2", "Criterion #3", 3]
        ],
        "Criterion #2": [
            ["Sub-criterion #21", "Sub-criterion #22", 1 / 3],
            ["Sub-criterion #21", "Sub-criterion #23", 1],
            ["Sub-criterion #22", "Sub-criterion #23", 3]
        ]
    }

    ahp_obj.hierarchical_compare(comparisons=comparisons)

    crit1 = ahp_obj.criteria_table[1]  # type: Criterion
    crit2 = ahp_obj.criteria_table[2]  # type: Criterion
    crit3 = ahp_obj.criteria_table[3]  # type: Criterion
    crit21 = ahp_obj.criteria_table[4]  # type: Criterion
    crit22 = ahp_obj.criteria_table[5]  # type: Criterion
    crit23 = ahp_obj.criteria_table[6]  # type: Criterion

    # Comparisons against 'goal'
    ref_matrix = np.array(
        [
            [1, 3, 7],
            [1 / 3, 1, 3],
            [1 / 7, 1 / 3, 1]
        ]
    )
    eig_val, eig_vect = np.linalg.eig(ref_matrix)
    eig_max_ind = eig_val.argmax()
    priorities = eig_vect[:, eig_max_ind]
    ref_priorities = np.real(priorities / priorities.sum())  # Normalization
    npt.assert_array_almost_equal_nulp(ref_priorities[0], crit1.priority)
    npt.assert_array_almost_equal_nulp(ref_priorities[1], crit2.priority)
    npt.assert_array_almost_equal_nulp(ref_priorities[2], crit3.priority)

    # Comparisons against 'Criterion #2'
    ref_matrix = np.array(
        [
            [1, 1 / 3, 1],
            [3, 1, 3],
            [1, 1 / 3, 1]
        ]
    )
    eig_val, eig_vect = np.linalg.eig(ref_matrix)
    eig_max_ind = eig_val.argmax()
    priorities = eig_vect[:, eig_max_ind]
    ref_priorities = np.real(priorities / priorities.sum())  # Normalization
    ref_priorities *= crit2.priority  # Hierarchical weighting
    npt.assert_array_almost_equal_nulp(ref_priorities[0], crit21.priority)
    npt.assert_array_almost_equal_nulp(ref_priorities[1], crit22.priority)
    npt.assert_array_almost_equal_nulp(ref_priorities[2], crit23.priority)

    # Priorities of all covering criteria must sum to one
    priorities = np.array([crit.priority for crit in ahp_obj.criteria_table if crit.is_covering()], dtype="float64")
    npt.assert_array_almost_equal_nulp(1., priorities.sum(), nulp=1)

def test_hierarchical_comparison_failure1(ahp_obj):
    """
    **Failure testing**
       Raise exception when a key-label refer to a covering criterion
    """
    comparisons = {
        "goal": [
            ["Criterion #1", "Criterion #2", 3],
            ["Criterion #1", "Criterion #3", 7],
            ["Criterion #2", "Criterion #3", 3]
        ],
        "Criterion #1": [
            ["Sub-criterion #21", "Sub-criterion #22", 1 / 3],
            ["Sub-criterion #21", "Sub-criterion #23", 1],
            ["Sub-criterion #22", "Sub-criterion #23", 3]
        ]
    }

    with pytest.raises(NameError) as excinfo:
        ahp_obj.hierarchical_compare(comparisons=comparisons)
    assert 'Cannot make comparisons against a covering criterion' in str(excinfo.value)

def test_hierarchical_comparison_failure2(ahp_obj):
    """
    **Failure testing**
       Raise exception when a key-label refer to a covering criterion
    """
    comparisons = {
        "goal": [
            ["Criterion #1", "Criterion #2", 3],
            ["Criterion #1", "Criterion #3", 7],
            ["Criterion #2", "Criterion #3", 3]
        ],
        "Criterion #2": [
            ["Sub-criterion #21", "Sub-criterion #22", 1 / 3],
            ["Sub-criterion #21", "Sub-criterion #23", 1],
            ["Sub-criterion #22", "wrong label", 3]
        ]
    }

    with pytest.raises(AssertionError):
        ahp_obj.hierarchical_compare(comparisons=comparisons)

def test_alternatives_comparison(ahp_obj):
    """
    **Success testing**
       Comparison of alternatives against covering criteria

    :param ahp_obj:
    :type ahp_obj: Ahp
    """

    crit_priorities = {
        'Criterion #1': 0.66941686944898771,
        'Criterion #3': 0.087946208819056057,
        'Sub-criterion #22': 0.14558215303917371,
        'Sub-criterion #21': 0.048527384346391249,
        'Sub-criterion #23': 0.048527384346391249
    }

    for descr in crit_priorities:
        ahp_obj.criteria_hash[descr].priority = crit_priorities[descr]

    comparisons = {
        'Criterion #1': [
            ['alternative #1', 'alternative #2', 3],
            ['alternative #1', 'alternative #3', 1 / 2],
            ['alternative #2', 'alternative #3', 1 / 9],
        ],
        'Sub-criterion #21': [
            ['alternative #1', 'alternative #2', 3],
            ['alternative #1', 'alternative #3', 1],
            ['alternative #2', 'alternative #3', 1 / 3],
        ],
        'Sub-criterion #22': [
            ['alternative #1', 'alternative #2', 1 / 2],
            ['alternative #1', 'alternative #3', 1 / 2],
            ['alternative #2', 'alternative #3', 1],
        ],
        'Sub-criterion #23': [
            ['alternative #1', 'alternative #2', 1 / 9],
            ['alternative #1', 'alternative #3', 1 / 9],
            ['alternative #2', 'alternative #3', 1],
        ],
        'Criterion #3': [
            ['alternative #1', 'alternative #2', 1],
            ['alternative #1', 'alternative #3', 3],
            ['alternative #2', 'alternative #3', 3],
        ],
    }

    ahp_obj.alternatives_compare(comparisons=comparisons)

    np.testing.assert_array_almost_equal_nulp(np.array(list(ahp_obj.goal_properties.values())).sum(), 1.)

def test_alternatives_comparison_failure(ahp_obj):
    """
    **Failure testing**
       Fails if the argument *comparison* does not refer to a proper criterion description

    :param ahp_obj:
    :type ahp_obj: Ahp
    """

    comparisons = {
        'Criterion @$': None,
        'Sub-criterion #21': None,
        'Sub-criterion #22': None,
        'WRONG CRITERION DESCRIPTION': None,
        'Criterion #3': None
    }

    with pytest.raises(NameError) as excinfo:
        ahp_obj.alternatives_compare(comparisons=comparisons)
    assert 'The description(s): \'Criterion @$\', \'WRONG CRITERION DESCRIPTION\'\ndo(es)' + \
           ' not match any criterion in the AHP tree' in str(excinfo.value)

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

max_stars_repo_name: flaudanum/Yet-another-AHP

max_issues_repo_name: flaudanum/Yet-another-AHP

max_forks_repo_name: flaudanum/Yet-another-AHP


text:
import numpy as np
import numpy.matlib as npm

def QAscores_5Bands(test_Rrs, test_lambda):
    '''Quality assurance system for Rrs spectra (Version 2.0)'''

    # function [maxCos, cos, clusterID, totScore] = QAscores_5Bands(test_Rrs, test_lambda)
    #
    # In this version, five-band Rrs at 412, 443, 488, 551 and 670 nm are used
    # in order to create comparable QA scores for different instruments (e.g., SeawiFS, MODISA, MEIRS, VIIRS, OLCI).
    #
    # ------------------------------------------------------------------------------
    # KNOWN VARIABLES :   ref_nRrs   -- Normalized Rrs spectra per-determined from water clustering (23x5 matrix)
    #                     ref_lambda -- Wavelengths for ref_nRrs (1x5 matrix)
    #                     upB        -- Upper boundary (23x5 matrix)
    #                     lowB       -- Lower boundary (23x5 matrix)
    #
    # INPUTS:            test_Rrs -- matrix (inRow*inCol), each row represents one Rrs spectrum; numpy array
    #                    test_lambda-- Wavelengths for test_Rrs; numpy array
    #
    # OUTPUTS:  maxCos     -- maximum cosine values
    #           cos        -- cosine values for every ref_nRrs spectra
    #           clusterID  -- idenfification of water types (from 1-23)
    #           totScore   -- total score assigned to test_Rrs
    # ------------------------------------------------------------------------------
    #
    # NOTE:
    #         1) Five wavelengths (412, 443, 488, 551, 670 nm) are assumed in the model
    #         2) If your Rrs data were measured at other wavelength, e.g. 440nm, you may want to change 440 to 443 before the model run;
    #             or modify the code below to find a cloest wavelength from the nine bands.
    #         3) The latest version may be found online at HTTP://oceanoptics.umb.edu/score_metric
    #
    # Reference:
    #         Wei, Lee, and Shang (2016).
    #         A system to measure the data quality of spectral remote sensing
    #         reflectance of aquatic environments. Journal of Geophysical Research,
    #         121, doi:10.1002/2016JC012126
    #
    # ------------------------------------------------------------------------------
    # Note:
    #     1) nanmean, nansum need statistics toolbox
    #     2) on less memory and multi-core system, it may further speedup using
    #        parfor
    #
    # Author: Jianwei Wei, NOAA/NESDIS Center for Satellite Applications and Research
    # Email: Jianwei.Wei@noaa.gov
    #
    # upated January-14-2020
    #
    # Translated from the original Matlab to python by Dirk Aurin, 2020-11-25
    # ------------------------------------------------------------------------------

    ''' Check input data '''
    if test_lambda.ndim > 1:
        row_lam, len_lam = test_lambda.shape
        if row_lam != 1:
            test_lambda = np.transpose(test_lambda)
            row_lam, len_lam = test_lambda.shape
    else:
        row_lam = 1
        len_lam = len(test_lambda)

    row, col = test_Rrs.shape
    if len_lam != col and len_lam != row:
        print('Rrs and lambda size mismatch, please check the input data!')
    elif len_lam == row:
        test_Rrs = np.transpose(test_Rrs)

    ''' 23 Normalized spectral water types '''
    ref_lambda = np.array([412,443,488,551,670])

    ref_nRrs = np.array([ \
        [0.74996,0.54868,0.35803,0.08539,0.00625], \
        [0.71125,0.55973,0.41201,0.11609,0.01041], \
        [0.65672,0.56203,0.47156,0.16365,0.01648], \
        [0.58645,0.55225,0.53263,0.25561,0.03157], \
        [0.49175,0.54116,0.60256,0.30151,0.03564], \
        [0.54323,0.52581,0.55413,0.33867,0.04925], \
        [0.43659,0.49708,0.61338,0.42321,0.05396], \
        [0.47248,0.48128,0.56584,0.46308,0.07609], \
        [0.38299,0.45771,0.61314,0.51734,0.07333], \
        [0.39674,0.43581,0.56188,0.56855,0.10327], \
        [0.46958,0.44289,0.51258,0.54089,0.14956], \
        [0.30719,0.39258,0.56133,0.64019,0.12493], \
        [0.38158,0.37420,0.47828,0.65599,0.21498], \
        [0.26304,0.34679,0.50187,0.69723,0.25485], \
        [0.27801,0.33191,0.50608,0.72609,0.17123], \
        [0.27331,0.31942,0.45465,0.68178,0.38685], \
        [0.20869,0.28428,0.46490,0.75741,0.27950], \
        [0.21723,0.26406,0.42696,0.80451,0.21960], \
        [0.27088,0.25728,0.35480,0.66337,0.53765], \
        [0.07913,0.19509,0.32714,0.59763,0.70357], \
        [0.23025,0.26588,0.39998,0.77532,0.33684], \
        [0.16532,0.21737,0.38643,0.77736,0.41755], \
        [0.18559,0.18272,0.26051,0.74943,0.53272], \
        ])

    upB = np.array([ \
        [0.78838,0.56847,0.38234,0.10635,0.04743], \
        [0.73772,0.57546,0.44515,0.14046,0.02886], \
        [0.68278,0.58352,0.51810,0.22583,0.07175], \
        [0.63377,0.57471,0.57787,0.29936,0.07270], \
        [0.53567,0.57032,0.66046,0.35981,0.06593], \
        [0.57995,0.54816,0.59574,0.40399,0.12234], \
        [0.48114,0.52110,0.65807,0.46495,0.08516], \
        [0.55356,0.51184,0.59875,0.51171,0.12100], \
        [0.42348,0.48890,0.67525,0.56493,0.11500], \
        [0.45994,0.45828,0.59186,0.63773,0.15269], \
        [0.52342,0.47307,0.57051,0.60074,0.26253], \
        [0.36610,0.42668,0.69114,0.68136,0.21055], \
        [0.43550,0.42450,0.53691,0.74009,0.31536], \
        [0.30584,0.40294,0.54691,0.74744,0.32909], \
        [0.35805,0.36806,0.56152,0.77329,0.21430], \
        [0.34431,0.37320,0.49672,0.73019,0.44881], \
        [0.27500,0.32132,0.50907,0.80017,0.35638], \
        [0.30812,0.29993,0.49224,0.87233,0.28781], \
        [0.33783,0.33282,0.44063,0.73547,0.60083], \
        [0.16938,0.22068,0.35378,0.65586,0.89736], \
        [0.33792,0.30652,0.43326,0.84038,0.40450], \
        [0.23603,0.25683,0.42523,0.84354,0.47431], \
        [0.25485,0.22345,0.34304,0.78502,0.61513], \
        ])

    lowB = np.array([ \
        [0.72630,0.53053,0.27434,0.04958,0.00135], \
        [0.68007,0.53213,0.38784,0.09658,0.00305], \
        [0.60823,0.54421,0.43849,0.13122,0.00773], \
        [0.53531,0.51909,0.49380,0.19941,0.01192], \
        [0.44375,0.49115,0.57837,0.24748,0.01842], \
        [0.49180,0.50349,0.51278,0.29531,0.03114], \
        [0.39096,0.46741,0.58006,0.35830,0.03630], \
        [0.42233,0.45802,0.49776,0.41048,0.04643], \
        [0.28442,0.39007,0.58491,0.46353,0.02408], \
        [0.34428,0.38950,0.52425,0.51943,0.05320], \
        [0.40201,0.40230,0.44712,0.48789,0.08806], \
        [0.19270,0.35270,0.50810,0.55867,0.03655], \
        [0.33731,0.32983,0.38686,0.58958,0.13853], \
        [0.22616,0.31012,0.45382,0.63880,0.21410], \
        [0.14776,0.25932,0.43186,0.66844,0.03853], \
        [0.21041,0.28604,0.38211,0.62868,0.31945], \
        [0.10575,0.23727,0.43373,0.71788,0.23281], \
        [0.09434,0.18851,0.36503,0.76615,0.08160], \
        [0.22957,0.24069,0.28999,0.59611,0.46263], \
        [0.04835,0.04480,0.12898,0.38997,0.63381], \
        [0.17148,0.22387,0.29863,0.73839,0.27090], \
        [0.05797,0.15605,0.30338,0.72705,0.33646], \
        [0.11525,0.14618,0.22647,0.69684,0.47248], \
        ])

    refRow, _ = ref_nRrs.shape

    ''' Match the ref_lambda and test_lambda '''
    idx0 = np.empty(len(ref_lambda), dtype='int') # for ref_lambda
    idx1 = np.empty(len(test_lambda), dtype='int') # for test_lambda

    for i, value in enumerate(test_lambda):
        pos, = np.where(ref_lambda == value) #find(ref_lambda==value);
        # if isempty(pos)
        if pos.size > 0:
            idx0[i] = pos.astype(int)
            idx1[i] = i
        else:
            idx1[i] = np.nan

    pos = np.isnan(idx1)
    np.delete(idx1, pos)

    test_lambda = test_lambda[idx1]
    test_Rrs = test_Rrs[:,idx1]
    ref_lambda = ref_lambda[idx0]
    ref_nRrs = ref_nRrs[:,idx0]
    upB = upB[:,idx0]
    lowB = lowB[:,idx0]

    ''' Match the ref_nRrs and test_Rrs '''
    # keep the original value
    test_Rrs_orig = test_Rrs

    ''' Normalization '''
    inRow, inCol = np.shape(test_Rrs)

    # transform spectrum to column, inCol*inRow
    test_Rrs = np.transpose(test_Rrs)
    test_Rrs_orig = np.transpose(test_Rrs_orig)

    # inCol*inRow
    nRrs_denom = np.sqrt(np.nansum(test_Rrs**2, 0))
    # nRrs_denom = repmat(nRrs_denom,[inCol,1]);
    nRrs_denom = npm.repmat(nRrs_denom, inCol, 1)
    nRrs = test_Rrs/nRrs_denom;

    # SAM input, inCol*inRow*refRow
    test_Rrs2 = np.repeat(test_Rrs_orig[:, :, np.newaxis], refRow, axis=2)

    # #for ref Rrs, inCol*refRow*inRow
    # test_Rrs2p = np.moveaxis(test_Rrs2, 2, 1)

    # inCol*inRow*refRow
    nRrs2_denom = np.sqrt(np.nansum(test_Rrs2**2, 0))
    # nRrs2_denom = repeat(nRrs2_denom, inCol, axis=2)
    nRrs2_denom = np.repeat(nRrs2_denom[:,:, np.newaxis], inCol, axis=2)
    nRrs2_denom = np.moveaxis(nRrs2_denom, 2, 0)
    nRrs2 = test_Rrs2/nRrs2_denom
    # inCol*refRow*inRow
    nRrs2 = np.moveaxis(nRrs2, 2, 1)

    ''' Adjust the ref_nRrs, according to the matched wavebands '''
    #row,_  = ref_nRrs.shape

    #### re-normalize the ref_adjusted
    ref_nRrs = np.transpose(ref_nRrs)

    # inCol*refRow*inRow
    ref_nRrs2 = np.repeat(ref_nRrs[:,:, np.newaxis], inRow, axis=2)

    # inCol*refRow*inRow
    ref_nRrs2_denom = np.sqrt(np.nansum(ref_nRrs2**2, 0))
    ref_nRrs2_denom = np.repeat(ref_nRrs2_denom[:,:, np.newaxis], inCol, axis=2)
    ref_nRrs2_denom = np.moveaxis(ref_nRrs2_denom, 2, 0)
    ref_nRrs_corr2 = ref_nRrs2/ref_nRrs2_denom

    ''' Classification '''
    #### calculate the Spectral angle mapper
    # inCol*refRow*inRow
    cos_denom = np.sqrt(np.nansum(ref_nRrs_corr2**2, 0) * np.nansum(nRrs2**2, 0))
    cos_denom = np.repeat(cos_denom[:, :, np.newaxis], inCol, axis=2)
    cos_denom = np.moveaxis(cos_denom, 2, 0)
    cos = (ref_nRrs_corr2*nRrs2)/cos_denom
    # refRow*inRow
    cos = np.sum(cos, 0)

    # 1*inRow
    maxCos = np.amax(cos, axis=0)
    clusterID = np.argmax(cos, axis=0) # finds location of max along an axis, returns int64
    posClusterID = np.isnan(maxCos)

    ''' Scoring '''
    upB_corr = np.transpose(upB)
    lowB_corr = np.transpose(lowB)

    ''' Comparison '''
    # inCol*inRow
    upB_corr2 = upB_corr[:,clusterID] * (1+0.01)
    lowB_corr2 = lowB_corr[:,clusterID] * (1-0.01)
    ref_nRrs2 = ref_nRrs[:,clusterID]

    #normalization
    ref_nRrs2_denom = np.sqrt(np.nansum(ref_nRrs2**2, 0))
    ref_nRrs2_denom = np.transpose(np.repeat(ref_nRrs2_denom[:,np.newaxis], inCol, axis=1))
    upB_corr2 = upB_corr2 / ref_nRrs2_denom
    lowB_corr2 = lowB_corr2 / ref_nRrs2_denom

    upB_diff = upB_corr2 - nRrs
    lowB_diff = nRrs - lowB_corr2

    C = np.empty([inCol,inRow], dtype='float')*0
    pos = np.logical_and(upB_diff>=0, lowB_diff>=0)
    C[pos] = 1

    #process all NaN spectral
    C[:,posClusterID] = np.nan

    totScore = np.nanmean(C, 0)
    clusterID = clusterID.astype('float')
    clusterID[posClusterID] = np.nan
    # Convert from index to water type 1-23
    clusterID = clusterID +1

    return maxCos, cos, clusterID, totScore

max_stars_repo_name: doizuc/HyperInSPACE

max_issues_repo_name: doizuc/HyperInSPACE

max_forks_repo_name: doizuc/HyperInSPACE


text:
"""
@author: Viet Nguyen <nhviet1009@gmail.com>
"""
import argparse

import cv2
import numpy as np

def get_args():
    parser = argparse.ArgumentParser("Image to ASCII")
    parser.add_argument("--input", type=str, default="data/input.jpg", help="Path to input image")
    parser.add_argument("--output", type=str, default="data/output.txt", help="Path to output text file")
    parser.add_argument("--mode", type=str, default="complex", choices=["simple", "complex"],
                        help="10 or 70 different characters")
    parser.add_argument("--num_cols", type=int, default=150, help="number of character for output's width")
    args = parser.parse_args()
    return args

def main(opt):
    if opt.mode == "simple":
        CHAR_LIST = '@%#*+=-:. '
    else:
        CHAR_LIST = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
    num_chars = len(CHAR_LIST)
    num_cols = opt.num_cols
    image = cv2.imread(opt.input)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    height, width = image.shape
    cell_width = width / opt.num_cols
    cell_height = 2 * cell_width
    num_rows = int(height / cell_height)
    if num_cols > width or num_rows > height:
        print("Too many columns or rows. Use default setting")
        cell_width = 6
        cell_height = 12
        num_cols = int(width / cell_width)
        num_rows = int(height / cell_height)

    output_file = open(opt.output, 'w')
    for i in range(num_rows):
        for j in range(num_cols):
            output_file.write(
                CHAR_LIST[min(int(np.mean(image[int(i * cell_height):min(int((i + 1) * cell_height), height),
                                          int(j * cell_width):min(int((j + 1) * cell_width),
                                                                  width)]) * num_chars / 255), num_chars - 1)])
        output_file.write("\n")
    output_file.close()

if __name__ == '__main__':
    opt = get_args()
    main(opt)

max_stars_repo_name: HiroForYou/ASCII-generator

max_issues_repo_name: HiroForYou/ASCII-generator

max_forks_repo_name: HiroForYou/ASCII-generator


text:
#!/usr/bin/env python

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

__all__ = ['plot_scree', 'plot2d_comps', 'plot_multiple_scree', 'lr_decision_boundary']

def lr_decision_boundary(ax, clf, X, y, legend_loc='best'):
  X,y = np.array(X), np.array(y)
  b = clf.intercept_[0]
  w1, w2 = clf.coef_.T

  c = -b/w2
  m = -w1/w2

  xmin, xmax = X[:, 0].min(), X[:, 0].max()
  ymin, ymax = X[:, 1].min(), X[:, 1].max()

  xd = np.array([xmin, xmax])
  yd = m*xd + c

  ax.plot(xd, yd, 'k', lw=1, ls='--')
  ax.fill_between(xd, yd, ymin, color='tab:blue', alpha=0.2)
  ax.fill_between(xd, yd, ymax, color='tab:red', alpha=0.2)
  targets = ['Negative', 'Positive']

  neg = ax.scatter(*X[y==0].T, s=8, alpha=0.5, color='b')
  pos = ax.scatter(*X[y==1].T, s=8, alpha=0.5, color='r')
  ax.set_xlim(xmin, xmax)
  ax.set_ylim(ymin, ymax)
  ax.set_xlabel(r'$PC_1$')
  ax.set_ylabel(r'$PC_2$')
  ax.legend([neg, pos], targets, loc=legend_loc)
  ax.grid(True)

def plot_multiple_scree(ax, plot_data, legend_values, legend_name, title=None):
  plot_df = []
  for legend_value, data in zip(legend_values, plot_data):
    df = pd.DataFrame({'Percentage of Variance Explained': data, 'Principal Components': list(range(1, len(data)+1))})
    df[legend_name] = legend_value
    plot_df.append(df)

  plot_df = pd.concat(plot_df)
  sns.barplot(x='Principal Components', y='Percentage of Variance Explained', data=plot_df, hue='Test Set', ax=ax)

def plot_scree(ax, data, labels, n_comps=10, title=None):
  data = data[:n_comps]
  labels = labels[:n_comps]
  # fig, ax = plt.subplots(1,1,figsize=(12,8))
  ax.bar(x=range(len(data)), height=data, tick_label=labels)
  ax.set_xlabel('Principal Components')
  ax.set_ylabel('% of variance explained')
  ax.set_title(title)

def plot2d_comps(ax, comps_df, comp_1, comp_2, alpha_x=0.1, alpha_y=0.1, title=None):
  # fig, ax = plt.subplots(1, 1, figsize = (10,8))
  ax.set_xlabel(r'$PC_1$')
  ax.set_ylabel(r'$PC_2$')
  ax.set_title(title, fontsize = 15)
  targets = ['Negative', 'Positive'][::-1]
  target_ints = [0, 1][::-1]
  colors = ['b', 'r'][::-1]
  alphas = [alpha_x, alpha_y]

  for target_int, color in zip(target_ints, colors):
    idxs = comps_df['labels'] == target_int
    ax.scatter(comps_df.loc[idxs, comp_1], comps_df.loc[idxs, comp_2], c = color, alpha = alphas[target_int], s = 30)

  ax.legend(targets)
  ax.xaxis.set_tick_params(labelsize=13)
  ax.yaxis.set_tick_params(labelsize=13)
  ax.grid(True)


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

# In[1]:

import os

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.framework import constant_op, ops
from tensorflow.python.ops import control_flow_ops, math_ops, state_ops
from tensorflow.python.training import optimizer

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

# In[2]:

class COCOB(optimizer.Optimizer):
    def __init__(self, alpha=100, use_locking=False, name="COCOB"):
        super(COCOB, self).__init__(use_locking, name)
        self._alpha = alpha

    def _create_slots(self, var_list):
        for v in var_list:
            with ops.colocate_with(v):
                gradients_sum = constant_op.constant(
                    0, shape=v.get_shape(), dtype=v.dtype.base_dtype
                )
                grad_norm_sum = constant_op.constant(
                    0, shape=v.get_shape(), dtype=v.dtype.base_dtype
                )
                L = constant_op.constant(1e-8, shape=v.get_shape(), dtype=v.dtype.base_dtype)
                tilde_w = constant_op.constant(0.0, shape=v.get_shape(), dtype=v.dtype.base_dtype)
                reward = constant_op.constant(0.0, shape=v.get_shape(), dtype=v.dtype.base_dtype)
            self._get_or_make_slot(v, L, "L", self._name)
            self._get_or_make_slot(v, grad_norm_sum, "grad_norm_sum", self._name)
            self._get_or_make_slot(v, gradients_sum, "gradients_sum", self._name)
            self._get_or_make_slot(v, tilde_w, "tilde_w", self._name)
            self._get_or_make_slot(v, reward, "reward", self._name)

    def _apply_dense(self, grad, var):
        gradients_sum = self.get_slot(var, "gradients_sum")
        grad_norm_sum = self.get_slot(var, "grad_norm_sum")
        tilde_w = self.get_slot(var, "tilde_w")
        L = self.get_slot(var, "L")
        reward = self.get_slot(var, "reward")
        L_update = tf.maximum(L, tf.abs(grad))
        gradients_sum_update = gradients_sum + grad
        grad_norm_sum_update = grad_norm_sum + tf.abs(grad)
        reward_update = tf.maximum(reward - grad * tilde_w, 0)
        new_w = (
            -gradients_sum_update
            / (L_update * (tf.maximum(grad_norm_sum_update + L_update, self._alpha * L_update)))
            * (reward_update + L_update)
        )
        var_update = var - tilde_w + new_w
        tilde_w_update = new_w
        gradients_sum_update_op = state_ops.assign(gradients_sum, gradients_sum_update)
        grad_norm_sum_update_op = state_ops.assign(grad_norm_sum, grad_norm_sum_update)
        var_update_op = state_ops.assign(var, var_update)
        tilde_w_update_op = state_ops.assign(tilde_w, tilde_w_update)
        L_update_op = state_ops.assign(L, L_update)
        reward_update_op = state_ops.assign(reward, reward_update)
        return control_flow_ops.group(
            *[
                gradients_sum_update_op,
                var_update_op,
                grad_norm_sum_update_op,
                tilde_w_update_op,
                reward_update_op,
                L_update_op,
            ]
        )

    def _apply_sparse(self, grad, var):
        return self._apply_dense(grad, var)

    def _resource_apply_dense(self, grad, handle):
        return self._apply_dense(grad, handle)

# In[3]:

mnist = input_data.read_data_sets("", validation_size=0)

# In[4]:

class Model:
    def __init__(self, learning_rate=0.01):
        self.X = tf.placeholder(tf.float32, shape=[None, 784])
        self.Y = tf.placeholder(tf.float32, shape=[None, 10])
        w1 = tf.Variable(tf.random_normal([784, 200]))
        b1 = tf.Variable(tf.random_normal([200]))
        w2 = tf.Variable(tf.random_normal([200, 100]))
        b2 = tf.Variable(tf.random_normal([100]))
        w3 = tf.Variable(tf.random_normal([100, 10]))
        b3 = tf.Variable(tf.random_normal([10]))
        feedforward = tf.nn.relu(tf.matmul(self.X, w1) + b1)
        feedforward = tf.nn.relu(tf.matmul(feedforward, w2) + b2)
        self.logits = tf.matmul(feedforward, w3) + b3
        self.cost = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(labels=self.Y, logits=self.logits)
        )
        self.optimizer = COCOB().minimize(self.cost)
        correct_prediction = tf.equal(tf.argmax(self.logits, 1), tf.argmax(self.Y, 1))
        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

# In[5]:

batch_size = 128
epoch = 10

train_images = mnist.train.images
test_images = mnist.test.images

tf.reset_default_graph()
sess = tf.InteractiveSession()
model = Model()
sess.run(tf.global_variables_initializer())

# In[6]:

LOSS, ACC_TRAIN, ACC_TEST = [], [], []
for i in range(epoch):
    total_loss, total_acc = 0, 0
    for n in range(0, (mnist.train.images.shape[0] // batch_size) * batch_size, batch_size):
        batch_x = train_images[n : n + batch_size, :]
        batch_y = np.zeros((batch_size, 10))
        batch_y[np.arange(batch_size), mnist.train.labels[n : n + batch_size]] = 1.0
        cost, _ = sess.run(
            [model.cost, model.optimizer], feed_dict={model.X: batch_x, model.Y: batch_y}
        )
        total_acc += sess.run(model.accuracy, feed_dict={model.X: batch_x, model.Y: batch_y})
        total_loss += cost
    total_loss /= mnist.train.images.shape[0] // batch_size
    total_acc /= mnist.train.images.shape[0] // batch_size
    ACC_TRAIN.append(total_acc)
    total_acc = 0
    for n in range(
        0, (mnist.test.images[:1000, :].shape[0] // batch_size) * batch_size, batch_size
    ):
        batch_x = test_images[n : n + batch_size, :]
        batch_y = np.zeros((batch_size, 10))
        batch_y[np.arange(batch_size), mnist.test.labels[n : n + batch_size]] = 1.0
        total_acc += sess.run(model.accuracy, feed_dict={model.X: batch_x, model.Y: batch_y})
    total_acc /= mnist.test.images[:1000, :].shape[0] // batch_size
    ACC_TEST.append(total_acc)
    print(
        "epoch: %d, accuracy train: %f, accuracy testing: %f" % (i + 1, ACC_TRAIN[-1], ACC_TEST[-1])
    )

max_stars_repo_name: gitter-badger/mlmodels


text:
import streamlit as st
import cv2
import mediapipe as mp
import pickle
import numpy as np
from time import sleep

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

# load model
file_name = "RF_model_full.pkl"
model = pickle.load(open(file_name,'rb'))

classes = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D','E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q','R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

font = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (10,500)
fontScale = 4
fontColor = (255,0,0)
thickness = 4
lineType = 2

st.title("DL Project")
run = st.checkbox('run')
FRAME_WINDOW = st.image([])

def print_points(lms,size):
    w, h, d = size
    for i in [8,12,16]:
        x  = lms.landmark[i].x
        y = lms.landmark[i].y
        z = lms.landmark[i].z
        st.write(f"x {x*size[0]} y {y*size[1]} z {z*size[0]} ")
        # print(lms.landmark[i], type(lms.landmark[i]),lms.landmark.x)
    # for id, lm in enumerate(lms.landmark):
    #     px, py = int(lm.x * w), int(lm.y * h)
    # print(lm.landmark[8])
    # print(px)
    return

def clean(dic):
    len_dic = len(dic.keys())
    features = []
    temp = [0]*42
    for side, item in dic.items():
        for lm in item.landmark:
            features.append(lm.x)
            features.append(lm.y)
    if len(features) == 42 and list(dic.keys())[0] == 'Right':
        features = features + temp
    if len(features) == 42 and list(dic.keys())[0] == 'Left':
        features = temp + features
#     print(features, len(features))
    return features

cap = cv2.VideoCapture(0)
while run:
    with mp_hands.Hands(
    model_complexity=0,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5) as hands:
        while run:
            success, image = cap.read()
            if not success:
                print("Ignoring empty camera frame.")
            # If loading a video, use 'break' instead of 'continue'.
                continue

            # To improve performance, optionally mark the image as not writeable to
            # pass by reference.
            size = image.shape
            image.flags.writeable = False
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            results = hands.process(image)

            # Draw the hand annotations on the image.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            if results.multi_hand_landmarks:
                land_marks = {}
                for hand_type, hand_landmarks in zip(results.multi_handedness , results.multi_hand_landmarks):
                    # print(f"[INFO] {i} {hand_landmarks}")
                    # print_points(hand_landmarks,size)
                    # model needed.
                    land_marks[hand_type.classification[0].label] = hand_landmarks
                    mp_drawing.draw_landmarks(
                        image,
                        hand_landmarks,
                        mp_hands.HAND_CONNECTIONS,
                        mp_drawing_styles.get_default_hand_landmarks_style(),
                        mp_drawing_styles.get_default_hand_connections_style())
            # Flip the image horizontally for a selfie-view display.
                x = np.array(clean(land_marks))
                # print(x)
                x_ = x.reshape(1,-1)
                # print("[INFO] X : ",x," X_ ",x_)
                y = model.predict(x_)
                # print(type(y), y[0])
                cv2.putText(image,classes[y[0]], bottomLeftCornerOfText, font, fontScale,fontColor,thickness,lineType)
            image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
            # cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
            # FRAME_WINDOW.image(cv2.flip(image, 1))
            FRAME_WINDOW.image(image)
            # if cv2.waitKey(5) & 0xFF == 27:
            #     break
            # sleep(2)

    cap.release()
else :
    st.write("Stopped")

max_stars_repo_name: kishoreKunisetty/DLProject

max_issues_repo_name: kishoreKunisetty/DLProject

max_forks_repo_name: kishoreKunisetty/DLProject


text:
"""
Entry point into GUI.
"""
import os
import sys
import time
import pickle
import logging

import motmot.FlyMovieFormat.FlyMovieFormat as FMF
import numpy as np

from PyQt5.QtCore import (pyqtSignal, pyqtSlot, Qt)
from PyQt5.QtGui import (QIcon, QKeySequence, QPixmap, QCursor)
from PyQt5.QtWidgets import (
    QAction,
    QActionGroup,
    QApplication,
    QDockWidget,
    QFileDialog,
    QFrame,
    QLabel,
    QMainWindow,
    QSlider
)

from dialogs.batch import BatchTrackingDialog
from dialogs.statproc import StatProcessing
from widgets.video import MainVideoPlayer
from widgets.fileio import FileExplorer
from widgets.statistics import StatisticWindowWidget, ClassificationWindowWidget

from ..ts import FixedCourtshipTrackingSummary as FCTS
import courtship.stats.spatial as spatial_stats
import courtship.stats.wing as wing_stats
import courtship.stats.centroid as centroid_stats

DIR = os.path.dirname(__file__)
logging.basicConfig()

class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.root_folder = os.path.expanduser("~")
        self.logger = logging.getLogger(name='MainWindow Logger')

        self.set_main_widget()

        #create menus
        self.file_menu = self.menuBar().addMenu('&File')
        self.view_menu = self.menuBar().addMenu('View')
        self.video_menu = self.menuBar().addMenu('Video')
        self.tracking_menu = self.menuBar().addMenu('Tracking')
        self.analysis_menu = self.menuBar().addMenu('Analysis')

        #create toolbar for navigating through videos
        self.video_toolbar = self.addToolBar('Video')
        self.video_trackbar = self.addToolBar('Video Track Bar')

        self.set_file_menu()
        self.set_video_menu()
        self.set_tracking_menu()
        self.set_analysis_menu()

        self.set_slider()
        self.set_status()
        self.set_file_explorer()
        self.set_statistic_toolbar()
        self.set_classification_toolbar()

        self.set_view_menu()

    def set_main_widget(self):
        self.video_player = MainVideoPlayer()
        self.setCentralWidget(self.video_player)

    def add_menu_action(self, menu, name, connection, status_tip,
        shortcut=None, icon=None, tool_bar=None, return_action=False):
        """Helper function for adding a QAction to a menu.

        Parameters
        ----------
        menu : QMenu
            Which menu to add new QAction to.

        name : string
            Name of new QAction. This is displayed as the QAction
            text in the QMenu.

        connection : function
            Which function should the QAction be connected to.

        status_tip : string
            Status tip to display on MouseOver.

        shortcut : QKeySequence or None (default = None)
            Shortcut to execute action.

        icon : QIcon or None (default = None)
            Icon to attach to QAction.

        tool_bar : QToolBar or None
            QToolBar to attach QAction to (in addition to the specified QMenu).

        return_action : bool (default = False)
            Whether or not to return the action.
        """
        if icon is None:
            action = QAction(name, self)
        else:
            action = QAction(icon, name, self)

        if shortcut is not None:
            action.setShortcut(shortcut)

        action.setStatusTip(status_tip)
        action.triggered.connect(connection)
        menu.addAction(action)

        if tool_bar is not None:
            tool_bar.addAction(action)

        if return_action:
            return action

    def set_file_menu(self):
        self.add_menu_action(
            menu=self.file_menu,
            name='Open Directory',
            connection=self.open_file_directory,
            status_tip='Open a folder in the file explorer.',
            shortcut=QKeySequence.Open
            )
        self.add_menu_action(
            menu=self.file_menu,
            name='Open Video',
            connection=self.open_video,
            status_tip='Open an individual video.',
            shortcut=None
            )
        self.add_menu_action(
            menu=self.file_menu,
            name='Open Tracking Summary',
            connection=self.open_tracking_summary,
            status_tip='Open a tracked video file (.fcts, .xlsx, etc)'
            )

    def set_view_menu(self):
        """Sets up menu to allow users to select which DockWidgets/ToolBars
        are displayed."""
        #file explorer
        display_file_explorer_action = self.file_dock_widget.toggleViewAction()
        self.view_menu.addAction(display_file_explorer_action)

        #statistic widget
        display_statistic_widget_action = self.statistic_toolbar.toggleViewAction()
        self.view_menu.addAction(display_statistic_widget_action)

        #video toolbar
        display_video_toolbar_action = self.video_toolbar.toggleViewAction()
        self.view_menu.addAction(display_video_toolbar_action)

        #slider toolbar
        display_slider_toolbar_action = self.video_trackbar.toggleViewAction()
        self.view_menu.addAction(display_slider_toolbar_action)

    def set_video_menu(self):
        self.previous_action = self.add_menu_action(
            menu=self.video_menu,
            name='Previous Frame',
            connection=self.video_player.previous,
            status_tip='Go to previous frame.',
            icon=QIcon(os.path.join(DIR, 'icons', 'prev_icon.png')),
            tool_bar=self.video_toolbar,
            return_action=True
            )
        self.previous_action.setEnabled(False)
        self.previous_action.setShortcut(QKeySequence(','))

        self.play_action = self.add_menu_action(
            menu=self.video_menu,
            name='Play',
            connection=self.video_player.on,
            status_tip='Play Video.',
            icon=QIcon(os.path.join(DIR, 'icons', 'play_icon.png')),
            tool_bar=self.video_toolbar,
            return_action=True
            )
        self.play_action.setEnabled(False)
        self.play_action.setShortcut(QKeySequence(']'))

        self.stop_action = self.add_menu_action(
            menu=self.video_menu,
            name='Stop',
            connection=self.video_player.off,
            status_tip='Stop Video.',
            icon=QIcon(os.path.join(DIR, 'icons', 'stop_icon.png')),
            tool_bar=self.video_toolbar,
            return_action=True
            )
        self.stop_action.setEnabled(False)
        self.stop_action.setShortcut(QKeySequence('['))

        self.next_action = self.add_menu_action(
            menu=self.video_menu,
            name='Next Frame',
            connection=self.video_player.next,
            status_tip='Go to next frame.',
            icon=QIcon(os.path.join(DIR, 'icons', 'next_icon.png')),
            tool_bar=self.video_toolbar,
            return_action=True
            )
        self.next_action.setEnabled(False)
        self.next_action.setShortcut(QKeySequence('.'))

        self.stop_start_action = self.add_menu_action(
            menu=self.video_menu,
            name='Pause/Play',
            connection=self.stop_start,
            status_tip='Start playing, pause, or resume playing video.',
            return_action=True
            )
        self.stop_start_action.setEnabled(False)
        self.stop_start_action.setShortcut(QKeySequence(Qt.Key_Space))

        self.video_zoom_action = self.add_menu_action(
            menu=self.video_menu,
            name='Zoom',
            connection=self.zoom,
            status_tip='Zoom in on a segment of video.',
            return_action=True
        )
        self.video_zoom_enabled = False
        self.video_zoom_action.setEnabled(False)
        self.video_zoom_action.setShortcut(QKeySequence('Z'))

        self.video_menu.addSeparator()

        # Actions to control the speed of the video.
        self.videoMenuSpeedMenu = self.video_menu.addMenu('Playback Speed')
        speedFunctions = [
            self.adjustSpeed1x,
            self.adjustSpeed1p5x,
            self.adjustSpeed2x,
            self.adjustSpeed3x,
            self.adjustSpeed5x,
            self.adjustSpeed10x
        ]
        speedFunctionNames = [
            '1x', '1.5x', '2x', '3x', '5x', '10x'
        ]
        speedActionGroup = QActionGroup(self)
        for i in xrange(len(speedFunctionNames)):
            action = self.add_menu_action(
                menu=self.videoMenuSpeedMenu,
                name=speedFunctionNames[i],
                connection=speedFunctions[i],
                status_tip='Adjust the playback speed of the current video.',
                return_action=True
            )
            action.setCheckable(True)
            speedActionGroup.addAction(action)
            if i == 0:
                action.setChecked(True)

    def set_tracking_menu(self):
        self.add_menu_action(
            menu=self.tracking_menu,
            name='Batch Processing',
            connection=self.track_batch,
            status_tip='Open Batch Tracking Wizard.'
            )

    def set_analysis_menu(self):
        self.add_menu_action(
            menu=self.analysis_menu,
            name='Signal Processing',
            connection=self.signal_processing,
            status_tip='Open Signal Processing Dialog.'
            )

    def open_file_directory(self):
        file_dialog = QFileDialog()
        directory_name = str(file_dialog.getExistingDirectory(
            caption='Open Folder',
            options=QFileDialog.ShowDirsOnly))

        if not os.path.isdir(directory_name):
            self.logger.warning('Passed directory was not valid (in ' +
                'MainWindow.open_file_directory(), line ~282 of entry.py).')
            return

        self.file_explorer.set_path(directory_name)
        self.root_folder = directory_name

    def open_video(self, video_fname=None):
        """Opens an individual video for display in the central widget."""
        if not isinstance(video_fname, str):
            file_dialog = QFileDialog(self)
            video_fname = str(file_dialog.getOpenFileName(
                caption='Open Video File',
                filter='Video Files (*.fmf)',
                directory=self.root_folder
                )[0])

        if not os.path.isfile(video_fname):
            self.logger.warning('Passed `video_fname` was not valid (in ' +
                'MainWindow.open_video(), line 290 of entry.py).')
            return

        self.video_player.set_video(video_fname)
        self.video_player.image_annotation = False
        self.video_player.tracking_summary = None
        self.enable_video_controls()

    def enable_video_controls(self):
        """Enables all video controls and connects signals/slots if necessary."""
        #connect video_player signals
        self.video_player.frame_changed.connect(self.update_frame_label)

        #enable video controls
        self.previous_action.setEnabled(True)
        self.next_action.setEnabled(True)
        self.play_action.setEnabled(True)
        self.stop_action.setEnabled(True)
        self.stop_start_action.setEnabled(True)
        self.video_zoom_action.setEnabled(True)

        #enable slider
        self.slider.setMaximum(self.video_player.video.get_n_frames() - 1)
        self.slider.setValue(0)
        self.slider.setEnabled(True)

        self.frame_label.setText('Frame: 0')
        self.video_label.setText('Current Video: {}'.format(
            self.video_player.video_file_name))

    def open_tracking_summary(self, fcts_fname=None, video_fname=None):
        """Opens a tracking summary file (.fcts, .xlsx, etc) for display."""
        if not isinstance(fcts_fname, str):
            file_dialog = QFileDialog(self)
            fcts_fname=str(file_dialog.getOpenFileName(
                caption='Open Tracking Summary File',
                filter='Tracking Summary File (*.fcts *.xlsx)',
                directory=self.root_folder
                )[0])

        if not os.path.isfile(fcts_fname):
            self.logger.warning('Passed `fcts_fname` was not valid (in ' +
                'MainWindow.open_tracking_summary(), line 332 of entry.py).')
            return

        file_type = fcts_fname.split('.')[-1]
        if file_type == 'fcts':
            with open(fcts_fname, 'rb') as f:
                tracking_summary = pickle.load(f)
        elif file_type == 'xlsx':
            tracking_summary = FCTS().from_xlsx(fcts_fname) # TODO: Add from_xlsx() function
        else:
            return

        if os.path.exists(tracking_summary.video.filename):
            self.video_player.set_video(tracking_summary.video.filename)
        else:
            if not isinstance(video_fname, str):
                file_dialog = QFileDialog(self)
                video_fname = str(file_dialog.getOpenFileName(
                    caption='Select Associated Video File',
                    filter='Video File (*.fmf)',
                    directory=self.root_folder
                    )[0])
            self.video_player.set_video(video_fname)

        self.video_player.tracking_summary = tracking_summary
        self.video_player.image_annotation = True
        self.video_player.update_label()
        self.enable_video_controls()

        # setup statistic toolbar
        self.statistic_toolbar.setEnabled(True)
        # calculate statistics about fly held in current experiment.
        lwa, rwa = wing_stats.individual_wing_angles(tracking_summary.male)
        fwa = wing_stats.full_wing_angle(tracking_summary.male)
        wd = wing_stats.wing_distances(tracking_summary.male)
        nn = spatial_stats.nearest_neighbor_centroid(
            tracking_summary.male, tracking_summary.female)
        n2e, t2e = spatial_stats.nose_and_tail_to_ellipse(
            tracking_summary.male, tracking_summary.female)
        vel = centroid_stats.centroid_velocity(tracking_summary.male)
        self.statistics_widget.statistics = {
            'LeftWingAngle': lwa,
            'RightWingAngle': rwa,
            'FullWingAngle': fwa,
            'WingDistance': wd,
            'Centroid-to-Centroid': nn,
            'Nose-to-Ellipse': n2e,
            'Rear-to-Ellipse': t2e,
            'CentroidVelocity': vel
        }
        self.statistics_widget.update_combobox()
        self.statistics_widget.update_stat_label(0)
        self.statistic_toolbar.show()

        self.classification_toolbar.setEnabled(True)
        self.classification_widget.set_tracking_summary(tracking_summary)
        self.classification_widget.statistics = \
            tracking_summary.male.get_all_behaviors_as_dict()
        self.classification_widget.update_combobox()
        self.classification_widget.update_stat_label(0)
        self.classification_toolbar.show()

    def set_slider(self):
        """Places slider widget in video track bar menu."""
        self.slider = QSlider(Qt.Horizontal)
        self.slider.setMinimum(0)
        self.video_trackbar.addWidget(self.slider)
        self.slider.valueChanged[int].connect(self.video_player.slider_update_label)
        self.video_player.frame_changed.connect(self.update_slider)
        self.slider.setEnabled(False)

    def set_status(self):
        """Annotates video statistics at bottom of main window."""
        status = self.statusBar()
        status.setSizeGripEnabled(True)

        self.frame_label = QLabel()
        self.frame_label.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
        self.frame_label.setText('Frame: NA (00:00:00)')

        self.video_label = QLabel()
        self.video_label.setText('Current Video: NA')

        status.addPermanentWidget(self.frame_label)
        status.addWidget(self.video_label)

    def set_file_explorer(self):
        """Initializes the file explorer as a DockWidget."""
        self.file_explorer = FileExplorer()
        self.file_dock_widget = QDockWidget('File Explorer', self)
        self.file_dock_widget.setWidget(self.file_explorer)
        self.addDockWidget(Qt.RightDockWidgetArea, self.file_dock_widget)
        self.file_explorer.open_video.connect(self.load_video_from_file_exp)

    def set_statistic_toolbar(self):
        self.statistic_toolbar = QDockWidget('Tracking Statistics')
        self.statistics_widget = StatisticWindowWidget()
        self.statistic_toolbar.setWidget(self.statistics_widget)
        self.statistic_toolbar.setEnabled(False)
        self.statistic_toolbar.hide()
        self.video_player.frame_changed.connect(self.statistics_widget.update_stat_label)
        self.addDockWidget(Qt.TopDockWidgetArea, self.statistic_toolbar)

    def set_classification_toolbar(self):
        self.classification_toolbar = QDockWidget('Behavioral Classifications')
        self.classification_widget = ClassificationWindowWidget()
        self.classification_toolbar.setWidget(self.classification_widget)
        self.classification_toolbar.setEnabled(False)
        self.classification_toolbar.hide()
        self.video_player.frame_changed.connect(self.classification_widget.update_stat_label)
        self.addDockWidget(Qt.TopDockWidgetArea, self.classification_toolbar)
        self.tabifyDockWidget(self.statistic_toolbar, self.classification_toolbar)

    def track_batch(self):
        dialog = BatchTrackingDialog(self.root_folder)
        if dialog.exec_():
            return

    def signal_processing(self):
        dialog = StatProcessing(root_folder = self.root_folder, parent = self)
        dialog.show()

    @pyqtSlot(int, str, int)
    def update_frame_label(self, ix, hms, frame_rate):
        self.frame_label.setText(
            'Frame: {} ({}) | FPS: {} | Playback Speed: {}x'.format(
                ix, hms, frame_rate, self.video_player.playback_speed
                )
            )

    @pyqtSlot(str)
    def load_video_from_file_exp(self, fname):
        self.open_video(str(fname))

    @pyqtSlot(int)
    def update_slider(self, ix):
        self.slider.setValue(ix)

    @pyqtSlot()
    def stop_start(self):
        if self.video_player.is_playing:
            self.video_player.off()
        else:
            self.video_player.on()

    @pyqtSlot()
    def zoom(self):
        """Allows user to select a region of the video to zoom in on."""
        if self.video_zoom_enabled:
            self.video_zoom_enabled = False
            self.video_player.zoom_enabled = False
            non_zooming_cursor = QCursor(Qt.ArrowCursor)
            self.video_player.setCursor(non_zooming_cursor)
            self.video_player.close_zoom_window()
            return

        self.video_zoom_enabled = True
        zooming_cursor = QCursor(Qt.CrossCursor)
        self.video_player.setCursor(zooming_cursor)
        self.video_player.zoom_enabled = True

    # Playback speed adjustments
    @pyqtSlot()
    def adjustSpeed1x(self):
        self.video_player.playback_speed = 1

    @pyqtSlot()
    def adjustSpeed1p5x(self):
        self.video_player.playback_speed = 1.5

    @pyqtSlot()
    def adjustSpeed2x(self):
        self.video_player.playback_speed = 2

    @pyqtSlot()
    def adjustSpeed3x(self):
        self.video_player.playback_speed = 3

    @pyqtSlot()
    def adjustSpeed5x(self):
        self.video_player.playback_speed = 5

    @pyqtSlot()
    def adjustSpeed10x(self):
        self.video_player.playback_speed = 10

def main():
    app = QApplication(sys.argv)
    # app.setStyleSheet(qdarkstyle.load_stylesheet(pyside = False))
    app_icon = QIcon(QPixmap(os.path.join(DIR, 'icons', 'logo.png')))

    main_window = MainWindow()
    main_window.setWindowIcon(app_icon)
    main_window.setWindowTitle('courtship')

    main_window.show()
    app.exec_()

if __name__ == '__main__':
    main()

max_stars_repo_name: regginold/drosophila-courtship

max_issues_repo_name: benshahary/drosophila-courtship

max_forks_repo_name: benshahary/drosophila-courtship


text:
import numpy as np
from nearpy.filters.vectorfilter import VectorFilter

class PositionFilter(VectorFilter):
    """
    Sorts vectors with respect to distance and returns the N nearest.
    """

    def __init__(self, radius, attribute):
        """
        Keeps the count threshold.
        """
        self.radius = radius
        self.attribute = attribute

    def __call__(self, neighbors):
        """
        Returns subset of specified input list.
        """
        neighbors[attribute.name]
        return np.argsort(distances)[:self.N]

max_stars_repo_name: MarcCote/NearPy

max_issues_repo_name: MarcCote/NearPy

max_forks_repo_name: MarcCote/NearPy


text:
import numpy as np
import cv2
from matplotlib import pyplot as plt
import sys
img = cv2.imread(sys.argv[1])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

corners = cv2.goodFeaturesToTrack(gray, 25, 0.01, 10)
corners = np.int0(corners)

for i in corners:
    x, y = i.ravel()
    cv2.circle(img, (x, y), 3, 255, -1)
cv2.imshow('window', img)
cv2.waitKey(0)

max_stars_repo_name: Soldie/Human-detection-and-Tracking

max_issues_repo_name: Soldie/Human-detection-and-Tracking

max_forks_repo_name: Soldie/Human-detection-and-Tracking


text:
import data as d
import math
import numpy as np
import pytorch_lightning as pl
import torch
import torch.nn as nn
import torch.nn.functional as F
from pytorch_lightning import LightningDataModule
from pytorch_lightning.metrics.functional import bleu_score
from torch import Tensor
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torchtext.data import BucketIterator, Batch
from transformer.optimizer import Optimizer
from typing import Optional

class PositionalEncoding(nn.Module):
    def __init__(self, d_model, dropout, max_len=5000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(p=dropout)

        max_len = max_len
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0., max_len).unsqueeze(1)
        div_term = torch.exp(torch.arange(0., d_model, 2) * -(math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        self.register_buffer('pe', pe)

    def forward(self, x):
        x = x + self.pe[:x.size(0), :].detach().unsqueeze(dim=1)
        return self.dropout(x)

class TransformerDecoderLayerWithAttn(nn.TransformerDecoderLayer):
    def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation="relu"):
        super().__init__(d_model, nhead, dim_feedforward, dropout, activation)

    def forward(self, tgt: Tensor, memory: Tensor, tgt_mask: Optional[Tensor] = None, memory_mask: Optional[Tensor] = None,
                tgt_key_padding_mask: Optional[Tensor] = None, memory_key_padding_mask: Optional[Tensor] = None) -> Tensor:
        tgt2 = self.self_attn(tgt, tgt, tgt, attn_mask=tgt_mask,
                              key_padding_mask=tgt_key_padding_mask)[0]
        tgt = tgt + self.dropout1(tgt2)
        tgt = self.norm1(tgt)
        tgt2, attn_weights = self.multihead_attn(tgt, memory, memory, attn_mask=memory_mask,
                                                 key_padding_mask=memory_key_padding_mask)
        tgt = tgt + self.dropout2(tgt2)
        tgt = self.norm2(tgt)
        tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt))))
        tgt = tgt + self.dropout3(tgt2)
        tgt = self.norm3(tgt)
        return tgt, attn_weights

class TransformerDecoderWithAttn(nn.TransformerDecoder):
    __constants__ = ['norm']

    def __init__(self, decoder_layer, num_layers, norm=None):
        super().__init__(decoder_layer, num_layers, norm)

    def forward(self, tgt: Tensor, memory: Tensor, tgt_mask: Optional[Tensor] = None,
                memory_mask: Optional[Tensor] = None, tgt_key_padding_mask: Optional[Tensor] = None,
                memory_key_padding_mask: Optional[Tensor] = None) -> Tensor:
        output = tgt
        attns = []

        for mod in self.layers:
            output, attn = mod(output, memory, tgt_mask=tgt_mask,
                         memory_mask=memory_mask,
                         tgt_key_padding_mask=tgt_key_padding_mask,
                         memory_key_padding_mask=memory_key_padding_mask)
            attns.append(attn)

        if self.norm is not None:
            output = self.norm(output)

        return output, attns

class TransformerWithAttn(nn.Transformer):
    def __init__(self, src_vocab_size: int, tgt_vocab_size: int, src_pad_key: int, tgt_pad_key: int, max_len: int,
                 d_model: int = 512, nhead: int = 8, num_encoder_layers: int = 6,
                 num_decoder_layers: int = 6, dim_feedforward: int = 2048, dropout: float = 0.1,
                 activation: str = "relu"):
            super().__init__(d_model, nhead, num_encoder_layers,
                 num_decoder_layers, dim_feedforward, dropout,
                 activation, custom_decoder=TransformerDecoderWithAttn(TransformerDecoderLayerWithAttn(d_model, nhead, dim_feedforward, dropout, activation), num_decoder_layers))

            self.src_pad_key = src_pad_key
            self.tgt_pad_key = tgt_pad_key
            self.pos_encoding = PositionalEncoding(d_model, dropout, max_len=max_len+3)
            self.src_embedding = nn.Embedding(src_vocab_size, d_model)
            self.tgt_embedding = nn.Embedding(tgt_vocab_size, d_model)
            self.layer_norm = nn.LayerNorm(d_model, eps=1e-6)
            self.classifier = nn.Linear(d_model, tgt_vocab_size)
            self.tgt_vocab_size = tgt_vocab_size
            self.d_model = d_model

            self._init_weights()

    @torch.no_grad()
    def _init_weights(self):
        for name, p in self.named_parameters():
            # if "embed" in name:
            #     nn.init.normal_(p, mean=0., std=0.01)
            if "bias" in name:
                nn.init.zeros_(p)
            elif "norm" in name:
                continue
            else:
                nn.init.xavier_uniform_(p)

        # zero padding weights in embedding
        self.src_embedding.weight.data[self.src_pad_key].zero_()
        self.tgt_embedding.weight.data[self.tgt_pad_key].zero_()

    def encode(self, src: Tensor):
        src_key_padding_mask = src.eq(self.src_pad_key) # batch x seq length
        src_emb = src.transpose(1,0)  # seq length x batch

        src_emb = self.src_embedding(src_emb) * math.sqrt(self.d_model)
        src_emb = self.pos_encoding(src_emb)

        return self.encoder(src_emb, src_key_padding_mask=src_key_padding_mask), src_key_padding_mask

    def decode_step(self, src_key_padding_mask, encoder_memory: Tensor, tgt_so_far: Tensor):
        tgt_key_padding_mask = tgt_so_far.eq(self.tgt_pad_key) # batch x seq length

        tgt = tgt_so_far.transpose(1,0)  # seq length x batch

        length = tgt.size(dim=0)  # seq length
        tgt_mask = self.generate_square_subsequent_mask(length).to(src_key_padding_mask.device) # length x lengths

        tgt = self.tgt_embedding(tgt) * math.sqrt(self.d_model)
        tgt = self.pos_encoding(tgt)

        output, attns = self.decoder(tgt, encoder_memory, tgt_mask=tgt_mask,
                                     tgt_key_padding_mask=tgt_key_padding_mask,
                                     memory_key_padding_mask=src_key_padding_mask)
        output = self.layer_norm(output)
        output = self.classifier(output)
        return output.transpose(1,0), attns

    def forward(self, src: Tensor, tgt: Tensor) -> Tensor:
        """
        input: batch x src length
        output: batch x tgt length
        """
        tgt_key_padding_mask = tgt.eq(self.tgt_pad_key) # batch x seq length

        tgt_emb = tgt.transpose(1,0)  # seq length x batch
        length = tgt_emb.size(dim=0)  # seq length
        tgt_mask = self.generate_square_subsequent_mask(length).to(src.device) # length x length

        memory, src_key_padding_mask = self.encode(src) # seq_len x batch x d_model

        tgt_emb = self.tgt_embedding(tgt_emb) * math.sqrt(self.d_model)
        tgt_emb = self.pos_encoding(tgt_emb)

        output, attns = self.decoder(tgt_emb, memory, tgt_mask=tgt_mask,
                              tgt_key_padding_mask=tgt_key_padding_mask,
                              memory_key_padding_mask=src_key_padding_mask)
        output = self.layer_norm(output)
        output = self.classifier(output) # seq length x batch x vocab
        return output.transpose(1,0), attns  # seq length x batch x vocab -> batch x seq length x voacb

    @torch.no_grad()
    def greedy_decode(self, src, max_len: int):
        batch_size = src.size(dim=0)
        eos_id = d.TGT.vocab.stoi[d.EOS_WORD]
        memory, src_key_padding_mask = self.encode(src)

        tgt_so_far = torch.full((batch_size, 1), fill_value=d.TGT.vocab.stoi[d.BOS_WORD], device=src.device, dtype=torch.long)
        text_so_far = [[]] * batch_size

        # a subsequent mask is intersected with this in decoder forward pass
        finished = 0
        for _ in range(max_len):
            with torch.no_grad():
                logits, _ = self.decode_step(src_key_padding_mask, memory, tgt_so_far)
                logits = F.log_softmax(logits[:, -1, :], dim=-1)
                _, logits = torch.max(logits, dim=-1)
                for batch_idx, token_id in enumerate(logits.tolist()):
                    if token_id == eos_id:
                        finished += 1
                    else:
                        text_so_far[batch_idx].append(d.TGT.vocab.itos[token_id])
                tgt_so_far = torch.cat((tgt_so_far, logits.view(-1, 1)), dim=1)
            if finished == batch_size:
                break

        return text_so_far

global max_src_in_batch, max_tgt_in_batch
def batch_size_fn(new, count, sofar):
    "Keep augmenting batch and calculate total number of tokens + padding."
    # code from http://nlp.seas.harvard.edu/2018/04/03/attention.html
    global max_src_in_batch, max_tgt_in_batch
    if count == 1:
        max_src_in_batch = 0
        max_tgt_in_batch = 0
    max_src_in_batch = max(max_src_in_batch,  len(new.src))
    max_tgt_in_batch = max(max_tgt_in_batch,  len(new.trg) + 2)
    src_elements = count * max_src_in_batch
    tgt_elements = count * max_tgt_in_batch
    return max(src_elements, tgt_elements)

class TranslationDataModule(LightningDataModule):
    def __init__(self, batch_size, src_lang, tgt_lang) -> None:
        super().__init__()
        self.batch_size = batch_size
        self.src_lang = src_lang
        self.tgt_lang = tgt_lang

    def setup(self):
        print("Loading data...")
        self.train_set, self.val_set, self.test_set = d.load_dataset(src_lang=self.src_lang, tgt_lang=self.tgt_lang)
        src_vocab, tgt_vocab = d.load_vocab(src_lang=self.src_lang, tgt_lang=self.tgt_lang, train_data=self.train_set, val_data=self.val_set, test_data=self.test_set)
        self.src_pad_key = src_vocab.stoi[d.BLANK_WORD]
        self.tgt_pad_key = tgt_vocab.stoi[d.BLANK_WORD]
        self.tgt_vocab_size = len(tgt_vocab)
        self.src_vocab_size = len(src_vocab)
        print("Done.")

    def train_dataloader(self):
        return BucketIterator(self.train_set, self.batch_size, batch_size_fn=batch_size_fn, sort_within_batch=True,
                              train=True, shuffle=True, repeat=False, sort=False)

    def val_dataloader(self):
        return BucketIterator(self.val_set, 1, sort_within_batch=True, sort=False,
                              train=False, shuffle=False)

    def transfer_batch_to_device(self, batch, device):
        if isinstance(batch, Batch):
            for field in batch.fields:
                batch_field = getattr(batch, field)
                batch_device = super().transfer_batch_to_device(batch_field, device)
                setattr(batch, field, batch_device)
        else:
            batch = super().transfer_batch_to_device(batch, device)
        return batch

class TransformerModel(pl.LightningModule):
    def __init__(self, src_vocab_size, tgt_vocab_size, src_pad_key, tgt_pad_key, max_len: int,
                 d_model: int = 512, nhead: int = 8, num_encoder_layers: int = 6,
                 num_decoder_layers: int = 6, dim_feedforward: int = 2048, dropout: float = 0.1,
                 activation: str = "relu", smoothing: float = 0.1) -> None:
        super().__init__()

        # setup model and loss
        self.trafo = TransformerWithAttn(src_vocab_size=src_vocab_size, tgt_vocab_size=tgt_vocab_size,
                                         src_pad_key=src_pad_key, tgt_pad_key=tgt_pad_key,
                                         max_len=max_len, d_model=d_model, nhead=nhead, num_encoder_layers=num_encoder_layers,
                                         num_decoder_layers=num_decoder_layers, dim_feedforward=dim_feedforward,
                                         dropout=dropout, activation=activation)
        self.loss_fn = LabelSmoothingLoss(tgt_pad_key, smoothing)
        self.save_hyperparameters()

    def freeze_weights(self):
        for param in self.trafo.encoder.parameters():
            param.requires_grad = False

    def forward(self, src, tgt):
        # batch x seq_len
        return self.trafo(src, tgt)

    def configure_optimizers(self):
        return Optimizer(self.parameters(), d_model=self.trafo.d_model, factor=1.0, warmup_steps=8000)

    def training_step(self, batch, batch_idx):
        src, tgt = batch.src, batch.trg # batch x seq_len
        pred, _ = self(src, tgt[:,:-1])
        loss = self.loss_fn(pred, tgt[:,1:])

        with torch.no_grad():
            mask = tgt[:,1:].ne(self.trafo.tgt_pad_key)
            ntokens = mask.view(-1).sum(dim=0).item()
            self.log('tgt_tokens', mask.view(-1).sum(dim=0), reduce_fx=torch.sum)
            acc = (pred.argmax(dim=-1).masked_select(mask) == tgt[:,1:].masked_select(mask)).float().mean()
            self.log('train_acc', acc, on_step=True, on_epoch=True, prog_bar=False, logger=True)
        self.log('src_tokens', src.ne(self.trafo.src_pad_key).view(-1).sum(dim=0), on_step=True, on_epoch=False, prog_bar=False, logger=True, reduce_fx=torch.sum)
        loss = loss / ntokens
        self.log('train_loss', loss, on_step=True, on_epoch=True, prog_bar=False, logger=True)
        return loss

    def validation_step(self, batch, batch_idx):
        src, tgt = batch.src, batch.trg
        beam_text = self.trafo.greedy_decode(src, max_len=d.MAX_LEN)
        tgt_text = []
        for batch_index in range(tgt.size(dim=0)):
            sentence = []
            for token in tgt[batch_index].tolist():
                if token == self.trafo.tgt_pad_key:
                    break
                else:
                    sentence.append(d.TGT.vocab.itos[token])
            tgt_text.append(sentence)

        return {'pred_text': beam_text, 'tgt_text': tgt_text}

    def validation_step_end(self, batch_parts):
        pred_corpus = []
        tgt_corpus = []
        for pred_sentence, tgt_sentence in zip(batch_parts['pred_text'], batch_parts['tgt_text']):
            pred_corpus.append(' '.join(pred_sentence).replace('@@ ', '').split())
            tgt_corpus.append([' '.join(tgt_sentence).replace('@@ ', '').split()])

        return {"pred_corpus": pred_corpus, "tgt_corpus": tgt_corpus}

    def validation_epoch_end(self, validation_step_outputs):
        pred_corpus = []
        tgt_corpus = []
        for out in validation_step_outputs:
            pred_corpus += out['pred_corpus']
            tgt_corpus += out['tgt_corpus']
        score = bleu_score(pred_corpus, tgt_corpus)
        self.log('val_bleu', score)

class LabelSmoothingLoss(nn.Module):
    def __init__(self, tgt_pad_index, smoothing: float):
        super(LabelSmoothingLoss, self).__init__()

        assert 0.0 < smoothing <= 1.0
        self.smoothing = smoothing
        self.tgt_pad_index = tgt_pad_index
        self.criterion = nn.KLDivLoss(reduction="sum")

    def forward(self, preds, labels):
        """
        preds (FloatTensor): (batch_size, seq_len, vocabulary_size)
        labels (LongTensor): (batch_size, seq_len)
        """

        batch_size, num_tokens, vocab_size = preds.size()

        # build smoothed label distribution (batch * seq_len x)
        with torch.no_grad():
            non_label_prob = self.smoothing/(vocab_size - 2)
            label_dist = torch.full((batch_size*num_tokens, vocab_size), fill_value=non_label_prob, dtype=torch.float, device=preds.device)
            # distribute real labels
            label_prob = 1.0 - self.smoothing
            label_dist.scatter_(1, labels.reshape(-1).unsqueeze(dim=1), label_prob)
            # mask padding
            padding_positions = torch.nonzero(labels.reshape(-1) == self.tgt_pad_index, as_tuple=False).squeeze()
            label_dist.index_fill_(0, padding_positions, 0.0)

        return self.criterion(F.log_softmax(preds, dim=-1).view(-1, vocab_size), label_dist)

max_stars_repo_name: MunzT/NMTVis

max_issues_repo_name: MunzT/NMTVis

max_forks_repo_name: MunzT/NMTVis


text:
import random
from collections import defaultdict, namedtuple
from enum import Enum
import matplotlib.pyplot as plt
import matplotlib.cm as cm

import numpy as np
from itertools import product

class Action(Enum):
    UP = 0
    DOWN = 1
    LEFT = 2
    RIGHT = 3

GridState = namedtuple('GridState', ['x', 'y', 'cows_vec'])

class MDP:
    """
            (9, 9)
         ...
    (0,0)
    """
    width = 10
    height = 10

    @staticmethod
    def _try_move(s, a):
        return {
            Action.UP: GridState(s.x, s.y + 1, s.cows_vec),
            Action.DOWN: GridState(s.x, s.y - 1, s.cows_vec),
            Action.LEFT: GridState(s.x - 1, s.y, s.cows_vec),
            Action.RIGHT: GridState(s.x + 1, s.y, s.cows_vec)
        }[a]

    def _move(self, cell, direction):
        next_cell = self._try_move(cell, direction)
        if next_cell.x not in range(0, self.width) or next_cell.y not in range(0, self.height):
            return cell
        if (next_cell.x, next_cell.y) in self.cow_positions:
            cow_ind = self.cow_positions.index((next_cell.x, next_cell.y))
            new_cow_vec = tuple(True if i == cow_ind else e for i, e in enumerate(cell.cows_vec))
            next_cell = GridState(next_cell.x, next_cell.y, new_cow_vec)

        return next_cell

    def _get_actions(self):
        actions = {}
        for s in self.states:
            actions[s] = []
            if s.x > 0:
                actions[s].append(Action.LEFT)
            if s.x < self.width - 1:
                actions[s].append(Action.RIGHT)
            if s.y > 0:
                actions[s].append(Action.DOWN)
            if s.y < self.width - 1:
                actions[s].append(Action.UP)
        return actions

    @staticmethod
    def reverse_action(action):
        return {Action.UP: Action.DOWN,
                Action.DOWN: Action.UP,
                Action.LEFT: Action.RIGHT,
                Action.RIGHT: Action.LEFT
                }[action]

    def _get_reward(self):
        reward_partial = {}

        for s in self.states:
            for a in self.actions[s]:
                next_state = self.transitions[(s, a)]
                if next_state == self.END_STATE:
                    reward_partial[(s, a)] = 100
                if (next_state.x, next_state.y) in self.cow_positions:
                    cow_ind = self.cow_positions.index((next_state.x, next_state.y))
                    if not s.cows_vec[cow_ind]:
                        reward_partial[(s, a)] = 50

        return defaultdict(int, reward_partial)

    def _get_states(self):
        states = []
        for x in range(self.width):
            for y in range(self.height):
                # cow_vectors = product([False, True], repeat=len(self.cow_positions))
                ncows = len(self.cow_positions)
                if (x, y) not in self.cow_positions:
                    cow_vectors = tuple(product([False, True], repeat=ncows))
                else:
                    cow_ind = self.cow_positions.index((x, y))
                    cow_vectors = tuple(prod for prod in product([False, True], repeat=ncows) if prod[cow_ind])
                for cw in cow_vectors:
                    states.append(GridState(x, y, cw))

        return states

    def __init__(self, lion_pos=(0, 0), cow_positions=([(9, 9)])):
        self.lion_x, self.lion_y = lion_pos
        self.cow_positions = cow_positions

        self.START_STATE = GridState(self.lion_x, self.lion_y, tuple(False for _ in cow_positions))
        self.END_STATE = GridState(self.lion_x, self.lion_y, tuple(True for _ in cow_positions))
        self.states = self._get_states()
        self.actions = self._get_actions()
        self.transitions = {(s, a): self._move(s, a) for s in self.states for a in self.actions[s]}
        self.reward = self._get_reward()

def get_policy(mdp, quality, eps=0.3):
    policy = {}
    for s in mdp.states:
        best_act_ind = np.argmax([quality[(s, a)] for a in mdp.actions[s]])
        best_act = mdp.actions[s][best_act_ind]
        for a in mdp.actions[s]:
            policy[(s, a)] = 1 - eps + eps / len(mdp.actions[s]) if a == best_act \
                else eps / len(mdp.actions[s])
    return policy

def get_action(mdp, policy, s):
    prob = random.uniform(0, 1)
    for a in mdp.actions[s]:
        if prob <= policy[(s, a)]:
            return a
        prob -= policy[(s, a)]
    raise ValueError("Policy probabilities should sum up to 1")

def sarsa(mdp, n_episodes, alpha, gamma):
    timestampes = []
    nepisodes = []

    quality = {}
    for s in mdp.states:
        for a in mdp.actions[s]:
            quality[(s, a)] = 1

    time_cnt = 0
    epi_cnt = 0
    for _ in range(n_episodes):
        s = mdp.START_STATE
        a = random.choice(mdp.actions[s])
        while s != mdp.END_STATE:
            r = mdp.reward[(s, a)]
            ss = mdp.transitions[(s, a)]
            policy = get_policy(mdp, quality)
            aa = get_action(mdp, policy, ss)
            quality[(s, a)] += alpha * (r + gamma * quality[(ss, aa)] - quality[(s, a)])
            s = ss
            a = aa
            timestampes.append(time_cnt)
            nepisodes.append(epi_cnt)

            time_cnt += 1
        epi_cnt += 1
    return get_policy(mdp, quality), timestampes, nepisodes

def generate_episode(mdp, policy):
    episode = []
    reward = 0
    rewards = []

    s = mdp.START_STATE
    a = random.choice(mdp.actions[s])

    while s != mdp.END_STATE:
        episode.append((s, a))
        r = mdp.reward[(s, a)]
        rewards.append(r)
        ss = mdp.transitions[(s, a)]
        aa = get_action(mdp, policy, ss)
        s = ss
        a = aa
        reward += r

    rewards_cumsum = np.cumsum(rewards[::-1])[::-1]
    reward_after = {}
    for (s, a), r in reversed(list(zip(episode, rewards_cumsum))):
        if (s, a) not in reward_after:
            reward_after[(s, a)] = r

    return episode, reward, defaultdict(int, reward_after)

def on_policy_first_visit_mc_control(mdp, eps, n_episodes):
    timestampes = []
    nepisodes = []

    quality = {}
    returns = {}
    policy = {}
    for s in mdp.states:
        for a in mdp.actions[s]:
            quality[(s, a)] = -2
            returns[(s, a)] = []
            policy[(s, a)] = 1 / len(mdp.actions[s])

    time_cnt = 0
    epi_cnt = 0
    while True:
        episode, reward, reward_after = generate_episode(mdp, policy)

        for _ in episode:
            nepisodes.append(epi_cnt)
            timestampes.append(time_cnt)
            time_cnt += 1
        epi_cnt += 1

        episode_unique = list(set(episode))
        for s, a in episode_unique:
            returns[(s, a)].append(reward_after[(s, a)])
            quality[(s, a)] = np.mean(returns[(s, a)])
        epi_states = [s for s, _ in episode_unique]

        for s in epi_states:
            best_act_ind = np.argmax([quality[(s, a)] for a in mdp.actions[s]])
            best_act = mdp.actions[s][best_act_ind]
            for a in mdp.actions[s]:
                policy[(s, a)] = 1 - eps + eps / len(mdp.actions[s]) if a == best_act \
                    else eps / len(mdp.actions[s])

        epi_cnt += 1
        if epi_cnt > n_episodes:
            return policy, timestampes, nepisodes

def value_iteration(mdp, gamma, theta):
    values = {}
    for s in mdp.states:
        values[s] = 1
    niter = 0
    while True:
        niter += 1
        delta = 0
        for state in mdp.states:
            v = values[state]
            candidates = [r + gamma * values[ss]
                          for a in mdp.actions[state]
                          for ss in [mdp.transitions[(state, a)]] for r in [mdp.reward[(state, a)]]]
            values[state] = max(candidates)
            delta = max(delta, abs(v - values[state]))
        if delta < theta:
            print("Number of iterations", niter)
            return values

def plot_policy(policy_vals):
    skeys = sorted(policy_vals.keys())
    x = np.array([a for a, _ in skeys])
    y = np.array([b for _, b in skeys])

    temp = np.array([policy_vals[k] for k in skeys])

    nrows, ncols = 10, 10
    grid = temp.reshape((nrows, ncols))

    plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min()), cmap=cm.Greys)
    plt.show()

def main():
    pass

if __name__ == '__main__':
    main()

max_stars_repo_name: Rauf-Kurbanov/rl-course

max_issues_repo_name: Rauf-Kurbanov/rl-course

max_forks_repo_name: Rauf-Kurbanov/rl-course