def _encode(self, s): rails = OrderedDict() z = utils.zigzag(range(self.key)) for char, rail in zip(s, z): rails.setdefault(rail, []).append(char) out = itertools.chain.from_iterable(rails.values()) return ''.join(out)
def _fence(self, s): rails = OrderedDict() z = utils.zigzag(range(self.key)) for key in range(self.key): rails.setdefault(key, []) keys = list(range(self.key)) for char, rail in zip(s, z): for k in keys: if k != rail: rails[k].append('.') else: rails[k].append(char) out = (' '.join(val) for val in rails.values()) return '\n'.join(out)
import sys import cv2 import numpy as np from utils import pointsInsideCircle, compare, zigzag from math import pi as PI W = 8 #block size for comparision Dsim = 0.1 #threshold for symmetry Nd = 25 #nearest block quadrants_points = pointsInsideCircle(W/4) #(i,j) position of blocks which are partially/completely inside circle of radius W/2 zigzag_points = zigzag(W/2) test_image = cv2.imread(sys.argv[1],0) height,width = test_image.shape[:2] #print (height,width) vectors_list = [] for j in range(0,height-W+1): for i in range(0,width-W+1): block = test_image[j:j+W,i:i+W] dct_block = cv2.dct(np.float32(block)) feature_block = [[],[],[],[]] for index,coeff_list in enumerate(zigzag_points): for coeff in coeff_list: feature_block[index].append(dct_block[coeff[0],coeff[1]]) feature_block_np = np.array(feature_block) feature_vector = [] for quadrant,points in quadrants_points.iteritems(): summ = 0
"""Temperature. Nothing to do but to read the temperature measured by the micro:bit. """ from microbit import * from utils import NUMBERS, image_from_number, zigzag while True: temp = str(temperature()) img = image_from_number(temp) def get_temp_to_display(): return image_from_number(str(temperature())) zigzag(get_temp_to_display, 50, 300)
Nothing to do but to read the time elapsed since the demo has been started, in the HH:MM:SS format. """ from microbit import * from utils import NUMBERS, image_from_number, zigzag def get_time_elapsed(start): """Return the time elapsed since start, formatted as HH:MM:SS.""" seconds = (running_time() - start) // 1000 minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) return "{hours:02}:{minutes:02}:{seconds:02}".format(seconds=seconds, minutes=minutes, hours=hours) start = running_time() while True: def get_time_to_display(): seconds = (running_time() - start) // 1000 minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) formatted = "{hours:02}:{minutes:02}:{seconds:02}".format( seconds=seconds, minutes=minutes, hours=hours) return image_from_number(formatted) zigzag(get_time_to_display, 30, 300)
def _decode(self, s): # rails = OrderedDict() # keys = range(self.key) # z = utils.zigzag(keys) # rail_counts = Counter(itertools.islice(z, len(s))) # print(rail_counts) # could easily start at zero, again [TODO] to test use of counter # and dict as being robust and superior over array/list rails_in_order = list(range(1, self.key+1)) z = utils.zigzag(rails_in_order) #### using str here to ensure that OrderedCounter below actually works # as opposed to typical Counter, which will preserve order of int keys ## [TODO] remove str() before final version rail_slices = list(str(n) for n in itertools.islice(z, len(s))) # rail_slices = list(itertools.islice(z, len(s))) # z = utils.zigzag(rails_in_order) # zippy = (list(zip(z, s))) #### c is not guaranteed to store keys in order period = 2 * (self.key - 1) one_period_of_zigzags = rail_slices[:period] #### combine with OrderedDict # c = Counter(rail_slices) # how many characters are in each row? d = OrderedCounter(rail_slices) # print(d) # rail_lengths = { rail: rail_len for rail, rail_len in d.items() } # rail_counts = [ (rail,c[rail]) for rail in rails_in_order] # rail_counts2 = [(rail, crail) for rail,crail in d.items()] # print('---') # print(rail_counts) # print(rail_counts2) # print('===') t = iter(s) # rails = [iter(take(c[rail], t)) for rail in rails_in_order] fence = {k: iter(take(v, t)) for k, v in d.items()} # rail: count # rail_iters = (rails[rail] for rail in rail_slices) rail_iters2 = (fence[rail] for rail in one_period_of_zigzags) # out = map(next, rail_iters) # out = [next(rail_iter) for rail_iter in rail_iters] out = utils.roundrobin(*rail_iters2) # x = [take(c[rail], t) for rail in rails_in_order] # y = itertools.tee(t, self.key) # [take(c[rail], m) for m in r # ends = itertools.accumulate(c[rail] for rail in rails_in_order) # rails = { rail: iter(list(itertools.islice(t, c[rail]))) # for rail in rails_in_order } # rails = { rail: iter(x[rail]) for rail in rails_in_order } # rails = [iter(x[rail]) for rail in rails_in_order] # rails = { rail: iter( list( next(t) for __ in range(count) )) # for rail, count in c.items() } # out = map(next, (rails[rail] for rail in rail_slices)) ### use roundrobin? # out = map(next, map(rails.__getitem__, rail_slices)) # out = [next(rail_contents) for rail_contents in # (rails[rail] for rail in rail_slices)] return ''.join(out)
def test_zizag_1(): a = np.array([[1, 4], [2, 5]]) res = np.array([1, 4, 2, 5]) assert np.all(zigzag(a) == res) == True
def test_zigzag_3(): a = np.arange(16).reshape((4, 4)) print(a) res = np.array([0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15]) assert np.all(zigzag(a) == res) == True
def test_zigzag_2(): a = np.arange(9).reshape((3, 3)) print(a) res = np.array([0, 1, 3, 6, 4, 2, 5, 7, 8]) assert np.all(zigzag(a) == res) == True