Skip to content

Instantly share code, notes, and snippets.

View pavlin-policar's full-sized avatar

Pavlin Poličar pavlin-policar

View GitHub Profile
def _get_required_decimals(x: np.ndarray) -> int:
"""Determine the number of decimals needed to nicely show the data."""
req_decimals = np.floor(np.log10(x))
dec = np.percentile(req_decimals, [5])
return abs(int(dec))
from collections import OrderedDict
from typing import Iterable
import numpy as np
import scipy.sparse as sp
import scipy.stats as stats
import pandas as pd
def FDR(p_values: Iterable, dependent=False, m=None, ordered=False) -> Iterable:
from sklearn.base import BaseEstimator
from sklearn.neighbors import NearestNeighbors
import numpy as np
import networkx as nx
from community import best_partition
def jaccard(x: set, y: set) -> float:
return len(x & y) / len(x | y)
import scipy.sparse as sp
import numpy as np
def plot_marker(
marker,
dataset,
embedding: np.ndarray,
binary=True,
s=1,
def confidence_histogram(y_true, y_probs, n_bins=10, ax=None):
if ax is None:
fig, ax = plt.subplots(figsize=(4, 4))
confidences = np.max(y_probs, axis=1)
predictions = np.argmax(y_probs, axis=1)
accuracies = predictions == y_true
bins = np.linspace(0, 1 - 1 / n_bins, n_bins)
bin_indices = np.digitize(confidences, bins=bins[1:])
"""Adapted from Orange 2"""
import numpy as np
import matplotlib.pyplot as plt
def compute_critical_difference(avg_ranks, N, alpha="0.05", type="nemenyi"):
""" Returns critical difference for Nemenyi or Bonferroni-Dunn test
according to given alpha (either alpha="0.05" or alpha="0.1") for average
ranks and number of tested data sets N. Type can be either "nemenyi" for
@pavlin-policar
pavlin-policar / h5ad.r
Last active September 16, 2019 13:26
Utility functions to read/write H5AD files from R
read.h5ad = function (fname) {
#' Read an H5AD file
#'
#' @param fname str: The path to the H5AD file
library(reticulate)
library(Matrix)
ad = import("anndata", convert = FALSE)
sp = import("scipy.sparse", convert = FALSE)
@pavlin-policar
pavlin-policar / max_heap.py
Created November 22, 2018 11:11
Python implementation of max heap/priority queue
class MaxHeap:
def __init__(self, collection=None):
self._heap = []
if collection is not None:
for el in collection:
self.push(el)
def push(self, value):
self._heap.append(value)
@pavlin-policar
pavlin-policar / lagrange_interpolation.py
Created May 1, 2018 19:36
Lagrange interpolating polynomial for both arbitrary and equidistant points
from typing import Callable
import numpy as np
import matplotlib.pyplot as plt
def lagrangian_interpolation(xs: np.ndarray, ys: np.ndarray) -> Callable:
"""Make a Lagrangian interpolating polynomial function."""
def _interpolate(x: float) -> float:
result = 0
@pavlin-policar
pavlin-policar / ridge_circulant.py
Created April 30, 2018 18:45
Ridge regression on circulant matrices
import numpy as np
# Initialize the generating vector
x = np.array([1, 2, 3, 4, 5])
y = np.array([5, 4, 3, 2, 1])
# Initialize the permutation matrix
P = np.array([
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],