def templateIncorrectPatternForSimulation(pattern, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore=ccore_flag) try: net.simulate(10, 10, pattern) except: return assert False
def templateIncorrectPatternForTraining(patterns, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag) try: net.train(patterns) except: return assert False
def templatePatternVisualizer(self, collect_dynamic, ccore_flag=False): net = syncpr(5, 0.1, 0.1, ccore=ccore_flag) output_dynamic = net.simulate(10, 10, [-1, 1, -1, 1, -1], solve_type.RK4, collect_dynamic) syncpr_visualizer.show_pattern(output_dynamic, 5, 2) syncpr_visualizer.animate_pattern_recognition(output_dynamic, 1, 5)
def templateTrainNetworkAndRecognizePattern(self, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag) patterns = [] patterns += [[1, 1, 1, 1, 1, -1, -1, -1, -1, -1]] patterns += [[-1, -1, -1, -1, -1, 1, 1, 1, 1, 1]] net.train(patterns) # recognize it for i in range(len(patterns)): output_dynamic = net.simulate(10, 10, patterns[i], solve_type.RK4, True) ensembles = output_dynamic.allocate_sync_ensembles(0.5) assert len(ensembles) == 2 assert len(ensembles[0]) == len(ensembles[1]) # sort results ensembles[0].sort() ensembles[1].sort() assert (ensembles[0] == [0, 1, 2, 3, 4]) or (ensembles[0] == [5, 6, 7, 8, 9]) assert (ensembles[1] == [0, 1, 2, 3, 4]) or (ensembles[1] == [5, 6, 7, 8, 9])
def testOutputDinamicLengthSimulation(self): net = syncpr(5, 0.1, 0.1, ccore=False) output_dynamic = net.simulate(10, 10, [-1, 1, -1, 1, -1], solution=solve_type.FAST, collect_dynamic=True) assert len(output_dynamic) == 11
def templateOutputDynamicLengthDynamicSimulation(collect_flag, ccore_flag): net = syncpr(5, 0.1, 0.1, ccore_flag); output_dynamic = net.simulate_dynamic([-1, 1, -1, 1, -1], solution = solve_type.FAST, collect_dynamic = collect_flag); if (collect_flag is True): assert len(output_dynamic) > 1; else: assert len(output_dynamic) == 1;
def templateOutputDynamicLengthStaticSimulation(collect_flag, ccore_flag): net = syncpr(5, 0.1, 0.1, ccore_flag); output_dynamic = net.simulate_static(10, 10, [-1, 1, -1, 1, -1], solution = solve_type.FAST, collect_dynamic = collect_flag); if (collect_flag is True): assert len(output_dynamic) == 11; # 10 steps without initial values. else: assert len(output_dynamic) == 1;
def templateStaticSimulation(self, ccore_falg): net = syncpr(10, 0.1, 0.1, ccore_falg) patterns = [] patterns += [[1, 1, 1, 1, 1, -1, -1, -1, -1, -1]] patterns += [[-1, -1, -1, -1, -1, 1, 1, 1, 1, 1]] net.train(patterns) net.simulate_static(20, 10, patterns[0], solve_type.RK4) memory_order = net.memory_order(patterns[0]) assert (memory_order > 0.95) and (memory_order <= 1.000005)
def templateDynamicSimulation(self, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag) patterns = [] patterns += [[1, 1, 1, 1, 1, -1, -1, -1, -1, -1]] patterns += [[-1, -1, -1, -1, -1, 1, 1, 1, 1, 1]] net.train(patterns) net.simulate_dynamic(patterns[0], order=0.998, solution=solve_type.RK4) memory_order = net.memory_order(patterns[0]) assert (memory_order > 0.998) and (memory_order <= 1.0)
def templateStaticSimulation(ccore_falg): net = syncpr(10, 0.1, 0.1, ccore_falg); patterns = []; patterns += [ [1, 1, 1, 1, 1, -1, -1, -1, -1, -1] ]; patterns += [ [-1, -1, -1, -1, -1, 1, 1, 1, 1, 1] ]; net.train(patterns); net.simulate_static(20, 10, patterns[0], solve_type.RK4); memory_order = net.memory_order(patterns[0]); assert (memory_order > 0.95) and (memory_order <= 1.000005);
def templateDynamicSimulation(ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag); patterns = []; patterns += [ [1, 1, 1, 1, 1, -1, -1, -1, -1, -1] ]; patterns += [ [-1, -1, -1, -1, -1, 1, 1, 1, 1, 1] ]; net.train(patterns); net.simulate_dynamic(patterns[0], order = 0.998, solution = solve_type.RK4); memory_order = net.memory_order(patterns[0]); assert (memory_order > 0.998) and (memory_order <= 1.0);
def templateGlobalSyncOrder(self, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag); patterns = [ [1, 1, 1, 1, 1, -1, -1, -1, -1, -1] ]; patterns += [ [-1, -1, -1, -1, -1, 1, 1, 1, 1, 1] ]; global_sync_order = net.sync_order(); assert (global_sync_order < 1.0) and (global_sync_order > 0.0); net.train(patterns); global_sync_order = net.sync_order(); assert (global_sync_order < 1.0) and (global_sync_order > 0.0);
def templateLocalSyncOrder(ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag); patterns = [ [1, 1, 1, 1, 1, -1, -1, -1, -1, -1] ]; patterns += [ [-1, -1, -1, -1, -1, 1, 1, 1, 1, 1] ]; local_sync_order = net.sync_local_order(); assert (local_sync_order < 1.0) and (local_sync_order > 0.0); net.train(patterns); local_sync_order = net.sync_local_order(); assert (local_sync_order < 1.0) and (local_sync_order > 0.0);
def templateLocalSyncOrder(self, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag) patterns = [[1, 1, 1, 1, 1, -1, -1, -1, -1, -1]] patterns += [[-1, -1, -1, -1, -1, 1, 1, 1, 1, 1]] local_sync_order = net.sync_local_order() assert (local_sync_order < 1.0) and (local_sync_order > 0.0) net.train(patterns) local_sync_order = net.sync_local_order() assert (local_sync_order < 1.0) and (local_sync_order > 0.0)
def templateGlobalSyncOrder(ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag) patterns = [[1, 1, 1, 1, 1, -1, -1, -1, -1, -1]] patterns += [[-1, -1, -1, -1, -1, 1, 1, 1, 1, 1]] global_sync_order = net.sync_order() assert (global_sync_order < 1.0) and (global_sync_order > 0.0) net.train(patterns) global_sync_order = net.sync_order() assert (global_sync_order < 1.0) and (global_sync_order > 0.0)
def templateMemoryOrder(ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag); patterns = []; patterns += [ [1, 1, 1, 1, 1, -1, -1, -1, -1, -1] ]; patterns += [ [-1, -1, -1, -1, -1, 1, 1, 1, 1, 1] ]; net.train(patterns); assert net.memory_order(patterns[0]) < 0.8; assert net.memory_order(patterns[1]) < 0.8; for pattern in patterns: net.simulate(20, 10, pattern, solve_type.RK4); memory_order = net.memory_order(pattern); assert (memory_order > 0.95) and (memory_order <= 1.000005);
def templateMemoryOrder(self, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag) patterns = [] patterns += [[1, 1, 1, 1, 1, -1, -1, -1, -1, -1]] patterns += [[-1, -1, -1, -1, -1, 1, 1, 1, 1, 1]] net.train(patterns) assert net.memory_order(patterns[0]) < 0.8 assert net.memory_order(patterns[1]) < 0.8 for pattern in patterns: net.simulate(20, 10, pattern, solve_type.RK4) memory_order = net.memory_order(pattern) assert (memory_order > 0.95) and (memory_order <= 1.000005)
def template_recognition_image(images, steps, time, corruption=0.1): samples = [] for file_name in images: data = read_image(file_name) image_pattern = rgb2gray(data) for index_pixel in range(len(image_pattern)): if (image_pattern[index_pixel] < 128): image_pattern[index_pixel] = 1 else: image_pattern[index_pixel] = -1 samples += [image_pattern] net = syncpr(len(samples[0]), 0.3, 0.3, ccore=True) net.train(samples) # Recognize the each learned pattern for i in range(len(samples)): sync_output_dynamic = net.simulate(steps, time, samples[i], solve_type.RK4, True) syncpr_visualizer.show_output_dynamic(sync_output_dynamic) syncpr_visualizer.show_pattern(sync_output_dynamic, 10, 10) # corrupt a little bit by black and white pixels for _ in range(math.floor(len(samples[i]) * corruption)): random.seed() random_pixel = math.floor(random.random() * len(samples[i])) samples[i][random_pixel] = 1 random_pixel = math.floor(random.random() * len(samples[i])) samples[i][random_pixel] = -1 sync_output_dynamic = net.simulate(steps, time, samples[i], solve_type.RK4, True) syncpr_visualizer.show_output_dynamic(sync_output_dynamic) syncpr_visualizer.show_pattern(sync_output_dynamic, 10, 10) syncpr_visualizer.animate_pattern_recognition( sync_output_dynamic, 10, 10, title="Pattern Recognition")
def template_recognition_image(images, steps, time, corruption = 0.1): samples = []; for file_name in images: data = read_image(file_name); image_pattern = rgb2gray(data); for index_pixel in range(len(image_pattern)): if (image_pattern[index_pixel] < 128): image_pattern[index_pixel] = 1; else: image_pattern[index_pixel] = -1; samples += [ image_pattern ]; net = syncpr(len(samples[0]), 0.3, 0.3, ccore = True); net.train(samples); # Recognize the each learned pattern for i in range(len(samples)): sync_output_dynamic = net.simulate(steps, time, samples[i], solve_type.RK4, True); syncpr_visualizer.show_output_dynamic(sync_output_dynamic); syncpr_visualizer.show_pattern(sync_output_dynamic, 10, 10); # corrupt a little bit by black and white pixels for _ in range( math.floor(len(samples[i]) * corruption) ): random.seed(); random_pixel = math.floor(random.random() * len(samples[i])); samples[i][random_pixel] = 1; random_pixel = math.floor(random.random() * len(samples[i])); samples[i][random_pixel] = -1; sync_output_dynamic = net.simulate(steps, time, samples[i], solve_type.RK4, True); syncpr_visualizer.show_output_dynamic(sync_output_dynamic); syncpr_visualizer.show_pattern(sync_output_dynamic, 10, 10); syncpr_visualizer.animate_pattern_recognition(sync_output_dynamic, 10, 10, title = "Pattern Recognition");
def templateTrainNetworkAndRecognizePattern(ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag); patterns = []; patterns += [ [1, 1, 1, 1, 1, -1, -1, -1, -1, -1] ]; patterns += [ [-1, -1, -1, -1, -1, 1, 1, 1, 1, 1] ]; net.train(patterns); # recognize it for i in range(len(patterns)): output_dynamic = net.simulate(10, 10, patterns[i], solve_type.RK4, True); ensembles = output_dynamic.allocate_sync_ensembles(0.5); assert len(ensembles) == 2; assert len(ensembles[0]) == len(ensembles[1]); # sort results ensembles[0].sort(); ensembles[1].sort(); assert (ensembles[0] == [0, 1, 2, 3, 4]) or (ensembles[0] == [5, 6, 7, 8, 9]); assert (ensembles[1] == [0, 1, 2, 3, 4]) or (ensembles[1] == [5, 6, 7, 8, 9]);
def templateIncorrectPatternForTraining(self, patterns, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag); self.assertRaises(Exception, net.train, patterns);
def templateIncorrectPatternForSimulation(self, pattern): net = syncpr(10, 0.1, 0.1); self.assertRaises(Exception, net.simulate, 10, 10, pattern);
def testCreateTenOscillatorsNetworkByCore(self): net = syncpr(10, 0.1, 0.1, True); assert len(net) == 10;
def templatePatternVisualizer(collect_dynamic, ccore_flag = False): net = syncpr(5, 0.1, 0.1, ccore = ccore_flag); output_dynamic = net.simulate(10, 10, [-1, 1, -1, 1, -1], solve_type.RK4, collect_dynamic); syncpr_visualizer.show_pattern(output_dynamic, 5, 2); syncpr_visualizer.animate_pattern_recognition(output_dynamic, 1, 5);
def templateIncorrectPatternForTraining(patterns, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag); try: net.train(patterns); except: return; assert False;
def templateOutputDynamic(self, solver, ccore=False): net = syncpr(5, 0.1, 0.1, ccore) output_dynamic = net.simulate(10, 10, [-1, 1, -1, 1, -1], solver, True) assert len(output_dynamic) == 11
def testCreateTenOscillatorsNetwork(self): net = syncpr(10, 0.1, 0.1) assert len(net) == 10
def testOutputDinamicLengthSimulation(self): net = syncpr(5, 0.1, 0.1); output_dynamic = net.simulate(10, 10, [-1, 1, -1, 1, -1], solution = solve_type.FAST, collect_dynamic = True); assert len(output_dynamic) == 11; # 10 steps without initial values.
def templatePatternVisualizer(self, collect_dynamic): net = syncpr(5, 0.1, 0.1); output_dynamic = net.simulate(10, 10, [-1, 1, -1, 1, -1], solve_type.RK4, collect_dynamic); # test that we don't have any exception during vizualization. syncpr_visualizer.show_pattern(output_dynamic, 5, 2);
def testCreateTenOscillatorsNetwork(self): net = syncpr(10, 0.1, 0.1); assert len(net) == 10;
def testCreateTenOscillatorsNetwork(self): net = syncpr(10, 0.1, 0.1, ccore=False); assert len(net) == 10;
def templateOutputDynamic(solver, ccore): net = syncpr(5, 0.1, 0.1, ccore); output_dynamic = net.simulate(10, 10, [-1, 1, -1, 1, -1], solver, True); assert len(output_dynamic) == 11; # 10 steps without initial values.
def testCreateHundredOscillatorsNetworkByCore(self): net = syncpr(100, 0.1, 0.1, True); assert len(net) == 100;
def testCreateHundredOscillatorsNetwork(self): net = syncpr(100, 0.1, 0.1, ccore=False) assert len(net) == 100
def templateIncorrectPatternForSimulation(pattern, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore=ccore_flag); try: net.simulate(10, 10, pattern); except: return; assert False;
def templateIncorrectPatternForSimulation(self, pattern): net = syncpr(10, 0.1, 0.1) self.assertRaises(Exception, net.simulate, 10, 10, pattern)
def testCreateHundredOscillatorsNetworkByCore(self): net = syncpr(100, 0.1, 0.1, True) assert len(net) == 100
def templateIncorrectPatternForTraining(self, patterns, ccore_flag): net = syncpr(10, 0.1, 0.1, ccore_flag) self.assertRaises(Exception, net.train, patterns)
def testCreateHundredOscillatorsNetwork(self): net = syncpr(100, 0.1, 0.1, ccore=False); assert len(net) == 100;