def create_binary_confusion_matrix(
    truth_binary_values: np.ndarray, prediction_binary_values: np.ndarray, name=None
) -> pd.Series:
    # This implementation is:
    # ~30x faster than sklearn.metrics.confusion_matrix
    # ~25x faster than sklearn.metrics.confusion_matrix(labels=[False, True])
    # ~6x faster than pandas.crosstab
    truth_binary_values = truth_binary_values.ravel()
    prediction_binary_values = prediction_binary_values.ravel()

    truth_binary_negative_values = 1 - truth_binary_values
    test_binary_negative_values = 1 - prediction_binary_values

    true_positive = np.sum(np.logical_and(truth_binary_values, prediction_binary_values))
    true_negative = np.sum(
        np.logical_and(truth_binary_negative_values, test_binary_negative_values)
    )
    false_positive = np.sum(np.logical_and(truth_binary_negative_values, prediction_binary_values))
    false_negative = np.sum(np.logical_and(truth_binary_values, test_binary_negative_values))

    # Storing the matrix as a Series instead of a DataFrame makes it easier to reference elements
    # and aggregate multiple matrices
    cm = pd.Series(
        {'TP': true_positive, 'TN': true_negative, 'FP': false_positive, 'FN': false_negative},
        name=name,
    )

    return cm
Ejemplo n.º 2
0
def get_metrics(gt_hat: np.ndarray, gt: np.ndarray, metrics: dict):
    gt_hat = gt_hat.ravel()
    gt = gt.ravel()
    to_delete = np.where(gt == 0)[0]
    gt, gt_hat = np.delete(gt, to_delete), np.delete(gt_hat, to_delete)
    ars_score = adjusted_rand_score(gt, gt_hat)
    nmi_score = normalized_mutual_info_score(gt, gt_hat)
    metrics[MetricsEnum.ARS_SCORE].append(ars_score)
    metrics[MetricsEnum.NMI_SCORE].append(nmi_score)
    print("ARS -> {}\nNMI -> {}".format(ars_score, nmi_score))
Ejemplo n.º 3
0
 def _add_noise(self, x_input: np.ndarray, x_transf: np.ndarray, fx: float) -> float:  # pylint: disable=unused-argument
     noise = 0
     noise_level = self._noise_level
     if noise_level:
         if not self._noise_dissymmetry or x_transf.ravel()[0] <= 0:
             side_point = self.transform(x_input + np.random.normal(0, 1, size=self.dimension))
             if self._noise_dissymmetry:
                 noise_level *= (1. + x_transf.ravel()[0]*100.)
             noise = noise_level * np.random.normal(0, 1) * (self.oracle_call(side_point) - fx)
     return fx + noise
Ejemplo n.º 4
0
 def __init__(self,
              data: np.ndarray,
              x: np.ndarray,
              y: np.ndarray,
              roi: Number_T = 0.02):
     x_ravel = x.ravel()
     y_ravel = y.ravel()
     self.tree = KDTree(np.dstack((x_ravel, y_ravel))[0])
     self.data = data
     self.roi = roi
Ejemplo n.º 5
0
    def __new__(cls,
                tensor_desc: TensorDesc,
                arr: np.ndarray = None):  # type: ignore
        arr_size = 0
        precision = ""
        if tensor_desc is not None:
            tensor_desc_size = int(np.prod(tensor_desc.dims))
            precision = tensor_desc.precision
            if arr is not None:
                arr = np.array(arr)  # Keeping array as numpy array
                arr_size = int(np.prod(arr.shape))
                if np.isfortran(arr):
                    arr = arr.ravel(order="F")
                else:
                    arr = arr.ravel(order="C")
                if arr_size != tensor_desc_size:
                    raise AttributeError(
                        f"Number of elements in provided numpy array "
                        f"{arr_size} and required by TensorDesc "
                        f"{tensor_desc_size} are not equal")
                if arr.dtype != precision_map[precision]:
                    raise ValueError(
                        f"Data type {arr.dtype} of provided numpy array "
                        f"doesn't match to TensorDesc precision {precision}")
                if not arr.flags["C_CONTIGUOUS"]:
                    arr = np.ascontiguousarray(arr)
            elif arr is None:
                arr = np.empty(0, dtype=precision_map[precision])
        else:
            raise AttributeError("TensorDesc can't be None")

        if precision in ["FP32"]:
            return TBlobFloat32(tensor_desc, arr, arr_size)
        elif precision in ["FP64"]:
            return TBlobFloat64(tensor_desc, arr, arr_size)
        elif precision in ["FP16", "BF16"]:
            return TBlobInt16(tensor_desc, arr.view(dtype=np.int16), arr_size)
        elif precision in ["I64"]:
            return TBlobInt64(tensor_desc, arr, arr_size)
        elif precision in ["U64"]:
            return TBlobUint64(tensor_desc, arr, arr_size)
        elif precision in ["I32"]:
            return TBlobInt32(tensor_desc, arr, arr_size)
        elif precision in ["U32"]:
            return TBlobUint32(tensor_desc, arr, arr_size)
        elif precision in ["I16"]:
            return TBlobInt16(tensor_desc, arr, arr_size)
        elif precision in ["U16"]:
            return TBlobUint16(tensor_desc, arr, arr_size)
        elif precision in ["I8", "BIN"]:
            return TBlobInt8(tensor_desc, arr, arr_size)
        elif precision in ["U8", "BOOL"]:
            return TBlobUint8(tensor_desc, arr, arr_size)
        else:
            raise AttributeError(f"Unsupported precision {precision} for Blob")
def stratified_perm_test(pop_a_1: np.ndarray,
                         pop_b_1: np.ndarray,
                         pop_a_2: np.ndarray,
                         pop_b_2: np.ndarray,
                         stat_test: t.Callable[[np.ndarray, np.ndarray],
                                               t.Union[float, int, np.number]],
                         perm_num: int = 1000,
                         random_seed: t.Optional[int] = None) -> float:
    """Tests whether the stratified ``test_values`` values of ``pop_a`` and ``pop_b`` match.

    The stratification is made between groups ``1`` and ``2``.

    Arguments
    ---------
    stat_test : :obj:`Callable`
        Statistical test. Must be a function that receives two numpy
        arrays and return some numeric value.

    perm_num : :obj:`int`, optional
        Number of permutations.

    random_seed : :obj:`int`, optional
        If given, set the random seed before the first permutation.

    Returns
    -------
    :obj:`float`
        p-value of the permutation test.
    """
    if random_seed is not None:
        np.random.seed(random_seed)

    if pop_a_1.ndim != 1:
        pop_a_1 = pop_a_1.ravel()

    if pop_b_1.ndim != 1:
        pop_b_1 = pop_b_1.ravel()

    if pop_a_2.ndim != 2:
        pop_a_2 = pop_a_2.ravel()

    if pop_b_2.ndim != 2:
        pop_b_2 = pop_b_2.ravel()

    truediff = stat_test(np.concatenate((pop_a_1, pop_a_2)),
                         np.concatenate((pop_b_1, pop_b_2)))

    stat_test_vals = np.zeros(perm_num)
    for ind in np.arange(perm_num):
        sh_a_1, sh_b_1 = _shuffle(pop_a_1, pop_b_1)
        sh_a_2, sh_b_2 = _shuffle(pop_a_2, pop_b_2)
        stat_test_vals[ind] = stat_test(np.concatenate((sh_a_1, sh_a_2)),
                                        np.concatenate((sh_b_1, sh_b_2)))

    return (np.sum(truediff <= stat_test_vals) + 1) / (perm_num + 1)
Ejemplo n.º 7
0
def output_intensity_mapping(image: np.ndarray):
    output_im = np.zeros(image.shape, dtype=np.uint8)
    arr_max = image.max()
    arr_min = image.min() + 1e-10

    for index in range(len(image.ravel())):
        output_im.ravel()[index] = \
            int(np.floor(((image.ravel()[index] - arr_min)
                          / (arr_max - arr_min)) * 255 + 0.5))

    return output_im
Ejemplo n.º 8
0
def create_and_save_histogram(data: numpy.ndarray,
                              file_name: Path,
                              add_inverse_cdf_results: bool = False):
    plt.clf()
    n, bins, patches = plt.hist(data.ravel(), bins='auto')
    if add_inverse_cdf_results:
        inverse_cdf = get_inverse_cdf(data.ravel())
        approximated_data_dist = inverse_cdf(numpy.random.rand(data.size))
        plt.hist(approximated_data_dist.ravel(), bins=bins)

    plt.savefig(file_name)
