def init_logger(default_level=pylogging.LogLevel.INFO, defaults=tuple(), add_help=False): """Initialize logging system Arguments: default_level: default log level for the logging system args: list of tuples for channel default values add_help: [bool] print help when called with --help, set to True if you don't have an own parser. Example: init_logger(args, [('sthal', 'INFO'), ('halbe', 'WARN')]) """ parser = argparse.ArgumentParser(description='PSP visualization tool', add_help=add_help) add_logger_options(parser, default_level) parser_args, _ = parser.parse_known_args() pylogging.reset() pylogging.default_config( level=parser_args.loglevel, fname=parser_args.logfile if parser_args.logfile else "", print_location=parser_args.loglocation, color=parser_args.logcolor, date_format=parser_args.logdate_format) for name, level in defaults: pylogging.set_loglevel(pylogging.get(name), to_level(level)) for name, level in parser_args.logchannel: pylogging.set_loglevel(pylogging.get(name), to_level(level))
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 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 main(): parser = argparse.ArgumentParser() parser.add_argument('--source-hicann', required=True, type=parse_hicann, metavar='<enum>|<x>,<y>', help='source HICANNOnWafer') parser.add_argument('--source-hline', required=False, type=int, help='source HLineOnHICANN') parser.add_argument('--source-vline', required=False, type=int, help='source VLineOnHICANN') parser.add_argument('--target-hicann', required=True, type=parse_hicann, metavar='<enum>|<x>,<y>', help='target HICANNOnWafer') parser.add_argument('--target-vertical', action='store_true', help='target should be VLineOnHICANN') parser.add_argument('--without-hicann', default=[], type=parse_hicann, nargs="+", metavar='<enum>|<x>,<y>', help='unavailable HICANNOnWafer') args = parser.parse_args() pylogging.reset() pylogging.default_config(date_format='absolute') alone = pyalone.Alone() for hicann in iter_all(HICANNOnWafer): if hicann in args.without_hicann: continue alone.add(hicann) if args.source_hline is not None: line = HLineOnHICANN(args.source_hline) elif args.source_vline is not None: line = VLineOnHICANN(args.source_vline) else: parser.error('Please specify one of --source-hline / --source-vline') source = pyalone.L1BusOnWafer(args.source_hicann, line) target = pyalone.Target(args.target_hicann, vertical if args.target_vertical else horizontal) routes = alone.find_routes(source, target) for route in routes: print(route)
def setUp(self): pylogging.reset() pylogging.default_config(pylogging.LogLevel.ERROR) pylogging.set_loglevel( pylogging.get("marocco"), pylogging.LogLevel.INFO) 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.persist = os.path.join( self.temporary_directory, "results.bin") self.marocco.neuron_placement.default_neuron_size(4)
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): pylogging.reset() pylogging.default_config(pylogging.LogLevel.ERROR) pylogging.set_loglevel(pylogging.get("marocco"), pylogging.LogLevel.INFO) 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.persist = os.path.join(self.temporary_directory, "results.bin") self.marocco.neuron_placement.default_neuron_size(4) self.marocco.continue_despite_synapse_loss = True self.marocco.calib_backend = pymarocco.PyMarocco.CalibBackend.Default self.marocco.defects.backend = pymarocco.Defects.Backend.Without
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 runTest(self): pylogging.reset() pylogging.default_config(pylogging.LogLevel.INFO) marocco = pymarocco.PyMarocco() marocco.neuron_placement.default_neuron_size(8) marocco.calib_backend = pymarocco.PyMarocco.CalibBackend.Default marocco.defects.backend = pymarocco.Defects.Backend.Without marocco.hicann_configurator = pysthal.HICANNConfigurator() marocco.continue_despite_synapse_loss = True marocco.backend = pymarocco.PyMarocco.ESS marocco.experiment_time_offset = 5e-7 n_exc = 100 # Number of excitatory neurons per group sim_duration = 200. pp_start = 50. # start = center of pulse-packet weight_exc = 0.002 # uS weight for excitatory to excitatory connections # (double than in reference paper) pynn.setup( max_delay=20., marocco=marocco, ) # v_thresh close to v_rest to make sure there are some spikes neuron_params = { 'v_rest': -65., 'v_thresh': -62.5, } exc_pop = pynn.Population(n_exc, pynn.IF_cond_exp, neuron_params) exc_pop.record() pop_stim = pynn.Population(n_exc, pynn.SpikeSourceArray, {'spike_times': [pp_start]}) conn = pynn.FixedNumberPreConnector(60, weights=weight_exc, delays=20.) pynn.Projection(pop_stim, exc_pop, conn, target='excitatory') pynn.run(sim_duration) pynn.end()
return out def main(): """main""" description = 'Shows information about HALbe coodinates' epilog = "example: {} --hicann 0 --hicann 5,5 --fpga 0 --power 1".format( sys.argv[0]) parser = argparse.ArgumentParser(description=description, epilog=epilog) add_coordinate(parser, "--wafer", Coordinate.Wafer, f_wafer) add_coordinate(parser, "--hicann", Coordinate.HICANNOnWafer, f_hicann) add_coordinate(parser, "--fpga", Coordinate.FPGAOnWafer, f_fpga) add_coordinate(parser, "--dnc", Coordinate.DNCOnWafer, f_dnc) add_coordinate(parser, "--power", Coordinate.PowerCoordinate, f_reticle) args = parser.parse_args() if args.values: for (value, ) in args.values: if value: print(value) else: parser.print_usage() if __name__ == '__main__': pylogging.reset() pylogging.default_config(date_format='absolute') main()
#!/usr/bin/env python """ example script for extracting synapse loss after mapping """ from pymarocco import PyMarocco import pyhmf as pynn import numpy as np import pylogging pylogging.reset() pylogging.default_config(level=pylogging.LogLevel.ERROR) def projectionwise_synapse_loss(proj, marocco): """ computes the synapse loss of a projection params: proj - a pyhmf.Projection marocco - the PyMarocco object after the mapping has run. returns: (nr of lost synapses, total synapses in projection) """ orig_weights = proj.getWeights(format='array') mapped_weights = marocco.stats.getWeights(proj) syns = np.where(~np.isnan(orig_weights)) realized_syns = np.where(~np.isnan(mapped_weights)) orig = len(syns[0]) realized = len(realized_syns[0]) print "Projection-Wise Synapse Loss", proj, (orig - realized) * 100. / orig return orig - realized, orig
import tempfile 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"""
import unittest import pycalibtic as cal import pyhalbe import pyhalco_hicann_v2 import pycellparameters as cell import numpy as np import pywrapstdvector # http://stackoverflow.com/a/22726782/1350789 from contextlib import contextmanager import tempfile import shutil import itertools import pylogging pylogging.default_config() @contextmanager def TemporaryDirectory(): name = tempfile.mkdtemp() try: yield name finally: shutil.rmtree(name) def initBackend(fname): lib = cal.loadLibrary(fname) backend = cal.loadBackend(lib) if not backend: raise Exception('unable to load %s' % fname)
def setUp(self): pylogging.reset() pylogging.default_config(pylogging.LogLevel.WARN)
Example Script for simulation of an AdEx neuron on the ESS Note: Neuron and synapse parameters are chosen to be within the parameter ranges of the default calibration. """ import pyhmf as pynn #import pyNN.nest as pynn from pymarocco import PyMarocco, Defects import pylogging import pysthal # configure logging pylogging.reset() pylogging.default_config(level=pylogging.LogLevel.INFO, fname="logfile.txt", dual=False) # Mapping config marocco = PyMarocco() marocco.backend = PyMarocco.ESS # choose Executable System Specification instead of real hardware marocco.calib_backend = PyMarocco.CalibBackend.Default marocco.defects.backend = Defects.Backend.None marocco.hicann_configurator = pysthal.HICANNConfigurator() marocco.experiment_time_offset = 5.e-7 # can be low for ESS, as no repeater locking required marocco.neuron_placement.default_neuron_size( 4) # default number of hardware neuron circuits per pyNN neuron marocco.persist = "nmpm1_adex_neuron_ess.bin" marocco.param_trafo.use_big_capacitors = False # set-up the simulator
#!/usr/bin/env python """ example script for extracting synapse loss after mapping """ from pymarocco import PyMarocco import pyhmf as pynn import numpy as np import pylogging pylogging.reset() pylogging.default_config(level=pylogging.LogLevel.ERROR) def projectionwise_synapse_loss(proj, marocco): """ computes the synapse loss of a projection params: proj - a pyhmf.Projection marocco - the PyMarocco object after the mapping has run. returns: (nr of lost synapses, total synapses in projection) """ orig_weights = proj.getWeights(format='array') mapped_weights = marocco.stats.getWeights(proj) syns = np.where(~np.isnan(orig_weights)) realized_syns = np.where(~np.isnan(mapped_weights)) orig = len(syns[0]) realized = len(realized_syns[0]) print "Projection-Wise Synapse Loss", proj, (orig - realized)*100./orig return orig-realized, orig def plot_projectionwise_synapse_loss(proj, marocco):
def setUp(self): import pylogging pylogging.default_config(pylogging.LogLevel.FATAL)
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)
Note: Neuron and synapse parameters are chosen to be within the parameter ranges of the default calibration. """ import pyhmf as pynn #import pyNN.nest as pynn from pymarocco import PyMarocco, Defects import pylogging import Coordinate as C import pysthal # configure logging pylogging.reset() pylogging.default_config(level=pylogging.LogLevel.INFO, fname="logfile.txt", dual=False) # Mapping config marocco = PyMarocco() marocco.backend = PyMarocco.ESS # choose Executable System Specification instead of real hardware marocco.calib_backend = PyMarocco.CalibBackend.Default marocco.defects.backend = Defects.Backend.None marocco.neuron_placement.skip_hicanns_without_neuron_blacklisting(False) marocco.hicann_configurator = pysthal.HICANNConfigurator() marocco.experiment_time_offset = 5.e-7 # can be low for ESS, as no repeater locking required marocco.neuron_placement.default_neuron_size(4) # default number of hardware neuron circuits per pyNN neuron marocco.persist = "nmpm1_adex_neuron_ess.bin" marocco.param_trafo.use_big_capacitors = False # set-up the simulator
parser.add_argument('--as_string', action='store_true') # No matter how long the program runs, it will always be stopped after # `wait` FPGA cycles. If the program finishes earlier, the PPU goes to # sleep mode while the FPGA still waits for this fixed amount of cycles. parser.add_argument( '--wait', type=int, default=int(1E7), help="Number of FPGA cycles after which the PPU is stopped") args = parser.parse_args() pylogging.reset() pylogging.default_config(level=pylogging.LogLevel.INFO, fname="", print_location=False, color=True, date_format='RELATIVE') # Load the data program = pydls.Ppu_program() program.read_from_file(args.program) mailbox = pydls.Mailbox() if args.data_in is not None: load_mailbox(mailbox, args.data_in) # Setup synram control register # These are magic numbers which configure the timing how the synram is # written. synram_config_reg = pydls.Synram_config_reg()
import pyhaldls_v2 as hal import pystadls_v2 as sta import dlens_v2.halco import pylogging as logger logger.reset() logger.default_config(level=logger.LogLevel.DEBUG)
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')