# -*- coding: utf-8 -*- """ Created on Thu Mar 1 11:30:31 2018 @author: z5030440 Contains all the utilities, convenience functions and small functions that do simple things """ import matplotlib.pyplot as plt import numpy as np import pdb def ecdf(x): """convenience function for computing the empirical CDF""" vals, counts = np.unique(x, return_counts=True) ecdf = np.cumsum(counts).astype(np.float64) ecdf /= ecdf[-1] return vals, ecdf def intensity_histogram(image): """plots histogram and cumulative distribution of the pixel intensities in an image""" imSize = image.shape if len(imSize) == 2: im = image[:,:].reshape(imSize[0] * imSize[1]) im = im[~np.isnan(im)] fig, (ax1, ax2) = plt.subplots(2,1, sharex=True, figsize = (8,6)) ax1.hist(im, bins=300) ax1.set_title('Probability density function') ax2.hist(im, bins=300, cumulative=True, histtype='step') ax2.set_title('Cumulative distribution') plt.show() else: for i in range(imSize[2]): im = image[:,:,i].reshape(imSize[0] * imSize[1]) im = im[~np.isnan(im)] fig, (ax1, ax2) = plt.subplots(2,1, sharex=True, figsize = (8,6)) ax1.hist(im, bins=300) ax1.set_title('Probability density function') ax2.hist(im, bins=300, cumulative=True, histtype='step') ax2.set_title('Cumulative distribution') plt.show() def compare_images(im1, im2): """plots 2 images next to each other, sharing the axis""" plt.figure() ax1 = plt.subplot(121) plt.imshow(im1, cmap='gray') ax2 = plt.subplot(122, sharex=ax1, sharey=ax1) plt.imshow(im2, cmap='gray') plt.show() def find_indices(lst, condition): "imitation of MATLAB find function" return [i for i, elem in enumerate(lst) if condition(elem)] def reject_outliers(data, m=2): "rejects outliers in a numpy array" return data[abs(data - np.mean(data)) < m * np.std(data)] def duplicates_dict(lst): "return duplicates and indices" # nested function def duplicates(lst, item): return [i for i, x in enumerate(lst) if x == item] return dict((x, duplicates(lst, x)) for x in set(lst) if lst.count(x) > 1)