Ejemplo n.º 9
0
def geo2mag(yeardec: float, glat: np.ndarray,
            glon: np.ndarray) -> T.Dict[str, T.Any]:
    fort_geo2mag = shutil.which("geo2mag", path=str(R))
    if not fort_geo2mag:
        raise ImportError(
            f"geo2mag executable not found in {R}, did you build Apex with CMake?"
        )

    glat = np.atleast_1d(glat)
    glon = np.atleast_1d(glon)

    orig_shape = glat.shape

    infile = tempfile.NamedTemporaryFile(mode="w", suffix=".nml", delete=False)
    f = infile.file  # type: ignore

    f.write(f"""
&input_shape
N = {glat.size}
/\n
""")

    f.write("""
&input
glat = """)
    f.write(",".join(glat.ravel().astype(str).tolist()) + "\n")

    f.write("glon = ")
    f.write(",".join(glon.ravel().astype(str).tolist()) + "\n")

    f.write("/\n")
    infile.close()  # avoids Permission Error in Fortran

    outfile = tempfile.NamedTemporaryFile(suffix=".nml", delete=False)

    cmd = [fort_geo2mag, f"{yeardec:.3f}", infile.name, outfile.name]
    logging.info(" ".join(cmd))
    ret = subprocess.run(cmd)
    if ret.returncode != 0:
        raise RuntimeError("Apex error")

    dat = read_namelist(outfile.name, "output")

    try:
        Path(infile.name).unlink()
        Path(outfile.name).unlink()
    except PermissionError:
        pass

    for k, v in dat.items():
        dat[k] = v.reshape(orig_shape)

    return dat
Ejemplo n.º 10
0
def assert_arrays_almost_equal(test: unittest.TestCase, actuals: np.ndarray, desires: np.ndarray, dif_ratio=1e-3, limit=1e-8):
    assert(isinstance(actuals, np.ndarray))
    assert(isinstance(desires, np.ndarray))

    test.assertEqual(len(actuals.shape), len(desires.shape))

    for _, (a, b) in enumerate(zip(actuals.shape, desires.shape)):
        test.assertEqual(a, b)

    for _, (input, ref) in enumerate(zip(actuals.ravel(), desires.ravel())):
        delta = max(abs(ref * dif_ratio), limit)
        test.assertAlmostEqual(input, ref, delta=delta)
Ejemplo n.º 11
0
def kmeans1d(x: np.ndarray,
             k: int,
             method: str = "ckmeans1d",
             method_kws: dict = None) -> np.ndarray:
    """1-dim k-means clustering.
    Uses Ckmeans.1d.dp through ``ckwrap`` if it is installed and `method` is
    'ckmeans1d'.

    Args:
        x: flattened to 1d
        k: number of clusters
        method: if 'ckmeans1d' ckwrap is used, otherwise 'kmeans' in 1d
        method_kws: passed through to the implementaion used

    Returns:
        array of labels mapping clusters to objects

    See Also:
        :func:`pewpew.lib.kmeans.kmeans`
    """
    kwargs = {
        "init": "kmeans++",
        "max_iterations": 1000,
        "weights": None,
        "method": "linear",
    }
    if method_kws is not None:
        kwargs.update(method_kws)

    if method == "ckmeans1d":  # pragma: no cover
        try:
            from ckwrap import ckmeans

            idx = ckmeans(
                x.ravel(),
                (k, k),
                weights=kwargs["weights"],
                method=kwargs["method"],
            ).labels
        except ImportError:
            logger.warning(
                "Unable to use ckmeans1d as ckwrap package not found.")
            return kmeans1d(x, k, method="kmeans", method_kws=method_kws)
    elif method == "kmeans":
        idx = kmeans(
            x.ravel(),
            k,
            init=kwargs["init"],  # type: ignore
            max_iterations=kwargs["max_iterations"],  # type: ignore
        ).labels
    else:  # pragma: no cover
        raise ValueError(f"Unknown method {method}.")
    return np.reshape(idx, x.shape)
Ejemplo n.º 12
0
    def map_data(self, lon: ndarray, lat: ndarray) -> ndarray:
        shape = lon.shape
        lon = lon.ravel()
        lat = lat.ravel()

        lon = (lon - self.lon_avg) / self.lon_std
        lat = (lat - self.lat_avg) / self.lat_std

        inputs = np.dstack([lon, lat])[0]
        outputs = self.knn.predict(inputs).reshape(shape)

        return outputs
