Created
April 4, 2014 09:17
-
-
Save kidpixo/9971010 to your computer and use it in GitHub Desktop.
mariolibrary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
My personal python library. | |
@author: damo_ma | |
""" | |
import numpy as np | |
def resize_2d_nonan(array,factor): | |
""" | |
Resize a 2D array by different factor on two axis sipping NaN values. | |
If a new pixel contains only NaN, it will be set to NaN | |
Parameters | |
---------- | |
array : 2D np array | |
factor : int or tuple. If int x and y factor wil be the same | |
Returns | |
------- | |
array : 2D np array scaled by factor | |
Created on Mon Jan 27 15:21:25 2014 | |
@author: damo_ma | |
""" | |
xsize, ysize = array.shape | |
if isinstance(factor,int): | |
factor_x = factor | |
factor_y = factor | |
elif isinstance(factor,tuple): | |
factor_x , factor_y = factor[0], factor[1] | |
else: | |
raise NameError('Factor must be a tuple (x,y) or an integer') | |
if not (xsize %factor_x == 0 or ysize % factor_y == 0) : | |
raise NameError('Factors must be intger multiple of array shape') | |
new_xsize, new_ysize = xsize/factor_x, ysize/factor_y | |
new_array = np.empty([new_xsize, new_ysize]) | |
new_array[:] = np.nan # this saves us an assignment in the loop below | |
# submatrix indexes : is the average box on the original matrix | |
subrow, subcol = np.indices((factor_x, factor_y)) | |
# new matrix indexs | |
row, col = np.indices((new_xsize, new_ysize)) | |
# | |
# # some output for testing | |
# for i, j, ind in zip(row.reshape(-1), col.reshape(-1),range(row.size)) : | |
# print '----------------------------------------------' | |
# print 'i: %i, j: %i, ind: %i ' % (i, j, ind) | |
# print 'subrow+i*new_ysize, subcol+j*new_xsize :' | |
# print i,'*',new_xsize,'=',i*factor_x | |
# print j,'*',new_ysize,'=',j*factor_y | |
# print subrow+i*factor_x | |
# print subcol+j*factor_y | |
# print '---' | |
# print 'array[subrow+i*factor_x,subcol+j*factor_y] : ' | |
# print array[subrow+i*factor_x,subcol+j*factor_y] | |
for i, j, ind in zip(row.reshape(-1), col.reshape(-1),range(row.size)) : | |
# define the small sub_matrix as view of input matrix subset | |
sub_matrix = array[subrow+i*factor_x,subcol+j*factor_y] | |
# modified from any(a) and all(a) to a.any() and a.all() | |
# see http://stackoverflow.com/a/10063039/1435167 | |
if not (np.isnan(sub_matrix)).all(): # if we haven't all NaN | |
if (np.isnan(sub_matrix)).any(): # if we haven no NaN at all | |
msub_matrix = np.ma.masked_array(sub_matrix,np.isnan(sub_matrix)) | |
(new_array.reshape(-1))[ind] = np.mean(msub_matrix) | |
else: # if we haven some NaN | |
(new_array.reshape(-1))[ind] = np.mean(sub_matrix) | |
# the case assign NaN if we have all NaN is missing due | |
# to the standard values of new_array | |
return new_array | |
def data_dropbox_path(): | |
import os | |
base_path = '/Users/damo_ma/Dropbox/' # on laptop | |
if not os.path.isdir(base_path) : | |
base_path = '/Users/damo_ma/Documents/Dropbox/' # on desktop | |
return base_path | |
def define_plot_matrix(plot_data, rows, cols): | |
# np.flipud : flip the image on the y (axes=1) to have right orientation | |
# The logic behind is linked to the regular grid generation in psql (fishnet) | |
fill_nan = np.zeros(((180-rows)/2 , cols)) | |
fill_nan[:,:] = np.nan | |
# pad the -90 / -80 and 80 / 90 with fake NaN | |
tmp = np.vstack( (fill_nan, plot_data.reshape(rows, cols), fill_nan) ) | |
tmp = np.flipud(tmp) | |
return tmp | |
def scale_nparr(input_arr,min_new=0,max_new=1): | |
""" | |
scale_nparr(input_arr,min_new=0,max_new=1) | |
Rescale input numpy array to given (min_new,max_new). | |
Deafult for scale_nparr(x) is to scale to (0,1) interval included. | |
""" | |
out = (input_arr-min(input_arr))/(max(input_arr)-min(input_arr)) # normalize | |
if (min_new != 0) and ( max_new !=1): | |
out = out*(max_new-min_new)+min_new | |
return out | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment