コード例 #1
0
ファイル: graphs.py プロジェクト: martok/intransidice
def enumerate_fixed_len_cycles(adj: np.ndarray, k):
    pattern = [0] * k
    vcount = adj.shape[0]
    indexarr = np.array(range(vcount), dtype=int)
    # can only be a chain part if it connects in *and* out
    any_chain_part = adj.any(axis=1) & adj.any(axis=0)

    def rotate(i: int, vt: int, ok_for_next: np.ndarray):
        nonlocal pattern
        # what can come after vt?
        vna = indexarr[ok_for_next & adj[vt]]
        if i == k - 1:
            vh = pattern[0]
            # final digit, directly check if it heads home
            connects = adj[vna, vh]
            yield from ((*pattern[:i], v) for v in vna[connects])
        else:
            lessparts = ok_for_next.copy()
            for v in vna:
                pattern[i] = v
                lessparts[v] = False
                yield from rotate(i + 1, v, lessparts)
                lessparts[v] = True

    def root():
        nonlocal pattern
        for vh in tqdm(indexarr[any_chain_part], miniters=1):
            if FOR_PROFILER and (vh >= 360):
                break
            pattern[0] = vh
            yield from rotate(1, vh, any_chain_part & (indexarr > vh))

    if k < 1:
        return
    yield from root()