Ejemplo n.º 13
0
    def solve(self, data: np.ndarray, max_iter: int = 5000, tol: float = 1e-4):

        super(SmoothBregman, self).solve(data=data, max_iter=max_iter, tol=tol)

        self.G.data = data.ravel()

        pk = np.zeros(self.domain_shape).ravel()

        if self.plot_iteration:
            plt.Figure()

        u = np.zeros(self.domain_shape)

        i = old_e = 0
        while True:
            print("current norm error: " +
                  str(np.linalg.norm(u.ravel() - data.ravel())))
            print("runs till norm <: " + str(self.assessment))

            self.solver = PdHgm(self.K, self.F_star, self.G)
            self.solver.max_iter = max_iter
            self.solver.tol = tol
            self.G.pk = pk

            self.solver.solve()

            u_new = np.reshape(self.solver.x, self.domain_shape)

            e = np.linalg.norm(u_new.ravel() - data.ravel())
            if e < self.assessment:
                # which iteration to choose -> norm nearest
                if np.abs(old_e - self.assessment) > np.abs(
                        e - self.assessment) and not self.stop_in_front:
                    u = u_new
                break
            old_e = e

            u = u_new

            #pk = pk - (self.lam / self.alpha) * (u.ravel() - data.ravel())
            pk = pk - self.lam * (u.ravel() - data.ravel())
            i = i + 1

            if self.plot_iteration:
                plt.imshow(u, vmin=0, vmax=1)
                plt.axis('off')
                plt.savefig(self.data_output_path + 'Bregman_iter' + str(i) +
                            '.png',
                            bbox_inches='tight',
                            pad_inches=0)
                plt.close()

        return u
Ejemplo n.º 14
0
def get_roc_curve(y_true: np.ndarray, y_score: np.ndarray) -> dict:
    """
    calculates roc curve data from y true and prediction scores
    includes fpr, tpr, thresholds, roc_auc
    at each level of y, micro and macro averaged

    Args:
        y_true: true y values
        y_score: y prediction scores

    Returns:
        dict with roc curve data
    """
    n_classes = y_true.shape[1]

    # Compute ROC curve and ROC area for each class
    roc_curves = {}
    for i in range(n_classes):
        fpr, tpr, thresholds = metrics.roc_curve(y_true[:, i], y_score[:, i])
        roc_curves[i] = {
            "fpr": fpr,
            "tpr": tpr,
            "thresholds": thresholds,
            "roc_auc": metrics.auc(fpr, tpr)
        }

    # Compute micro-average ROC curve and ROC area
    fpr, tpr, thresholds = metrics.roc_curve(y_true.ravel(), y_score.ravel())
    roc_curves["micro"] = {
        "fpr": fpr,
        "tpr": tpr,
        "thresholds": thresholds,
        "roc_auc": metrics.auc(fpr, tpr)
    }

    # Compute macro-average ROC curve and ROC area
    # First aggregate all false positive rates
    all_fpr = np.unique(
        np.concatenate([roc_curves[i]["fpr"] for i in range(n_classes)]))
    # Then interpolate all ROC curves at this points
    mean_tpr = np.zeros_like(all_fpr)
    for i in range(n_classes):
        mean_tpr += np.interp(all_fpr, roc_curves[i]["fpr"],
                              roc_curves[i]["tpr"])
    # Finally average it and compute AUC
    mean_tpr /= n_classes
    roc_curves["macro"] = {
        "fpr": all_fpr,
        "tpr": mean_tpr,
        "roc_auc": metrics.auc(all_fpr, mean_tpr)
    }

    return roc_curves
Ejemplo n.º 15
0
def get_error_cost(V: np.ndarray, V_approx: np.ndarray) -> float:
    """
    Compute error cost of encoding for description length
    :param V: original matrix
    :param V_approx: reconstructed matrix from encoded factors
    """
    # KL divergence as given in section 2.3 of RolX paper
    vec1 = V.ravel()
    vec2 = V_approx.ravel()
    kl_div = np.sum(
        np.where(vec1 != 0,
                 vec1 * np.log(vec1 / vec2) - vec1 + vec2, 0))
    return kl_div
Ejemplo n.º 16
0
def create_binary_confusion_matrix(
    truth_binary_values: np.ndarray,
    prediction_binary_values: np.ndarray,
    weights: Optional[np.ndarray] = None,
    name: Optional[Union[str, Tuple[str, ...]]] = None,
) -> pd.Series:
    # This implementation is:
    # ~30x faster than sklearn.metrics.confusion_matrix
    # ~25x faster than sklearn.metrics.confusion_matrix(labels=[False, True])
    # ~6x faster than pandas.crosstab
    truth_binary_values = truth_binary_values.ravel()
    prediction_binary_values = prediction_binary_values.ravel()
    if weights is not None:
        weights = weights.ravel()

    truth_binary_negative_values = 1 - truth_binary_values
    test_binary_negative_values = 1 - prediction_binary_values

    true_positives = np.logical_and(truth_binary_values,
                                    prediction_binary_values)
    true_negatives = np.logical_and(truth_binary_negative_values,
                                    test_binary_negative_values)
    false_positives = np.logical_and(truth_binary_negative_values,
                                     prediction_binary_values)
    false_negatives = np.logical_and(truth_binary_values,
                                     test_binary_negative_values)

    if weights is not None:
        true_positives = true_positives * weights
        true_negatives = true_negatives * weights
        false_positives = false_positives * weights
        false_negatives = false_negatives * weights

    true_positive = np.sum(true_positives)
    true_negative = np.sum(true_negatives)
    false_positive = np.sum(false_positives)
    false_negative = np.sum(false_negatives)

    # Storing the matrix as a Series instead of a DataFrame makes it easier to reference elements
    # and aggregate multiple matrices
    cm = pd.Series(
        {
            'TP': true_positive,
            'TN': true_negative,
            'FP': false_positive,
            'FN': false_negative
        },
        name=name,
    )

    return cm
