def test_cout_logging(self): logger.log_to_cout(logger.LogLevel.WARN) logger1 = logger.get("test"); logger2 = logger.get("xyz"); logger3 = logger.get("xyz.test"); logger.set_loglevel(logger2, logger.LogLevel.DEBUG) logger.set_loglevel(logger3, logger.LogLevel.INFO) logger.LOG4CXX_FATAL(logger1, "FATAL") logger.LOG4CXX_ERROR(logger1, "ERROR") logger.LOG4CXX_WARN (logger1, "WARN ") logger.LOG4CXX_INFO (logger1, "INFO ") logger.LOG4CXX_DEBUG(logger1, "DEBUG") logger.LOG4CXX_TRACE(logger1, "TRACE") logger2.FATAL("FATAL") logger2.ERROR("ERROR") logger2.WARN ("WARN ") logger2.INFO ("INFO ") logger2.DEBUG("DEBUG") logger2.TRACE("TRACE") logger.LOG4CXX_FATAL(logger3, "FATAL") logger.LOG4CXX_ERROR(logger3, "ERROR") logger.LOG4CXX_WARN (logger3, "WARN ") logger.LOG4CXX_INFO (logger3, "INFO ") logger.LOG4CXX_DEBUG(logger3, "DEBUG") logger.LOG4CXX_TRACE(logger3, "TRACE")
def test_reset(self): logger.log_to_cout(logger.LogLevel.WARN) logger1 = logger.get("test"); logger2 = logger.get("xyz"); logger.reset() self.assertEqual(0, logger1._get_number_of_appenders()) self.assertEqual(0, logger2._get_number_of_appenders()) self.assertEqual(0, logger.get_root()._get_number_of_appenders())
def test_file_logging(self): log = os.path.join(self.temp, 'test_file_logging.log') logger.log_to_file(log, logger.LogLevel.WARN) logger1 = logger.get("test"); logger2 = logger.get("xyz"); logger3 = logger.get("xyz.test"); logger.set_loglevel(logger1, logger.LogLevel.WARN) logger.set_loglevel(logger2, logger.LogLevel.DEBUG) logger.set_loglevel(logger3, logger.LogLevel.INFO) logger.LOG4CXX_FATAL(logger1, "FATAL") logger.LOG4CXX_ERROR(logger1, "ERROR") logger.LOG4CXX_WARN (logger1, "WARN") logger.LOG4CXX_INFO (logger1, "INFO") logger.LOG4CXX_DEBUG(logger1, "DEBUG") logger.LOG4CXX_TRACE(logger1, "TRACE") logger2.FATAL("FATAL") logger2.ERROR("ERROR") logger2.WARN ("WARN") logger2.INFO ("INFO") logger2.DEBUG("DEBUG") logger2.TRACE("TRACE") logger.LOG4CXX_FATAL(logger3, "FATAL") logger.LOG4CXX_ERROR(logger3, "ERROR") logger.LOG4CXX_WARN (logger3, "WARN") logger.LOG4CXX_INFO (logger3, "INFO") logger.LOG4CXX_DEBUG(logger3, "DEBUG") logger.LOG4CXX_TRACE(logger3, "TRACE") logger.reset() with open(log) as f: expected = \ """FATAL test FATAL ERROR test ERROR WARN test WARN FATAL xyz FATAL ERROR xyz ERROR WARN xyz WARN INFO xyz INFO DEBUG xyz DEBUG FATAL xyz.test FATAL ERROR xyz.test ERROR WARN xyz.test WARN INFO xyz.test INFO """ self.assertEqualLogLines(expected, f.read())
def test_append_to_logging_multiple_calls(self): import logging records = [] class Handler(logging.Handler): def emit(self, record): records.append({ "name": record.name, "msg": record.msg, "levelno": record.levelno }) logging.getLogger("").addHandler(Handler()) # Log messages to the "root" logger logger.append_to_logging("root") logger.append_to_logging("root") logger.append_to_logging("root") logger.append_to_logging("root2") logger.append_to_logging("root2") logger.append_to_logging("root2") # The message should only appear one time for each logger instead of # multiple times logger1 = logger.get("test1"); logger1.FATAL("msg1") self.assertEqual([ {'msg': 'msg1', 'levelno': 50, 'name': 'root.test1'}, {'msg': 'msg1', 'levelno': 50, 'name': 'root2.test1'}, ], records)
def default_test_msg(print_location, date_format): pylogging.reset() pylogging.default_config( level=pylogging.LogLevel.DEBUG, print_location=print_location, date_format=date_format, color=True) l = pylogging.get("default_config") l.debug("time format: {}".format(date_format))
def test_msg(print_location, date_format): pylogging.reset() layout = pylogging.ColorLayout(True, date_format) layout.setOption("printlocation", str(print_location).lower()) layout.activateOptions() appender = pylogging.ConsoleAppender(layout) appender.setOption("target", pylogging.ConsoleAppender.getSystemErr()) appender.activateOptions() l = pylogging.get_root() pylogging.set_loglevel(l, pylogging.LogLevel.DEBUG) l.addAppender(appender) l = pylogging.get("manual_config") l.warn("time format: {}".format(date_format))
def setUp(self): init_logger("INFO", [ ("marocco", "DEBUG"), ]) self.log = pylogging.get(__name__) self.temporary_directory = tempfile.mkdtemp(prefix="marocco-test-") self.marocco = pymarocco.PyMarocco() self.marocco.backend = pymarocco.PyMarocco.Without self.marocco.calib_backend = pymarocco.PyMarocco.CalibBackend.Default self.marocco.defects.backend = pymarocco.Defects.Backend.Without self.marocco.persist = os.path.join(self.temporary_directory, "results.bin")
def test_default_logger(self): log_all = os.path.join(self.temp, 'test_default_logger_all.log') log_default = os.path.join(self.temp, 'test_default_logger_default.log') logger1 = logger.get("test") logger.default_config(logger.LogLevel.DEBUG, log_all, date_format="NULL") # Loglevel should be ignored, because the root logger is configured logger_default = logger.get_old_logger(logger.LogLevel.TRACE) logger.append_to_file(log_default, logger_default) for l in (logger_default, logger1): logger.LOG4CXX_FATAL(l, "FATAL") logger.LOG4CXX_ERROR(l, "ERROR") logger.LOG4CXX_WARN(l, "WARN") logger.LOG4CXX_INFO(l, "INFO") logger.LOG4CXX_DEBUG(l, "DEBUG") logger.LOG4CXX_TRACE(l, "TRACE") logger.reset() with open(log_all) as f: expected = \ """FATAL PyLogging FATAL ERROR PyLogging ERROR WARN PyLogging WARN INFO PyLogging INFO DEBUG PyLogging DEBUG FATAL test FATAL ERROR test ERROR WARN test WARN INFO test INFO DEBUG test DEBUG """ self.assertEqualLogLines(expected, f.read()) with open(log_default) as f: expected = \ """FATAL PyLogging FATAL ERROR PyLogging ERROR WARN PyLogging WARN INFO PyLogging INFO DEBUG PyLogging DEBUG """ self.assertEqualLogLines(expected, f.read())
def setUp(self): init_logger("INFO", [ ("marocco", "DEBUG"), ]) self.log = pylogging.get(__name__) self.temporary_directory = tempfile.mkdtemp(prefix="marocco-test-") self.marocco = pymarocco.PyMarocco() self.marocco.backend = pymarocco.PyMarocco.None self.marocco.calib_backend = pymarocco.PyMarocco.CalibBackend.Default self.marocco.defects.backend = pymarocco.Defects.Backend.None self.marocco.neuron_placement.skip_hicanns_without_neuron_blacklisting( False) self.marocco.persist = os.path.join(self.temporary_directory, "results.bin")
def test_default_logger(self): log_all = os.path.join(self.temp, 'test_default_logger_all.log') log_default = os.path.join(self.temp, 'test_default_logger_default.log') logger1 = logger.get("test") logger.default_config(logger.LogLevel.DEBUG, log_all, date_format="NULL") # Loglevel should be ignored, because the root logger is configured logger_default = logger.get_old_logger(logger.LogLevel.TRACE) logger.append_to_file(log_default, logger_default) for l in (logger_default, logger1): logger.LOG4CXX_FATAL(l, "FATAL") logger.LOG4CXX_ERROR(l, "ERROR") logger.LOG4CXX_WARN (l, "WARN") logger.LOG4CXX_INFO (l, "INFO") logger.LOG4CXX_DEBUG(l, "DEBUG") logger.LOG4CXX_TRACE(l, "TRACE") logger.reset() with open(log_all) as f: expected = \ """FATAL PyLogging FATAL ERROR PyLogging ERROR WARN PyLogging WARN INFO PyLogging INFO DEBUG PyLogging DEBUG FATAL test FATAL ERROR test ERROR WARN test WARN INFO test INFO DEBUG test DEBUG """ self.assertEqualLogLines(expected, f.read()) with open(log_default) as f: expected = \ """FATAL PyLogging FATAL ERROR PyLogging ERROR WARN PyLogging WARN INFO PyLogging INFO DEBUG PyLogging DEBUG """ self.assertEqualLogLines(expected, f.read())
def test_vertical(self): pylogging.set_loglevel(pylogging.get("marocco"), pylogging.LogLevel.TRACE) marocco = self.marocco user_strat = placer() user_strat.m_hicann_on_wafer_ordering = user_strat.vertical user_strat.m_spiral_center = user_strat.spiral_neighbours marocco.neuron_placement.default_placement_strategy(user_strat) pynn.setup(marocco=marocco) pops = {} pops[0] = pynn.Population(128, pynn.IF_cond_exp, {}) pops[1] = pynn.Population(128, pynn.IF_cond_exp, {}) pops[2] = pynn.Population(128, pynn.IF_cond_exp, {}) proj1 = pynn.Projection(pops[0], pops[1], pynn.OneToOneConnector(weights=0.01)) proj2 = pynn.Projection(pops[1], pops[2], pynn.OneToOneConnector(weights=0.01)) h = {} h[pops[0]] = C.HICANNOnWafer(Enum(100)) # the next free hicann (vertical order) h[pops[1]] = C.HICANNOnWafer(Enum(72)) h[pops[2]] = C.HICANNOnWafer(Enum(48)) marocco.manual_placement.on_hicann(pops[0], h[pops[0]]) pynn.run(0) pynn.end() result = self.load_results() for key in pops: pop = pops[key] for nrn in pop: placement_item, = result.placement.find(nrn) logical_neuron = placement_item.logical_neuron() for denmem in logical_neuron: self.assertEqual(h[pop].toEnum(), denmem.toHICANNOnWafer().toEnum())
def test_append_to_logging_multiple_calls(self): import logging records = [] class Handler(logging.Handler): def emit(self, record): records.append({ "name": record.name, "msg": record.msg, "levelno": record.levelno }) logging.getLogger("").addHandler(Handler()) # Log messages to the "root" logger logger.append_to_logging("root") logger.append_to_logging("root") logger.append_to_logging("root") logger.append_to_logging("root2") logger.append_to_logging("root2") logger.append_to_logging("root2") # The message should only appear one time for each logger instead of # multiple times logger1 = logger.get("test1") logger1.FATAL("msg1") self.assertEqual([ { 'msg': 'msg1', 'levelno': 50, 'name': 'root.test1' }, { 'msg': 'msg1', 'levelno': 50, 'name': 'root2.test1' }, ], records)
def setup_logger(): """ Has to be called separately in every process. """ import argparse from pysthal.command_line_util import init_logger import pylogging logger = pylogging.get(__name__) init_logger("DEBUG", [ (__name__, "TRACE"), ("halbe.fgwriter", "INFO"), ("hicann-system", "INFO"), ("Default", "INFO"), # unset logger name, mostly hicann-system ]) parser = argparse.ArgumentParser() parser.add_argument("--output-dir", default=".") args, _ = parser.parse_known_args() pylogging.append_to_file(os.path.join(args.output_dir, LOGFILE)) return logger
def test_config_from_file(self): import inspect log = os.path.join(self.temp, 'log') config = os.path.join(self.temp, 'config') with open(config, 'w') as f: f.write(""" # Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=WARN, A1 # More detail from xyz, but only a bit more from xyz.test log4j.logger.xyz=TRACE log4j.logger.xyz.test=INFO # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.FileAppender log4j.appender.A1.File={log} #log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.ColorLayout log4j.appender.A1.layout.Color=true log4j.appender.A1.layout.PrintLocation=true """.format(log=log)) logger.config_from_file(config) logger1 = logger.get("test"); logger2 = logger.get("xyz"); logger3 = logger.get("xyz.test"); filename = inspect.getframeinfo(inspect.currentframe())[0] loglines = [] def logln(msg): f = inspect.stack()[1][0] l = inspect.getframeinfo(f)[1] loc = " -> [33m" + filename + "[0m:[31m" + str(l) + "[0m" loglines.append(msg) loglines.append('\n') loglines.append(loc) loglines.append('\n') logger.LOG4CXX_FATAL(logger1, "FATAL"); logln("[31mFATAL [0mtest FATAL") logger.LOG4CXX_ERROR(logger1, "ERROR"); logln("[31mERROR [0mtest ERROR") logger.LOG4CXX_WARN (logger1, "WARN"); logln("[33mWARN [0mtest WARN") logger.LOG4CXX_INFO (logger1, "INFO") logger.LOG4CXX_DEBUG(logger1, "DEBUG") logger.LOG4CXX_TRACE(logger1, "TRACE") logger2.FATAL("FATAL") ;logln("[31mFATAL [0mxyz FATAL") logger2.ERROR("ERROR") ;logln("[31mERROR [0mxyz ERROR") logger2.WARN ("WARN") ;logln("[33mWARN [0mxyz WARN") logger2.INFO ("INFO") ;logln("[32mINFO [0mxyz INFO") logger2.DEBUG("DEBUG") ;logln("[32mDEBUG [0mxyz DEBUG") logger2.TRACE("TRACE") ;logln("[32mTRACE [0mxyz TRACE") logger.LOG4CXX_FATAL(logger3, "FATAL") ;logln("[31mFATAL [0mxyz.test FATAL") logger.LOG4CXX_ERROR(logger3, "ERROR") ;logln("[31mERROR [0mxyz.test ERROR") logger.LOG4CXX_WARN (logger3, "WARN") ;logln("[33mWARN [0mxyz.test WARN") logger.LOG4CXX_INFO (logger3, "INFO") ;logln("[32mINFO [0mxyz.test INFO") logger.LOG4CXX_DEBUG(logger3, "DEBUG") logger.LOG4CXX_TRACE(logger3, "TRACE") logger.reset() # Hope this flushes the logger ;) with open(log) as f: expected = "".join(loglines) self.assertEqual(expected, f.read())
import argparse parser = argparse.ArgumentParser(description='smoke test for spike input') parser.add_argument('wafer', type=int) parser.add_argument('hicann', type=int) parser.add_argument('--outdir', type=str) parser.add_argument('--calib-path', type=str, default='./') parser.add_argument('--neuron-number', type=int, default=3) args = parser.parse_args() ########### # LOGGING # ########### pylogging.set_loglevel(pylogging.get("Default"), pylogging.LogLevel.INFO) pylogging.set_loglevel(pylogging.get("marocco"), pylogging.LogLevel.DEBUG) pylogging.set_loglevel(pylogging.get("control"), pylogging.LogLevel.DEBUG) pylogging.set_loglevel(pylogging.get("sthal.HICANNConfigurator.Time"), pylogging.LogLevel.DEBUG) pylogging.set_loglevel(pylogging.get("sthal.HICANNConfigurator"), pylogging.LogLevel.DEBUG) pylogging.set_loglevel(pylogging.get("Calibtic"), pylogging.LogLevel.DEBUG) pylogging.set_loglevel(pylogging.get("halbe"), pylogging.LogLevel.INFO) pylogging.set_loglevel(pylogging.get("hicann-system"), pylogging.LogLevel.INFO) #################### # MAROCCO SETTINGS # #################### marocco = PyMarocco()
def test_append_to_logging(self): # Store all Python logging messages in the "records" list import logging records = [] class Handler(logging.Handler): def emit(self, record): records.append({ "name": record.name, "msg": record.msg, "levelno": record.levelno }) root_logger = logging.getLogger("") root_logger.addHandler(Handler()) root_logger.setLevel(logging.NOTSET) # Log messages to the "root" logger logger.append_to_logging("root") self.assertEqual(1, logger.get_root()._get_number_of_appenders()) # Create two test loggers logger1 = logger.get("test1") logger.set_loglevel(logger1, logger.LogLevel.TRACE) logger2 = logger.get("test2") logger3 = logger.get("test2.test3") logger1.FATAL("msg1") logger1.ERROR("msg2") logger1.WARN("msg3") logger1.INFO("msg4") logger1.DEBUG("msg5") logger1.TRACE("msg6") logger2.FATAL("msg1") logger2.ERROR("msg2") logger2.WARN("msg3") logger2.INFO("msg4") logger2.DEBUG("msg5") logger2.TRACE("msg6") logger3.FATAL("msg1") logger3.ERROR("msg2") logger3.WARN("msg3") logger3.INFO("msg4") logger3.DEBUG("msg5") logger3.TRACE("msg6") self.assertEqual([{ 'msg': 'msg1', 'levelno': 50, 'name': 'root.test1' }, { 'msg': 'msg2', 'levelno': 40, 'name': 'root.test1' }, { 'msg': 'msg3', 'levelno': 30, 'name': 'root.test1' }, { 'msg': 'msg4', 'levelno': 20, 'name': 'root.test1' }, { 'msg': 'msg5', 'levelno': 10, 'name': 'root.test1' }, { 'msg': 'msg6', 'levelno': 10, 'name': 'root.test1' }, { 'msg': 'msg1', 'levelno': 50, 'name': 'root.test2' }, { 'msg': 'msg2', 'levelno': 40, 'name': 'root.test2' }, { 'msg': 'msg3', 'levelno': 30, 'name': 'root.test2' }, { 'msg': 'msg4', 'levelno': 20, 'name': 'root.test2' }, { 'msg': 'msg5', 'levelno': 10, 'name': 'root.test2' }, { 'msg': 'msg1', 'levelno': 50, 'name': 'root.test2.test3' }, { 'msg': 'msg2', 'levelno': 40, 'name': 'root.test2.test3' }, { 'msg': 'msg3', 'levelno': 30, 'name': 'root.test2.test3' }, { 'msg': 'msg4', 'levelno': 20, 'name': 'root.test2.test3' }, { 'msg': 'msg5', 'levelno': 10, 'name': 'root.test2.test3' }], records)
from datetime import datetime import json import numpy as np import pyhmf as pynn import pyhalco_hicann_v2 as C from pymarocco import PyMarocco from pymarocco import Defects from pysthal.command_line_util import init_logger init_logger("ERROR", []) import params as par import pylogging logger = pylogging.get("column-benchmark") # At the moment only the deflaut placement strategy is tested. Can be added later to test different strategy from pymarocco_runtime import ClusterByPopulationConnectivity as placer_pop from pymarocco_runtime import ClusterByNeuronConnectivity as placer_neuron_cluster from pymarocco_runtime import byNeuronBlockEnumAndPopulationIDasc as placer_enum_IDasc class CorticalNetwork(object): def __init__(self, marocco, scale, k_scale, seed): # total connection counter self.totalConnections = 0 self.marocco = marocco # scale compared to original in amount of neurons network which has about 80,000 neurons
from pyhalco_hicann_v2 import RowOnSynapseDriver, FGBlockOnHICANN from pyhalco_common import Enum, iter_all from pysthal.command_line_util import init_logger import pysthal import pyhmf as pynn from pymarocco import PyMarocco, Defects from pymarocco.runtime import Runtime from pymarocco.coordinates import LogicalNeuron from pymarocco.results import Marocco init_logger("WARN", [("guidebook", "DEBUG"), ("marocco", "DEBUG"), ("Calibtic", "DEBUG"), ("sthal", "INFO")]) import pylogging logger = pylogging.get("guidebook") neuron_parameters = { 'cm': 0.2, 'v_reset': -70., 'v_rest': -20., 'v_thresh': -10, 'e_rev_I': -100., 'e_rev_E': 60., 'tau_m': 20., 'tau_refrac': 0.1, 'tau_syn_E': 5., 'tau_syn_I': 5., } marocco = PyMarocco()
def test_file_logging_with_filter(self): logger1 = logger.get("test") logger2 = logger.get("xyz") logger3 = logger.get("xyz.test") logger.set_loglevel(logger1, logger.LogLevel.WARN) logger.set_loglevel(logger2, logger.LogLevel.DEBUG) logger.set_loglevel(logger3, logger.LogLevel.INFO) # Test different filter log = os.path.join(self.temp, 'test_file_logging_with_filter.log') app = logger.append_to_file(log, logger.get_root()) f = logger.LevelRangeFilter() f.setLevelMin(logger.LogLevel.DEBUG) f.setLevelMax(logger.LogLevel.WARN) app.addFilter(f) log2 = os.path.join(self.temp, 'test_file_logging_with_filter2.log') app = logger.append_to_file(log2, logger.get_root()) f = logger.LevelRangeFilter() f.setLevelMin(logger.LogLevel.ERROR) f.setLevelMax(logger.LogLevel.FATAL) app.addFilter(f) log3 = os.path.join(self.temp, 'test_file_logging_with_filter3.log') app = logger.append_to_file(log3, logger2) f = logger.LevelRangeFilter() f.setLevelMin(logger.LogLevel.ERROR) f.setLevelMax(logger.LogLevel.FATAL) app.addFilter(f) for l in (logger1, logger2, logger3): logger.LOG4CXX_FATAL(l, "FATAL") logger.LOG4CXX_ERROR(l, "ERROR") logger.LOG4CXX_WARN(l, "WARN") logger.LOG4CXX_INFO(l, "INFO") logger.LOG4CXX_DEBUG(l, "DEBUG") logger.LOG4CXX_TRACE(l, "TRACE") logger.reset() with open(log) as f: expected = """WARN test WARN WARN xyz WARN INFO xyz INFO DEBUG xyz DEBUG WARN xyz.test WARN INFO xyz.test INFO """ self.assertEqualLogLines(expected, f.read()) with open(log2) as f: expected = """FATAL test FATAL ERROR test ERROR FATAL xyz FATAL ERROR xyz ERROR FATAL xyz.test FATAL ERROR xyz.test ERROR """ self.assertEqualLogLines(expected, f.read()) with open(log3) as f: expected = """FATAL xyz FATAL ERROR xyz ERROR FATAL xyz.test FATAL ERROR xyz.test ERROR """ self.assertEqualLogLines(expected, f.read())
#!/usr/bin/env python import pyhmf as pynn import Coordinate as C from pymarocco import PyMarocco, Defects from pymarocco.results import Marocco import pylogging for domain in ["Calibtic", "marocco"]: pylogging.set_loglevel(pylogging.get(domain), pylogging.LogLevel.INFO) marocco = PyMarocco() marocco.calib_backend = PyMarocco.CalibBackend.Default marocco.defects.backend = Defects.Backend.None marocco.neuron_placement.skip_hicanns_without_neuron_blacklisting(False) marocco.persist = "results.xml.gz" pynn.setup(marocco = marocco) pop = pynn.Population(1, pynn.IF_cond_exp) marocco.manual_placement.on_hicann(pop, C.HICANNOnWafer(C.X(5), C.Y(5)), 4) pynn.run(10) pynn.end() results = Marocco.from_file(marocco.persist) for neuron in pop: for item in results.placement.find(neuron): for denmem in item.logical_neuron(): print denmem
print simulator_name import numpy as np import os, sys # NM-PM1 specific initialisation if simulator_name in ("nmpm1", "ess"): import pylogging import pyhmf as pynn from pymarocco import PyMarocco from pyhalbe.Coordinate import SynapseDriverOnHICANN, HICANNGlobal, X, Y, Enum, NeuronOnHICANN import Coordinate as C import pyhalbe import pyredman pylogging.set_loglevel(pylogging.get("Default"), pylogging.LogLevel.INFO) pylogging.set_loglevel(pylogging.get("marocco"), pylogging.LogLevel.DEBUG) pylogging.set_loglevel(pylogging.get("sthal.HICANNConfigurator.Time"), pylogging.LogLevel.DEBUG) h = pyredman.Hicann() def initBackend(fname): lib = pyredman.loadLibrary(fname) backend = pyredman.loadBackend(lib) if not backend: raise Exception('unable to load %s' % fname) return backend neuron_size = 4 marocco = PyMarocco()
import pylogging as pylog myLogger = pylog.get("PyN.pop") from pyNN import common import numpy from pyNN.random import RandomDistribution, NativeRNG from math import * import types import os import hwconfig_default_s1v2 as default import pyNN.hardware.spikey ################################ ## PyNN Object-Oriented API ## ################################ class PopulationIterator: ''' Implementation of an iterator supporting calls like 'for cell in population:' ''' def __init__(self, target): self.index = 0 self.target = target def __iter__(self): return self def next(self): if self.index >= self.target.size:
# This file contains the most convenient access functions for the Spikey neuromorphic system. # The functions contained here are intended to be an # intermediate interface for wrapping hardware access with PyNN. # The functions try to hide hardware specific details as far as possible. import pylogging as pylog myLogger = pylog.get("PyN.hal") # these modules will be needed import pyhal_config_s1v2 as conf import pyhal_buildingblocks_s1v2 as bb from pyhal_c_interface_s1v2 import vectorInt, vectorVectorInt import numpy import os import sys import pyhal_neurotypes as neurotypes import time if os.environ.has_key('PYNN_HW_PATH'): basePath = os.path.join(os.environ['PYNN_HW_PATH'], 'config') else: raise EnvironmentError( 'ERROR: The environment variable PYNN_HW_PATH is not defined!') class HardwareError(Exception): '''Exception caused by communication with the Spikey neuromorphic system.''' class HardwareAccessError(HardwareError): '''Exception caused by trying to access the Spikey neuromorphic system.'''
import pylogging as pylog myLogger = pylog.get("PyN.prj") from pyNN import common from pyNN import connectors import numpy from pyNN.random import RandomDistribution, NativeRNG from math import * import types import pyNN.hardware.spikey import hwconfig_default_s1v2 as default # this function is needed below in order to provide pyNN version backwards # compatibility def pynnVersionLarger05(): try: v = int(((pyNN.__version__.partition(' ')[0]).partition('.')[2] ).partition('.')[0]) return (v > 5) except: return True DEFAULT_BUFFER_SIZE = 10000
import pyhmf as pynn from pymarocco import PyMarocco, Defects from pymarocco.runtime import Runtime from pymarocco.coordinates import LogicalNeuron from pymarocco.results import Marocco init_logger("WARN", [ ("guidebook", "DEBUG"), ("marocco", "DEBUG"), ("Calibtic", "DEBUG"), ("sthal", "INFO") ]) import pylogging logger = pylogging.get("guidebook") neuron_parameters = { 'cm': 0.2, 'v_reset': -70., 'v_rest': -20., 'v_thresh': -10, 'e_rev_I': -100., 'e_rev_E': 60., 'tau_m': 20., 'tau_refrac': 0.1, 'tau_syn_E': 5., 'tau_syn_I': 5., } marocco = PyMarocco()
s = -average[-1] print s return (s, ) if __name__ == '__main__': # activate logging pylogging.reset() pylogging.default_config(level=pylogging.LogLevel.INFO, fname="", print_location=False, color=True, date_format='RELATIVE') log = pylogging.get("main") #################### #### Parameters #### #################### nStates = 6 nActions = 8 maxIteration = 1999 multipleRuns = 1 eta = 0.720201224558 exitatory = np.int64(63 * 0.273526901278) gamma = 0.0770639236749 inhibitory = np.int64(63 * 0.521493564443) lam = 0.5 rescaleFreq = np.int64(2000 * 0.682247163094)
def test_append_to_logging(self): # Store all Python logging messages in the "records" list import logging records = [] class Handler(logging.Handler): def emit(self, record): records.append({ "name": record.name, "msg": record.msg, "levelno": record.levelno }) root_logger = logging.getLogger("") root_logger.addHandler(Handler()) root_logger.setLevel(logging.NOTSET) # Log messages to the "root" logger logger.append_to_logging("root") self.assertEqual(1, logger.get_root()._get_number_of_appenders()) # Create two test loggers logger1 = logger.get("test1"); logger.set_loglevel(logger1, logger.LogLevel.TRACE) logger2 = logger.get("test2"); logger3 = logger.get("test2.test3"); logger1.FATAL("msg1") logger1.ERROR("msg2") logger1.WARN("msg3") logger1.INFO("msg4") logger1.DEBUG("msg5") logger1.TRACE("msg6") logger2.FATAL("msg1") logger2.ERROR("msg2") logger2.WARN("msg3") logger2.INFO("msg4") logger2.DEBUG("msg5") logger2.TRACE("msg6") logger3.FATAL("msg1") logger3.ERROR("msg2") logger3.WARN("msg3") logger3.INFO("msg4") logger3.DEBUG("msg5") logger3.TRACE("msg6") self.assertEqual([ {'msg': 'msg1', 'levelno': 50, 'name': 'root.test1'}, {'msg': 'msg2', 'levelno': 40, 'name': 'root.test1'}, {'msg': 'msg3', 'levelno': 30, 'name': 'root.test1'}, {'msg': 'msg4', 'levelno': 20, 'name': 'root.test1'}, {'msg': 'msg5', 'levelno': 10, 'name': 'root.test1'}, {'msg': 'msg6', 'levelno': 10, 'name': 'root.test1'}, {'msg': 'msg1', 'levelno': 50, 'name': 'root.test2'}, {'msg': 'msg2', 'levelno': 40, 'name': 'root.test2'}, {'msg': 'msg3', 'levelno': 30, 'name': 'root.test2'}, {'msg': 'msg4', 'levelno': 20, 'name': 'root.test2'}, {'msg': 'msg5', 'levelno': 10, 'name': 'root.test2'}, {'msg': 'msg1', 'levelno': 50, 'name': 'root.test2.test3'}, {'msg': 'msg2', 'levelno': 40, 'name': 'root.test2.test3'}, {'msg': 'msg3', 'levelno': 30, 'name': 'root.test2.test3'}, {'msg': 'msg4', 'levelno': 20, 'name': 'root.test2.test3'}, {'msg': 'msg5', 'levelno': 10, 'name': 'root.test2.test3'} ], records)
# *************************************************************************** # # This file contains the most important structures needed to create and # administrate abstract representations of neural networks intended # to be emulated on the Spikey neuromorphic system. # The building blocks for such representations are 'Neurons' and 'Network's # of 'Neuron's. These classes have methods for the creation of different types # of neurons and for interconnecting them. A check for the feasibility to map # these networks to the hardware is provided. # # *************************************************************************** import pylogging as pylog myLogger = pylog.get("PyN.bbl") # these modules will be needed import numpy import string import pyhal_neurotypes as neurotypes import copy # for nicer stdout messages printIndentation = ' ' class Network: '''This class is an abstract representation of a neural network to be emulated on the Spikey neuromorphic system.''' def __init__(self, maxSize=384, maxExternalInputs=512, maxExternalInputsPerNeuron=256,
#!/usr/bin/env python import argparse import os import pycalibtic import pylogging pylogging.default_config(date_format='absolute') pylogging.set_loglevel(pylogging.get("Default"), pylogging.LogLevel.INFO) pylogging.set_loglevel(pylogging.get("Calibtic"), pylogging.LogLevel.DEBUG) from pysthal.command_line_util import add_default_coordinate_options def get_calibtic_name(wafer, hicann): wafer_id = wafer.value() hicann_id = hicann.toEnum().value() name = "w{}-h{}".format(int(wafer_id), int(hicann_id)) return name def load_backend(path, ext): """ path: path to calibration files ext: extension of files aka backend type """ if ext == "xml": backend_type = "xml" elif ext == "dat": backend_type = "binary" elif ext == "txt": backend_type = "text"
import pylogging as pylog myLogger = pylog.get("PyN.syn") from pyNN import common from pyNN import synapses import numpy from pyNN.random import RandomDistribution, NativeRNG from math import * import types import hwconfig_default_s1v2 as default import pyNN.hardware.spikey class SynapseDynamics(common.SynapseDynamics): # common.SynapseDynamics sets: # - self.fast # - self.slow def __init__(self, fast=None, slow=None): common.SynapseDynamics.__init__(self, fast, slow) class STDPMechanism(common.STDPMechanism): parameters = {} possible_models = set(['stdp_synapse']) def __init__(self, timing_dependence=None, weight_dependence=None, voltage_dependence=None, dendritic_delay_fraction=0.0): if not isinstance(timing_dependence, SpikePairRule): raise Exception(
def inner_loop(): parser = argparse.ArgumentParser() parser.add_argument('--cal', type=str, default='../adv/calibration_20.json') parser.add_argument('--dac', type=str, default='../adv/dac_07_chip_20.json') parser.add_argument('--load_from', type=str, default='') parser.add_argument('--out', type=str, default='') parser.add_argument('--pl', type=int, choices=range(32), default=4) parser.add_argument('--lr', type=str, choices=['q', 'greedy', 'ann'], default='q') parser.add_argument('--generation', type=int, default=-1) parser.add_argument('--n_batch', type=int, default=1) parser.add_argument('--n_iter', type=int, default=1) parser.add_argument('--dependent', default=False, action='store_true') parser.add_argument('--verbose', default=False, action='store_true') args = parser.parse_args() with open(args.cal) as f: calibrated_config = json.load(f) with open(args.dac) as f: dac_config = json.load(f) pylogging.reset() pylogging.default_config(level=pylogging.LogLevel.INFO, fname="", print_location=False, color=True, date_format='RELATIVE') logger = pylogging.get('main') agent = SpikingBanditAgent(logger) n_batch = args.n_batch agent_hp = agent.default_hyperparameters if args.lr == 'q': learning_rule = IncrementalLearningRule() elif args.lr == 'ann': learning_rule = ANNLearningRule() if args.load_from != '': traj = pp.Trajectory(filename=args.load_from) traj.v_auto_load = True traj.f_load(index=-1, force=True) pop_size = traj.parameters.pop_size n_iter = traj.parameters.n_iteration max_fitness = -100 best_individual = None if args.generation == -1: gen_index = n_iter - 1 else: gen_index = args.generation for j in range(pop_size): traj.v_idx = gen_index * pop_size + j # print(traj.v_idx) fitness = traj.results.crun.fitness if fitness > max_fitness: max_fitness = fitness best_individual = dict( traj.parameters.individual.f_get_children()) best_individual.pop('seed', None) for k, v in best_individual.items(): best_individual[k] = best_individual[k][traj.v_idx] print(best_individual) if args.lr == 'q': agent_hp = dict( action_inhibition=best_individual['action_inhibition'], stim_inhibition=best_individual['stim_inhibition']) lr_hp = dict( learning_rate=best_individual['learning_rate'], learning_rate_decay=best_individual['learning_rate_decay'], weight_prior=best_individual['weight_prior']) learning_rule = IncrementalLearningRule(lr_hp) elif args.lr == 'ann': lr_hp = dict(learning_rate=best_individual['learning_rate'], ann_parameters=best_individual['ann_parameters']) agent_hp = agent.default_hyperparameters learning_rule = ANNLearningRule(lr_hp) else: logger.error('Learning rule {:s} not supported yet'.format( args.lr)) quit() bps = [] ar = [] regrets = [] with Connector(calibrated_config, dac_config, args.pl) as connector: for i in range(args.n_iter): bandit_probabilities = np.random.rand(n_batch, 2) if args.dependent: bandit_probabilities[:, 1] = 1. - bandit_probabilities[:, 0] bandit_probabilities = bandit_probabilities.reshape((-1, )) try: r = agent.play_bandit_batch(bandit_probabilities, 100, n_batch, agent_hp, learning_rule, connector) regrets.append(r[0]) except: continue ar.append(r[1]['a_r']) bps.append(bandit_probabilities) logger.info('iteration made') print(np.mean(regrets)) if args.verbose: spikes = r[1]['spikes'] logger.info(spikes[:20, :]) logger.info('') logger.info(spikes[-20:, :]) logger.info('A total of {} spikes was received'.format( spikes.shape[0])) if args.out != '': with open(args.out, 'wb') as f: pickle.dump(dict(bandit_probabilities=bps, a_r=ar), f) logger.info('Finished')
import pyhmf as pynn from pymarocco import PyMarocco import pylogging, pyhalbe pyhalbe.Debug.change_loglevel(2) pylogging.set_loglevel(pylogging.get("marocco"), pylogging.LogLevel.TRACE) pylogging.set_loglevel(pylogging.get("sthal"), pylogging.LogLevel.DEBUG) marocco = PyMarocco() marocco.neuron_placement.default_neuron_size(4) pynn.setup(marocco=marocco) neuron1 = pynn.Population(1, pynn.IF_cond_exp) inh = pynn.Population(1, pynn.SpikeSourceArray, {'spike_times': [0]}) exc = pynn.Population(1, pynn.SpikeSourceArray, {'spike_times': [0]}) exc_2 = pynn.Population(1, pynn.SpikeSourceArray, {'spike_times': [0]}) exc_3 = pynn.Population(1, pynn.SpikeSourceArray, {'spike_times': [0]}) c_exc = pynn.FixedProbabilityConnector(p_connect=1.0, weights=1) proj1 = pynn.Projection(inh, neuron1, c_exc, target='excitatory') proj2 = pynn.Projection(exc, neuron1, c_exc, target='excitatory') proj3 = pynn.Projection(exc_2, neuron1, c_exc, target='excitatory') proj4 = pynn.Projection(exc_3, neuron1, c_exc, target='inhibitory') pynn.run(10000) pynn.end()
import pylogging as pylog myLogger = pylog.get("PyN.wks") import os import xmltodict import curses import time import numpy if os.environ.has_key('PYNN_HW_PATH'): basePath = os.path.join(os.environ['PYNN_HW_PATH'], 'config') else: raise EnvironmentError( 'ERROR: The environment variable PYNN_HW_PATH is not defined!') homePath = os.environ['HOME'] def getNextKey(): '''get an unbuffered single character.''' import termios import fcntl import sys import os import time fd = sys.stdin.fileno() oldterm = termios.tcgetattr(fd) newattr = termios.tcgetattr(fd) newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr)
#!/usr/bin/env python """ Tool that inits a single HICANN """ import argparse import pylogging pylogging.default_config(date_format='absolute') pylogging.set_loglevel(pylogging.get("sthal"), pylogging.LogLevel.INFO) pylogging.set_loglevel(pylogging.get("Default"), pylogging.LogLevel.INFO) pylogging.set_loglevel(pylogging.get("halbe"), pylogging.LogLevel.INFO) from pyhalco_common import Enum import pyhalco_hicann_v2 as C import pysthal def set_floating_gate_to_zero(hicann): """Set all floating gate values to zero""" fgc = hicann.floating_gates for block in C.iter_all(C.FGBlockOnHICANN): blk = fgc[block] for cell in C.iter_all(C.FGCellOnFGBlock): blk.setRaw(cell, 0) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--wafer', type=int,
def test_config_from_file(self): import inspect log = os.path.join(self.temp, 'log') config = os.path.join(self.temp, 'config') with open(config, 'w') as f: f.write(""" # Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=WARN, A1 # More detail from xyz, but only a bit more from xyz.test log4j.logger.xyz=TRACE log4j.logger.xyz.test=INFO # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.FileAppender log4j.appender.A1.File={log} #log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.ColorLayout log4j.appender.A1.layout.Color=true log4j.appender.A1.layout.PrintLocation=true """.format(log=log)) logger.config_from_file(config) logger1 = logger.get("test") logger2 = logger.get("xyz") logger3 = logger.get("xyz.test") filename = inspect.getframeinfo(inspect.currentframe())[0] loglines = [] def logln(msg): f = inspect.stack()[1][0] l = inspect.getframeinfo(f)[1] loc = " -> [33m" + filename + "[0m:[31m" + str(l) + "[0m" loglines.append(msg) loglines.append('\n') loglines.append(loc) loglines.append('\n') logger.LOG4CXX_FATAL(logger1, "FATAL") logln("[31mFATAL [0mtest FATAL") logger.LOG4CXX_ERROR(logger1, "ERROR") logln("[31mERROR [0mtest ERROR") logger.LOG4CXX_WARN(logger1, "WARN") logln("[33mWARN [0mtest WARN") logger.LOG4CXX_INFO(logger1, "INFO") logger.LOG4CXX_DEBUG(logger1, "DEBUG") logger.LOG4CXX_TRACE(logger1, "TRACE") logger2.FATAL("FATAL") logln("[31mFATAL [0mxyz FATAL") logger2.ERROR("ERROR") logln("[31mERROR [0mxyz ERROR") logger2.WARN("WARN") logln("[33mWARN [0mxyz WARN") logger2.INFO("INFO") logln("[32mINFO [0mxyz INFO") logger2.DEBUG("DEBUG") logln("[32mDEBUG [0mxyz DEBUG") logger2.TRACE("TRACE") logln("[32mTRACE [0mxyz TRACE") logger.LOG4CXX_FATAL(logger3, "FATAL") logln("[31mFATAL [0mxyz.test FATAL") logger.LOG4CXX_ERROR(logger3, "ERROR") logln("[31mERROR [0mxyz.test ERROR") logger.LOG4CXX_WARN(logger3, "WARN") logln("[33mWARN [0mxyz.test WARN") logger.LOG4CXX_INFO(logger3, "INFO") logln("[32mINFO [0mxyz.test INFO") logger.LOG4CXX_DEBUG(logger3, "DEBUG") logger.LOG4CXX_TRACE(logger3, "TRACE") logger.reset() # Hope this flushes the logger ;) with open(log) as f: expected = "".join(loglines) self.assertEqual(expected, f.read())
def test_file_logging_with_filter(self): logger1 = logger.get("test"); logger2 = logger.get("xyz"); logger3 = logger.get("xyz.test"); logger.set_loglevel(logger1, logger.LogLevel.WARN) logger.set_loglevel(logger2, logger.LogLevel.DEBUG) logger.set_loglevel(logger3, logger.LogLevel.INFO) # Test different filter log = os.path.join(self.temp, 'test_file_logging_with_filter.log') app = logger.append_to_file(log, logger.get_root()) f = logger.LevelRangeFilter() f.setLevelMin(logger.LogLevel.DEBUG) f.setLevelMax(logger.LogLevel.WARN) app.addFilter(f) log2 = os.path.join(self.temp, 'test_file_logging_with_filter2.log') app = logger.append_to_file(log2, logger.get_root()) f = logger.LevelRangeFilter() f.setLevelMin(logger.LogLevel.ERROR) f.setLevelMax(logger.LogLevel.FATAL) app.addFilter(f) log3 = os.path.join(self.temp, 'test_file_logging_with_filter3.log') app = logger.append_to_file(log3, logger2) f = logger.LevelRangeFilter() f.setLevelMin(logger.LogLevel.ERROR) f.setLevelMax(logger.LogLevel.FATAL) app.addFilter(f) for l in (logger1, logger2, logger3): logger.LOG4CXX_FATAL(l, "FATAL") logger.LOG4CXX_ERROR(l, "ERROR") logger.LOG4CXX_WARN (l, "WARN") logger.LOG4CXX_INFO (l, "INFO") logger.LOG4CXX_DEBUG(l, "DEBUG") logger.LOG4CXX_TRACE(l, "TRACE") logger.reset() with open(log) as f: expected = """WARN test WARN WARN xyz WARN INFO xyz INFO DEBUG xyz DEBUG WARN xyz.test WARN INFO xyz.test INFO """ self.assertEqualLogLines(expected, f.read()) with open(log2) as f: expected = """FATAL test FATAL ERROR test ERROR FATAL xyz FATAL ERROR xyz ERROR FATAL xyz.test FATAL ERROR xyz.test ERROR """ self.assertEqualLogLines(expected, f.read()) with open(log3) as f: expected = """FATAL xyz FATAL ERROR xyz ERROR FATAL xyz.test FATAL ERROR xyz.test ERROR """ self.assertEqualLogLines(expected, f.read())
import unittest import pylogging import pyhmf as pynn import pyhalbe import pyhalco_common import pyhalco_hicann_v2 as C import numpy as np import debug_config import pymarocco from pymarocco.results import Marocco pylogging.reset() pylogging.default_config(pylogging.LogLevel.INFO) pylogging.set_loglevel(pylogging.get("marocco"), pylogging.LogLevel.DEBUG) def default_marocco(): marocco = pymarocco.PyMarocco() marocco.neuron_placement.default_neuron_size(4) marocco.synapse_routing.driver_chain_length(C.SynapseDriverOnQuadrant.size) marocco.experiment.speedup(10000.) marocco.defects.backend = pymarocco.Defects.Backend.Without return marocco class TestRateDependentInputPlacement(unittest.TestCase): """Test input placement with consider_firing_rate=True for diverse settings"""
from _hxtorch import * from hxtorch import nn import pylogging as logger logger.reset() logger.default_config(level=logger.LogLevel.WARN) logger.set_loglevel(logger.get("hxtorch"), logger.LogLevel.INFO)