Commit 671c687a authored by Ibrahim's avatar Ibrahim
Browse files

merge conflict in rl/ppo

parents 709ed8a4 4be4c2cd
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
"""
Type-checking, -conversion operations.
"""



import datetime
from typing import Any

import numpy as np



def is_datetype(d: Any) -> bool:
    """
    Check if an object represents a date.
    
    Arguments:
        d {Any} -- Any instance
    
    Returns:
        bool -- True if it represents a date or a time.
    """
    return isinstance(d, (datetime.datetime, datetime.date, datetime.time)) \
           or np.issubdtype(getattr(d, 'dtype', None), np.datetime64)
+0 −0

Empty file added.

+71 −0
Original line number Diff line number Diff line
from typing import Dict

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

from utils.type_operations import is_datetype

from ..helpers import is_datetype


def plot_surface(x: np.ndarray, y: np.ndarray, z: np.ndarray, ax=None, fig_kwargs:Dict={}, **kwargs) \
    -> Axes3D:
    """
    Plot a 3D surface given x, y, z coordinates.

    Arguments:
        x {np.ndarray} -- A 1D or 2D array (indexed as [x, y]). Can be numeric or
            date/time-like.
        y {np.ndarray} -- A 1D or 2D array (indexed as [x, y]). Can be numeric or
            date/time-like.
        z {np.ndarray} -- A 2D array indexed as [x, y].

    Keyword Arguments:
        ax {Axes3D} -- The axes on which to plot surface. (default: {None})
        fig_kwargs {Dict} -- Dictionary of arguments for plt.figure() creation.
        **kwargs -- Passed to `ax.plot_surface()`

    Returns:
        Axes3D -- The axes on which the surface was plotted.
    """
    if ax is None:
        fig = plt.figure(**fig_kwargs)
        ax = fig.add_subplot(111, projection='3d')

    xtime, ytime = is_datetype(x[0]), is_datetype(y[0])
    xgrid, ygrid, zgrid = map(np.asarray, (x, y, z))

    xdim = x.shape[0]
    ydim = y.shape[0] if y.ndim == 1 else y.shape[1]

    if xgrid.ndim == 1:
        xlabels = xgrid
        xgrid = np.repeat(x[:, None], axis=1, repeats=ydim)
    else:
        xlabels = xgrid[:, 0]
    if ygrid.ndim == 1:
        ylabels = ygrid
        ygrid = np.repeat(y[None, :], axis=0, repeats=xdim)
    else:
        ylabels = ygrid[0, :]

    if xtime:
        xgrid = np.repeat(np.arange(xdim).reshape(-1, 1), axis=1, repeats=ydim)
    if ytime:
        ygrid = np.repeat(np.arange(ydim).reshape(1, -1), axis=0, repeats=xdim)

    ax.plot_surface(xgrid, ygrid, zgrid, **kwargs)

    if xtime:
        xticklocs = np.asarray(tuple(filter(lambda x: 0 <= x < xdim, \
                                            ax.get_xticks()))).astype(int)
        ax.set_xticks(xticklocs)
        ax.set_xticklabels(xlabels[xticklocs], rotation=20)
    if ytime:
        yticklocs = np.asarray(tuple(filter(lambda x: 0 <= x < xdim, \
                                            ax.get_yticks()))).astype(int)
        ax.set_yticks(yticklocs)
        ax.set_yticklabels(ylabels[yticklocs], rotation=20)

    return ax
+3 −0
Original line number Diff line number Diff line
@@ -10,3 +10,6 @@ from .env import (
    rewards,
    get_from_env
)
from .types import (
    is_datetype
)
 No newline at end of file
+3 −2
Original line number Diff line number Diff line
from typing import Any, Union, Dict
from typing import Any, OrderedDict, Union, Dict
from copy import deepcopy

from torch.nn import Module
@@ -6,7 +6,8 @@ from sklearn.base import BaseEstimator



def clone(model: Union[BaseEstimator, Module], attrs: Dict[str, Any]=None) -> Union[BaseEstimator, Module]:
def clone(model: Union[BaseEstimator, Module, OrderedDict, Dict],
          attrs: Dict[str, Any]=None) -> Union[BaseEstimator, Module, OrderedDict, Dict]:
    """
    Copy a scikit-learn or pytorch model.

Loading