Пример #1
0
 def __init__(self,**params):
     Parameterized.__init__(self,**params)
     
     for (name,value) in self.get_param_values():
         if value == None:
            logger.error("The parameter %s was not initialized" % name) 
            raise ValueError("The parameter %s was not initialized" % name) 
Пример #2
0
    def __init__(self, **params):
        
        self.cached_get_param_values = None
        Parameterized.__init__(self, **params)
        self.module_path = inspect.getmodule(self).__name__
        self.name = self.__class__.__name__
        

        for name in self.params():
            o = self.params()[name]
            if not (isinstance(o,SNumber) or isinstance(o,SInteger) or isinstance(o,SString) or isinstance(o,SParameterSet)):
               raise ValueError("The parameter %s is not of type SNumber or SInteger or SString or SParameterSet but of type %s." % (name,type(o)))
 
        for (name, value) in self.get_param_values():
            if value == None and self.params()[name].allow_None==False:                
                logger.error("The parameter %s was not initialized" % name)
                raise ValueError("The parameter %s was not initialized" % name)

        def _expand_parameter_set(ps):
            """
            Helper function that expands parameter set.
            """
            ret = []
            for k in ps.keys():
                if isinstance(ps[k],MozaikExtendedParameterSet):
                   ret += [(k + '_' + p,v)  for p,v in  _expand_parameter_set(ps[k])]
                else:
                   ret.append((k,ps[k]))       
            return ret

        # find out which params are SParameterSet
        self.paramset_params_names =  [ps for ps in self.params().keys() if isinstance(self.params()[ps],SParameterSet)]

        # store expanded SParameterSet parameters
        self.expanded_paramset_params = []
        for ps in self.paramset_params_names:
                if getattr(self,ps) != None:
                    self.expanded_paramset_params+=_expand_parameter_set(getattr(self,ps))
        self.expanded_paramset_params.sort(key=lambda tup: tup[0])



        if self.expanded_paramset_params != []:
            self.expanded_params_names = zip(*self.expanded_paramset_params)[0]
        else:
            self.expanded_params_names= []

        assert ((set([t[0] for t in self.expanded_paramset_params]) & set(self.params().keys())) == set([])) , ("Conflict between MozaikParametrized and expanded ParameterSet parameters. Intersection: %s " % str(set([t[0] for t in self.expanded_paramset_params]) & set(self.params().keys())))

        # and lets also have a dictionary version of the parameters
        self.expanded_paramset_params_dict = self.params().copy()

        # remove SParemeterSet parameters 
        for key in self.expanded_paramset_params_dict.keys():
            if isinstance(self.expanded_paramset_params_dict[key],SParameterSet):
               del self.expanded_paramset_params_dict[key]
        
        self.expanded_paramset_params_dict.update(dict(self.expanded_paramset_params))
Пример #3
0
    def __init__(self, **params):
        
        self.cached_get_param_values = None
        Parameterized.__init__(self, **params)
        self.module_path = inspect.getmodule(self).__name__
        self.name = self.__class__.__name__
        

        for name in self.params():
            o = self.params()[name]
            if not (isinstance(o,SNumber) or isinstance(o,SInteger) or isinstance(o,SString) or isinstance(o,SParameterSet)):
               raise ValueError("The parameter %s is not of type SNumber or SInteger or SString or SParameterSet but of type %s." % (name,type(o)))
 
        for (name, value) in self.get_param_values():
            if value == None and self.params()[name].allow_None==False:                
                logger.error("The parameter %s was not initialized" % name)
                raise ValueError("The parameter %s was not initialized" % name)

        def _expand_parameter_set(ps):
            """
            Helper function that expands parameter set.
            """
            ret = []
            for k in ps.keys():
                if isinstance(ps[k],MozaikExtendedParameterSet):
                   ret += [(k + '_' + p,v)  for p,v in  _expand_parameter_set(ps[k])]
                else:
                   ret.append((k,ps[k]))       
            return ret

        # find out which params are SParameterSet
        self.paramset_params_names =  [ps for ps in self.params().keys() if isinstance(self.params()[ps],SParameterSet)]

        # store expanded SParameterSet parameters
        self.expanded_paramset_params = []
        for ps in self.paramset_params_names:
                if getattr(self,ps) != None:
                    self.expanded_paramset_params+=_expand_parameter_set(getattr(self,ps))
        self.expanded_paramset_params.sort(key=lambda tup: tup[0])



        if self.expanded_paramset_params != []:
            self.expanded_params_names = zip(*self.expanded_paramset_params)[0]
        else:
            self.expanded_params_names= []

        assert ((set([t[0] for t in self.expanded_paramset_params]) & set(self.params().keys())) == set([])) , ("Conflict between MozaikParametrized and expanded ParameterSet parameters. Intersection: %s " % str(set([t[0] for t in self.expanded_paramset_params]) & set(self.params().keys())))

        # and lets also have a dictionary version of the parameters
        self.expanded_paramset_params_dict = self.params().copy()

        # remove SParemeterSet parameters 
        for key in self.expanded_paramset_params_dict.keys():
            if isinstance(self.expanded_paramset_params_dict[key],SParameterSet):
               del self.expanded_paramset_params_dict[key]
        
        self.expanded_paramset_params_dict.update(dict(self.expanded_paramset_params))
Пример #4
0
    def  __init__(self, datastore, **params):
        Parameterized.__init__(self, **params)
        self.datastore = datastore

        ### lets first find the values of the two parameters in the datastore
        self.x_axis_values = list(parameter_value_list(datastore.get_analysis_result(),self.x_axis_parameter))
        self.y_axis_values = list(parameter_value_list(param_filter_query(datastore,**{self.x_axis_parameter:self.x_axis_values[0]}).get_analysis_result(),self.y_axis_parameter))
        
        ### and verify it forms a grid
        for v in self.x_axis_values:
            assert set(self.y_axis_values) == parameter_value_list(param_filter_query(datastore,**{self.x_axis_parameter:v}).get_analysis_result(),self.y_axis_parameter)
Пример #5
0
    def  __init__(self, datastore, **params):
        Parameterized.__init__(self, **params)
        self.datastore = datastore

        ### lets first find the values of the two parameters in the datastore
        self.x_axis_values = list(parameter_value_list(datastore.get_analysis_result(),self.x_axis_parameter))
        self.y_axis_values = list(parameter_value_list(param_filter_query(datastore,**{self.x_axis_parameter:self.x_axis_values[0]}).get_analysis_result(),self.y_axis_parameter))
        
        ### and verify it forms a grid
        for v in self.x_axis_values:
            assert set(self.y_axis_values) == parameter_value_list(param_filter_query(datastore,**{self.x_axis_parameter:v}).get_analysis_result(),self.y_axis_parameter)
Пример #6
0
    def __init__(self, **params):
        Parameterized.__init__(self, **params)
        self.module_path = inspect.getmodule(self).__name__
        self.name = self.__class__.__name__
        
        for name in self.params():
            o = self.params()[name]
            if not (isinstance(o,SNumber) or isinstance(o,SInteger) or isinstance(o,SString)):
               raise ValueError("The parameter %s is not of type SNumber or SInteger or SString" % name)
 
        for (name, value) in self.get_param_values():
            if value == None and self.params()[name].allow_None==False:                
                logger.error("The parameter %s was not initialized" % name)
                raise ValueError("The parameter %s was not initialized" % name)
Пример #7
0
    def __init__(self, **params):
        Parameterized.__init__(self, **params)
        self.module_path = inspect.getmodule(self).__name__
        self.name = self.__class__.__name__

        for name in self.params():
            o = self.params()[name]
            if not (isinstance(o, SNumber) or isinstance(o, SInteger)
                    or isinstance(o, SString)):
                raise ValueError(
                    "The parameter %s is not of type SNumber or SInteger or SString"
                    % name)

        for (name, value) in self.get_param_values():
            if value == None and self.params()[name].allow_None == False:
                logger.error("The parameter %s was not initialized" % name)
                raise ValueError("The parameter %s was not initialized" % name)
Пример #8
0
    def __setattr__(self, attribute_name, value):
        """
        We need to override the Parametrized __setattr__ to handle setting of SParameterSet parameters.
        """
        def set_in_dict(path, dt, value):
            keys = path.split('_')
            for key in keys[:-1]:
                dt = dt[key]
            dt[keys[-1]] = value

        if hasattr(self, 'expanded_params_names'):
            if attribute_name in self.expanded_params_names:
                self.expanded_paramset_params_dict[attribute_name] = value

                n = []
                for z in self.expanded_paramset_params:
                    if z[0] == attribute_name:
                        n.append((z[0], value))
                    else:
                        n.append(z)
                self.expanded_paramset_params = n

                #HAAAAAAAAAAAAACK
                if attribute_name == 'stimulating_signal_parameters_orientation':
                    getattr(
                        self, 'direct_stimulation_parameters'
                    )['stimulating_signal_parameters']['orientation'] = value

                if attribute_name == 'stimulating_signal_parameters_scale':
                    getattr(self, 'direct_stimulation_parameters'
                            )['stimulating_signal_parameters']['scale'] = value

                if attribute_name == 'stimulating_signal_parameters_contrast':
                    getattr(
                        self, 'direct_stimulation_parameters'
                    )['stimulating_signal_parameters']['contrast'] = value

                if attribute_name == 'stimulating_signal_parameters_size':
                    getattr(self, 'direct_stimulation_parameters'
                            )['stimulating_signal_parameters']['size'] = value

                #set_in_dict(attribute_name,getattr(self,'direct_stimulation_parameters'),value)
                #set_in_dict(path[1:,],self.params()[path[0]],value)
                return
        Parameterized.__setattr__(self, attribute_name, value)
        Parameterized.__setattr__(self, 'cached_get_param_values', None)
Пример #9
0
    def __setattr__(self,attribute_name,value):
        """
        We need to override the Parametrized __setattr__ to handle setting of SParameterSet parameters.
        """
        def set_in_dict(path, dt,value):
            keys = path.split('_')
            for key in keys[:-1]:
                dt = dt[key]
            dt[keys[-1]]=value

        if hasattr(self, 'expanded_params_names'):
            if attribute_name in self.expanded_params_names:
                self.expanded_paramset_params_dict[attribute_name] = value;

                n =[]
                for z in self.expanded_paramset_params:
                    if z[0] == attribute_name:
                       n.append((z[0],value)) 
                    else:
                       n.append(z)   
                self.expanded_paramset_params = n

                #HAAAAAAAAAAAAACK
                if attribute_name == 'stimulating_signal_parameters_orientation':
                    getattr(self,'direct_stimulation_parameters')['stimulating_signal_parameters']['orientation']=value

                if attribute_name == 'stimulating_signal_parameters_scale':
                    getattr(self,'direct_stimulation_parameters')['stimulating_signal_parameters']['scale']=value

                if attribute_name == 'stimulating_signal_parameters_contrast':
                    getattr(self,'direct_stimulation_parameters')['stimulating_signal_parameters']['contrast']=value

                if attribute_name == 'stimulating_signal_parameters_size':
                    getattr(self,'direct_stimulation_parameters')['stimulating_signal_parameters']['size']=value


                #set_in_dict(attribute_name,getattr(self,'direct_stimulation_parameters'),value)
                #set_in_dict(path[1:,],self.params()[path[0]],value)
                return
        Parameterized.__setattr__(self,attribute_name,value)
        Parameterized.__setattr__(self,'cached_get_param_values',None)
Пример #10
0
def process_argv(argv):
    """
    Process command-line arguments (minus argv[0]!), rearrange and execute.
    """
    # Initial preparation
    import __main__
    for (k, v) in global_constants.items():
        exec '%s = %s' % (k, v) in __main__.__dict__

    # Allow param.normalize_path.prefix to be overridden in the
    # startup files, but otherwise force it to exist before doing
    # anything else
    param.normalize_path.prefix = default_output_path()
    exec_startup_files()
    set_output_path(param.normalize_path.prefix)

    # Tell the user how many cores are in use, if available
    openmp_main = Parameterized(
        name="OpenMP")  # Dummy object just for messages
    try:
        import os, multiprocessing
        total_cores = multiprocessing.cpu_count()
        num_threads = int(os.environ.get('OMP_NUM_THREADS', total_cores))
        openmp_main.verbose(
            "Using %d threads on a machine with %d detected CPUs", num_threads,
            total_cores)
    except:
        pass

    # Repeatedly process options, if any, followed by filenames, if any, until nothing is left
    topo_parser.disable_interspersed_args()
    args = argv
    option = None
    global something_executed
    while True:
        # Process options up until the first filename
        (option, args) = topo_parser.parse_args(args, option)

        # Handle filename
        if args:
            filename = args.pop(0)
            #print "Executing %s" % (filename)
            filedir = os.path.dirname(os.path.abspath(filename))
            sys.path.insert(
                0, filedir)  # Allow imports relative to this file's path
            sim_name_from_filename(filename)  # Default value of topo.sim.name

            execfile(filename, __main__.__dict__)
            something_executed = True

        if not args:
            break

    global_params.check_for_unused_names()

    # If no scripts and no commands were given, pretend -i was given.
    if not something_executed: interactive()

    if option.gui: topo.guimain.title(topo.sim.name)

    ## INTERACTIVE SESSION BEGINS HERE (i.e. can't have anything but
    ## some kind of cleanup code afterwards)
    if os.environ.get('PYTHONINSPECT'):
        print "Output path: %s" % param.normalize_path.prefix
        print BANNER
        # CBALERT: should probably allow a way for users to pass
        # things to IPython? Or at least set up some kind of
        # topographica ipython config file. Right now, a topo_parser
        # option has to be added for every ipython option we want to
        # support (e.g. see --pdb)

        if ipython_shell_interface == "IPython.Shell":
            # IPython 0.10 and earlier

            # Stop IPython namespace hack?
            # http://www.nabble.com/__main__-vs-__main__-td14606612.html
            __main__.__name__ = "__mynamespace__"

            ipython_args = [
                '-noconfirm_exit', '-nobanner', '-pi1',
                CommandPrompt.get_format(), '-pi2',
                CommandPrompt2.get_format(), '-po',
                OutputPrompt.get_format()
            ]
            if option.pdb:
                ipython_args.append('-pdb')

            ipshell = IPShell(ipython_args, user_ns=__main__.__dict__)
            ipshell.mainloop(sys_exit=1)

        elif ipython_shell_interface == "InteractiveShellEmbed":
            # IPython 0.11 and later

            config = Config()

            if ipython_prompt_interface == "PromptManager":
                config.PromptManager.in_template = CommandPrompt.get_format()
                config.PromptManager.in2_template = CommandPrompt2.get_format()
                config.PromptManager.out_template = OutputPrompt.get_format()
            else:
                config.InteractiveShell.prompt_in1 = CommandPrompt.get_format()
                config.InteractiveShell.prompt_in2 = CommandPrompt2.get_format(
                )
                config.InteractiveShell.prompt_out = OutputPrompt.get_format()
            config.InteractiveShell.confirm_exit = False
            ipshell = IPShell(config=config,
                              user_ns=__main__.__dict__,
                              banner1="",
                              exit_msg="")
            if option.pdb:
                ipshell.call_pdb = True

            # Load Topographica IPython extension in embedded shell
            try:
                ipshell.extension_manager.load_extension('topo.misc.ipython')
            except:
                cmdline_main.warning(
                    "Could not load IPython extension 'topo.misc.ipython'; ignored error was:\n%s"
                    % traceback.format_exc())

            ipshell()

    global return_code
    if return_code != 0:
        cmdline_main.warning(
            "Errors encountered; exiting with return code %d" % return_code)

    sys.exit(return_code)
Пример #11
0
from param import parameterized
from param.parameterized import Parameterized
from topo.base.simulation import OptionalSingleton

try:
    # By default, use a non-GUI backend for matplotlib.
    from matplotlib import pyplot as plt
    plt.switch_backend('agg')

    matplotlib_imported = True
except ImportError:
    matplotlib_imported = False

# Dummy object just for messages
cmdline_main = Parameterized(name="CommandLine")

ipython_shell_interface = None
ipython_prompt_interface = None
try:
    try:
        from IPython.terminal.embed import InteractiveShellEmbed as IPShell
    except ImportError:  # Prior to IPython 1.0, InteractiveShellEmbed was found in the frontend package
        from IPython.frontend.terminal.embed import InteractiveShellEmbed as IPShell  # pyflakes:ignore (try/except import)
    from IPython.config.loader import Config
    ipython_shell_interface = "InteractiveShellEmbed"
    try:
        from IPython.core.prompts import PromptManager  # pyflakes:ignore (try/except import)
        ipython_prompt_interface = "PromptManager"
    except ImportError:
        pass
Пример #12
0
def process_argv(argv):
    """
    Process command-line arguments (minus argv[0]!), rearrange and execute.
    """
    # Initial preparation
    import __main__
    for (k,v) in global_constants.items():
        exec '%s = %s' % (k,v) in __main__.__dict__

    # Allow param.normalize_path.prefix to be overridden in the
    # startup files, but otherwise force it to exist before doing
    # anything else
    param.normalize_path.prefix = default_output_path()
    exec_startup_files()
    set_output_path(param.normalize_path.prefix)

    # Tell the user how many cores are in use, if available
    openmp_main=Parameterized(name="OpenMP") # Dummy object just for messages
    try:
        import os,multiprocessing
        total_cores = multiprocessing.cpu_count()
        num_threads = int(os.environ.get('OMP_NUM_THREADS',total_cores))
        openmp_main.verbose("Using %d threads on a machine with %d detected CPUs" % (num_threads, total_cores))
    except:
        pass

    # Repeatedly process options, if any, followed by filenames, if any, until nothing is left
    topo_parser.disable_interspersed_args()
    args=argv
    option=None
    global something_executed
    while True:
        # Process options up until the first filename
        (option,args) = topo_parser.parse_args(args,option)

        # Handle filename
        if args:
            filename=args.pop(0)
            #print "Executing %s" % (filename)
            filedir = os.path.dirname(os.path.abspath(filename))
            sys.path.insert(0,filedir) # Allow imports relative to this file's path
            sim_name_from_filename(filename) # Default value of topo.sim.name

            execfile(filename,__main__.__dict__)
            something_executed=True

        if not args:
            break

    global_params.check_for_unused_names()

    # If no scripts and no commands were given, pretend -i was given.
    if not something_executed: interactive()

    if option.gui: topo.guimain.title(topo.sim.name)

    ## INTERACTIVE SESSION BEGINS HERE (i.e. can't have anything but
    ## some kind of cleanup code afterwards)
    if os.environ.get('PYTHONINSPECT'):
        print "Output path: %s" % param.normalize_path.prefix
        print BANNER
        # CBALERT: should probably allow a way for users to pass
        # things to IPython? Or at least set up some kind of
        # topographica ipython config file. Right now, a topo_parser
        # option has to be added for every ipython option we want to
        # support (e.g. see --pdb)

        if ipython_shell_interface == "IPython.Shell":
            # IPython 0.10 and earlier

            # Stop IPython namespace hack?
            # http://www.nabble.com/__main__-vs-__main__-td14606612.html
            __main__.__name__="__mynamespace__"

            ipython_args = ['-noconfirm_exit','-nobanner',
                            '-pi1',CommandPrompt.get_format(),
                            '-pi2',CommandPrompt2.get_format(),
                            '-po',OutputPrompt.get_format()]
            if option.pdb:
                ipython_args.append('-pdb')

            ipshell = IPShell(ipython_args,user_ns=__main__.__dict__)
            ipshell.mainloop(sys_exit=1)

        elif ipython_shell_interface == "InteractiveShellEmbed":
            # IPython 0.11 and later

            config = Config()

            if ipython_prompt_interface == "PromptManager":
                config.PromptManager.in_template = CommandPrompt.get_format()
                config.PromptManager.in2_template = CommandPrompt2.get_format()
                config.PromptManager.out_template = OutputPrompt.get_format()
            else:
                config.InteractiveShell.prompt_in1 = CommandPrompt.get_format()
                config.InteractiveShell.prompt_in2 = CommandPrompt2.get_format()
                config.InteractiveShell.prompt_out = OutputPrompt.get_format()
            config.InteractiveShell.confirm_exit = False
            ipshell = IPShell(config=config,user_ns=__main__.__dict__,
                              banner1="",exit_msg="")
            if option.pdb:
                ipshell.call_pdb = True

            # Load Topographica IPython extension in embedded shell
            ipshell.extension_manager.load_extension('topo.misc.ipython')
            ipshell()

    global return_code
    if return_code != 0:
        cmdline_main.warning("Errors encountered; exiting with return code %d" % return_code)

    sys.exit(return_code)
Пример #13
0
    def __setstate__(self,state):
        """
        Execute the startup commands and set class attributes.
        """
        self.startup_commands = state['startup_commands']

        for cmd in self.startup_commands:
            exec cmd in __main__.__dict__

        to_restore = {}

        ########## pre-processing (renames, moves, etc)
        for class_path,state in state['class_attributes'].items():
            # from e.g. "topo.base.parameter.Parameter", we want "topo.base.parameter"

            if class_path in self.do_not_restore:
                #print "Did not restore:",class_path
                break

            for p_name,p_obj in state.items():
                if p_name in self.param_moves.get(class_path,{}):
                    assert p_name not in self.param_name_changes.get(class_path,{})

                    new_class_path,new_p_name = self.param_moves[class_path][p_name]

                    if new_class_path not in to_restore:
                        to_restore[new_class_path] = {}

                    Parameterized().message("%s.%s has been moved to %s.%s"%(class_path,p_name,new_class_path,new_p_name))
                    assert new_p_name not in to_restore[new_class_path]
                    to_restore[new_class_path][new_p_name]=p_obj


                elif p_name in self.param_name_changes.get(class_path,{}):
                    new_p_name = self.param_name_changes[class_path][p_name]

                    if class_path not in to_restore:
                        to_restore[class_path] = {}

                    Parameterized().message("%s's %s parameter has been renamed to %s."%(class_path,p_name,new_p_name))
                    to_restore[class_path][new_p_name] = p_obj

                else:
                    if class_path not in to_restore:
                        to_restore[class_path] = {}
                    to_restore[class_path][p_name]= p_obj


        ########## restoring
        for class_path in to_restore:
            module_path = class_path[0:class_path.rindex('.')]
            class_name = class_path[class_path.rindex('.')+1::]

            try:
                module = __import__(module_path,fromlist=[module_path])
            except:
                Parameterized().warning("Could not find module '%s' to restore parameter values of '%s' (module might have been moved or renamed; if you are using this module, please file a support request via topographica.org"%(module_path,class_path))
                break

            try:
                class_=getattr(module,class_name)
            except:
                Parameterized().warning("Could not find class '%s' to restore its parameter values (class might have been removed or renamed; if you are using this class, please file a support request via topographica.org)."%class_path)
                break

            for p_name,p_obj in to_restore[class_path].items():

                if p_name not in class_.params():
                    # CEBALERT: GlobalParams's source code never has
                    # parameters. If we move Parameter saving and
                    # restoring to Parameterized, could allow
                    # individual classes to customize Parameter
                    # restoration.
                    if class_.__name__!='GlobalParams':
                        Parameterized(name='load_snapshot').warning("%s.%s found in snapshot, but '%s' is no longer defined as a Parameter by the current version of %s. If you are using this class, please file a support request via topographica.org." % (class_.__name__, p_name,p_name,class_.__name__))
                else:
                    setattr(class_,p_name,p_obj)
Пример #14
0
 def get_param_values(self,onlychanged=False):
     if self.cached_get_param_values == None:
        Parameterized.__setattr__(self,'cached_get_param_values',Parameterized.get_param_values(self,onlychanged))
     return self.cached_get_param_values
Пример #15
0
launch an interactive graphical user interface; \
equivalent to -c 'from topo.misc.commandline import gui ; gui()'. \
Implies -a.""")

topo_parser.add_option("--pdb",
                       action="store_true",
                       dest="pdb",
                       help="""\
Automatically call the pdb debugger after every uncaught \
exception. See IPython documentation for further details.""")

# Keeps track of whether something has been performed, when deciding whether to assume -i
something_executed = False

# Dummy object used for user messages about OpenMP
openmp_main = Parameterized(name="OpenMP")


def c_action(option, opt_str, value, parser):
    """Callback function for the -c option."""
    #print "Processing %s '%s'" % (opt_str,value)
    exec value in __main__.__dict__
    global something_executed
    something_executed = True
    openmp_settings_names = [
        'openmp_threads', 'openmp_min_threads', 'openmp_max_threads'
    ]
    openmp_present = [
        True for k in openmp_settings_names if (k in __main__.__dict__)
    ]
    if openmp_present and parser.values.gui:
Пример #16
0
 def get_param_values(self,onlychanged=False):
     if self.cached_get_param_values == None:
        Parameterized.__setattr__(self,'cached_get_param_values',Parameterized.get_param_values(self,onlychanged))
     return self.cached_get_param_values