コード例 #1
0
    def ipython_break(frame, depth, #pylint: disable-msg=W0613
                      color_scheme='Linux'):
        '''Start an interactive debugger session based on the IPython
        debugger'''
        #This code is a pretty dirty hack to get around some obscure
        #IPython issue
        global IPYTHON_TRACER

        if not IPYTHON_TRACER:
            # This is the first time this procedure is called
            # Due to some funky IPython 0.10 internals, we need the following:
            import IPython

            if map(int, IPython.__version__.split('.'))[:2] == [0, 10]:
                from IPython.Shell import IPShellEmbed
                IPShellEmbed(argv=[])

        old_colors = ExceptionColors.active_scheme_name
        ExceptionColors.active_scheme_name = color_scheme

        if not IPYTHON_TRACER:
            IPYTHON_TRACER = Tracer(color_scheme)

        IPYTHON_TRACER.debugger.set_trace(frame)
        ExceptionColors.active_scheme_name = old_colors
コード例 #2
0
def debug():
    """
    Invokes a debugger at the point where it is called, but only if
    the Ipython-shell is available.
    """

    if HaveIpython:
        Tracer()
    else:
        return
コード例 #3
0
ファイル: utils.py プロジェクト: personalrobotics/moped
def breakpoint():
    """BREAKPOINT - Set breakpoint. Opens an IPDB window when the next
        exception is raised.
        
        Usage: breakpoint()

        NOTE: In order for the breakpoints to do anything, you need to 
        execute your script with 'ipython -pdb YOUR_SCRIPT.py'.

    """
    from IPython.Debugger import Tracer

    Tracer()
コード例 #4
0
ファイル: utils.py プロジェクト: personalrobotics/moped
def breaknow():
    """BREAKNOW - Set unconditional breakpoint. Raises an exception and
        opens the IPDB shell.
        
        Usage: breaknow()

        NOTE: In order for the breakpoints to do anything, you need to 
        execute your script with 'ipython -pdb YOUR_SCRIPT.py'.

    """
    from IPython.Debugger import Tracer

    Tracer()
    raise KeyError, "You set an unconditional breakpoint here, so we stop."
コード例 #5
0
"""
Program for running bwa on nuclear reads from 5 different cell lines

"""
from IPython.Debugger import Tracer
debug = Tracer()

from subprocess import Popen, call

import os
import glob
# You tested having on read pair in the index and another not -- that works
# fine. You must also try the funny faste file output by bedtools.


def get_dsets(dset_dir, cell_lines):
    """
    Return the paths to the datsets for nuclear rna-seq
    """

    dsets = {}
    for line in glob.glob(dset_dir + '/*'):

        # I'm cheating for now!
        #for line in open('those_files', 'rb'):

        if 'NucleusPap' not in line or 'fastq' not in line:
            continue

        dname, fname = os.path.split(line.rstrip())