コード例 #2
0
def conv2d(src_image: np.ndarray, kernel: np.ndarray):

    assert src_image.any() and kernel.any()
    src_image = src_image.copy()
    src_type = src_image.dtype
    # src_image = src_image.astype(np.float)
    # cv2.imshow('Src', src_image)
    krn_h, krn_w = kernel.shape[:2]
    if krn_h % 2 + krn_w % 2 != 2:
        print('Warning: kernel dims not odd')
    mask = rotate_kernel(kernel)
    # mask = (kernel)
    # print(src_image.shape)
    # print(np.array(src_image.shape[:2])+2*np.array([krn_h//2, krn_w//2]))
    pad_zeros = np.zeros(np.array(src_image.shape[:2]) +
                         2 * np.array([krn_h // 2, krn_w // 2]),
                         dtype=src_image.dtype)
    win_w = krn_w // 2
    win_h = krn_h // 2
    pad_zeros[win_h:src_image.shape[0] + win_h,
              win_w:src_image.shape[1] + win_w] = src_image
    ret_image = np.zeros_like(src_image, dtype=np.float)
    for i in range(src_image.shape[0]):
        for j in range(src_image.shape[1]):
            ret_image[i,
                      j] = np.sum(pad_zeros[i:i + krn_h, j:j + krn_w] * mask)
    i_min = np.min(ret_image)
    i_max = np.max(ret_image)
    ret_image = np.clip(ret_image, i_min, i_max)
    if i_min == i_max:
        return ret_image - i_min
    else:
        return ((ret_image - i_min) / (i_max - i_min) *
                (L - 1)).astype(src_type)
コード例 #3
0
def Fit_inside_limits_polynomial(x: np.ndarray, y: np.ndarray, lowerx: float,
                                 upperx: float, power: int):

    if lowerx and upperx and power and x.any() and y.any():

        return calculate_results(x, y, lowerx, upperx, power)

    else:
        return x, y, make_zero_array(x)
コード例 #4
0
def Fit_outside_limits_polynomial(x: np.ndarray, y: np.ndarray, lowerx: float,
                                  upperx: float, power: int, offset: str):

    if lowerx and upperx and power and x.any() and y.any() and offset:

        return calculate_results1(x, y, lowerx, upperx, power, offset)

    else:

        return x, y, make_zero_array(x)
コード例 #5
0
ファイル: calculus.py プロジェクト: finsberg/ldrb
def bislerp(
    Qa: np.ndarray,
    Qb: np.ndarray,
    t: float,
) -> np.ndarray:
    r"""
    Linear interpolation of two orthogonal matrices.
    Assiume that :math:`Q_a` and :math:`Q_b` refers to
    timepoint :math:`0` and :math:`1` respectively.
    Using spherical linear interpolation (slerp) find the
    orthogonal matrix at timepoint :math:`t`.
    """

    if ~Qa.any() and ~Qb.any():
        return np.zeros((3, 3))
    if ~Qa.any():
        return Qb
    if ~Qb.any():
        return Qa

    tol = 1e-12
    qa = quaternion.from_rotation_matrix(Qa)
    qb = quaternion.from_rotation_matrix(Qb)

    quat_i = quaternion.quaternion(0, 1, 0, 0)
    quat_j = quaternion.quaternion(0, 0, 1, 0)
    quat_k = quaternion.quaternion(0, 0, 0, 1)

    quat_array = [
        qa,
        -qa,
        qa * quat_i,
        -qa * quat_i,
        qa * quat_j,
        -qa * quat_j,
        qa * quat_k,
        -qa * quat_k,
    ]

    dot_arr = [abs((qi.components * qb.components).sum()) for qi in quat_array]
    max_idx = int(np.argmax(dot_arr))
    max_dot = dot_arr[max_idx]
    qm = quat_array[max_idx]

    if max_dot > 1 - tol:
        return Qb

    qm_slerp = quaternion.slerp(qm, qb, 0, 1, t)
    qm_norm = qm_slerp.normalized()
    Qab = quaternion.as_rotation_matrix(qm_norm)
    return Qab
コード例 #6
0
def log_predictions(y_pred: np.ndarray) -> None:
    """
    logs predictions
    includes selected and percent selected

    Args:
        y_pred: y prediction
    """
    logger.info("labels shape: %s.", y_pred.shape)
    logger.info("selected: \t%s.", y_pred.sum(axis=0).tolist())
    logger.info("percent selected: \t%s.",
                float_array_string(y_pred.mean(axis=0)))
    logger.info("selected any:  %s.", y_pred.any(axis=1).sum())
    logger.info("percent selected any:  %.4f.", y_pred.any(axis=1).mean())
コード例 #7
0
def display_prediction(
    prediction: np.ndarray,
    class_labels: list,
    image: np.ndarray = None,
    image_path: str = None,
    text_origin: tuple = (10, 30),
    font_scale: float = 0.5,
    text_color: tuple = (0, 255, 0),
    text_thickness: int = 1,
    resize_ratio: int = None,
):
    try:
        if image_path:
            image = read_image(image_path)
        elif image.any():
            pass
        else:
            raise Exception(Error.NO_IMAGE_OR_PATH_ERROR)
        if resize_ratio:
            image = imutils.resize(
                image,
                width=image.shape[1] * resize_ratio,
            )

        return display_image(image=cv2.putText(
            image,
            f"Class: {class_labels[prediction[0]]}",
            text_origin,
            cv2.FONT_HERSHEY_COMPLEX,
            font_scale,
            text_color,
            text_thickness,
        ))
    except Exception as e:
        raise e
コード例 #8
0
def sum(
    values: np.ndarray, mask: np.ndarray, skipna: bool = True, min_count: int = 0,
):
    """
    Sum for 1D masked array.

    Parameters
    ----------
    values : np.ndarray
        Numpy array with the values (can be of any dtype that support the
        operation).
    mask : np.ndarray
        Boolean numpy array (True values indicate missing values).
    skipna : bool, default True
        Whether to skip NA.
    min_count : int, default 0
        The required number of valid values to perform the operation. If fewer than
        ``min_count`` non-NA values are present the result will be NA.
    """
    if not skipna:
        if mask.any():
            return libmissing.NA
        else:
            if check_below_min_count(values.shape, None, min_count):
                return libmissing.NA
            return np.sum(values)
    else:
        if check_below_min_count(values.shape, mask, min_count):
            return libmissing.NA

        if _np_version_under1p17:
            return np.sum(values[~mask])
        else:
            return np.sum(values, where=~mask)
コード例 #9
0
def get_data_numpy(data_array: np.ndarray):
    x_array = None  # np.empty(0) and remove Opt ? and is not Nones below
    y_array = None
    imped: Opt[np.ndarray] = None
    phase: Opt[np.ndarray] = None
    data: Tuple[np.ndarray, ...] = ()

    if not data_array.any():
        print("Warning: Data is empty")
        # data is still None

    elif len(data_array[0]) == 4:
        x_array = data_array[:, [2]]  # pd
        y_array = data_array[:, [3]]  # current
        data = (x_array, y_array)

    elif len(data_array[0]) == 6:
        freq = data_array[:, [1]]
        imped = data_array[:, [2]]
        phase = data_array[:, [3]]  # ; print(len(phase))
        # number = data_array[:, [0]]
        # signdif = data_array[:, [4]]
        # time = data_array[:, [5]]
        phase_rads = np.multiply(phase, (np.pi/180))
        freq_log = np.log10(freq)
        imped_log = np.log10(imped)
        phase_cos = np.cos(phase_rads)
        phase_sin = np.sin(phase_rads)
        imag_imped = np.absolute(np.multiply(imped, phase_sin, dtype=float))
        real_imped = np.multiply(imped, phase_cos, dtype=float)
        data = (freq_log, imped_log, phase, imag_imped, real_imped)
    else:
        print("Warning: Data malformed")

    return data
コード例 #10
0
def remove_object(src: np.ndarray,
                  drop_mask: np.ndarray,
                  keep_mask: Optional[np.ndarray] = None) -> np.ndarray:
    """Remove an object on the source image.

    :param src: A source image in RGB or grayscale format.
    :param drop_mask: A binary object mask to remove.
    :param keep_mask: An optional binary object mask to be protected from
        removal. If not specified, no area is protected.
    :return: A copy of the source image where the drop_mask is removed.
    """
    src = _check_src(src)

    drop_mask = _check_mask(drop_mask, src.shape[:2])

    if keep_mask is not None:
        keep_mask = _check_mask(keep_mask, src.shape[:2])

    gray = src if src.ndim == 2 else _rgb2gray(src)

    while drop_mask.any():
        energy = _get_energy(gray)
        energy[drop_mask] -= DROP_MASK_ENERGY
        if keep_mask is not None:
            energy[keep_mask] += KEEP_MASK_ENERGY
        seam = _get_backward_seam(energy)
        seam_mask = _get_seam_mask(src, seam)
        gray = _remove_seam_mask(gray, seam_mask)
        drop_mask = _remove_seam_mask(drop_mask, seam_mask)
        src = _remove_seam_mask(src, seam_mask)
        if keep_mask is not None:
            keep_mask = _remove_seam_mask(keep_mask, seam_mask)

    return src
コード例 #11
0
def disparate_impact_check(disparity_grid: np.ndarray) -> bool:
    """
    Checks if any sub-population pair violates chosen disparate impact measure.

    Parameters
    ----------
    disparity_grid : numpy.ndarray
        A square, diagonally symmetric, boolean numpy array representing
        pair-wise disparity between subpopulations. See the return value of
        :func:`fatf.fairness.models.measures.demographic_parity` as an example.

    Raises
    ------
    IncorrectShapeError
        The disparity grid matrix is not 2-dimensional or square.
    TypeError
        The disparity grid matrix is not of boolean type.
    ValueError
        The disparity grid matrix is a structured numpy array or is not
        diagonally symmetric.

    Returns
    -------
    is_disparate : boolean
        ``True`` if there is a disparity between any pair of sub-populations,
        ``False`` otherwise.
    """
    assert fudt.validate_binary_matrix(disparity_grid,
                                       'disparate impact'), 'Invalid matrix.'
    is_disparate = disparity_grid.any()
    return is_disparate
コード例 #12
0
def disc_diffusion_kron(A: np.ndarray, Q: np.ndarray, Ad: np.ndarray) -> np.ndarray:
    """Discretize diffusion matrix by solving indirectly the Lyapunov equation

    Charles C. Driver, Manuel C. Voelkle.
    Introduction to Hierarchical Continuous Time Dynamic Modelling With ctsem

    Args:
        A: State matrix
        Q: Diffusion matrix
        Ad: Discrete state matrix

    Returns:
        Process noise covariance matrix
    """
    if not Q.any():
        return Q

    nx = Q.shape[0]
    Ix = np.eye(nx)
    A_sharp = np.kron(A, Ix) + np.kron(Ix, A)
    b = np.atleast_2d(Q.ravel()).T
    try:
        x = solve(-A_sharp, b)
    except (LinAlgWarning, LinAlgError):
        x = lstsq(-A_sharp, b, rcond=-1)[0]

    return disc_diffusion_stationary(np.reshape(x, (nx, nx)), Ad)
コード例 #13
0
    def update_distribution(self,
                            previous_distribution: np.ndarray,
                            rewards: np.ndarray,
                            dones: np.ndarray,
                            gamma: np.ndarray = 0.9) -> np.ndarray:
        """
        Calculates the distributional Bellman update.
        Args:
            previous_distribution(np.ndarray):  previous distributions, size: batch_size x number_atoms
            rewards(np.ndarray):  rewards, size: batch_size x 1
            dones(np.ndarray): dones, boolean flag, size: batch_size x 1
            gamma(np.ndarray): gamma, size batch_size x 1

        Returns:
            np.ndarray: Bellman updated distributions
        """
        batch_size = previous_distribution.shape[0]
        next_distribution = np.zeros_like(previous_distribution)
        ones = np.ones(batch_size, dtype=np.int)

        for j in np.arange(self.number_atoms):
            # Reward + discounting and clipping
            tzj = rewards + gamma * self.support[j]
            tzj = np.maximum(np.minimum(tzj, self.vmax), self.vmin)
            # project it to the atom space
            bj = ((tzj - self.vmin) / self.delta_z)
            # distribute the values to the neighboring atoms
            low = np.floor(bj).astype(np.int)
            up = np.ceil(bj).astype(np.int)
            upper_weight = bj - low
            lower_weight = 1. - upper_weight
            next_distribution[range(batch_size),
                              low.squeeze()] += previous_distribution[
                                  range(batch_size),
                                  j * ones] * lower_weight.squeeze()
            next_distribution[range(batch_size),
                              up.squeeze()] += previous_distribution[
                                  range(batch_size),
                                  j * ones] * upper_weight.squeeze()

        if dones.any():
            # Set distributions to zero
            next_distribution[dones.squeeze(), :] = 0.0
            # Clip reward
            tzj = np.maximum(np.minimum(rewards[dones], self.vmax), self.vmin)
            # project it to the atom space
            bj = ((tzj - self.vmin) / self.delta_z).squeeze()
            # distribute the values to the neighboring atoms
            low = np.floor(bj).astype(np.int)
            up = np.ceil(bj).astype(np.int)
            upper_weight = bj - low
            lower_weight = 1. - upper_weight
            next_distribution[dones.squeeze(), low] += lower_weight
            next_distribution[dones.squeeze(), up] += upper_weight
        return next_distribution
コード例 #14
0
def the_board_is_empty(matrix: np.ndarray):
    """
    Функция, проверяющая, пуста ли доска
    :param matrix: Текущая доска
    :return: Да / нет
    """

    if matrix.any():
        return False
    else:
        print("Доска пуста.")
        return True