def default(self): """ Return a new default configuration object EXAMPLES:: sage: from sage.repl.configuration import sage_ipython_config sage: sage_ipython_config.default() {'InteractiveShell': {'colors': ... """ from sage.repl.interpreter import SageTerminalInteractiveShell cfg = Config( TerminalIPythonApp=Config( display_banner=False, verbose_crash=True, test_shell=False, shell_class=SageTerminalInteractiveShell, ), InteractiveShell=Config( prompts_class=SagePrompts, ast_node_interactivity='all', colors=self.colors(), simple_prompt=self.simple_prompt(), term_title=self.term_title(), confirm_exit=False, separate_in='' ), InteractiveShellApp=Config(extensions=[SAGE_EXTENSION]), ) if self._doctest_mode(): # Using the file-backed history causes problems in parallel tests cfg.HistoryManager = Config(hist_file=':memory:') return cfg
def test_fromdictmerge2(self): c1 = Config({"Foo": {"baz": 2}}) c2 = Config({"Foo": {"bar": 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1) self.assertEqual(c1.Foo.baz, 2) self.assertNotIn("baz", c2.Foo)
def test_fromdictmerge2(self): c1 = Config({'Foo' : {'baz' : 2}}) c2 = Config({'Foo' : {'bar' : 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1) self.assertEqual(c1.Foo.baz, 2) self.assertNotIn('baz', c2.Foo)
def shellwidget_config(self): """ Generate a Config instance for shell widgets using our config system This lets us create each widget with its own config (as opposed to IPythonQtConsoleApp, where all widgets have the same config) """ # ---- IPython config ---- try: profile_path = osp.join(get_ipython_dir(), 'profile_default') full_ip_cfg = load_pyconfig_files(['ipython_qtconsole_config.py'], profile_path) # From the full config we only select the IPythonWidget section # because the others have no effect here. ip_cfg = Config({'IPythonWidget': full_ip_cfg.IPythonWidget}) except: ip_cfg = Config() # ---- Spyder config ---- spy_cfg = Config() # Make the pager widget a rich one (i.e a QTextEdit) spy_cfg.IPythonWidget.kind = 'rich' # Gui completion widget completion_type_o = CONF.get('ipython_console', 'completion_type') completions = {0: "droplist", 1: "ncurses", 2: "plain"} spy_cfg.IPythonWidget.gui_completion = completions[completion_type_o] # Pager pager_o = self.get_option('use_pager') if pager_o: spy_cfg.IPythonWidget.paging = 'inside' else: spy_cfg.IPythonWidget.paging = 'none' # Calltips calltips_o = self.get_option('show_calltips') spy_cfg.IPythonWidget.enable_calltips = calltips_o # Buffer size buffer_size_o = self.get_option('buffer_size') spy_cfg.IPythonWidget.buffer_size = buffer_size_o # Prompts in_prompt_o = self.get_option('in_prompt') out_prompt_o = self.get_option('out_prompt') if in_prompt_o: spy_cfg.IPythonWidget.in_prompt = in_prompt_o if out_prompt_o: spy_cfg.IPythonWidget.out_prompt = out_prompt_o # Merge IPython and Spyder configs. Spyder prefs will have prevalence # over IPython ones ip_cfg._merge(spy_cfg) return ip_cfg
def test_merge_no_copies(self): c = Config() c2 = Config() c2.Foo.trait = [] c.merge(c2) c2.Foo.trait.append(1) self.assertIs(c.Foo, c2.Foo) self.assertEqual(c.Foo.trait, [1]) self.assertEqual(c2.Foo.trait, [1])
def test_auto_section(self): c = Config() self.assertNotIn("A", c) assert not c._has_section("A") A = c.A A.foo = "hi there" self.assertIn("A", c) assert c._has_section("A") self.assertEqual(c.A.foo, "hi there") del c.A self.assertEqual(c.A, Config())
def load_config_file(self, filename, path=None): """Load config files by filename and path.""" filename, ext = os.path.splitext(filename) new_config = Config() for config in self._load_config_files(filename, path=path, log=self.log, raise_config_file_errors=self.raise_config_file_errors, ): new_config.merge(config) # add self.cli_config to preserve CLI config priority new_config.merge(self.cli_config) self.update_config(new_config)
def test_auto_section(self): c = Config() self.assertNotIn('A', c) assert not c._has_section('A') A = c.A A.foo = 'hi there' self.assertIn('A', c) assert c._has_section('A') self.assertEqual(c.A.foo, 'hi there') del c.A self.assertEqual(c.A, Config())
def test_collision(self): a = Config() b = Config() self.assertEqual(a.collisions(b), {}) a.A.trait1 = 1 b.A.trait2 = 2 self.assertEqual(a.collisions(b), {}) b.A.trait1 = 1 self.assertEqual(a.collisions(b), {}) b.A.trait1 = 0 self.assertEqual(a.collisions(b), { 'A': { 'trait1': "1 ignored, using 0", } }) self.assertEqual(b.collisions(a), { 'A': { 'trait1': "0 ignored, using 1", } }) a.A.trait2 = 3 self.assertEqual(b.collisions(a), { 'A': { 'trait1': "0 ignored, using 1", 'trait2': "2 ignored, using 3", } })
def test_merge_exists(self): c1 = Config() c2 = Config() c1.Foo.bar = 10 c1.Foo.bam = 30 c2.Foo.bar = 20 c2.Foo.wow = 40 c1.merge(c2) self.assertEqual(c1.Foo.bam, 30) self.assertEqual(c1.Foo.bar, 20) self.assertEqual(c1.Foo.wow, 40) c2.Foo.Bam.bam = 10 c1.merge(c2) self.assertEqual(c1.Foo.Bam.bam, 10)
def load_default_config(ipython_dir=None): """Load the default config file from the default ipython_dir. This is useful for embedded shells. """ if ipython_dir is None: ipython_dir = get_ipython_dir() profile_dir = os.path.join(ipython_dir, 'profile_default') config = Config() for cf in Application._load_config_files("ipython_config", path=profile_dir): config.update(cf) return config
def test_deepcopy(self): c1 = Config() c1.Foo.bar = 10 c1.Foo.bam = 30 c1.a = 'asdf' c1.b = range(10) c1.Test.logger = logging.Logger('test') c1.Test.get_logger = logging.getLogger('test') c2 = copy.deepcopy(c1) self.assertEqual(c1, c2) self.assertTrue(c1 is not c2) self.assertTrue(c1.Foo is not c2.Foo) self.assertTrue(c1.Test is not c2.Test) self.assertTrue(c1.Test.logger is c2.Test.logger) self.assertTrue(c1.Test.get_logger is c2.Test.get_logger)
def test_custom(self): config = Config() config.foo = 'foo' config.bar = 'bar' c1 = Configurable(config=config) c2 = Configurable(config=c1.config) c3 = Configurable(config=c2.config) self.assertEqual(c1.config, config) self.assertEqual(c2.config, config) self.assertEqual(c3.config, config) # Test that copies are not made self.assertTrue(c1.config is config) self.assertTrue(c2.config is config) self.assertTrue(c3.config is config) self.assertTrue(c1.config is c2.config) self.assertTrue(c2.config is c3.config)
def test_collision(self): a = Config() b = Config() self.assertEqual(a.collisions(b), {}) a.A.trait1 = 1 b.A.trait2 = 2 self.assertEqual(a.collisions(b), {}) b.A.trait1 = 1 self.assertEqual(a.collisions(b), {}) b.A.trait1 = 0 self.assertEqual(a.collisions(b), {"A": {"trait1": "1 ignored, using 0"}}) self.assertEqual(b.collisions(a), {"A": {"trait1": "0 ignored, using 1"}}) a.A.trait2 = 3 self.assertEqual(b.collisions(a), {"A": {"trait1": "0 ignored, using 1", "trait2": "2 ignored, using 3"}})
def test_merge_doesnt_exist(self): c1 = Config() c2 = Config() c2.bar = 10 c2.Foo.bar = 10 c1.merge(c2) self.assertEqual(c1.Foo.bar, 10) self.assertEqual(c1.bar, 10) c2.Bar.bar = 10 c1.merge(c2) self.assertEqual(c1.Bar.bar, 10)
def test_setget(self): c = Config() c.a = 10 self.assertEqual(c.a, 10) self.assertEqual('b' in c, False)
The code in this file is deliberately extra-verbose, meant for learning.""" from __future__ import print_function # The basics to get you going: # IPython injects get_ipython into builtins, so you can know if you have nested # copies running. # Try running this code both at the command line and from inside IPython (with # %run example-embed.py) from traitlets.config.loader import Config try: get_ipython except NameError: nested = 0 cfg = Config() prompt_config = cfg.PromptManager prompt_config.in_template = 'In <\\#>: ' prompt_config.in2_template = ' .\\D.: ' prompt_config.out_template = 'Out<\\#>: ' else: print("Running nested copies of IPython.") print("The prompts for the nested copy have been modified") cfg = Config() nested = 1 # First import the embeddable shell class from IPython.terminal.embed import InteractiveShellEmbed # Now create an instance of the embeddable shell. The first argument is a # string with options exactly as you would type them if you were starting
def _get_tcp_km(): c = Config() km = AsyncMultiKernelManager(config=c) return km
def kernel_config(): """Create a config object with IPython kernel options.""" import ipykernel from IPython.core.application import get_ipython_dir from traitlets.config.loader import Config, load_pyconfig_files # ---- IPython config ---- try: profile_path = osp.join(get_ipython_dir(), 'profile_default') cfg = load_pyconfig_files(['ipython_config.py', 'ipython_kernel_config.py'], profile_path) except: cfg = Config() # ---- Spyder config ---- spy_cfg = Config() # Until we implement Issue 1052 spy_cfg.InteractiveShell.xmode = 'Plain' # - Using Jedi slow completions a lot for objects with big repr's. # - Jedi completions are not available in Python 2. if not PY2: spy_cfg.IPCompleter.use_jedi = False # Run lines of code at startup run_lines_o = os.environ.get('SPY_RUN_LINES_O') if run_lines_o is not None: spy_cfg.IPKernelApp.exec_lines = [x.strip() for x in run_lines_o.split(',')] else: spy_cfg.IPKernelApp.exec_lines = [] # Clean terminal arguments input clear_argv = "import sys;sys.argv = [''];del sys" spy_cfg.IPKernelApp.exec_lines.append(clear_argv) # Load %autoreload magic spy_cfg.IPKernelApp.exec_lines.append( "get_ipython().kernel._load_autoreload_magic()") # Default inline backend configuration # This is useful to have when people doesn't # use our config system to configure the # inline backend but want to use # '%matplotlib inline' at runtime if LooseVersion(ipykernel.__version__) < LooseVersion('4.5'): dpi_option = 'savefig.dpi' else: dpi_option = 'figure.dpi' spy_cfg.InlineBackend.rc = {'figure.figsize': (6.0, 4.0), dpi_option: 72, 'font.size': 10, 'figure.subplot.bottom': .125, 'figure.facecolor': 'white', 'figure.edgecolor': 'white'} # Pylab configuration mpl_backend = None pylab_o = os.environ.get('SPY_PYLAB_O') if pylab_o == 'True' and is_module_installed('matplotlib'): # Set Matplotlib backend backend_o = os.environ.get('SPY_BACKEND_O') if backend_o is not None: if backend_o == '1': if is_module_installed('PyQt5'): auto_backend = 'qt5' elif is_module_installed('PyQt4'): auto_backend = 'qt4' elif is_module_installed('_tkinter'): auto_backend = 'tk' else: auto_backend = 'inline' else: auto_backend = '' backends = {'0': 'inline', '1': auto_backend, '2': 'qt5', '3': 'qt4', '4': 'osx', '5': 'gtk3', '6': 'gtk', '7': 'wx', '8': 'tk'} mpl_backend = backends[backend_o] # Automatically load Pylab and Numpy, or only set Matplotlib # backend autoload_pylab_o = os.environ.get('SPY_AUTOLOAD_PYLAB_O') == 'True' command = "get_ipython().kernel._set_mpl_backend('{0}', {1})" spy_cfg.IPKernelApp.exec_lines.append( command.format(mpl_backend, autoload_pylab_o)) # Inline backend configuration if mpl_backend == 'inline': # Figure format format_o = os.environ.get('SPY_FORMAT_O') formats = {'0': 'png', '1': 'svg'} if format_o is not None: spy_cfg.InlineBackend.figure_format = formats[format_o] # Resolution resolution_o = os.environ.get('SPY_RESOLUTION_O') if resolution_o is not None: spy_cfg.InlineBackend.rc[dpi_option] = float(resolution_o) # Figure size width_o = float(os.environ.get('SPY_WIDTH_O')) height_o = float(os.environ.get('SPY_HEIGHT_O')) if width_o is not None and height_o is not None: spy_cfg.InlineBackend.rc['figure.figsize'] = (width_o, height_o) # Enable Cython magic if is_module_installed('Cython'): spy_cfg.IPKernelApp.exec_lines.append('%load_ext Cython') # Run a file at startup use_file_o = os.environ.get('SPY_USE_FILE_O') run_file_o = os.environ.get('SPY_RUN_FILE_O') if use_file_o == 'True' and run_file_o is not None: spy_cfg.IPKernelApp.file_to_run = run_file_o # Autocall autocall_o = os.environ.get('SPY_AUTOCALL_O') if autocall_o is not None: spy_cfg.ZMQInteractiveShell.autocall = int(autocall_o) # To handle the banner by ourselves in IPython 3+ spy_cfg.ZMQInteractiveShell.banner1 = '' # Greedy completer greedy_o = os.environ.get('SPY_GREEDY_O') == 'True' spy_cfg.IPCompleter.greedy = greedy_o # Sympy loading sympy_o = os.environ.get('SPY_SYMPY_O') == 'True' if sympy_o and is_module_installed('sympy'): lines = sympy_config(mpl_backend) spy_cfg.IPKernelApp.exec_lines.append(lines) # Merge IPython and Spyder configs. Spyder prefs will have prevalence # over IPython ones cfg._merge(spy_cfg) return cfg
def _get_tcp_km_sub(): c = Config() km = AsyncMKMSubclass(config=c) return km
def _get_ipc_km(): c = Config() c.KernelManager.transport = "ipc" c.KernelManager.ip = "test" km = AsyncMultiKernelManager(config=c) return km
def test_fromdictmerge(self): c1 = Config() c2 = Config({'Foo' : {'bar' : 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1)
def test_builtin(self): c1 = Config() c1.format = "json"
def config(self, s): """configure IPython %config Class[.trait=value] This magic exposes most of the IPython config system. Any Configurable class should be able to be configured with the simple line:: %config Class.trait=value Where `value` will be resolved in the user's namespace, if it is an expression or variable name. Examples -------- To see what classes are available for config, pass no arguments:: In [1]: %config Available objects for config: TerminalInteractiveShell HistoryManager PrefilterManager AliasManager IPCompleter DisplayFormatter To view what is configurable on a given class, just pass the class name:: In [2]: %config IPCompleter IPCompleter options ----------------- IPCompleter.omit__names=<Enum> Current: 2 Choices: (0, 1, 2) Instruct the completer to omit private method names Specifically, when completing on ``object.<tab>``. When 2 [default]: all names that start with '_' will be excluded. When 1: all 'magic' names (``__foo__``) will be excluded. When 0: nothing will be excluded. IPCompleter.merge_completions=<CBool> Current: True Whether to merge completion results into a single list If False, only the completion results from the first non-empty completer will be returned. IPCompleter.limit_to__all__=<CBool> Current: False Instruct the completer to use __all__ for the completion Specifically, when completing on ``object.<tab>``. When True: only those names in obj.__all__ will be included. When False [default]: the __all__ attribute is ignored IPCompleter.greedy=<CBool> Current: False Activate greedy completion This will enable completion on elements of lists, results of function calls, etc., but can be unsafe because the code is actually evaluated on TAB. but the real use is in setting values:: In [3]: %config IPCompleter.greedy = True and these values are read from the user_ns if they are variables:: In [4]: feeling_greedy=False In [5]: %config IPCompleter.greedy = feeling_greedy """ from traitlets.config.loader import Config # some IPython objects are Configurable, but do not yet have # any configurable traits. Exclude them from the effects of # this magic, as their presence is just noise: configurables = sorted(set([ c for c in self.shell.configurables if c.__class__.class_traits(config=True) ]), key=lambda x: x.__class__.__name__) classnames = [c.__class__.__name__ for c in configurables] line = s.strip() if not line: # print available configurable names print("Available objects for config:") for name in classnames: print(" ", name) return elif line in classnames: # `%config TerminalInteractiveShell` will print trait info for # TerminalInteractiveShell c = configurables[classnames.index(line)] cls = c.__class__ help = cls.class_get_help(c) # strip leading '--' from cl-args: help = re.sub(re.compile(r'^--', re.MULTILINE), '', help) print(help) return elif reg.match(line): cls, attr = line.split('.') return getattr(configurables[classnames.index(cls)], attr) elif '=' not in line: msg = "Invalid config statement: %r, "\ "should be `Class.trait = value`." ll = line.lower() for classname in classnames: if ll == classname.lower(): msg = msg + '\nDid you mean %s (note the case)?' % classname break raise UsageError(msg % line) # otherwise, assume we are setting configurables. # leave quotes on args when splitting, because we want # unquoted args to eval in user_ns cfg = Config() exec("cfg." + line, locals(), self.shell.user_ns) for configurable in configurables: try: configurable.update_config(cfg) except Exception as e: error(e)
def kernel_config(): """Create a config object with IPython kernel options.""" from IPython.core.application import get_ipython_dir from traitlets.config.loader import Config, load_pyconfig_files if not IS_EXT_INTERPRETER: from spyder.config.main import CONF from spyder.utils.programs import is_module_installed else: # We add "spyder" to sys.path for external interpreters, # so this works! # See create_kernel_spec of plugins/ipythonconsole from config.main import CONF from utils.programs import is_module_installed # ---- IPython config ---- try: profile_path = osp.join(get_ipython_dir(), 'profile_default') cfg = load_pyconfig_files(['ipython_config.py', 'ipython_kernel_config.py'], profile_path) except: cfg = Config() # ---- Spyder config ---- spy_cfg = Config() # Until we implement Issue 1052 spy_cfg.InteractiveShell.xmode = 'Plain' spy_cfg.IPCompleter.use_jedi = False # Run lines of code at startup run_lines_o = CONF.get('ipython_console', 'startup/run_lines') if run_lines_o: spy_cfg.IPKernelApp.exec_lines = [x.strip() for x in run_lines_o.split(',')] else: spy_cfg.IPKernelApp.exec_lines = [] # Clean terminal arguments input clear_argv = "import sys;sys.argv = [''];del sys" spy_cfg.IPKernelApp.exec_lines.append(clear_argv) # Pylab configuration mpl_backend = None mpl_installed = is_module_installed('matplotlib') pylab_o = CONF.get('ipython_console', 'pylab') if mpl_installed and pylab_o: # Get matplotlib backend backend_o = CONF.get('ipython_console', 'pylab/backend') if backend_o == 1: if is_module_installed('PyQt5'): auto_backend = 'qt5' elif is_module_installed('PyQt4'): auto_backend = 'qt4' elif is_module_installed('_tkinter'): auto_backend = 'tk' else: auto_backend = 'inline' else: auto_backend = '' backends = {0: 'inline', 1: auto_backend, 2: 'qt5', 3: 'qt4', 4: 'osx', 5: 'gtk3', 6: 'gtk', 7: 'wx', 8: 'tk'} mpl_backend = backends[backend_o] # Automatically load Pylab and Numpy, or only set Matplotlib # backend autoload_pylab_o = CONF.get('ipython_console', 'pylab/autoload') if autoload_pylab_o: spy_cfg.IPKernelApp.exec_lines.append( "%pylab {0}".format(mpl_backend)) else: spy_cfg.IPKernelApp.exec_lines.append( "%matplotlib {0}".format(mpl_backend)) # Inline backend configuration if mpl_backend == 'inline': # Figure format format_o = CONF.get('ipython_console', 'pylab/inline/figure_format', 0) formats = {0: 'png', 1: 'svg'} spy_cfg.InlineBackend.figure_format = formats[format_o] # Resolution if is_module_installed('ipykernel', '<4.5'): dpi_option = 'savefig.dpi' else: dpi_option = 'figure.dpi' spy_cfg.InlineBackend.rc = {'figure.figsize': (6.0, 4.0), dpi_option: 72, 'font.size': 10, 'figure.subplot.bottom': .125, 'figure.facecolor': 'white', 'figure.edgecolor': 'white'} resolution_o = CONF.get('ipython_console', 'pylab/inline/resolution') spy_cfg.InlineBackend.rc[dpi_option] = resolution_o # Figure size width_o = float(CONF.get('ipython_console', 'pylab/inline/width')) height_o = float(CONF.get('ipython_console', 'pylab/inline/height')) spy_cfg.InlineBackend.rc['figure.figsize'] = (width_o, height_o) # Enable Cython magic if is_module_installed('Cython'): spy_cfg.IPKernelApp.exec_lines.append('%load_ext Cython') # Run a file at startup use_file_o = CONF.get('ipython_console', 'startup/use_run_file') run_file_o = CONF.get('ipython_console', 'startup/run_file') if use_file_o and run_file_o: spy_cfg.IPKernelApp.file_to_run = run_file_o # Autocall autocall_o = CONF.get('ipython_console', 'autocall') spy_cfg.ZMQInteractiveShell.autocall = autocall_o # To handle the banner by ourselves in IPython 3+ spy_cfg.ZMQInteractiveShell.banner1 = '' # Greedy completer greedy_o = CONF.get('ipython_console', 'greedy_completer') spy_cfg.IPCompleter.greedy = greedy_o # Sympy loading sympy_o = CONF.get('ipython_console', 'symbolic_math') if sympy_o and is_module_installed('sympy'): lines = sympy_config(mpl_backend) spy_cfg.IPKernelApp.exec_lines.append(lines) # Merge IPython and Spyder configs. Spyder prefs will have prevalence # over IPython ones cfg._merge(spy_cfg) return cfg
def test_fromdictmerge(self): c1 = Config() c2 = Config({"Foo": {"bar": 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1)
src = 'nbextensions' destination = os.path.join(data_dir, 'nbextensions') print("Install notebook extensions to %s" % destination) recursive_overwrite(src, destination) # # 3. Update nbconvert configuration # json_config = os.path.join(jupyter_config_dir(), 'jupyter_nbconvert_config.json') print("Configuring %s" % json_config) if os.path.isfile(json_config) is True: cl = JSONFileConfigLoader(json_config) config = cl.load_config() else: config = Config() newconfig = Config() # Set template path, pre- and postprocessors of notebook extensions newconfig.Exporter.template_path = [os.path.join(data_dir, 'templates')] newconfig.Exporter.preprocessors = ["pre_codefolding.CodeFoldingPreprocessor", "pre_pymarkdown.PyMarkdownPreprocessor"] newconfig.NbConvertApp.postprocessor_class = 'post_embedhtml.EmbedPostProcessor' config.merge(newconfig) config.version = 1 s=json.dumps(config, indent=2, separators=(',', ': '), sort_keys=True) with open(json_config, 'w') as f: f.write(s) py_config = os.path.join(jupyter_config_dir(), 'jupyter_nbconvert_config.py') print("Configuring %s" % py_config) new_py_config = 'jupyter_nbconvert_config.py'
def kernel_config(): """Create a config object with IPython kernel options""" import os from IPython.core.application import get_ipython_dir from spyderlib.config.main import CONF from spyderlib.utils.programs import is_module_installed from traitlets.config.loader import Config, load_pyconfig_files # ---- IPython config ---- try: profile_path = osp.join(get_ipython_dir(), 'profile_default') ip_cfg = load_pyconfig_files(['ipython_config.py', 'ipython_qtconsole_config.py'], profile_path) except: ip_cfg = Config() # ---- Spyder config ---- spy_cfg = Config() # Until we implement Issue 1052 spy_cfg.InteractiveShell.xmode = 'Plain' # Run lines of code at startup run_lines_o = CONF.get('ipython_console', 'startup/run_lines') if run_lines_o: spy_cfg.IPKernelApp.exec_lines = [x.strip() for x in run_lines_o.split(',')] else: spy_cfg.IPKernelApp.exec_lines = [] # Pylab configuration mpl_backend = None mpl_installed = is_module_installed('matplotlib') pylab_o = CONF.get('ipython_console', 'pylab') external_interpreter = \ os.environ.get('EXTERNAL_INTERPRETER', '').lower() == "true" if mpl_installed and pylab_o: # Get matplotlib backend if not external_interpreter: if os.environ["QT_API"] == 'pyqt5': qt_backend = 'qt5' else: qt_backend = 'qt' backend_o = CONF.get('ipython_console', 'pylab/backend', 0) backends = {0: 'inline', 1: qt_backend, 2: qt_backend, 3: 'osx', 4: 'gtk', 5: 'wx', 6: 'tk'} mpl_backend = backends[backend_o] else: mpl_backend = 'inline' # Automatically load Pylab and Numpy, or only set Matplotlib # backend autoload_pylab_o = CONF.get('ipython_console', 'pylab/autoload') if autoload_pylab_o: spy_cfg.IPKernelApp.exec_lines.append( "%pylab {0}".format(mpl_backend)) else: spy_cfg.IPKernelApp.exec_lines.append( "%matplotlib {0}".format(mpl_backend)) # Inline backend configuration if backends[backend_o] == 'inline': # Figure format format_o = CONF.get('ipython_console', 'pylab/inline/figure_format', 0) formats = {0: 'png', 1: 'svg'} spy_cfg.InlineBackend.figure_format = formats[format_o] # Resolution spy_cfg.InlineBackend.rc = {'figure.figsize': (6.0, 4.0), 'savefig.dpi': 72, 'font.size': 10, 'figure.subplot.bottom': .125, 'figure.facecolor': 'white', 'figure.edgecolor': 'white' } resolution_o = CONF.get('ipython_console', 'pylab/inline/resolution') spy_cfg.InlineBackend.rc['savefig.dpi'] = resolution_o # Figure size width_o = float(CONF.get('ipython_console', 'pylab/inline/width')) height_o = float(CONF.get('ipython_console', 'pylab/inline/height')) spy_cfg.InlineBackend.rc['figure.figsize'] = (width_o, height_o) # Run a file at startup use_file_o = CONF.get('ipython_console', 'startup/use_run_file') run_file_o = CONF.get('ipython_console', 'startup/run_file') if use_file_o and run_file_o: spy_cfg.IPKernelApp.file_to_run = run_file_o # Autocall autocall_o = CONF.get('ipython_console', 'autocall') spy_cfg.ZMQInteractiveShell.autocall = autocall_o # To handle the banner by ourselves in IPython 3+ spy_cfg.ZMQInteractiveShell.banner1 = '' # Greedy completer greedy_o = CONF.get('ipython_console', 'greedy_completer') spy_cfg.IPCompleter.greedy = greedy_o # Sympy loading sympy_o = CONF.get('ipython_console', 'symbolic_math') if sympy_o: lines = sympy_config(mpl_backend) spy_cfg.IPKernelApp.exec_lines.append(lines) # Merge IPython and Spyder configs. Spyder prefs will have prevalence # over IPython ones ip_cfg._merge(spy_cfg) return ip_cfg
def test_flasherflatfieldcalculator(): """test of flasherFlatFieldCalculator""" tel_id = 0 n_gain = 2 n_events = 10 n_pixels = 1855 ff_level = 10000 subarray = SubarrayDescription( "test array", tel_positions={0: np.zeros(3) * u.m}, tel_descriptions={ 0: TelescopeDescription.from_name( optics_name="SST-ASTRI", camera_name="CHEC" ), }, ) subarray.tel[0].camera.readout.reference_pulse_shape = np.ones((1, 2)) subarray.tel[0].camera.readout.reference_pulse_sample_width = u.Quantity(1, u.ns) config = Config({"FixedWindowSum": { "peak_index": 15, "window_shift": 0, "window_width": 10, }}) ff_calculator = FlasherFlatFieldCalculator( subarray=subarray, charge_product="FixedWindowSum", sample_size=n_events, tel_id=tel_id, config=config, ) # create one event data = EventAndMonDataContainer() data.meta["origin"] = "test" # initialize mon and r1 data data.mon.tel[tel_id].pixel_status.hardware_failing_pixels = np.zeros( (n_gain, n_pixels), dtype=bool ) data.mon.tel[tel_id].pixel_status.pedestal_failing_pixels = np.zeros( (n_gain, n_pixels), dtype=bool ) data.mon.tel[tel_id].pixel_status.flatfield_failing_pixels = np.zeros( (n_gain, n_pixels), dtype=bool ) data.r1.tel[tel_id].waveform = np.zeros((n_gain, n_pixels, 40)) data.r1.tel[tel_id].trigger_time = 1000 # flat-field signal put == delta function of height ff_level at sample 20 data.r1.tel[tel_id].waveform[:, :, 20] = ff_level print(data.r1.tel[tel_id].waveform[0, 0, 20]) # First test: good event while ff_calculator.num_events_seen < n_events: if ff_calculator.calculate_relative_gain(data): assert data.mon.tel[tel_id].flatfield print(data.mon.tel[tel_id].flatfield) assert np.mean(data.mon.tel[tel_id].flatfield.charge_median) == ff_level assert np.mean(data.mon.tel[tel_id].flatfield.relative_gain_median) == 1 assert np.mean(data.mon.tel[tel_id].flatfield.relative_gain_std) == 0 # Second test: introduce some failing pixels failing_pixels_id = np.array([10, 20, 30, 40]) data.r1.tel[tel_id].waveform[:, failing_pixels_id, :] = 0 data.mon.tel[tel_id].pixel_status.pedestal_failing_pixels[ :, failing_pixels_id ] = True while ff_calculator.num_events_seen < n_events: if ff_calculator.calculate_relative_gain(data): # working pixel have good gain assert data.mon.tel[tel_id].flatfield.relative_gain_median[0, 0] == 1 # bad pixels do non influence the gain assert np.mean(data.mon.tel[tel_id].flatfield.relative_gain_std) == 0