コード例 #6
0
class mymagic:

	if sys.platform.lower().startswith('win'):
		if hasattr(__IPYTHON__, 'rc'): __IPYTHON__.rc.editor = 'scite'
		else: __IPYTHON__.editor = 'scite'
		
	############################################################################

	try: from IPython.Debugger import Tracer # IPython v 0.10 and earlier
	except ImportError: from IPython.core.debugger import Tracer # IPython v 0.11 and later
	__IPYTHON__.dbstop = Tracer()
	# calling __IPYTHON__.dbstop()  is easier than having to remember and type
	# "from IPython.Debugger import Tracer; Tracer()()" just to invoke the debugger
	# (and *much* easier than having to remember the version-specific variants of that)
	
	############################################################################
	@makemagic
	def magic_edit(fn):
		"""\
A more Matlab-like replacement for IPython's default editing behaviour:
search the path for possible matches; edit them in the background; don't
execute the result.  Set your editor with the line 'editor WHATEVER' in
ipythonrc, or by otherwise setting __IPYTHON__.rc.editor = 'WHATEVER'

Examples:

edit site.py          #  edits existing file on path with .py extension     $BLAH/Lib/site.py
edit ipythonrc        #  edits existing file on path without .py extension  $HOME/.ipython/ipythonrc
edit site             #  adds .py extension and edits existing file on path $BLAH/Lib/site.py

edit IPython/iplib.py #  edits existing file in subdirectory on path $BLAH/IPython/iplib.py
edit IPython/iplib    #  edits existing file in subdirectory on path $BLAH/IPython/iplib.py
edit IPython.iplib    #  edits existing file in package on path      $BLAH/IPython/iplib.py
edit xml              #  edits __init__.py file in existing package on path $BLAH/xml/__init__.py

edit foo.py           #  edits new file with .py extension     ./foo.py
edit foo.txt          #  edits new file with non-.py extension ./foo.txt
edit foo              #  adds .py extension and edits new file ./foo.py

edit IPython/foo.py   #  edits new file in existing subdirectory on path $BLAH/IPython/foo.py
edit IPython/foo      #  edits new file in existing subdirectory on path $BLAH/IPython/foo.py
edit IPython.foo      #  edits new file in existing subdirectory on path $BLAH/IPython/foo.py

		"""###

		# NB: one (unlikely) case is not handled:  a new file with a non-.py extension
		#     in an existing subdirectory on the path, e.g. edit IPython/foo.txt

		if hasattr(__IPYTHON__, 'rc'): editor = __IPYTHON__.rc.editor
		else: editor = __IPYTHON__.editor
		
		if sys.platform.lower().startswith('win'): editor = 'start ' + editor
		ed = lambda x: os.system(editor + ' "' + os.path.abspath(x) + '"')
		got = lambda x: os.path.isfile(os.path.abspath(x)) and (ed(x) or True)
				
		if got(fn): return
		# this allows for spaces in the path, e.g.  edit C:\Documents and Settings\me\Desktop\blah.py
		# but if the whole thing isn't an existing file, assume a space-delimited list of filenames:
		
		for original in fn.split():
			stem = original
			if stem.lower().endswith('.py'): stem = stem[:-3]
			original_withpy = stem + '.py'
			path_withoutpy = os.path.join(*stem.split('.'))
			path_withpy = path_withoutpy + '.py'
			potential_location = ''
			for p in sys.path:
				if   got(os.path.join(p, original)): break
				elif got(os.path.join(p, original_withpy)): break
				elif got(os.path.join(p, path_withpy)): break
				elif got(os.path.join(p, path_withoutpy)): break
				elif stem == original and got(os.path.join(p, path_withoutpy, '__init__.py')): break
				elif potential_location == '':
					pp = os.path.join(p, path_withoutpy)
					dd = os.path.realpath(os.path.dirname(pp))
					d  = os.path.realpath(p)
					if len(dd) > len(d) and os.path.isdir(dd): potential_location = pp
			else: # not found anywhere else, so edit as new
				if potential_location == '': potential_location = original
				fn, xtn = os.path.splitext(potential_location)
				if xtn == '': xtn = '.py'
				ed(fn + xtn)
		
	############################################################################
	@makemagic
	def magic_addpath(d=None):
		"""\
Make absolute paths out of the input(s) and append them to sys.path if they are
not already there.  Supply a space-delimited list of arguments, or one argument
enclosed in quotes (which may, in that case, contain a space).
"""###
		if d == None: dd = (os.getcwd(),)
		else:
			if (d.startswith('"') and d.endswith('"')) or (d.startswith("'") and d.endswith("'")):
				dd = (d[1:-1],)
			else:
				dd = d.split()
		for d in dd:
			d = os.path.realpath(d)
			if os.path.isdir(d):
				if not d in sys.path: sys.path.append(d)
			else:
				print 'no such directory "%s"' % d
		
	############################################################################
	@makemagic
	def magic_desk(subdir=''):
		"""\
cd to the Desktop, but keep a global record of where we were (stored in
__IPYTHON__.undeskdir) so that we can snap back with %undesk
"""###
		if sys.platform.lower().startswith('win'):
			homedrive = os.environ.get('HOMEDRIVE')
			homepath = os.environ.get('HOMEPATH')
			userprofile = os.environ.get('USERPROFILE')
			if userprofile == None:
				d = os.path.join(homedrive, homepath, 'Desktop')
			else:
				d = os.path.join(userprofile, 'Desktop')
		else:
			d = os.path.join(os.environ.get('HOME'), 'Desktop')
		if subdir != '':
			return os.path.realpath(os.path.join(d, subdir))
		here = os.path.realpath(os.curdir)
		if os.path.realpath(d) == here:
			print "already at desktop"
		else:
			__IPYTHON__.undeskdir = here
			print 'changing directory from ' + here
			print '                     to ' + d
			print 'type %undesk to change back'
			os.chdir(d)
		
	############################################################################
	@makemagic
	def magic_undesk(arg):
		"""\
Undo a previous call to %desk
"""###
		d = getattr(__IPYTHON__, 'undeskdir', None)
		if d == None:
			print 'no global record of previous call to %desk'
		else:
			print 'changing directory from ' + os.path.realpath(os.curdir)
			print '                back to ' + d
			os.chdir(d)
	
	
	############################################################################
	@makemagic
	def magic_pip(d=''):
		try:
			import pip
		except ImportError:
			import sys; sys.stderr.write("""
The pip module is not installed. See the instructions
at http://www.pip-installer.org/en/latest/installing.html
In short:
(1) Ensure setuptools (i.e. easy_install) is installed first
    (download and run ez_setup.py if not).
(2) Download and run get-pip.py
(3) Quit and relaunch Python
(4) Then the first thing you should do with pip is:
    pip install --upgrade setuptools
""")
			return
		try:
			return pip.main(d.split())
		except SystemExit:
			pass
		
	############################################################################
	@makemagic
	def magic_easy_install(d=None):
		"""\
Run the easy_install command from IPython, invoking the setuptools script
Scripts/easy_install-script.py (on Windows) or easy_install (on anything else).
Particularly useful on Windows, where IPython may well be the best command-line
you have.
"""###
		def readpth(pth):
			sitedir = os.path.split(pth)[0] # assume easy-install.pth is directly in the site-packages dir
			lines = [x.strip() for x in open(pth, 'rt').readlines()]
			return  [os.path.realpath(os.path.join(sitedir, x)) for x in lines if not x.startswith('import') and not x.startswith('#')]
			# assume a line is a relative path unless it begins with # or import

		def isbelow(x, p):
			x,p = os.path.realpath(x), os.path.realpath(p)
			return x == p or x.startswith(os.path.join(p, ''))

		pth = [os.path.join(os.path.realpath(x), 'easy-install.pth') for x in sys.path] # assume 'easy-install.pth' is on the path
		pth = ([x for x in pth if os.path.isfile(x)]+[None])[0]
		if pth != None: oldpaths = readpth(pth)
		
		############ This is the part that actually runs easy_install.
		############ The rest, before and after, is to attempt to keep the session going with intelligent updates to
		############ sys.path and sys.modules (still won't work for upgrades to IPython and some messy others like simplejson).
		home = os.environ.get('PYTHONHOME')
		if sys.platform.lower().startswith('win'):
			if home == None: home = os.path.dirname(sys.executable)
			getmagic('run')('"' + os.path.join(home, 'Scripts', 'easy_install-script.py') + '" ' + d)
		else:
			# TODO: need to set $PYTHONHOME for system call using env?  If so, how to fill it in if home==None?
			os.system('"' + sys.executable + '" "`which easy_install`" ' + d)
		############
		
		if pth == None:
			print "restart this python session to take advantage of the new package"
			return
			
		newpaths = readpth(pth)
		addpaths = [x for x in newpaths if x not in oldpaths]
		gone     = [x for x in oldpaths if x not in newpaths]
		rmpaths  = []
		for x in sys.path:
			if not len(x): continue
			for stem in gone:
				if isbelow(x,stem):
					rmpaths.append(x)
					break
		stripped = [x for x in sys.path if x not in rmpaths]
		for i in range(len(stripped)):
			if len(stripped[i]): stripped[i] = os.path.realpath(stripped[i])
		sys.path[:] = stripped + addpaths
		if len(addpaths): print('\n  '.join(['Added to sys.path:'] + map(repr, addpaths)))
		if len(rmpaths):  print('\n  '.join(['Removed from sys.path:'] + map(repr, rmpaths)))
		rmmod = []
		for k,v in sys.modules.items():
			v = getattr(v, '__file__', None)
			if not isinstance(v, basestring): continue
			for p in rmpaths:
				if isbelow(v, p): rmmod.append(k); break
		rmmod = [sys.modules.pop(k) for k in sorted(rmmod)]
		if len(rmmod):  print('\n  '.join(['Removed from sys.modules:'] + map(repr, rmmod)))
		
	############################################################################
	@makemagic
	def magic_reimport(dd):
		"""\
The syntax

    %reimport foo, bar.*

is a shortcut for the following:

    import foo; foo = reload(foo)
    import bar; bar = reload(bar); from bar import *
"""###
		ipython = getworkspace()
		for d in dd.replace(',', ' ').split(' '):
			if len(d):
				bare = d.endswith('.*')
				if bare: d = d[:-2]
				exec 'import xx; xx = reload(xx)'.replace('xx', d) in ipython
				if bare: exec 'from xx import *'.replace('xx', d) in ipython
		
	
		
	############################################################################
	@makemagic
	def magic_loadpylab(d=''):
		"""\
The syntax

	%loadpylab

imports matplotlib and pylab into the IPython workspace, ensuring that, if matplotlib
has not previously been loaded, it is loaded in interactive mode.

	%loadpylab *

is the same, but additionally imports the requested symbols from pylab, directly into
the workspace.

"""###
		import sys
		ipython = getworkspace()
		try: import matplotlib
		except ImportError: print "WARNING: failed to import matplotlib"
		else:
コード例 #7
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
import os
import sys
import numpy as np
import numpy.lib.recfunctions as rec
import pymc as pm
from copy import copy
from scipy.stats import norm
from scipy.integrate import quad
try:
    from IPython.Debugger import Tracer
    debug_here = Tracer()
except:

    def debug_here():
        pass


#for import settings file
from kabuki import Hierarchical, Knode
from kabuki.utils import load_csv
import stop_likelihoods_wtf
from collections import OrderedDict

#settings file
path__analysisDescription = sys.argv[3]

#make dict for input arguments
vars = dict()
コード例 #8
0
ファイル: Shortcuts.py プロジェクト: neurotechcenter/BCpy2000
class MyMagics(Magics):

    if sys.platform.lower().startswith('win'):
        if hasattr(__IPYTHON__, 'rc'): __IPYTHON__.rc.editor = 'scite'
        else: __IPYTHON__.editor = 'scite'

    ############################################################################

    dbstop = None
    try:
        if not dbstop:
            from IPython.Debugger import Tracer
            dbstop = Tracer()  # IPython v 0.10 and earlier
    except:
        pass
    try:
        if not dbstop:
            from IPython.core.debugger import Tracer
            dbstop = Tracer()  # IPython v 0.11 and later
    except:
        pass
    try:
        if not dbstop:
            from IPython.core import debugger
            dbstop = debugger.Pdb(
            ).set_trace  # Some other f***ing version of IWishYoudStopMovingTheGoalposts
    except:
        pass

    sys.dbstop = __IPYTHON__.dbstop = dbstop
    # because calling sys.dbstop()  is easier than having to remember and type
    # "from IPython.Debugger import Tracer; Tracer()()" just to invoke the debugger
    # (and *much* easier than having to remember the version-specific variants of that)
    # Note however that you can also run in debug mode and set breakpoints with:
    #   %run -d -b LINENUMBER file.py
    #   %run -d -b OTHERFILENAME:LINENUMBER file.py

    ############################################################################
    @line_magic
    def edit(self, arg):
        """\
A more Matlab-like replacement for IPython's default editing behaviour:
search the path for possible matches; edit them in the background; don't
execute the result (NB: the latter can also be achieved in IPython with
%edit -x).  Set your editor with the line 'editor WHATEVER' in
ipythonrc, or by otherwise setting __IPYTHON__.rc.editor = 'WHATEVER'

Examples:

edit site.py          #  edits existing file on path with .py extension     $BLAH/Lib/site.py
edit ipythonrc        #  edits existing file on path without .py extension  $HOME/.ipython/ipythonrc
edit site             #  adds .py extension and edits existing file on path $BLAH/Lib/site.py

edit IPython/iplib.py #  edits existing file in subdirectory on path $BLAH/IPython/iplib.py
edit IPython/iplib    #  edits existing file in subdirectory on path $BLAH/IPython/iplib.py
edit IPython.iplib    #  edits existing file in package on path      $BLAH/IPython/iplib.py
edit xml              #  edits __init__.py file in existing package on path $BLAH/xml/__init__.py

edit foo.py           #  edits new file with .py extension     ./foo.py
edit foo.txt          #  edits new file with non-.py extension ./foo.txt
edit foo              #  adds .py extension and edits new file ./foo.py

edit IPython/foo.py   #  edits new file in existing subdirectory on path $BLAH/IPython/foo.py
edit IPython/foo      #  edits new file in existing subdirectory on path $BLAH/IPython/foo.py
edit IPython.foo      #  edits new file in existing subdirectory on path $BLAH/IPython/foo.py

Most Windows or Unixoid editors will behave well if you give them the name of a not-yet-
existing file to edit.  On OSX, however, if you're using an editor script that redirects
to `open -a Some.app file.txt`, this will fail if file.txt doesn't exist. For this reason,
you can use `edit -t ....` where the -t stands for "touch":  the file will be created if it
doesn't already exist, before being opened in the editor.  (Typos will then sometimes mean
that you're left with unwanted 0-byte files hanging around, needing to be deleted by hand.)
		"""###

        # NB: one (unlikely) case is not handled:  a new file with a non-.py extension
        #     in an existing subdirectory on the path, e.g. edit IPython/foo.txt

        if hasattr(__IPYTHON__, 'rc'): editor = __IPYTHON__.rc.editor
        else: editor = __IPYTHON__.editor

        windows = sys.platform.lower().startswith('win')
        if windows: editor = 'start ' + editor

        def edit(x, mode='existing'):
            if not x: return os.system(editor) or True
            x = os.path.abspath(x)
            if mode == 'existing' and not os.path.isfile(x): return False
            if os.path.isdir(x):
                return sys.stderr.write(
                    'cannot edit directory %s\n' % x) and False
            if mode == '-t' and not os.path.isfile(x): open(x, 'a').close()
            os.system(editor + ' "' + x.strip('"') + '"')
            return True

        if edit(arg): return
        # that allows for spaces in the path, e.g.  edit C:\Documents and Settings\me\Desktop\blah.py
        # but if the whole thing isn't an existing file, we'll assume a space-delimited list of filenames.
        arg = arg.strip()
        if arg.startswith('(') and arg.endswith(')'):
            arg = eval('[' + arg[1:-1] + ']', getworkspace())
        else:
            if windows:
                arg = arg.replace(
                    '\\', '\\\\'
                )  # otherwise shlex.split will decode/eat backslashes that might be important as file separators
            import shlex
            arg = shlex.split(arg)  # shlex.split copes with quoted substrings
        creation_policy = arg.pop(0) if (arg and arg[0] == '-t') else 'auto'

        for original in arg:
            stem = original
            if stem.lower().endswith('.py'): stem = stem[:-3]
            original_withpy = stem + '.py'
            path_withoutpy = os.path.join(*stem.split('.'))
            path_withpy = path_withoutpy + '.py'
            potential_location = ''
            for p in sys.path:
                if edit(os.path.join(p, original)): break
                elif edit(os.path.join(p, original_withpy)): break
                elif edit(os.path.join(p, path_withpy)): break
                elif edit(os.path.join(p, path_withoutpy)): break
                elif stem == original and edit(
                        os.path.join(p, path_withoutpy, '__init__.py')):
                    break
                elif potential_location == '':
                    pp = os.path.join(p, path_withoutpy)
                    dd = os.path.realpath(os.path.dirname(pp))
                    d = os.path.realpath(p)
                    if len(dd) > len(d) and os.path.isdir(dd):
                        potential_location = pp
            else:  # not found anywhere else, so edit as new
                if potential_location == '': potential_location = original
                fn, xtn = os.path.splitext(potential_location)
                if xtn == '': xtn = '.py'
                edit(fn + xtn, creation_policy)

    ############################################################################
    @line_magic
    def addpath(self, d=None):
        """\
Make absolute paths out of the input(s) and append them to sys.path if they are
not already there.  Supply a space-delimited list of arguments, or one argument
enclosed in quotes (which may, in that case, contain a space).
"""###
        if d == None: dd = (os.getcwd(), )
        else:
            d = d.replace('\\ ', ' ').strip()
            if (d.startswith('"')
                    and d.endswith('"')) or (d.startswith("'")
                                             and d.endswith("'")):
                dd = (d[1:-1], )
            else:
                dd = d.split()
        for d in dd:
            d = os.path.realpath(d)
            if os.path.isdir(d):
                if not d in sys.path: sys.path.append(d)
            else:
                print(('no such directory "%s"' % d))

    ############################################################################
    @line_magic
    def desk(self, subdir=''):
        """\
cd to the Desktop, but keep a global record of where we were (stored in
__IPYTHON__.undeskdir) so that we can snap back with %undesk
"""###
        if sys.platform.lower().startswith('win'):
            homedrive = os.environ.get('HOMEDRIVE')
            homepath = os.environ.get('HOMEPATH')
            userprofile = os.environ.get('USERPROFILE')
            if userprofile == None:
                d = os.path.join(homedrive, homepath, 'Desktop')
            else:
                d = os.path.join(userprofile, 'Desktop')
        else:
            d = os.path.join(os.environ.get('HOME'), 'Desktop')
        if subdir != '':
            return os.path.realpath(os.path.join(d, subdir))
        here = os.path.realpath(os.curdir)
        if os.path.realpath(d) == here:
            print("already at desktop")
        else:
            __IPYTHON__.undeskdir = here
            print(('changing directory from ' + here))
            print(('                     to ' + d))
            print('type %undesk to change back')
            os.chdir(d)

    ############################################################################
    @line_magic
    def undesk(self, arg):
        """\
Undo a previous call to %desk
"""

  ###
        d = getattr(__IPYTHON__, 'undeskdir', None)
        if d == None:
            print('no global record of previous call to %desk')
        else:
            print(('changing directory from ' + os.path.realpath(os.curdir)))
            print(('                back to ' + d))
            os.chdir(d)

    ############################################################################
    @line_magic
    def pip(self, d=''):
        try:
            import pip
        except ImportError:
            import sys
            sys.stderr.write("""
The pip module is not installed. See the instructions
at http://www.pip-installer.org/en/latest/installing.html
In short:
(1) Ensure setuptools (i.e. easy_install) is installed first
    (download and run ez_setup.py if not).
(2) Download and run get-pip.py
(3) Quit and relaunch Python
(4) Then the first thing you should do with pip is:
    pip install --upgrade setuptools
""")
            return
        try:
            return pip.main(d.split())
        except SystemExit:
            pass

    ############################################################################
    @line_magic
    def easy_install(self, d=None):
        """\
Run the easy_install command from IPython, invoking the setuptools script
Scripts/easy_install-script.py (on Windows) or easy_install (on anything else).
Particularly useful on Windows, where IPython may well be the best command-line
you have.
"""###

        def readpth(pth):
            sitedir = os.path.split(
                pth
            )[0]  # assume easy-install.pth is directly in the site-packages dir
            lines = [x.strip() for x in open(pth, 'rt').readlines()]
            return [
                os.path.realpath(os.path.join(sitedir, x)) for x in lines
                if not x.startswith('import') and not x.startswith('#')
            ]
            # assume a line is a relative path unless it begins with # or import

        def isbelow(x, p):
            x, p = os.path.realpath(x), os.path.realpath(p)
            return x == p or x.startswith(os.path.join(p, ''))

        pth = [
            os.path.join(os.path.realpath(x), 'easy-install.pth')
            for x in sys.path
        ]  # assume 'easy-install.pth' is on the path
        pth = ([x for x in pth if os.path.isfile(x)] + [None])[0]
        if pth != None: oldpaths = readpth(pth)

        ############ This is the part that actually runs easy_install.
        ############ The rest, before and after, is to attempt to keep the session going with intelligent updates to
        ############ sys.path and sys.modules (still won't work for upgrades to IPython and some messy others like simplejson).
        home = os.environ.get('PYTHONHOME')
        if sys.platform.lower().startswith('win'):
            if home == None: home = os.path.dirname(sys.executable)
            getmagic('run')(
                '"' + os.path.join(home, 'Scripts', 'easy_install-script.py') +
                '" ' + d)
        else:
            # TODO: need to set $PYTHONHOME for system call using env?  If so, how to fill it in if home==None?
            bang('"' + sys.executable + '" "`which easy_install`" ' + d)
        ############

        if pth == None:
            print(
                "restart this python session to take advantage of the new package"
            )
            return

        newpaths = readpth(pth)
        addpaths = [x for x in newpaths if x not in oldpaths]
        gone = [x for x in oldpaths if x not in newpaths]
        rmpaths = []
        for x in sys.path:
            if not len(x): continue
            for stem in gone:
                if isbelow(x, stem):
                    rmpaths.append(x)
                    break
        stripped = [x for x in sys.path if x not in rmpaths]
        for i in range(len(stripped)):
            if len(stripped[i]): stripped[i] = os.path.realpath(stripped[i])
        sys.path[:] = stripped + addpaths
        if len(addpaths):
            print(('\n  '.join(['Added to sys.path:'] +
                               list(map(repr, addpaths)))))
        if len(rmpaths):
            print(('\n  '.join(['Removed from sys.path:'] +
                               list(map(repr, rmpaths)))))
        rmmod = []
        for k, v in list(sys.modules.items()):
            v = getattr(v, '__file__', None)
            if not isinstance(v, str): continue
            for p in rmpaths:
                if isbelow(v, p):
                    rmmod.append(k)
                    break
        rmmod = [sys.modules.pop(k) for k in sorted(rmmod)]
        if len(rmmod):
            print(('\n  '.join(['Removed from sys.modules:'] +
                               list(map(repr, rmmod)))))

    ############################################################################
    @line_magic
    def search(self, dd):
        """\
search  TERM
search  TERM   TOP_LEVEL_DIR
search  TERM   TOP_LEVEL_DIR   FILE_PATTERN

Searches case-insensitively for TERM through all files matching FILE_PATTERN (default: '*')
in the directory tree starting at TOP_LEVEL_DIR (default: '.') and prints the filename, line
number and line content for any matches.

Because, on Windows, native file content searches are not reliable and IPython is going to
be the best command-line you have.

Also, for convenience, the following syntax does the simpler job of matching file names
rather than file content:     search --filename ...


		"""###
        import os, sys, fnmatch, re, getopt
        args = [
            ''.join(matches) for matches in re.findall(
                '|'.join([r'"([^"]*)"', r"'([^']*)'", r'(\S+)']), dd)
        ]
        opts, args = getopt.getopt(args, '', ['filename'])
        opts = dict(opts)
        if not 1 <= len(args) <= 3:
            sys.stderr.write(
                'arguments: SEARCH_STRING [TOP_DIR] [FILE_PATTERN]\n')
            sys.stderr.flush()
            return
        search_string, root_dir, file_pattern = (args + ['', '', ''])[:3]
        sys.stdout.write('\n')
        sys.stdout.flush()
        if not root_dir: root_dir = '.'
        matches = []
        if not os.path.isdir(root_dir):
            sys.stderr.write(str(root_dir) + ' is not a directory\n')
            sys.stderr.flush()
            return

        search_string = str(search_string).lower()
        limit = 200
        for root, dirnames, filenames in os.walk(root_dir):
            if file_pattern:
                filenames = fnmatch.filter(filenames, file_pattern)
            for filename in filenames:
                file_path = os.path.join(root, filename)
                if '--filename' in opts:
                    if search_string in str(
                            os.path.split(filename)[1]).lower():
                        sys.stdout.write('%s\n' % (file_path))
                        sys.stdout.flush()
                        matches.append(file_path)
                    continue
                matches.append(file_path)
                try:
                    file_handle = open(file_path)
                except IOError:
                    sys.stderr.write('/!\\   failed to open %s\n\n' %
                                     file_path)
                    sys.stderr.flush()
                for line_number, line in enumerate(file_handle):
                    hit = search_string in str(line).lower()
                    if hit:
                        try:
                            sys.stdout.write(
                                '%s:%d:\n    %s\n\n' %
                                (file_path.replace('\\', '/'), line_number + 1,
                                 str(line).strip()[:limit]))
                        except UnicodeDecodeError:
                            sys.stdout.write(
                                '%s:%d  matches (no preview available)\n\n' %
                                (file_path, line_number + 1))
                        sys.stdout.flush()
        if len(matches) == 0:
            sys.stderr.write('/!\\   no files matched the input pattern\n')
            sys.stderr.flush()
        else:
            sys.stdout.write('\n')

    ############################################################################
    @line_magic
    def reimport(self, dd):
        """\
The syntax

    %reimport foo, bar.*

is a shortcut for the following:

    import foo; foo = reload(foo)
    import bar; bar = reload(bar); from bar import *
"""###
        ipython = getworkspace()
        for d in dd.replace(',', ' ').split(' '):
            if len(d):
                bare = d.endswith('.*')
                if bare: d = d[:-2]
                exec('import xx; xx = reload(xx)'.replace('xx', d), ipython)
                if bare: exec('from xx import *'.replace('xx', d), ipython)

    ############################################################################
    @line_magic
    def loadpylab(self, d=''):
        """\
The syntax

	%loadpylab

imports matplotlib and pylab into the IPython workspace, ensuring that, if matplotlib
has not previously been loaded, it is loaded in interactive mode.

	%loadpylab *

is the same, but additionally imports the requested symbols from pylab, directly into
the workspace.

"""###
        import sys
        ipython = getworkspace()
        try:
            import matplotlib
        except ImportError:
            print("WARNING: failed to import matplotlib")
        else:
            if not 'matplotlib.backends' in sys.modules:
                matplotlib.interactive(True)
            try:
                exec('import matplotlib, pylab', ipython)
            except ImportError:
                print("WARNING: failed to import pylab")
            #if __OLDER_IPYTHON__:
            if len(d): exec('from pylab import ' + d, ipython)

############################################################################

    @line_magic
    def njh(self, *pargs):
        """\
Imports a number of packages, including the BCPy2000 sub-packages WavTools and
SigTools, but also copy, struct, ctypes, numpy, scipy and matplotlib and pylab
(the latter in interactive mode).
"""###
        ipython = getworkspace()
        exec('import copy,struct,ctypes,time,numpy,scipy', ipython)
        try:
            exec('import WavTools,SigTools', ipython)
        except ImportError:
            exec('import BCPy2000.Paths', ipython)
            exec('import WavTools,SigTools', ipython)

        getmagic('loadpylab')()

############################################################################

    @line_magic
    def pp(self, name=''):
        """\
An ipython command-line shortcut to SigTools.summarize, to give a quick look at
object attributes--- especially useful for numpy arrays.
"""###
        if name == None: return
        ipython = getworkspace()
        val = eval(name, ipython)
        from . import SigTools
        print((SigTools.summarize(val)))

############################################################################

    @line_magic
    def l(self, dd='.'):
        if dd in [None, '']: dd = '.'
        dd = dd.replace('\\ ', ' ').strip().strip('"\'')
        if sys.platform.lower().startswith('win'):
            cmd = 'dir "%s"' % dd.replace('/', '\\')
        else:
            cmd = 'ls -AFlh %s' % dd
        bang(cmd, shell=True)

############################################################################

    @line_magic
    def bootstrap_shortcuts(self, *pargs):
        """\
import BCPy2000.Shortcuts allows you access to these useful shortcuts (edit,
search, desk, addpath, easy_install...).  This shortcut writes the import
command into an .ipy file in the default ipython profile directory of your
home directory, so that the shortcuts will be loaded automatically whenever
you start IPython (versions 0.13 and later) in future.
"""###
        code = """\
try: import BCPy2000.Shortcuts
except ImportError: print ("Failed to import BCPy2000.Shortcuts")
"""
        import os, sys
        if __OLDER_IPYTHON__:
            sys.stderr.write(
                "\nFAILED: this bootstrapper only works for newer versions of IPython.\nYou'll have to add the following lines manually to ipy_user_conf.py\nin the _ipython or .ipython subdirectory of your home directory:\n\n%s\n"
                % code)
            return
        if sys.platform.lower().startswith('win'):
            home = os.environ['USERPROFILE']
        else:
            home = os.environ['HOME']
        startup_dir = os.path.join(home, '.ipython', 'profile_default',
                                   'startup')
        startup_file = os.path.join(
            startup_dir, 'BCPy2000Shortcuts.ipy'
        )  # cannot be a .py file, must be an .ipy - the latter seem to be executed later, when ipython is running and its instance can be accessed with get_ipython()
        if not os.path.isdir(startup_dir): os.makedirs(startup_dir)
        open(startup_file, 'wt').write(code)
        sys.stderr.write(
            '\nSUCCESS: the file\n   %s\nhas been created containing the following lines:\n\n%s\n'
            % (startup_file, code))
コード例 #9
0
ファイル: stopsignal.py プロジェクト: twiecki/stopsignal
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
import numpy as np
import numpy.lib.recfunctions as rec
import pymc as pm
from copy import copy
from scipy.stats import norm
from scipy.integrate import quad
try:
    from IPython.Debugger import Tracer; debug_here = Tracer()
except:
    def debug_here(): pass

from kabuki import Hierarchical, Knode
import stop_likelihoods
from collections import OrderedDict

def cython_Go(value, imu_go, isigma_go, itau_go):
    """Ex-Gaussian log-likelihood of GoRTs"""
    return stop_likelihoods.Go(value, imu_go, isigma_go, itau_go)

def cython_SRRT(value, issd, imu_go, isigma_go, itau_go, imu_stop, isigma_stop, itau_stop):
    """Censored ExGaussian log-likelihood of SRRTs"""
    return stop_likelihoods.SRRT(value, issd, imu_go, isigma_go, itau_go, imu_stop, isigma_stop, itau_stop)

def cython_Inhibitions(value, imu_go, isigma_go, itau_go, imu_stop, isigma_stop, itau_stop):
    """Censored ExGaussian log-likelihood of inhibitions"""
    return stop_likelihoods.Inhibitions(value, imu_go, isigma_go, itau_go, imu_stop, isigma_stop, itau_stop)

