Skip to content

Instantly share code, notes, and snippets.

View N-McA's full-sized avatar

N-McA

View GitHub Profile
def get_perceptual_axis_center_in_figure_space(fig, ax):
xs = ax.get_xlim()
if ax.get_xscale() == 'log':
xs = np.log10(xs)
c_x = sum(xs) / 2
c_x = 10**c_x
else:
c_x = sum(xs) / 2
import numpy as np
from pathlib import Path
import h5py
def resolve_name(f, name_ref):
return ''.join([chr(i) for i in f[name_ref]])
def flat(x):
@N-McA
N-McA / numpy_index_extractor.py
Created April 9, 2018 11:32
Pull out an array of indexes in numpy, eg for lookup in image
def extract(idxs, target):
trailing_shape = target.shape[idxs.shape[1]:]
flat_idxs = np.ravel_multi_index(idxs.T, target.shape[:idxs.shape[1]])
result_shape = (idxs.shape[0], ) + trailing_shape
return target.reshape([-1, *trailing_shape])[flat_idxs].reshape(result_shape)