Skip to content

Instantly share code, notes, and snippets.

@odebeir
Created May 7, 2012 13:57
Show Gist options
  • Save odebeir/2627923 to your computer and use it in GitHub Desktop.
Save odebeir/2627923 to your computer and use it in GitHub Desktop.
BSP - Monday 7 May - some tests
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
def create_data(n):
data = np.reshape(range(n**2),(n,n))
data = data[:,:,np.newaxis]
print '-'*40
print data[:,:,0],data.shape
print '-'*40
return data
def fold(data,seq):
"""fold a 3D table using the sequence seq as:
0 : up/down
1 : down/up
2 : left/right
3 : right/left
"""
if len(seq) == 0:
r = data
else:
if seq[0]==0:
n2 = data.shape[0]/2
dfold = np.dstack((data[n2-1::-1,:],data[n2:,:]))
r = fold(dfold,seq[1:])
if seq[0]==1:
n2 = data.shape[0]/2
dfold = np.dstack((data[:n2-1:-1,:],data[:n2,:]))
r = fold(dfold,seq[1:])
if seq[0]==2:
n2 = data.shape[1]/2
dfold = np.dstack((data[:,n2-1::-1],data[:,n2:]))
r = fold(dfold,seq[1:])
if seq[0]==3:
n2 = data.shape[1]/2
dfold = np.dstack((data[:,:n2-1:-1],data[:,:n2]))
r = fold(dfold,seq[1:])
return r
print 'basic tests'
data = create_data(2)
for s in range(4):
r = fold(data,[s])
print 'apply ',s,'-->',r.shape
print r[:,:,0]
print r[:,:,1]
from itertools import permutations
seq = [0,1,2,3]
test = [12, 15, 0, 3, 13, 14, 1, 2, 8, 11, 4, 7, 9, 10, 5, 6]
#test = [2, 1, 13, 14, 15, 12, 0, 3, 7, 4, 8, 11, 10, 9, 5, 6]
data = create_data(4)
print 'test=',test
for c in permutations(seq,4):
r = fold(data,c)
print c,'-->', r.flatten(), (r.flatten() == np.asarray(test)).all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment