def testMultipleCraftersUseSameCreases(self): counter, b_creases = count_creases(fold=int, unfold=str) @pattern(crafter=self.other_id) @pattern(crafter=self.id) class Foo(object): folds = { self.id: 'a=uint:8, b=uint:4', self.other_id: 'b=uint:4, c=uint:7' } creases = {'b': b_creases} __init__ = init(*list('abc')) __eq__ = equals(*list('abc')) original_foo = Foo(129, '12', 100) data = fold(original_foo, crafter=self.id) other_data = fold(original_foo, crafter=self.other_id) foo = unfold(data, Foo, crafter=self.id) other_foo = unfold(other_data, Foo, crafter=self.other_id) assert foo.a == original_foo.a assert foo.b == original_foo.b assert other_foo.b == original_foo.b assert other_foo.c == original_foo.c assert counter['fold'] == counter['unfold'] == 2
def test_origami(): """Uses the test values to test different methods in origami.""" # create a container for solutions solutions = {} # load test values f_true, f_def, N, sigma2, R, edges = test_values() solutions['True'] = Spectrum(edges, f_true, 0) solutions['Default'] = Spectrum(edges, f_def, 0) # test MAXED f_MAXED = ori.unfold(N, sigma2, R, f_def, method='MAXED', params={'Omega': 3}) solutions['MAXED'] = Spectrum(edges, f_MAXED, 0) # test Gravel f_Gravel = ori.unfold(N, sigma2, R, f_def, method='Gravel', params={'max_iter': 1000, 'tol': 1E-4}) solutions['Gravel'] = Spectrum(edges, f_Gravel, 0) # compare results visually fig = plt.figure(0) ax = fig.add_subplot(111) for name, spec in solutions.items(): ax.plot(*spec.plot('plot', 'int'), label=name) # produce legend ax.legend() return
def testPatternInvalidInit(self): @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8' def __init__(self, a): self.a = a foo = Foo(10) data = fold(foo, crafter=self.id) with pytest.raises(OrigamiException): unfold(data, Foo, crafter=self.id)
def testMultipleCraftersSameFolds(self): @pattern(crafter=self.id) @pattern(crafter=self.other_id) class Foo(object): folds = 'a=uint:8, b=uint:1' __init__ = init('a', 'b') __eq__ = equals('a', 'b') original_foo = Foo(129, 1) data = fold(original_foo, crafter=self.id) other_data = fold(original_foo, crafter=self.other_id) foo = unfold(data, Foo, crafter=self.id) other_foo = unfold(other_data, Foo, crafter=self.other_id) assert original_foo == foo assert foo == other_foo
def unfold(self): """Docstring.""" # unfold grv_solution, evolution = unfold(self.N, self.N * 0.05, self.R, self.ds, method='Gravel', params=self.params) max_solution = unfold(self.N, self.N * 0.05, self.R, self.ds, method='MAXED', params=self.params) return grv_solution, evolution, max_solution
def testUnfoldByString(self): @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8, b=uint:1' __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo(129, 1) data = fold(foo, crafter=self.id) other_foo = unfold(data, "Foo", crafter=self.id) assert foo == other_foo
def testMultipleCraftersDifferentFolds(self): @pattern(crafter=self.id) @pattern(crafter=self.other_id) class Foo(object): folds = { self.id: 'a=uint:8, b=uint:1', self.other_id: 'b=uint:1, c=uint:7' } __init__ = init(*list('abc')) __eq__ = equals(*list('abc')) original_foo = Foo(129, 1, 100) data = fold(original_foo, crafter=self.id) other_data = fold(original_foo, crafter=self.other_id) foo = unfold(data, Foo, crafter=self.id) other_foo = unfold(other_data, Foo, crafter=self.other_id) assert foo.a == original_foo.a assert foo.b == original_foo.b assert other_foo.b == original_foo.b assert other_foo.c == original_foo.c
def testUnfoldIntoInstance(self): @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8, b=uint:1' __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo(129, 1) other_foo = Foo() data = fold(foo, crafter=self.id) third_foo = unfold(data, other_foo, crafter=self.id) assert foo == third_foo assert other_foo is third_foo
def testPatternWithSlots(self): @pattern(crafter=self.id) class Foo(object): __slots__ = ['a'] folds = 'a=uint:8' def __init__(self, a=10): self.a = a __eq__ = equals('a') foo = Foo(120) data = fold(foo, crafter=self.id) other_foo = unfold(data, Foo, crafter=self.id) assert foo == other_foo
def testFormatCrease(self): counter, uint8_creases = count_creases(fold=int, unfold=str) @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8, b=uint:1' creases = {'uint:8': uint8_creases} __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo('129', 1) data = fold(foo, crafter=self.id) other_foo = unfold(data, Foo, crafter=self.id) assert foo == other_foo assert counter['fold'] == counter['unfold'] == 1
def testFormatCreaseWithCustomFmt(self): counter, my_int_creases = count_creases(fold=int, unfold=str) my_int_creases['fmt'] = 'uint:8' @pattern(crafter=self.id) class Foo(object): folds = 'a=my_int, b=uint:1' creases = {'my_int': my_int_creases} __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo('129', 1) data = fold(foo, crafter=self.id) other_foo = unfold(data, Foo, crafter=self.id) assert foo == other_foo assert counter['fold'] == counter['unfold'] == 1
def testNameCreaseHasPriority(self): name_counter, name_creases = count_creases(fold=int, unfold=str) fmt_counter, fmt_creases = count_creases(fold=int, unfold=str) @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8, b=uint:1' creases = {'a': name_creases, 'uint:8': fmt_creases} __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo('129', 1) data = fold(foo, crafter=self.id) other_foo = unfold(data, Foo, crafter=self.id) assert foo == other_foo assert name_counter['fold'] == name_counter['unfold'] == 1 assert fmt_counter['fold'] == fmt_counter['unfold'] == 0
def testFoldLearnedPattern(self): @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8' __init__ = init('a') __eq__ = equals('a') @pattern(crafter=self.id) class Bar(object): folds = 'a=Foo' __init__ = init('a') __eq__ = equals('a') foo = Foo(129) bar = Bar(foo) data = fold(bar, crafter=self.id) other_bar = unfold(data, Bar, crafter=self.id) assert bar == other_bar
def unfold_(value): counter['unfold'] += 1 return unfold(value)