Go_like = pm.stochastic_from_dist(name="Ex-Gauss GoRT",
コード例 #10
0
ファイル: model.py プロジェクト: jzuhone/xija
import pyyaks.context as pyc
from .files import files as xija_files
from . import clogging

# HDF5 version of commanded states table
H5FILE = '/proj/sot/ska/data/cmd_states/cmd_states.h5'

src = pyc.CONTEXT['src'] if 'src' in pyc.CONTEXT else pyc.ContextDict('src')
files = (pyc.CONTEXT['file'] if 'file' in pyc.CONTEXT else pyc.ContextDict(
    'files', basedir=os.getcwd()))
files.update(xija_files)

if 'debug' in globals():
    from IPython.Debugger import Tracer
    pdb_settrace = Tracer()

logger = clogging.config_logger('xija', level=clogging.INFO)

DEFAULT_DT = 328.0
dt_factors = np.array([1.0, 0.5, 0.25, 0.2, 0.125, 0.1, 0.05, 0.025])

#int calc_model(int n_times, int n_preds, int n_tmals, double dt,
#               double **mvals, int **tmal_ints, double **tmal_floats)


def convert_type_star_star(array, ctype_type):
    f4ptr = ctypes.POINTER(ctype_type)
    return (f4ptr * len(array))(*[row.ctypes.data_as(f4ptr) for row in array])

コード例 #11
0
  descent1 - func - gradient descent
  proj - func - project given point onto constraint manifold
  tgt - func - approximate tangent space to manifold
  armijoproj - func - Armijo stepsize rule, projected onto manifold
  descent1proj - func - gradient descent, projected onto manifold

by Sam Burden, Humberto Gonzalez, Ram Vasudevan, Berkeley 2011
"""

import numpy as np
import pylab as plt
import scipy as sp
import scipy.optimize as op

try:
  from IPython.Debugger import Tracer; dbg = Tracer()
except:
  dbg = lambda : 1/0
import sys

class Struct(object):
    """ 
    Struct  struct-like class

    Acts like a dict, but keys are members of the object.

    Also, its string representation is Python code which 
    yields the object when executed.

    >>> a = Struct(foo='bar', goo=7)
    >>> b = a.copy()
コード例 #12
0
        def theexecpthook(type, value, traceback):
            fnc()
            ip_excepthook(type, value, traceback)
            # write this to a File without Colors
            if not filename == None:
                outFile = open(filename, "a")
                outFile.write("--" + time.ctime() + " --\n")
                outFile.write(fileTraceback.text(type, value, traceback))
                outFile.write("\n-- --\n")
                outFile.close()

        # assign it
        sys.excepthook = theexecpthook

    from IPython.Debugger import Tracer
    ST = Tracer()  # "ST" = "start trace"

except ImportError:
    # IPython seems not to be installed
    # create dummy functions
    def IPS():
        print "(EE): IPython is not available"
        pass

    def ip_syshook(*args, **kwargs):
        pass

    def ST():
        pass

コード例 #13
0
ファイル: bellman.py プロジェクト: zsh1313/bellman
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and   
// that both that copyright notice and this permission notice appear
// in supporting documentation.  The authors make no representations
// about the suitability of this software for any purpose.          
// It is provided "as is" without express or implied warranty.
//                                                            
  


import pylab
import scipy, time, sys
import matplotlib.pyplot as plt
import pyublas, _debugMsg, _maximizer as mx
import lininterp2 as linterp
from IPython.Debugger import Tracer; BREAKPOINT = Tracer()

def maximizer1d(controlGridList, bellmanParams, parallel):
	def objFn(x):
		return bellmanParams.objectiveFunction([x])
	fArray = scipy.array(map(objFn, controlGridList[0]))
	argmax = scipy.argmax(fArray)
	return (1, [controlGridList[0][argmax]], fArray[argmax])
	
# wArray is a multidimensional array on a grid
# stateGridList is a list of 1d arrays representing the grid coordinates
# bellmanParams is an object containing the problem-specific information
#   objectiveFunction() is implemented in C++
#   should implement these member functions: setStateVars, setPrevIteration, getControlGridList, getNControls
# parallel is a bool, if true, use the parallel grid search algorithm
def grid_bellman(stateGridList, wArray, bellmanParams, parallel=True):
コード例 #14
0
ファイル: sample_patterns.py プロジェクト: virtualritz/aqsis
from pylab import *
from IPython.Debugger import Tracer; keyboard = Tracer()

#------------------------------------------------------------------------------
# Experimental code for generating samples positions which are well-distributed
# in time and space, and appropriate for use with interleaved sampling.
#
# Demo of some of the experimental code:
'''
tileWidth = 21
t = jitteredSamp(tileWidth**2)
ts = makeTileSet(t, r=2)
h = SpatialHash()
showFiltered(t[applyTileSet(30, ts, h)], lambda x,y:x, gaussKer(8), 4)
show()
'''


def scatter3D(*args, **kwargs):
    '''Convenience function to plot 3D scatter diagrams'''
    from mpl_toolkits.mplot3d import Axes3D
    ax = Axes3D(gcf())
    ax.scatter(*args, **kwargs)

def centred_figimage(image, *args, **kwargs):
    '''
    Show an image in the centre of a figure at natural (unscaled) resolution.
    '''
    bound = gcf().get_window_extent()
    xo = (bound.width - image.shape[0])//2
    yo = (bound.height - image.shape[1])//2