Ejemplo n.º 17
0
def apply_affine(A: Affine, x: np.ndarray, y: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
    """
    broadcast A*(x_i, y_i) across all elements of x/y arrays in any shape (usually 2d image)
    """

    shape = x.shape

    A = np.asarray(A).reshape(3, 3)
    t = A[:2, -1].reshape((2, 1))
    A = A[:2, :2]

    x, y = A @ np.vstack([x.ravel(), y.ravel()]) + t
    x, y = (a.reshape(shape) for a in (x, y))
    return (x, y)
Ejemplo n.º 18
0
def plot_contours(ax, clf, xx: np.ndarray, yy: np.ndarray, **params):
    """
    Generates a plot with the decision boundaries for a classifier.

    :param ax: matplotlib axes object
    :param clf: a classifier
    :param xx: meshgrid ndarray
    :param yy: meshgrid ndarray
    :param params: dictionary of params to pass to contourf, optional
    """
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
    return out
Ejemplo n.º 19
0
 def interpolate(self, lon: np.ndarray, lat: np.ndarray,
                 dates: np.ndarray) -> np.ndarray:
     """Interpolate the SSH to the required coordinates."""
     ds = self._select_ds(
         dates.min(),  # type: ignore
         dates.max())  # type: ignore
     assert np.all(np.diff(ds.ocean_time.values) == self._dt)
     interpolator = pyinterp.backends.xarray.RegularGridInterpolator(
         ds[self.ssh])
     return interpolator(dict(lat_rho=lat.ravel(),
                              lon_rho=lon.ravel(),
                              ocean_time=dates.ravel()),
                         method="bilinear",
                         bounds_error=False).reshape(lon.shape)
Ejemplo n.º 20
0
def calculate_roc_auc(y_true: np.ndarray, y_prob: np.ndarray, class_names: List[str],
                      task: str = None) -> Tuple[dict, dict, dict]:
    """
    Calculates Receiver Operating Characteristic Area Under Curve.
    Apart from ROC scores the function outputs the False Positives Ratios,
    and True Positive Ratios, which can be used to plot ROC curves.
    Parameters
    ----------
    y_true: np.ndarray
        Array of true labels (last shape of array should equal to number of classes).
    y_prob: np.ndarray
        Array of predicted probabilities (last shape of array should equal to number of classes).
    class_names: List of strings
        Names of classes.
    task: str, optional
        Type of classification task. Types of task supported: ['binary', 'multiclass', 'multilabel'].
    Returns
    -------
    out: Tuple of 3 dictionaries
        The outputs are 3 dictionaries: False Positives Ratios and True Positive Ratios which are dictionaries
        in which keys are class names and values are numpy arrays, and a dictionary of ROC AUC scores.
    """
    fpr = dict()
    tpr = dict()
    roc_auc_dict = dict()

    if not task:
        task = check_task(y_true, y_prob)

    if task in ['binary', 'multiclass']:
        encoder = OneHotEncoder()
        y_true = encoder.fit_transform(y_true).toarray()

    for i, name in enumerate(class_names):
        fpr[name], tpr[name], _ = roc_curve(y_true[:, i], y_prob[:, i])
        roc_auc_dict[name] = auc(fpr[name], tpr[name])

    # Compute micro-average ROC curve and ROC area
    fpr["micro avg"], tpr["micro avg"], _ = roc_curve(y_true.ravel(), y_prob.ravel())
    if task == 'binary':
        # if task is binary avg roc auc is the same as roc auc of both classes
        # therefore we use the calculated value of one of the classes to fill it
        roc_auc_dict["micro avg"] = roc_auc_dict[class_names[0]]
    else:
        roc_auc_dict["micro avg"] = auc(fpr["micro avg"], tpr["micro avg"])

    fpr, tpr, roc_auc_dict = calculate_macro_auc(fpr, tpr, roc_auc_dict, class_names)

    return fpr, tpr, roc_auc_dict
def bootstrap_gp_fit(x: np.ndarray,
                     y: np.ndarray,
                     N: int = 10) -> Dict[str, Union[float, np.ndarray]]:
    """
    Fit gaussian process to x and y data N times. Return the resulting mean predictions and gp parameters/quantities
    as well as their standard deviation

    Arguments:
        x: Inputs to gaussian process
        y: Outputs to which gaussian process will be fit
        N: Number of times GP is fit to data

    Returns:
        bootstrap_dict: Dictionary containing relevant GP parameters
    """
    def _fit_gaussian(x_, y_):
        kernel = C(1.0, (1e-3, 1e4)) * RBF(1.0, (1e-3, 1e4))
        gp = GaussianProcessRegressor(kernel=kernel,
                                      n_restarts_optimizer=10,
                                      normalize_y=True)
        gp.fit(x, y)
        y_pred, sigma = gp.predict(x, return_std=True)

        nrmse = np.sqrt(np.mean((y_pred - np.array(y))**2)) / np.mean(y)

        constant = gp.kernel_.k1.constant_value
        length_scale = gp.kernel_.k2.length_scale
        return y_pred.reshape((1, -1)), sigma.reshape(
            (1, -1)), nrmse, constant, length_scale

    results = joblib.Parallel(n_jobs=multiprocessing.cpu_count())(
        joblib.delayed(_fit_gaussian)(x, y) for _ in range(N))
    y_pred_list, sigma_list, nrmse_list, constant_list, length_scale_list = list(
        zip(*results))
    y_pred_arr, sigma_arr = np.array(y_pred_list), np.array(sigma_list)

    bootstrap_dict = {
        'mn_length_scale': np.mean(length_scale_list),
        'mn_constant': np.mean(constant_list),
        'mn_nrmse': np.mean(nrmse_list),
        'std_length_scale': np.std(length_scale_list),
        'std_constant': np.std(constant_list),
        'std_nrmse': np.std(nrmse_list),
        'mn_y_pred': np.mean(y_pred_arr, axis=0)[0],
        'mn_sigma': np.mean(sigma_arr, axis=0)[0],
        'y': y.ravel(),
        'x': x.ravel()
    }
    return bootstrap_dict
Ejemplo n.º 22
0
def wmm(glats: np.ndarray, glons: np.ndarray, alt_km: float,
        yeardec: float) -> xarray.Dataset:

    glats = np.atleast_2d(glats).astype(float)  # to coerce all else to float64
    glons = np.atleast_2d(glons)

    assert glats.shape == glons.shape

    mag = xarray.Dataset(coords={'glat': glats[:, 0], 'glon': glons[0, :]})
    north = np.empty(glats.size)
    east = np.empty(glats.size)
    down = np.empty(glats.size)
    total = np.empty(glats.size)
    decl = np.empty(glats.size)
    incl = np.empty(glats.size)

    for i, (glat, glon) in enumerate(zip(glats.ravel(), glons.ravel())):

        x = ct.c_double()
        y = ct.c_double()
        z = ct.c_double()
        T = ct.c_double()
        D = ct.c_double()
        mI = ct.c_double()

        ret = libwmm.wmmsub(ct.c_double(glat), ct.c_double(glon),
                            ct.c_double(alt_km), ct.c_double(yeardec),
                            ct.byref(x), ct.byref(y), ct.byref(z), ct.byref(T),
                            ct.byref(D), ct.byref(mI))

        assert ret == 0

        north[i] = x.value
        east[i] = y.value
        down[i] = z.value
        total[i] = T.value
        decl[i] = D.value
        incl[i] = mI.value

    mag['north'] = (('glat', 'glon'), north.reshape(glats.shape))
    mag['east'] = (('glat', 'glon'), east.reshape(glats.shape))
    mag['down'] = (('glat', 'glon'), down.reshape(glats.shape))
    mag['total'] = (('glat', 'glon'), total.reshape(glats.shape))
    mag['incl'] = (('glat', 'glon'), incl.reshape(glats.shape))
    mag['decl'] = (('glat', 'glon'), decl.reshape(glats.shape))

    mag.attrs['time'] = yeardec

    return mag
Ejemplo n.º 23
0
def main(X: np.ndarray, y: np.ndarray) -> None:

    model = SVR(kernel='poly',
                degree=3,
                C=101,
                coef0=1,
                epsilon=0.1,
                tol=1e-3,
                gamma='auto')
    model.fit(X, y.ravel(), sample_weight=None)
    y_pred = model.predict(X)
    print("Mean Squared Error Result of training")
    print(mean_squared_error(y.ravel(), y_pred))
    filename = 'svm_model.save'
    pickle.dump(model, open(filename, 'wb'))
Ejemplo n.º 24
0
    def _train_epoch(self, feats: np.ndarray, label: np.ndarray):
        """
        학습시 1회 에폭에 대한 행동을 정의합니다.
        grid_search가 True인 경우 grid search를 수행합니다.

        :param feats: 학습할 피쳐
        :param label: 라벨 리스트
        """

        if self.grid_search:
            self.model = self._grid_search(feats, label.ravel())
        else:
            self.model.fit(feats, label.ravel())

        self._save_model()
Ejemplo n.º 25
0
 def _map_points(self, x: np.ndarray, y: np.ndarray) -> np.ma.MaskedArray:
     _MAX_RETURN = 5
     _FILL_VALUE = -1e5
     xdim, ydim = x.shape
     _, idx = self.tree.query(np.dstack((x.ravel(), y.ravel()))[0],
                              distance_upper_bound=self.md,
                              k=_MAX_RETURN)
     idx_all = idx.ravel()
     data_indexing = np.append(self.data_ravel, _FILL_VALUE)
     dist_indexing = np.append(self.dist_ravel, 0)
     target_rad = np.ma.masked_equal(data_indexing[idx_all], _FILL_VALUE)
     weight = dist_indexing[idx_all]
     inp = target_rad.reshape(xdim, ydim, _MAX_RETURN)
     wgt = weight.reshape(xdim, ydim, _MAX_RETURN)
     return np.ma.average(inp, weights=1 / wgt, axis=2)
Ejemplo n.º 26
0
def singlePointBinaryCrossover(p1: np.ndarray,
                               p2: np.ndarray) -> [np.ndarray, np.ndarray]:
    index = int(np.floor(np.random.default_rng().random() * p1.size))

    c1 = np.append(p1.ravel()[:index], p2.ravel()[index:])
    c2 = np.append(p2.ravel()[:index], p1.ravel()[index:])

    c1 = c1.reshape(p1.shape)
    c2 = c2.reshape(p1.shape)
    if p1.shape != p2.shape:
        print(p1.shape, ":::", p2.shape)
    gaussianMutation(c1)
    gaussianMutation(c2)

    return c1, c2
Ejemplo n.º 27
0
def mtx_similar1(arr1:np.ndarray, arr2:np.ndarray) ->float:
   
    farr1 = arr1.ravel()
    farr2 = arr2.ravel()
    len1 = len(farr1)
    len2 = len(farr2)
    if len1 > len2:
        farr1 = farr1[:len2]
    else:
        farr2 = farr2[:len1]

    numer = np.sum(farr1 * farr2)
    denom = np.sqrt(np.sum(farr1**2) * np.sum(farr2**2))
    similar = numer / denom 
    return  (similar+1) / 2
Ejemplo n.º 28
0
def dump_lattice_gro(vectors: numpy.ndarray) -> str:
    """
    Extracts final line of a .gro file from the lattice vectors for pbc.

    Parameters
    ----------
    vectors : numpy.ndarray((3,3))
        An array with a lattice vector in each row.

    Returns
    -------
    line : string
        The final line of a .gro file.

    """

    # Reorder the vectors comps
    vectors = vectors.ravel()
    index = (0, 4, 8, 1, 2, 3, 5, 6, 7)
    new_vector = numpy.zeros_like(vectors)
    for i, val in enumerate(index):
        new_vector[i] = vectors[val]
    if numpy.any(new_vector[3:]):
        lim = 9
    else:
        lim = 3
    return ' '.join(['{:9.5f}'.format(i) for i in new_vector[:lim]])
Ejemplo n.º 29
0
def array_to_rest_datadef(data_type: str,
                          array: np.ndarray,
                          names: Optional[List[str]] = []) -> Dict:
    """
    Construct a payload Dict from a numpy array

    Parameters
    ----------
    data_type
    array
    names

    Returns
    -------
       Dict representing Seldon payload

    """
    datadef: Dict = {"names": names}
    if data_type == "tensor":
        datadef["tensor"] = {
            "shape": array.shape,
            "values": array.ravel().tolist()
        }
    elif data_type == "ndarray":
        datadef["ndarray"] = array.tolist()
    elif data_type == "tftensor":
        tftensor = tf.make_tensor_proto(array)
        jStrTensor = json_format.MessageToJson(tftensor)
        jTensor = json.loads(jStrTensor)
        datadef["tftensor"] = jTensor
    else:
        datadef["ndarray"] = array.tolist()
    return datadef
Ejemplo n.º 30
0
def as_tensor_table(array: np.ndarray):
    size = array.size
    shape = array.shape
    indices = np.unravel_index(range(size), shape)
    out = {f"dim{i}": idx for i, idx in enumerate(indices)}
    out["val"] = array.ravel()
    return out
Ejemplo n.º 31
0
 def get_cells_to_nums(
         ordered_nums: np.ndarray) -> Dict[Tuple[int, ...], int]:
     flattened = ordered_nums.ravel()
     word_divs = np.flatnonzero(np.diff(flattened, prepend=-1))
     nums = flattened[word_divs]
     groups = np.split(np.arange(len(flattened)), word_divs[1:])
     return dict(zip(map(tuple, groups), nums))
Ejemplo n.º 32
0
def logmart(A: np.ndarray, b: np.ndarray,
            *,
            relax: float = 1.,
            x0: float = None,
            sigma: float = None,
            max_iter: int = 200) -> tuple:
    """
    Displays delta Chisquare.
    Program is stopped if Chisquare increases.
    A is NxM array
    b is Nx1 vector
    returns Mx1 vector

    relax	     user specified relaxation constant	(default is 20.)
    x0	     user specified initial guess (N vector)  (default is backproject y, i.e., y#A)
    max_iter	user specified max number of iterations (default is 20)

    AUTHOR:	Joshua Semeter
    LAST MODIFIED:	5-2015

      Simple test problem
    A = np.diag([5, 5, 5])
    x = np.array([1,2,3])
    b = A @ x
    """
# %% parameter check
    if b.ndim != 1:
        raise ValueError('y must be a column vector')
    if A.ndim != 2:
        raise ValueError('A must be a matrix')
    if A.shape[0] != b.size:
        raise ValueError('A and y number of rows must match')
    if not isinstance(relax, float):
        raise ValueError('relax is a scalar float')

    b = b.copy()  # needed to avoid modifying outside this function!
# %% set defaults
    if sigma is None:
        sigma = np.ones_like(b)

    if x0 is None:  # backproject
        x = A.T @ b / A.ravel().sum()
        xA = A @ x
        x = x * b.max() / xA.max()
    elif isinstance(x0, (float, int)) or x0.size == 1:  # replicate
        x = x0 * np.ones_like(b)
    else:
        x = x0
# %% make sure there are no 0's in y
    b[b <= 1e-8] = 1e-8
    # W=sigma;
    # W=linspace(1,0,size(A,1))';
    # W=rand(size(A,1),1);
    W = np.ones(A.shape[0])
    W = W / W.sum()

    i = 0
    done = False
    arg = ((A @ x - b)/sigma)**2.
    chi2 = np.sqrt(arg.sum())

# %%  iterate solution, plot estimated data (diag elems of x#A)
    while not done:
        i += 1
        xold = x
        xA = A @ x
        t = (1/xA).min()
        C = relax*t*(1.-(xA/b))
        x = x / (1 - x*(A.T @ (W*C)))
# %% monitor solution
        chiold = chi2
        chi2 = np.sqrt((((xA - b)/sigma)**2).sum())
        # dchi2=(chi2-chiold);
        done = ((chi2 > chiold) & (i > 2)) | (i == max_iter) | (chi2 < 0.7)
# %% plot
#        figure(9); clf; hold off;
#        Nest=reshape(x,69,83);
#        imagesc(Nest); caxis([0,1e11]);
#        set(gca,'YDir','normal'); set(gca,'XDir','normal');
#        pause(0.02)
    y_est = A @ xold

    return xold, y_est, chi2, i