<commit_before>#!/usr/bin/env python2

from sys import stdout, stderr, exit
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter as ADHF
from os.path import basename
from itertools import chain, combinations
import logging
import csv

from newick_parser import Tree, Branch, Leaf

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)

ROOT_NODE = 'GO:0008150'

def readAssociations(data):
    res = dict()
    for row in csv.reader(data, delimiter='\t'):
        if len(row) < 11:
            continue
        if row[8] != 'P':
            continue
        goId = row[4]
        for i in row[10].split('|'):
            if i not in res:
                res[i] = list()
            if goId not in res[i]:
                res[i].append(goId)
    return res

def readTerm(data):
    isTerm = False
    curItem = StringIO()
    for line in data:
        if isTerm:
            if not line or line == '\n':
                isTerm = False
                curItem.reset()
                yield curItem
                curItem.reset()
                curItem.truncate()
            curItem.write(line)
        elif line == '[Term]\n':
            isTerm = True
        # otherwise continue

def readGO(data):
    res = dict()
    ns_dict = dict()
    for term in readTerm(data):
        goId = None
        isARels = list()
        namespace = None
        for line in term:
            if line.startswith('id:'):
                goId = line[4:-1]
            elif line.startswith('namespace:'):
                namespace = line[11:-1]
            elif line.startswith('is_a:'):
                isARels.append(line[6:line.find(' !')])
        if isARels and namespace == 'biological_process':
            res[goId] = isARels
        ns_dict[goId] = namespace

    # clean out everything except biological process:
    for k,v in res.items():
        if ns_dict[k] != 'biological_process':
            del res[k]
    return res

def getAllRootPaths(goId, goData):
    if not goId:
        return list()
    if goId not in goData:
        return [[goId]]

    res = list()
    for i in goData[goId]:
        paths = getAllRootPaths(i, goData)
        for path in paths:
            res.append([goId] + path)
    return res
def readGenes(data):

    genes = set()
    for line in csv.reader(data, delimiter='\t'):
        genes.add(line[3])

    return genes

def constructLevelMap(goTree):

    levels = {goTree.subtree: 0}
    queue = [(goTree.subtree, 0)]

    while queue:
        node, l = queue.pop()
        if type(node) == Branch:
            for c in node.subtrees:
                levels[c] = l+1
                queue.append((c, l+1))
    return levels

def constructGOTree(pathDict):
    # return tree and links from each gene to its leaf representations
    jpaths = list(set(chain(*pathDict.values())))
    max_l = max(map(len, jpaths))
    pNodes = [None] * len(jpaths)
    t = Tree(None)
    l = 0
    while l <= max_l:
        l += 1
        for i in xrange(len(jpaths)):
            p = jpaths[i]
            lastp = len(p)
            # skip path if too short
            if l > lastp:
                continue

            # we are at the root
            b = None
            if not (l - 1):
                if t.subtree and t.subtree.label != p[-l]:
                    # ooops
                    import pdb; pdb.set_trace()
                elif not t.subtree:
                    t.subtree = Branch(list(), label=p[-l])
                b = t.subtree
            # for all other nodes that have yet not been identified
            elif p[-l] not in [x.label for x in pNodes[i].subtrees]:
                b = Branch(list(), label=p[-l])
                pNodes[i].subtrees.append(b)
                b.setAncestor(pNodes[i])
            # for identified nodes
            else:
                b = [x for x in pNodes[i].subtrees if x.label == p[-l]][0]
            pNodes[i] = b

    leafMapping = dict()
    for i in xrange(len(pNodes)):
        leafMapping[pNodes[i].label] = pNodes[i]

    # construct gene representation link list
    links = dict()
    for gene, reps in pathDict.items():
        gl = list()
        for rep in reps:
            l = Leaf(gene)
            parent = leafMapping[rep[0]]
            parent.subtrees.append(l)
            l.setAncestor(parent)
            gl.append(l)
        links[gene] = gl
    return t, links

def nearestNeighborDist(t, links, levels, genes):

    res = {gene:float('inf') for gene in genes}

    levelLists = dict()
    pushLists = dict()

    for gene in genes:
        if links.has_key(gene):
            for rep in links[gene]:
                l = levels[rep]
                if not levelLists.has_key(l):
                    levelLists[l] = set()
                levelLists[l].add(rep)
                pushLists[rep] = {rep.label:l}

    mx_level = max(levelLists.keys())
    levelLists = [levelLists.get(l, set()) for l in xrange(mx_level+1)]

    for l in xrange(mx_level, -1, -1):
        for node in levelLists[l]:
            elems = pushLists[node]
            if len(elems) < 2:
                # if only one element is present, it is by definition the
                # push-up represenative
                ((gene, l1), ) = elems.items()
            else:
                for (el1, l1), (el2, l2) in combinations(elems.items(), 2):
                    d = float(l1+l2-2*l-1)/(l1+l2)
                    res[el1] = min(res[el1], d)
                    res[el2] = min(res[el2], d)
                # identify push-up represenative
                (gene, l1) = min(elems.items(), key=lambda x: x[1])

            p = node.getAncestor()
            if p != None:
                # if p!= None, then, by definition, l > 0
                levelLists[l-1].add(p)
                if not pushLists.has_key(p):
                    pushLists[p] = dict()
                if not pushLists[p].has_key(gene):
                    pushLists[p][gene] = l1
                else:
                    pushLists[p][gene] = min(l1, pushLists[p][gene])
    return res

def printClusterDistances(t, links, levels, nn_genome, cluster_data):
    for line in csv.reader(cluster_data, delimiter='\t'):
        genes = set(filter(lambda x: links.has_key(x), line[0].split(';')))
        nn_cluster = nearestNeighborDist(t, links, levels, genes)
        print '%s\t%s' %(line[0], sum(nn_cluster[g]-nn_genome[g] for g in
            genes))

if __name__ == '__main__':
    parser = ArgumentParser(formatter_class=ADHF)
    parser.add_argument('go_obo_file', type=str,
            help='Gene Ontology hierarchy in OBO format')
    parser.add_argument('go_associations', type=str,
            help='Gene Ontology assocations file that links GO terms ' + \
                    'with genes')
    parser.add_argument('genome_annotations', type=str,
            help='genome annotation file')
    parser.add_argument('cluster_file', type=str,
            help='gene clusters in proprietary format')

    args = parser.parse_args()

    # setup logging
    ch = logging.StreamHandler(stderr)
    ch.setLevel(logging.ERROR)
    ch.setFormatter(logging.Formatter('!! %(message)s'))
    cf = logging.FileHandler('%s.nn_go.log' %basename(args.cluster_file).rsplit('.',
        1)[0], mode='w')
    cf.setLevel(logging.DEBUG)
    cf.setFormatter(logging.Formatter('%(levelname)s\t%(asctime)s\t%(message)s'))
    LOG.addHandler(cf)
    LOG.addHandler(ch)

    LOG.info('loading GO OBO file')
    goData = readGO(open(args.go_obo_file))
    LOG.info('loading GO associations')
    assoData = readAssociations(open(args.go_associations))
    LOG.info('laoding gene annotations')
    genes = readGenes(open(args.genome_annotations))

    pathDict = {gene: list(chain(*[map(tuple, getAllRootPaths(rep, goData)) for
        rep in reps])) for gene, reps in assoData.items() if gene in genes}

    LOG.info('found %s out of %s in GO hierarchy' %(len(pathDict), len(genes)))

    LOG.info('construcing GO hierarchy')
    t, links = constructGOTree(pathDict)
    LOG.info('top-down assignment of levels')
    levels = constructLevelMap(t)

    LOG.info('nearest neighbor analysis over all annotated genes')
    nearest_gene_dists = nearestNeighborDist(t, links, levels, genes)

<commit_msg>minor bugfixes<commit_after>#!/usr/bin/env python2

from sys import stdout, stderr, exit
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter as ADHF
from os.path import basename
from itertools import chain, combinations
from cStringIO import StringIO
import logging
import csv

from newick_parser import Tree, Branch, Leaf

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)

ROOT_NODE = 'GO:0008150'

def readAssociations(data):
    res = dict()
    for row in csv.reader(data, delimiter='\t'):
        if len(row) < 11:
            continue
        if row[8] != 'P':
            continue
        goId = row[4]
        for i in row[10].split('|'):
            if i not in res:
                res[i] = list()
            if goId not in res[i]:
                res[i].append(goId)
    return res

def readTerm(data):
    isTerm = False
    curItem = StringIO()
    for line in data:
        if isTerm:
            if not line or line == '\n':
                isTerm = False
                curItem.reset()
                yield curItem
                curItem.reset()
                curItem.truncate()
            curItem.write(line)
        elif line == '[Term]\n':
            isTerm = True
        # otherwise continue

def readGO(data):
    res = dict()
    ns_dict = dict()
    for term in readTerm(data):
        goId = None
        isARels = list()
        namespace = None
        for line in term:
            if line.startswith('id:'):
                goId = line[4:-1]
            elif line.startswith('namespace:'):
                namespace = line[11:-1]
            elif line.startswith('is_a:'):
                isARels.append(line[6:line.find(' !')])
        if isARels and namespace == 'biological_process':
            res[goId] = isARels
        ns_dict[goId] = namespace

    # clean out everything except biological process:
    for k,v in res.items():
        if ns_dict[k] != 'biological_process':
            del res[k]
    return res

def getAllRootPaths(goId, goData):
    if not goId:
        return list()
    if goId not in goData:
        return [[goId]]

    res = list()
    for i in goData[goId]:
        paths = getAllRootPaths(i, goData)
        for path in paths:
            res.append([goId] + path)
    return res
def readGenes(data):

    genes = set()
    for line in csv.reader(data, delimiter='\t'):
        genes.add(line[3])

    return genes

def constructLevelMap(goTree):

    levels = {goTree.subtree: 0}
    queue = [(goTree.subtree, 0)]

    while queue:
        node, l = queue.pop()
        if type(node) == Branch:
            for c in node.subtrees:
                levels[c] = l+1
                queue.append((c, l+1))
    return levels

def constructGOTree(pathDict):
    # return tree and links from each gene to its leaf representations
    jpaths = list(set(chain(*pathDict.values())))
    max_l = max(map(len, jpaths))
    pNodes = [None] * len(jpaths)
    t = Tree(None)
    l = 0
    while l <= max_l:
        l += 1
        for i in xrange(len(jpaths)):
            p = jpaths[i]
            lastp = len(p)
            # skip path if too short
            if l > lastp:
                continue

            # we are at the root
            b = None
            if not (l - 1):
                if t.subtree and t.subtree.label != p[-l]:
                    # ooops
                    import pdb; pdb.set_trace()
                elif not t.subtree:
                    t.subtree = Branch(list(), label=p[-l])
                b = t.subtree
            # for all other nodes that have yet not been identified
            elif p[-l] not in [x.label for x in pNodes[i].subtrees]:
                b = Branch(list(), label=p[-l])
                pNodes[i].subtrees.append(b)
                b.setAncestor(pNodes[i])
            # for identified nodes
            else:
                b = [x for x in pNodes[i].subtrees if x.label == p[-l]][0]
            pNodes[i] = b

    leafMapping = dict()
    for i in xrange(len(pNodes)):
        leafMapping[pNodes[i].label] = pNodes[i]

    # construct gene representation link list
    links = dict()
    for gene, reps in pathDict.items():
        gl = list()
        for rep in reps:
            l = Leaf(gene)
            parent = leafMapping[rep[0]]
            parent.subtrees.append(l)
            l.setAncestor(parent)
            gl.append(l)
        links[gene] = gl
    return t, links

def nearestNeighborDist(t, links, levels, genes):

    res = {gene:float('inf') for gene in genes}

    levelLists = dict()
    pushLists = dict()

    for gene in genes:
        if links.has_key(gene):
            for rep in links[gene]:
                l = levels[rep]
                if not levelLists.has_key(l):
                    levelLists[l] = set()
                levelLists[l].add(rep)
                pushLists[rep] = {rep.label:l}

    if not levelLists:
        return levelLists

    mx_level = max(levelLists.keys())
    levelLists = [levelLists.get(l, set()) for l in xrange(mx_level+1)]

    for l in xrange(mx_level, -1, -1):
        for node in levelLists[l]:
            elems = pushLists[node]
            if len(elems) < 2:
                # if only one element is present, it is by definition the
                # push-up represenative
                ((gene, l1), ) = elems.items()
            else:
                for (el1, l1), (el2, l2) in combinations(elems.items(), 2):
                    d = float(l1+l2-2*l-1)/(l1+l2)
                    res[el1] = min(res[el1], d)
                    res[el2] = min(res[el2], d)
                # identify push-up represenative
                (gene, l1) = min(elems.items(), key=lambda x: x[1])

            p = node.getAncestor()
            if p != None:
                # if p!= None, then, by definition, l > 0
                levelLists[l-1].add(p)
                if not pushLists.has_key(p):
                    pushLists[p] = dict()
                if not pushLists[p].has_key(gene):
                    pushLists[p][gene] = l1
                else:
                    pushLists[p][gene] = min(l1, pushLists[p][gene])
    return res

def printClusterDistances(t, links, levels, nn_genome, cluster_data, out):
    for line in csv.reader(cluster_data, delimiter='\t'):
        genes = set(filter(lambda x: links.has_key(x), line[0].split(';')))
        nn_cluster = nearestNeighborDist(t, links, levels, genes)
        if len(nn_cluster) > 2:
            print >> out, '%s\t%s\t%s' %(line[0], len(nn_cluster), not
                    nn_cluster and float('inf') or \
                    sum(nn_cluster[g]-nn_genome[g] for g in genes))

if __name__ == '__main__':
    parser = ArgumentParser(formatter_class=ADHF)
    parser.add_argument('go_obo_file', type=str,
            help='Gene Ontology hierarchy in OBO format')
    parser.add_argument('go_associations', type=str,
            help='Gene Ontology assocations file that links GO terms ' + \
                    'with genes')
    parser.add_argument('genome_annotations', type=str,
            help='genome annotation file')
    parser.add_argument('cluster_file', type=str,
            help='gene clusters in proprietary format')

    args = parser.parse_args()

    # setup logging
    ch = logging.StreamHandler(stderr)
    ch.setLevel(logging.ERROR)
    ch.setFormatter(logging.Formatter('!! %(message)s'))
    cf = logging.FileHandler('%s.nn_go.log' %basename(args.cluster_file).rsplit('.',
        1)[0], mode='w')
    cf.setLevel(logging.DEBUG)
    cf.setFormatter(logging.Formatter('%(levelname)s\t%(asctime)s\t%(message)s'))
    LOG.addHandler(cf)
    LOG.addHandler(ch)

    LOG.info('loading GO OBO file')
    goData = readGO(open(args.go_obo_file))
    LOG.info('loading GO associations')
    assoData = readAssociations(open(args.go_associations))
    LOG.info('laoding gene annotations')
    genes = readGenes(open(args.genome_annotations))

    pathDict = {gene: list(chain(*[map(tuple, getAllRootPaths(rep, goData)) for
        rep in reps])) for gene, reps in assoData.items() if gene in genes}

    LOG.info('found %s out of %s in GO hierarchy' %(len(pathDict), len(genes)))

    LOG.info('construcing GO hierarchy')
    t, links = constructGOTree(pathDict)
    LOG.info('top-down assignment of levels')
    levels = constructLevelMap(t)

    LOG.info('nearest neighbor analysis over all annotated genes')
    nearest_gene_dists = nearestNeighborDist(t, links, levels, genes)
    printClusterDistances(t, links, levels, nearest_gene_dists,
            open(args.cluster_file), stdout)
<|endoftext|>


<commit_before>"""Edit the Python Preferences file."""
#
# This program is getting more and more clunky. It should really
# be rewritten in a modeless way some time soon.

from Dlg import *
from Events import *
from Res import *
import Controls
import string
import struct
import macfs
import MacOS
import os
import sys
import Res # For Res.Error
import pythonprefs
import EasyDialogs
import Help

# resource IDs in our own resources (dialogs, etc)
MESSAGE_ID = 256

DIALOG_ID = 511
TEXT_ITEM = 1
OK_ITEM = 2
CANCEL_ITEM = 3
DIR_ITEM = 4
TITLE_ITEM = 5
OPTIONS_ITEM = 7
HELP_ITEM = 9

# The options dialog. There is a correspondence between
# the dialog item numbers and the option.
OPT_DIALOG_ID = 510

# Map dialog item numbers to option names (and the reverse)
opt_dialog_map = [
	None,
	None,
	"inspect",
	"verbose",
	"optimize",
	"unbuffered",
	"debugging",
	"tabwarn",
	"nosite",
	"nonavservice",
	"nointopt",
	"noargs",
	"delayconsole",
	]
opt_dialog_dict = {}
for i in range(len(opt_dialog_map)):
	if opt_dialog_map[i]:
		opt_dialog_dict[opt_dialog_map[i]] = i
# 1 thru 10 are the options
# The GUSI creator/type and delay-console
OD_CREATOR_ITEM = 18
OD_TYPE_ITEM = 19
OD_OK_ITEM = 1
OD_CANCEL_ITEM = 2
OD_HELP_ITEM = 20
OD_KEEPALWAYS_ITEM = 14
OD_KEEPOUTPUT_ITEM = 15
OD_KEEPERROR_ITEM = 16
OD_KEEPNEVER_ITEM = 17

def optinteract(options):
	"""Let the user interact with the options dialog"""
	d = GetNewDialog(OPT_DIALOG_ID, -1)
	htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
	SetDialogItemText(htext, options['creator'])
	htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
	SetDialogItemText(htext, options['type'])
	d.SetDialogDefaultItem(OD_OK_ITEM)
	d.SetDialogCancelItem(OD_CANCEL_ITEM)
	while 1:
		for name in opt_dialog_dict.keys():
			num = opt_dialog_dict[name]
			ctl = d.GetDialogItemAsControl(num)
			ctl.SetControlValue(options[name])
		ctl = d.GetDialogItemAsControl(OD_KEEPALWAYS_ITEM)
		ctl.SetControlValue(options['keep_console'] == 3)
		ctl = d.GetDialogItemAsControl(OD_KEEPOUTPUT_ITEM)
		ctl.SetControlValue(options['keep_console'] == 1)
		ctl = d.GetDialogItemAsControl(OD_KEEPERROR_ITEM)
		ctl.SetControlValue(options['keep_console'] == 2)
		ctl = d.GetDialogItemAsControl(OD_KEEPNEVER_ITEM)
		ctl.SetControlValue(options['keep_console'] == 0)
		n = ModalDialog(None)
		if n == OD_OK_ITEM:
			htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
			ncreator = GetDialogItemText(htext)
			htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
			ntype = GetDialogItemText(htext)
			if len(ncreator) == 4 and len(ntype) == 4:
				options['creator'] = ncreator
				options['type'] = ntype
				return options
			else:
				MacOS.SysBeep()
		elif n == OD_CANCEL_ITEM:
			return
		elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
			pass
		elif n == OD_KEEPALWAYS_ITEM:
			options['keep_console'] = 3;
		elif n == OD_KEEPOUTPUT_ITEM:
			options['keep_console'] = 1;
		elif n == OD_KEEPERROR_ITEM:
			options['keep_console'] = 2;
		elif n == OD_KEEPNEVER_ITEM:
			options['keep_console'] = 0;
		elif n == OD_HELP_ITEM:
			onoff = Help.HMGetBalloons()
			Help.HMSetBalloons(not onoff)
		elif 1 <= n <= len(opt_dialog_map):
			options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])

def interact(options, title):
	"""Let the user interact with the dialog"""
	try:
		# Try to go to the "correct" dir for GetDirectory
		os.chdir(options['dir'].as_pathname())
	except os.error:
		pass
	d = GetNewDialog(DIALOG_ID, -1)
	htext = d.GetDialogItemAsControl(TITLE_ITEM)
	SetDialogItemText(htext, title)
	path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
	data = string.joinfields(options['path'], '\r')
	path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)

	d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
	d.SelectDialogItemText(TEXT_ITEM, 0, 0)
##	d.SetDialogDefaultItem(OK_ITEM)
	d.SetDialogCancelItem(CANCEL_ITEM)
	d.GetDialogWindow().ShowWindow()
	d.DrawDialog()
	while 1:
		n = ModalDialog(None)
		if n == OK_ITEM:
			break
		if n == CANCEL_ITEM:
			return None
##		if n == REVERT_ITEM:
##			return [], pythondir
		if n == DIR_ITEM:
			fss, ok = macfs.GetDirectory('Select python home folder:')
			if ok:
				options['dir'] = fss
		elif n == HELP_ITEM:
			onoff = Help.HMGetBalloons()
			Help.HMSetBalloons(not onoff)
		if n == OPTIONS_ITEM:
			noptions = options
			for k in options.keys():
				noptions[k] = options[k]
			noptions = optinteract(noptions)
			if noptions:
				options = noptions
	data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
	tmp = string.splitfields(data, '\r')
	newpath = []
	for i in tmp:
		if i:
			newpath.append(i)
	options['path'] = newpath
	return options

def edit_preferences():
	handler = pythonprefs.PythonOptions()
	options = handler.load()
	if options['noargs']:
		EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
	result = interact(options, 'System-wide preferences')
	if result:
		handler.save(result)

def edit_applet(name):
	handler = pythonprefs.AppletOptions(name)
	result = interact(handler.load(), os.path.split(name)[1])
	if result:
		handler.save(result)

def main():
	try:
		h = FSpOpenResFile('EditPythonPrefs.rsrc', 1)
	except Res.Error:
		pass	# Assume we already have acces to our own resource

	if len(sys.argv) <= 1:
		edit_preferences()
	else:
		for appl in sys.argv[1:]:
			edit_applet(appl)

if __name__ == '__main__':
	main()
<commit_msg>Added some of the new Numeric modules (which weren't rebuilt).<commit_after>"""Edit the Python Preferences file."""
#
# This program is getting more and more clunky. It should really
# be rewritten in a modeless way some time soon.

from Dlg import *
from Events import *
from Res import *
import Controls
import string
import struct
import macfs
import MacOS
import os
import sys
import Res # For Res.Error
import pythonprefs
import EasyDialogs
import Help

# resource IDs in our own resources (dialogs, etc)
MESSAGE_ID = 256

DIALOG_ID = 511
TEXT_ITEM = 1
OK_ITEM = 2
CANCEL_ITEM = 3
DIR_ITEM = 4
TITLE_ITEM = 5
OPTIONS_ITEM = 7
HELP_ITEM = 9

# The options dialog. There is a correspondence between
# the dialog item numbers and the option.
OPT_DIALOG_ID = 510

# Map dialog item numbers to option names (and the reverse)
opt_dialog_map = [
	None,
	None,
	"inspect",
	"verbose",
	"optimize",
	"unbuffered",
	"debugging",
	"tabwarn",
	"nosite",
	"nonavservice",
	"nointopt",
	"noargs",
	"delayconsole",
	]
opt_dialog_dict = {}
for i in range(len(opt_dialog_map)):
	if opt_dialog_map[i]:
		opt_dialog_dict[opt_dialog_map[i]] = i
# 1 thru 10 are the options
# The GUSI creator/type and delay-console
OD_CREATOR_ITEM = 18
OD_TYPE_ITEM = 19
OD_OK_ITEM = 1
OD_CANCEL_ITEM = 2
OD_HELP_ITEM = 20
OD_KEEPALWAYS_ITEM = 14
OD_KEEPOUTPUT_ITEM = 15
OD_KEEPERROR_ITEM = 16
OD_KEEPNEVER_ITEM = 17

def optinteract(options):
	"""Let the user interact with the options dialog"""
	d = GetNewDialog(OPT_DIALOG_ID, -1)
	htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
	SetDialogItemText(htext, options['creator'])
	htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
	SetDialogItemText(htext, options['type'])
	d.SetDialogDefaultItem(OD_OK_ITEM)
	d.SetDialogCancelItem(OD_CANCEL_ITEM)
	while 1:
		for name in opt_dialog_dict.keys():
			num = opt_dialog_dict[name]
			ctl = d.GetDialogItemAsControl(num)
			ctl.SetControlValue(options[name])
		ctl = d.GetDialogItemAsControl(OD_KEEPALWAYS_ITEM)
		ctl.SetControlValue(options['keep_console'] == 3)
		ctl = d.GetDialogItemAsControl(OD_KEEPOUTPUT_ITEM)
		ctl.SetControlValue(options['keep_console'] == 1)
		ctl = d.GetDialogItemAsControl(OD_KEEPERROR_ITEM)
		ctl.SetControlValue(options['keep_console'] == 2)
		ctl = d.GetDialogItemAsControl(OD_KEEPNEVER_ITEM)
		ctl.SetControlValue(options['keep_console'] == 0)
		n = ModalDialog(None)
		if n == OD_OK_ITEM:
			htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
			ncreator = GetDialogItemText(htext)
			htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
			ntype = GetDialogItemText(htext)
			if len(ncreator) == 4 and len(ntype) == 4:
				options['creator'] = ncreator
				options['type'] = ntype
				return options
			else:
				MacOS.SysBeep()
		elif n == OD_CANCEL_ITEM:
			return
		elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
			pass
		elif n == OD_KEEPALWAYS_ITEM:
			options['keep_console'] = 3;
		elif n == OD_KEEPOUTPUT_ITEM:
			options['keep_console'] = 1;
		elif n == OD_KEEPERROR_ITEM:
			options['keep_console'] = 2;
		elif n == OD_KEEPNEVER_ITEM:
			options['keep_console'] = 0;
		elif n == OD_HELP_ITEM:
			onoff = Help.HMGetBalloons()
			Help.HMSetBalloons(not onoff)
		elif 1 <= n <= len(opt_dialog_map):
			options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])

def interact(options, title):
	"""Let the user interact with the dialog"""
	try:
		# Try to go to the "correct" dir for GetDirectory
		os.chdir(options['dir'].as_pathname())
	except os.error:
		pass
	d = GetNewDialog(DIALOG_ID, -1)
	htext = d.GetDialogItemAsControl(TITLE_ITEM)
	SetDialogItemText(htext, title)
	path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
	data = string.joinfields(options['path'], '\r')
	path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)

	d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
	d.SelectDialogItemText(TEXT_ITEM, 0, 0)
##	d.SetDialogDefaultItem(OK_ITEM)
	d.SetDialogCancelItem(CANCEL_ITEM)
	d.GetDialogWindow().ShowWindow()
	d.DrawDialog()
	while 1:
		n = ModalDialog(None)
		if n == OK_ITEM:
			break
		if n == CANCEL_ITEM:
			return None
##		if n == REVERT_ITEM:
##			return [], pythondir
		if n == DIR_ITEM:
			fss, ok = macfs.GetDirectory('Select python home folder:')
			if ok:
				options['dir'] = fss
		elif n == HELP_ITEM:
			onoff = Help.HMGetBalloons()
			Help.HMSetBalloons(not onoff)
		if n == OPTIONS_ITEM:
			noptions = options
			for k in options.keys():
				noptions[k] = options[k]
			noptions = optinteract(noptions)
			if noptions:
				options = noptions
	data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
	tmp = string.splitfields(data, '\r')
	newpath = []
	for i in tmp:
		if i:
			newpath.append(i)
	options['path'] = newpath
	return options

def edit_preferences():
	handler = pythonprefs.PythonOptions()
	options = handler.load()
	if options['noargs']:
		EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
	result = interact(options, 'System-wide preferences')
	if result:
		handler.save(result)

def edit_applet(name):
	handler = pythonprefs.AppletOptions(name)
	result = interact(handler.load(), os.path.split(name)[1])
	if result:
		handler.save(result)

def main():
	try:
		h = FSpOpenResFile('EditPythonPrefs.rsrc', 1)
	except Res.Error:
		pass	# Assume we already have acces to our own resource

	MacOS.SchedParams(1, 0)
	if len(sys.argv) <= 1:
		edit_preferences()
	else:
		for appl in sys.argv[1:]:
			edit_applet(appl)

if __name__ == '__main__':
	main()
<|endoftext|>


<commit_before>#!/usr/bin/env python2.7

import malibu, sqlite3, unittest
from malibu.database import dbmapper
from nose.tools import *
from sqlite3 import IntegrityError

class DBMapperTestCase(unittest.TestCase):

    def setUp(self):

        self.db = sqlite3.connect(":memory:")

    def tearDown(self):

        self.db.close()

    def recordCreate_test(self):

        dbm = DBMap(self.db)
        dbm.set_test_col("Test")
        dbm.set_example(True)
        dbm.set_description("This is a test.")

        self.assertEquals(dbm._test_col, "Test")
        self.assertEquals(dbm._example, True)
        self.assertEquals(dbm._description, "This is a test.")

    def recordLoad_test(self):

        dbm = DBMap(self.db)

        dbm.create()
        load_id = dbm.get_id()

        dbm.set_test_col("Test")
        dbm.set_example(True)
        dbm.set_description("This is a test.")

        del dbm

        dbm = DBMap(self.db)
        record = DBMap.load(id = load_id)

        self.assertEquals(record._test_col, "Test")
        self.assertEquals(record._example, True)
        self.assertEquals(record._description, "This is a test.")

    def recordNew_test(self):

        dbm = DBMap(self.db)

        record = DBMap.new(test_col = "Test", example = False, description = "This is not a test.")

        self.assertEquals(record._test_col, "Test")
        self.assertEquals(record._example, False)
        self.assertEquals(record._description, "This is not a test.")

    def recordNewLoad_test(self):

        dbm = DBMap(self.db)

        record = DBMap.new(test_col = "Test", example = False, description = "This is not a test.")

        self.assertEquals(record._test_col, "Test")
        self.assertEquals(record._example, False)
        self.assertEquals(record._description, "This is not a test.")

        del record

        record = DBMap.load(test_col = "Test")

        self.assertEquals(record._test_col, "Test")
        self.assertEquals(record._example, False)
        self.assertEquals(record._description, "This is not a test.")

    def recordFind_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find(example = False)

        self.assertEquals(len(result), 2)
        self.assertEquals(result[0]._test_col, "TestA")
        self.assertEquals(result[1]._test_col, "TestB")

    def recordFindAll_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()

        self.assertEquals(len(result), 2)
        self.assertEquals(result[0]._test_col, "TestA")
        self.assertEquals(result[1]._test_col, "TestB")

    def recordFilterEquals_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()
        result = result.filter_equals("test_col", "TestA")

        self.assertEquals(len(result), 1)
        self.assertEquals(result[0]._test_col, "TestA")

    def recordFilterIequals_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()
        result = result.filter_iequals("test_col", "testa")

        self.assertEquals(len(result), 1)
        self.assertEquals(result[0]._test_col, "TestA")

    def recordFilterInequals_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()
        result = result.filter_inequals("test_col", "TestB")

        self.assertEquals(len(result), 1)
        self.assertEquals(result[0]._test_col, "TestA")

    def recordFilterRegex_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()
        result = result.filter_regex("test_col", "Test[AB]")

        self.assertEquals(len(result), 2)
        self.assertEquals(result[0]._test_col, "TestA")
        self.assertEquals(result[1]._test_col, "TestB")

    def recordJoin_test(self):

        dbm = DBMap(self.db)
        dbl = DBMapLink(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        id_a = DBMap.load(test_col = "TestA").get_id()
        id_b = DBMap.load(test_col = "TestB").get_id()

        DBMapLink.new(map_id = id_a, some_text = "This definitely is not a test.")
        DBMapLink.new(map_id = id_b, some_text = "This might be a test.")
        DBMapLink.new(map_id = id_a, some_text = "This could be a test.")

        result = DBMap.join(DBMapLink, "id", "map_id")

        self.assertEquals(len(result), 3)
        self.assertEquals(result[0][0]._id, 1)
        self.assertEquals(result[0][1]._id, 1)
        self.assertEquals(result[1][0]._id, 2)
        self.assertEquals(result[1][1]._id, 2)
        self.assertEquals(result[2][0]._id, 1)
        self.assertEquals(result[2][1]._id, 3)

    def recordJoinFilter_test(self):

        dbm = DBMap(self.db)
        dbl = DBMapLink(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        id_a = DBMap.load(test_col = "TestA").get_id()
        id_b = DBMap.load(test_col = "TestB").get_id()

        DBMapLink.new(map_id = id_a, some_text = "This definitely is not a test.")
        DBMapLink.new(map_id = id_b, some_text = "This might be a test.")
        DBMapLink.new(map_id = id_a, some_text = "This could be a test.")

        result = DBMap.join(DBMapLink, "id", "map_id")

        self.assertEquals(len(result), 3)

    def recordUniqueConstraint_test(self):

        dbo = DBMap(self.db)
        dbo = DBMapLink(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        id_a = DBMap.load(test_col = "TestA").get_id()
        id_b = DBMap.load(test_col = "TestB").get_id()

        DBMapLink.new(map_id = id_a, some_text = "This definitely is not a test.", map_val = 'test')
        DBMapLink.new(map_id = id_b, some_text = "This might be a test.", map_val = 'notatest')

        self.assertRaises(IntegrityError, DBMapLink.new, **{'map_id' : id_a, 'some_text' : "This could be a test.", 'map_val' : 'test'})

class DBMap(dbmapper.DBMapper):

    def __init__(self, db):

        keys = ['id', 'test_col', 'example', 'description']
        ktypes = ['integer', 'text', 'boolean', 'text']

        DBMap.set_db_options(db, keys, ktypes)

        dbmapper.DBMapper.__init__(self, db, keys, ktypes)

class DBMapLink(dbmapper.DBMapper):

    def __init__(self, db):

        keys = ['id', 'map_id', 'some_text', 'map_val']
        ktypes = ['integer', 'integer', 'text', 'text']

        DBMapLink.set_db_options(db, keys, ktypes)

        options = dbmapper.DBMapper.get_default_options()
        options[dbmapper.DBMapper.INDEX_UNIQUE].append('map_val')

        dbmapper.DBMapper.__init__(self, db, keys, ktypes)
<commit_msg>1.0: changed options set order in dbmapper test.<commit_after>#!/usr/bin/env python2.7

import malibu, sqlite3, unittest
from malibu.database import dbmapper
from nose.tools import *
from sqlite3 import IntegrityError

class DBMapperTestCase(unittest.TestCase):

    def setUp(self):

        self.db = sqlite3.connect(":memory:")

    def tearDown(self):

        self.db.close()

    def recordCreate_test(self):

        dbm = DBMap(self.db)
        dbm.set_test_col("Test")
        dbm.set_example(True)
        dbm.set_description("This is a test.")

        self.assertEquals(dbm._test_col, "Test")
        self.assertEquals(dbm._example, True)
        self.assertEquals(dbm._description, "This is a test.")

    def recordLoad_test(self):

        dbm = DBMap(self.db)

        dbm.create()
        load_id = dbm.get_id()

        dbm.set_test_col("Test")
        dbm.set_example(True)
        dbm.set_description("This is a test.")

        del dbm

        dbm = DBMap(self.db)
        record = DBMap.load(id = load_id)

        self.assertEquals(record._test_col, "Test")
        self.assertEquals(record._example, True)
        self.assertEquals(record._description, "This is a test.")

    def recordNew_test(self):

        dbm = DBMap(self.db)

        record = DBMap.new(test_col = "Test", example = False, description = "This is not a test.")

        self.assertEquals(record._test_col, "Test")
        self.assertEquals(record._example, False)
        self.assertEquals(record._description, "This is not a test.")

    def recordNewLoad_test(self):

        dbm = DBMap(self.db)

        record = DBMap.new(test_col = "Test", example = False, description = "This is not a test.")

        self.assertEquals(record._test_col, "Test")
        self.assertEquals(record._example, False)
        self.assertEquals(record._description, "This is not a test.")

        del record

        record = DBMap.load(test_col = "Test")

        self.assertEquals(record._test_col, "Test")
        self.assertEquals(record._example, False)
        self.assertEquals(record._description, "This is not a test.")

    def recordFind_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find(example = False)

        self.assertEquals(len(result), 2)
        self.assertEquals(result[0]._test_col, "TestA")
        self.assertEquals(result[1]._test_col, "TestB")

    def recordFindAll_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()

        self.assertEquals(len(result), 2)
        self.assertEquals(result[0]._test_col, "TestA")
        self.assertEquals(result[1]._test_col, "TestB")

    def recordFilterEquals_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()
        result = result.filter_equals("test_col", "TestA")

        self.assertEquals(len(result), 1)
        self.assertEquals(result[0]._test_col, "TestA")

    def recordFilterIequals_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()
        result = result.filter_iequals("test_col", "testa")

        self.assertEquals(len(result), 1)
        self.assertEquals(result[0]._test_col, "TestA")

    def recordFilterInequals_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()
        result = result.filter_inequals("test_col", "TestB")

        self.assertEquals(len(result), 1)
        self.assertEquals(result[0]._test_col, "TestA")

    def recordFilterRegex_test(self):

        dbm = DBMap(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        result = DBMap.find_all()
        result = result.filter_regex("test_col", "Test[AB]")

        self.assertEquals(len(result), 2)
        self.assertEquals(result[0]._test_col, "TestA")
        self.assertEquals(result[1]._test_col, "TestB")

    def recordJoin_test(self):

        dbm = DBMap(self.db)
        dbl = DBMapLink(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        id_a = DBMap.load(test_col = "TestA").get_id()
        id_b = DBMap.load(test_col = "TestB").get_id()

        DBMapLink.new(map_id = id_a, some_text = "This definitely is not a test.")
        DBMapLink.new(map_id = id_b, some_text = "This might be a test.")
        DBMapLink.new(map_id = id_a, some_text = "This could be a test.")

        result = DBMap.join(DBMapLink, "id", "map_id")

        self.assertEquals(len(result), 3)
        self.assertEquals(result[0][0]._id, 1)
        self.assertEquals(result[0][1]._id, 1)
        self.assertEquals(result[1][0]._id, 2)
        self.assertEquals(result[1][1]._id, 2)
        self.assertEquals(result[2][0]._id, 1)
        self.assertEquals(result[2][1]._id, 3)

    def recordJoinFilter_test(self):

        dbm = DBMap(self.db)
        dbl = DBMapLink(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        id_a = DBMap.load(test_col = "TestA").get_id()
        id_b = DBMap.load(test_col = "TestB").get_id()

        DBMapLink.new(map_id = id_a, some_text = "This definitely is not a test.")
        DBMapLink.new(map_id = id_b, some_text = "This might be a test.")
        DBMapLink.new(map_id = id_a, some_text = "This could be a test.")

        result = DBMap.join(DBMapLink, "id", "map_id")

        self.assertEquals(len(result), 3)

    def recordUniqueConstraint_test(self):

        dbo = DBMap(self.db)
        dbo = DBMapLink(self.db)

        DBMap.new(test_col = "TestA", example = False, description = "This is not a test.")
        DBMap.new(test_col = "TestB", example = False, description = "This is still not a test.")

        id_a = DBMap.load(test_col = "TestA").get_id()
        id_b = DBMap.load(test_col = "TestB").get_id()

        DBMapLink.new(map_id = id_a, some_text = "This definitely is not a test.", map_val = 'test')
        DBMapLink.new(map_id = id_b, some_text = "This might be a test.", map_val = 'notatest')

        self.assertRaises(IntegrityError, DBMapLink.new, **{'map_id' : id_a, 'some_text' : "This could be a test.", 'map_val' : 'test'})

class DBMap(dbmapper.DBMapper):

    def __init__(self, db):

        keys = ['id', 'test_col', 'example', 'description']
        ktypes = ['integer', 'text', 'boolean', 'text']

        DBMap.set_db_options(db, keys, ktypes)

        dbmapper.DBMapper.__init__(self, db, keys, ktypes)

class DBMapLink(dbmapper.DBMapper):

    def __init__(self, db):

        keys = ['id', 'map_id', 'some_text', 'map_val']
        ktypes = ['integer', 'integer', 'text', 'text']

        options = dbmapper.DBMapper.get_default_options()
        options[dbmapper.DBMapper.INDEX_UNIQUE].append('map_val')

        DBMapLink.set_db_options(db, keys, ktypes)

        dbmapper.DBMapper.__init__(self, db, keys, ktypes)
<|endoftext|>


<commit_before>##==============================================================#
## SECTION: Imports                                             #
##==============================================================#

from testlib import *

from auxly.filesys import File

##==============================================================#
## SECTION: Class Definitions                                   #
##==============================================================#

class TestCase(BaseTest):
    def test_file_1(test):
        """Basic File usage."""
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.write("hello"))
        test.assertTrue(f.append(" world"))
        test.assertTrue(f.exists())
        test.assertEqual("hello world", f.read())
        test.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", f.checksum())

    def test_file_2(test):
        """Basic File usage."""
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.write("hello"))
        test.assertTrue(f.write("world"))
        test.assertEqual("world", f.read())
        test.assertTrue(f.erase())
        test.assertEqual("", f.read())

    def test_file_3(test):
        """Basic File usage."""
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.write(123))
        test.assertEqual("123", f.read())

    def test_file_4(test):
        """Basic File usage."""
        import os
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.writeline("hello"))
        test.assertEqual("hello" + os.linesep, f.read())

    def test_file_5(test):
        """Basic File usage."""
        import os
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.writeline("hello"))
        test.assertTrue(f.append("world"))
        test.assertEqual("hello" + os.linesep + "world", f.read())

    def test_file_6(test):
        """Basic File usage."""
        import os
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.writeline("hello"))
        test.assertTrue(f.appendline("world"))
        test.assertEqual("hello" + os.linesep + "world" + os.linesep, f.read())

##==============================================================#
## SECTION: Main Body                                           #
##==============================================================#

if __name__ == '__main__':
    unittest.main()
<commit_msg>Minor test updates.<commit_after>##==============================================================#
## SECTION: Imports                                             #
##==============================================================#

from testlib import *

from auxly.filesys import File

##==============================================================#
## SECTION: Class Definitions                                   #
##==============================================================#

class TestCase(BaseTest):
    def test_file_1(test):
        """Basic File usage."""
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.write("hello"))
        test.assertTrue(f.append(" world"))
        test.assertTrue(f.exists())
        test.assertEqual("hello world", f.read())
        test.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", f.checksum())

    def test_file_2(test):
        """Basic File usage."""
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.write("hello"))
        test.assertTrue(f.write("world"))
        test.assertEqual("world", f.read())
        test.assertFalse(f.isempty())
        test.assertTrue(f.empty())
        test.assertEqual("", f.read())
        test.assertTrue(f.isempty())

    def test_file_3(test):
        """Basic File usage."""
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.write(123))
        test.assertEqual("123", f.read())

    def test_file_4(test):
        """Basic File usage."""
        import os
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.writeline("hello"))
        test.assertEqual("hello" + os.linesep, f.read())

    def test_file_5(test):
        """Basic File usage."""
        import os
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.writeline("hello"))
        test.assertTrue(f.append("world"))
        test.assertEqual("hello" + os.linesep + "world", f.read())

    def test_file_6(test):
        """Basic File usage."""
        import os
        p = FNAME[0]
        f = File(p)
        test.assertFalse(f.exists())
        test.assertTrue(f.writeline("hello"))
        test.assertTrue(f.appendline("world"))
        test.assertEqual("hello" + os.linesep + "world" + os.linesep, f.read())

##==============================================================#
## SECTION: Main Body                                           #
##==============================================================#

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before># Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tempfile
import unittest

from nototools.gpos_diff import GposDiffFinder
from hb_input_test import make_font

class GposDiffFinderText(unittest.TestCase):
    def _expect_kerning_diffs(self, source_a, source_b, pairs, values):
        font_a = make_font('feature kern {\n%s\n} kern;' % source_a)
        font_b = make_font('feature kern {\n%s\n} kern;' % source_b)
        file_a = tempfile.NamedTemporaryFile()
        file_b = tempfile.NamedTemporaryFile()
        font_a.save(file_a.name)
        font_b.save(file_b.name)
        finder = GposDiffFinder(file_a.name, file_b.name, 0, 100)

        diffs = finder.find_kerning_diffs()
        self.assertIn('%d differences in kerning pairs' % len(pairs), diffs)
        for pair_diff in pairs:
            self.assertIn('%s pos %s %s %s' % pair_diff, diffs)
        self.assertIn('%d differences in kerning values' % len(values), diffs)
        for value_diff in values:
            self.assertIn('pos %s %s: %s vs %s' % value_diff, diffs)

    def test_simple(self):
        self._expect_kerning_diffs('''
                pos a b -10;
                pos a c -20;
            ''', '''
                pos a b -30;
                pos a d -40;
            ''',
            [('-', 'a', 'c', [-20]), ('+', 'a', 'd', [-40])],
            [('a', 'b', [-10], [-30])])

    def test_multiple_rules(self):
        self._expect_kerning_diffs('''
                @a_b = [a b];
                pos a d -10;
                pos @a_b d -20;
            ''', '''
                pos a d -30;
            ''',
            [('-', 'b', 'd', [-20])],
            [('a', 'd', [-10, -20], [-30])])

    def test_single_vs_class(self):
        self._expect_kerning_diffs('''
                pos a d -10;
            ''', '''
                @a_b = [a b];
                pos @a_b d -20;
            ''',
            [('+', 'b', 'd', [-20])],
            [('a', 'd', [-10], [-20])])

if __name__ == '__main__':
    unittest.main()
<commit_msg>Renamed current gpos tests so we can distinguish between kern and mark tests<commit_after># Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tempfile
import unittest

from nototools.gpos_diff import GposDiffFinder
from hb_input_test import make_font

class GposDiffFinderText(unittest.TestCase):
    def _expect_kerning_diffs(self, source_a, source_b, pairs, values):
        font_a = make_font('feature kern {\n%s\n} kern;' % source_a)
        font_b = make_font('feature kern {\n%s\n} kern;' % source_b)
        file_a = tempfile.NamedTemporaryFile()
        file_b = tempfile.NamedTemporaryFile()
        font_a.save(file_a.name)
        font_b.save(file_b.name)
        finder = GposDiffFinder(file_a.name, file_b.name, 0, 100)

        diffs = finder.find_kerning_diffs()
        self.assertIn('%d differences in kerning pairs' % len(pairs), diffs)
        for pair_diff in pairs:
            self.assertIn('%s pos %s %s %s' % pair_diff, diffs)
        self.assertIn('%d differences in kerning values' % len(values), diffs)
        for value_diff in values:
            self.assertIn('pos %s %s: %s vs %s' % value_diff, diffs)

    def test_kern_simple(self):
        self._expect_kerning_diffs('''
                pos a b -10;
                pos a c -20;
            ''', '''
                pos a b -30;
                pos a d -40;
            ''',
            [('-', 'a', 'c', [-20]), ('+', 'a', 'd', [-40])],
            [('a', 'b', [-10], [-30])])

    def test_kern_multiple_rules(self):
        self._expect_kerning_diffs('''
                @a_b = [a b];
                pos a d -10;
                pos @a_b d -20;
            ''', '''
                pos a d -30;
            ''',
            [('-', 'b', 'd', [-20])],
            [('a', 'd', [-10, -20], [-30])])

    def test_kern_single_vs_class(self):
        self._expect_kerning_diffs('''
                pos a d -10;
            ''', '''
                @a_b = [a b];
                pos @a_b d -20;
            ''',
            [('+', 'b', 'd', [-20])],
            [('a', 'd', [-10], [-20])])

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>import datetime
from framework.auth.core import User
from tests.base import OsfTestCase
from tests.factories import UserFactory, NodeLogFactory
from nose.tools import *  # PEP8 asserts
from website.project.model import Node, NodeLog

from scripts.analytics.node_log_events import NodeLogEvents

class TestNodeLogAnalytics(OsfTestCase):

    def setUp(self):
        super(TestNodeLogAnalytics, self).setUp()

        self.user_one = UserFactory()
        self.user_two = UserFactory()

        # Two node logs for user one
        self.node_log_node_created = NodeLogFactory(action='node_created', user=self.user_one)
        self.node_log_file_added = NodeLogFactory(action='file_added', user=self.user_one)

        # Two node logs for user two
        self.node_log_wiki_updated = NodeLogFactory(action='wiki_updated', user=self.user_two)
        self.node_log_project_created = NodeLogFactory(action='project_created', user=self.user_two)

        self.end_date = datetime.datetime.utcnow() - datetime.timedelta(1)

        for node_log in NodeLog.find():
            node_log.date = self.end_date - datetime.timedelta(0.1)
            node_log.save()

        self.results = NodeLogEvents().get_events(self.end_date.date())

        self.node_log_node_created.reload()
        self.node_log_file_added.reload()
        self.node_log_wiki_updated.reload()
        self.node_log_project_created.reload()

    def tearDown(self):
        super(TestNodeLogAnalytics, self).tearDown()
        Node.remove()
        User.remove()

    def test_results_structure(self):
        expected = [
            {
                'keen': {'timestamp': self.node_log_node_created.date.isoformat()},
                'date': self.node_log_node_created.date.isoformat(),
                'action': 'node_created',
                'user_id': self.user_one._id
            },
            {
                'keen': {'timestamp': self.node_log_file_added.date.isoformat()},
                'date': self.node_log_file_added.date.isoformat(),
                'action': 'file_added',
                'user_id': self.user_one._id
            },
            {
                'keen': {'timestamp': self.node_log_wiki_updated.date.isoformat()},
                'date': self.node_log_wiki_updated.date.isoformat(),
                'action': 'wiki_updated',
                'user_id': self.user_two._id
            },
            {
                'keen': {'timestamp': self.node_log_project_created.date.isoformat()},
                'date': self.node_log_project_created.date.isoformat(),
                'action': 'project_created',
                'user_id': self.user_two._id
            }
        ]

        assert_items_equal(expected, self.results)

<commit_msg>Fix tzinfo in node_log_event tests<commit_after>import pytz
import datetime
from framework.auth.core import User
from tests.base import OsfTestCase
from tests.factories import UserFactory, NodeLogFactory
from nose.tools import *  # PEP8 asserts
from website.project.model import Node, NodeLog

from scripts.analytics.node_log_events import NodeLogEvents

class TestNodeLogAnalytics(OsfTestCase):

    def setUp(self):
        super(TestNodeLogAnalytics, self).setUp()

        self.user_one = UserFactory()
        self.user_two = UserFactory()

        # Two node logs for user one
        self.node_log_node_created = NodeLogFactory(action='node_created', user=self.user_one)
        self.node_log_file_added = NodeLogFactory(action='file_added', user=self.user_one)

        # Two node logs for user two
        self.node_log_wiki_updated = NodeLogFactory(action='wiki_updated', user=self.user_two)
        self.node_log_project_created = NodeLogFactory(action='project_created', user=self.user_two)

        self.end_date = datetime.datetime.utcnow() - datetime.timedelta(1)

        for node_log in NodeLog.find():
            node_log.date = self.end_date - datetime.timedelta(0.1)
            node_log.save()

        self.results = NodeLogEvents().get_events(self.end_date.date())

        self.node_log_node_created.reload()
        self.node_log_file_added.reload()
        self.node_log_wiki_updated.reload()
        self.node_log_project_created.reload()

    def tearDown(self):
        super(TestNodeLogAnalytics, self).tearDown()
        Node.remove()
        User.remove()

    def test_results_structure(self):
        expected = [
            {
                'keen': {'timestamp': self.node_log_node_created.date.replace(tzinfo=pytz.UTC).isoformat()},
                'date': self.node_log_node_created.date.replace(tzinfo=pytz.UTC).isoformat(),
                'action': 'node_created',
                'user_id': self.user_one._id
            },
            {
                'keen': {'timestamp': self.node_log_file_added.date.replace(tzinfo=pytz.UTC).isoformat()},
                'date': self.node_log_file_added.date.replace(tzinfo=pytz.UTC).isoformat(),
                'action': 'file_added',
                'user_id': self.user_one._id
            },
            {
                'keen': {'timestamp': self.node_log_wiki_updated.date.replace(tzinfo=pytz.UTC).isoformat()},
                'date': self.node_log_wiki_updated.date.replace(tzinfo=pytz.UTC).isoformat(),
                'action': 'wiki_updated',
                'user_id': self.user_two._id
            },
            {
                'keen': {'timestamp': self.node_log_project_created.date.replace(tzinfo=pytz.UTC).isoformat()},
                'date': self.node_log_project_created.date.replace(tzinfo=pytz.UTC).isoformat(),
                'action': 'project_created',
                'user_id': self.user_two._id
            }
        ]

        assert_items_equal(expected, self.results)

<|endoftext|>


<commit_before># from re import search
from scripttest import TestFileEnvironment

bindir = ""
script = "prepare_graphprot_seqs.py"
# test file environment
testdir = "test/testenv_prepare_graphprot_seqs"
# directories relative to test file environment
bindir_rel = "../../" + bindir
datadir_rel = "../../test/"

env = TestFileEnvironment(testdir)

def test_no_such_genome_id():
    """Try using an unknown genome id for automatic chromosome limits extraction."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        expect_error=True
    )
    # assert search("chromosome_limits", run.stdout), "Error message did not contain reference to 'chromosome_limits', was :'\n{}'".format(run.stdout)
    assert(run.returncode != 0)

def test_stranded_shuffling_plus():
    """Test if negatives are shuffled on the correct strand. Shuffling plus strand peaks on a minus strand region should result in zero negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords_onlyplus.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        "--chromosome_limits", datadir_rel + "artificial_genome.limits",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_plus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_minus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 0" in run.stderr, "Error, expecting zero negative instances."

def test_stranded_shuffling_minus():
    """Test if negatives are shuffled on the correct strand. Shuffling minus strand peaks on a plus strand region should result in zero negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords_onlyminus.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        "--chromosome_limits", datadir_rel + "artificial_genome.limits",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_minus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_plus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 0" in run.stderr, "Error, expecting zero negative instances." + run.stderr + run.stdout

def test_stranded_shuffling_on_plus():
    """Test if negatives are shuffled on the correct strand. Shuffling minus two plus strand peaks and one minus strand peak on a plus strand region should result in two negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        "--chromosome_limits", datadir_rel + "artificial_genome.limits",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_on_plus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_plus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 2" in run.stderr, "Error, expecting two negative instances." + run.stdout + run.stderr

def test_stranded_shuffling_minus_genomedef():
    """Test if negatives are shuffled on the correct strand. Shuffling minus strand peaks on a plus strand region should result in zero negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords_onlyminus.bed",
        "hg19",
        datadir_rel + "artificial_genome.fa",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_minus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_plus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 0" in run.stderr, "Error, expecting zero negative instances." + run.stderr + run.stdout

def test_stranded_shuffling_on_plus_genomedef():
    """Test if negatives are shuffled on the correct strand. Shuffling minus two plus strand peaks and one minus strand peak on a plus strand region should result in two negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords.bed",
        "hg19",
        datadir_rel + "artificial_genome.fa",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_on_plus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_plus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 2" in run.stderr, "Error, expecting two negative instances." + run.stdout + run.stderr

def test_stranded_shuffling_on_minus():
    """Test if negatives are shuffled on the correct strand. Shuffling minus two plus strand peaks and one minus strand peak on a minus strand region should result in one negative."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        "--chromosome_limits", datadir_rel + "artificial_genome.limits",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_on_minus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_minus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 1" in run.stderr, "Error, expecting one negative instance." + run.stdout + run.stderr
<commit_msg>fix scripttest tests<commit_after># from re import search
from scripttest import TestFileEnvironment

bindir = "bin/"
script = "prepare_graphprot_seqs.py"
# test file environment
testdir = "test/testenv_prepare_graphprot_seqs"
# directories relative to test file environment
bindir_rel = "../../" + bindir
datadir_rel = "../../test/"

env = TestFileEnvironment(testdir)

def test_no_such_genome_id():
    """Try using an unknown genome id for automatic chromosome limits extraction."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        expect_error=True
    )
    # assert search("chromosome_limits", run.stdout), "Error message did not contain reference to 'chromosome_limits', was :'\n{}'".format(run.stdout)
    assert(run.returncode != 0)

def test_stranded_shuffling_plus():
    """Test if negatives are shuffled on the correct strand. Shuffling plus strand peaks on a minus strand region should result in zero negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords_onlyplus.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        "--chromosome_limits", datadir_rel + "artificial_genome.limits",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_plus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_minus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 0" in run.stderr, "Error, expecting zero negative instances."

def test_stranded_shuffling_minus():
    """Test if negatives are shuffled on the correct strand. Shuffling minus strand peaks on a plus strand region should result in zero negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords_onlyminus.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        "--chromosome_limits", datadir_rel + "artificial_genome.limits",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_minus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_plus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 0" in run.stderr, "Error, expecting zero negative instances." + run.stderr + run.stdout

def test_stranded_shuffling_on_plus():
    """Test if negatives are shuffled on the correct strand. Shuffling minus two plus strand peaks and one minus strand peak on a plus strand region should result in two negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        "--chromosome_limits", datadir_rel + "artificial_genome.limits",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_on_plus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_plus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 2" in run.stderr, "Error, expecting two negative instances." + run.stdout + run.stderr

def test_stranded_shuffling_minus_genomedef():
    """Test if negatives are shuffled on the correct strand. Shuffling minus strand peaks on a plus strand region should result in zero negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords_onlyminus.bed",
        "hg19",
        datadir_rel + "artificial_genome.fa",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_minus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_plus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 0" in run.stderr, "Error, expecting zero negative instances." + run.stderr + run.stdout

def test_stranded_shuffling_on_plus_genomedef():
    """Test if negatives are shuffled on the correct strand. Shuffling minus two plus strand peaks and one minus strand peak on a plus strand region should result in two negatives."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords.bed",
        "hg19",
        datadir_rel + "artificial_genome.fa",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_on_plus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_plus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 2" in run.stderr, "Error, expecting two negative instances." + run.stdout + run.stderr

def test_stranded_shuffling_on_minus():
    """Test if negatives are shuffled on the correct strand. Shuffling minus two plus strand peaks and one minus strand peak on a minus strand region should result in one negative."""
    run = env.run(
        bindir_rel + script,
        datadir_rel + "artificial_coords.bed",
        "unknown_genome",
        datadir_rel + "artificial_genome.fa",
        "--chromosome_limits", datadir_rel + "artificial_genome.limits",
        "--seq_length", "20",
        "--core_length", "10",
        "--output_file_prefix", "test_stranded_shuffling_on_minus",
        "--negative_site_candidate_regions_fn", datadir_rel + "artificial_candidate_region_minus.bed",
        "-v",
        expect_stderr=True,
    )
    assert "derived negative cores: 1" in run.stderr, "Error, expecting one negative instance." + run.stdout + run.stderr
<|endoftext|>


<commit_before>from ..tools.velocity_embedding import velocity_embedding as compute_velocity_embedding
from .utils import interpret_colorkey, default_basis, default_size, get_components, savefig
from .scatter import scatter
from .docs import doc_scatter, doc_params

from matplotlib import rcParams
from matplotlib.colors import is_color_like
import matplotlib.pyplot as pl
import numpy as np
import pandas as pd

@doc_params(scatter=doc_scatter)
def velocity_embedding(adata, basis=None, vkey='velocity', density=1, scale=1, X=None, V=None, color=None, use_raw=None,
                       layer=None, color_map=None, colorbar=False, palette=None, size=None, alpha=.2, perc=None,
                       sort_order=True, groups=None, components=None, projection='2d', legend_loc='none', legend_fontsize=None,
                       legend_fontweight=None, right_margin=None, left_margin=None, xlabel=None, ylabel=None, title=None,
                       fontsize=None, figsize=None, dpi=None, frameon=None, show=True, save=None, ax=None, **kwargs):
    """\
    Scatter plot of velocities on the embedding

    Arguments
    ---------
    adata: :class:`~anndata.AnnData`
        Annotated data matrix.
    x: `str`, `np.ndarray` or `None` (default: `None`)
        x coordinate
    y: `str`, `np.ndarray` or `None` (default: `None`)
        y coordinate
    vkey: `str` or `None` (default: `None`)
        Key for annotations of observations/cells or variables/genes.
    density: `float` (default: 1)
        Amount of velocities to show - 0 none to 1 all
    scale: `float` (default: 1)
        Length of velocities in the embedding.
    {scatter}

    Returns
    -------
        `matplotlib.Axis` if `show==False`
    """
    basis = default_basis(adata) if basis is None else basis
    colors = pd.unique(color) if isinstance(color, (list, tuple, np.record)) else [color]
    layers = pd.unique(layer) if isinstance(layer, (list, tuple, np.ndarray, np.record)) else [layer]
    vkeys = pd.unique(vkey) if isinstance(vkey, (list, tuple, np.ndarray, np.record)) else [vkey]
    for key in vkeys:
        if key + '_' + basis not in adata.obsm_keys() and V is None:
            compute_velocity_embedding(adata, basis=basis, vkey=key)

    if len(colors) > 1:
        figsize = rcParams['figure.figsize'] if figsize is None else figsize
        for i, gs in enumerate(pl.GridSpec(1, len(colors), pl.figure(None, (figsize[0] * len(colors), figsize[1]), dpi=dpi))):
            velocity_embedding(adata, basis=basis, vkey=vkey, layer=layer, density=density, scale=scale,
                               perc=perc, color=colors[i], use_raw=use_raw, sort_order=sort_order, X=X, V=V,
                               alpha=alpha, groups=groups, components=components, projection=projection, legend_loc=legend_loc,
                               legend_fontsize=legend_fontsize, legend_fontweight=legend_fontweight,
                               color_map=color_map, palette=palette, frameon=frameon, right_margin=right_margin,
                               left_margin=left_margin, size=size, title=title, show=False, figsize=figsize, dpi=dpi,
                               save=None, ax=pl.subplot(gs), xlabel=xlabel, ylabel=ylabel, colorbar=colorbar,
                               fontsize=fontsize, **kwargs)
        if isinstance(save, str): savefig('' if basis is None else basis, dpi=dpi, save=save, show=show)
        if show: pl.show()
        else: return ax

    elif len(layers) > 1:
        figsize = rcParams['figure.figsize'] if figsize is None else figsize
        for i, gs in enumerate(pl.GridSpec(1, len(layers), pl.figure(None, (figsize[0] * len(layers), figsize[1]), dpi=dpi))):
            velocity_embedding(adata, basis=basis, vkey=vkey, layer=layers[i], density=density, scale=scale,
                               perc=None, color=color, use_raw=use_raw, sort_order=sort_order, X=X, V=V, alpha=alpha,
                               groups=groups, components=components, projection=projection, legend_loc=legend_loc,
                               legend_fontsize=legend_fontsize, legend_fontweight=legend_fontweight,
                               color_map=color_map, palette=palette, frameon=frameon, right_margin=right_margin,
                               left_margin=left_margin, size=size, title=title, show=False, figsize=figsize, dpi=dpi,
                               save=None, ax=pl.subplot(gs), xlabel=xlabel, ylabel=ylabel, colorbar=colorbar,
                               fontsize=fontsize, **kwargs)
        if isinstance(save, str): savefig('' if basis is None else basis, dpi=dpi, save=save, show=show)
        if show: pl.show()
        else: return ax

    elif len(vkeys) > 1:
        figsize = rcParams['figure.figsize'] if figsize is None else figsize
        for i, gs in enumerate(pl.GridSpec(1, len(vkeys), pl.figure(None, (figsize[0] * len(vkeys), figsize[1]), dpi=dpi))):
            velocity_embedding(adata, basis=basis, vkey=vkeys[i], layer=layer, density=density, scale=scale,
                               perc=None, color=color, use_raw=use_raw, sort_order=sort_order, X=X, V=V, alpha=alpha,
                               groups=groups, components=components, projection=projection, legend_loc=legend_loc,
                               legend_fontsize=legend_fontsize, legend_fontweight=legend_fontweight,
                               color_map=color_map, palette=palette, frameon=frameon, right_margin=right_margin,
                               left_margin=left_margin, size=size, title=title, show=False, figsize=figsize, dpi=dpi,
                               save=None, ax=pl.subplot(gs), xlabel=xlabel, ylabel=ylabel, colorbar=colorbar,
                               fontsize=fontsize, **kwargs)
        if isinstance(save, str): savefig('' if basis is None else basis, dpi=dpi, save=save, show=show)
        if show: pl.show()
        else: return ax

    else:
        if projection == '3d':
            from mpl_toolkits.mplot3d import Axes3D
            ax = pl.figure(None, figsize, dpi=dpi).gca(projection=projection) if ax is None else ax
        else:
            ax = pl.figure(None, figsize, dpi=dpi).gca() if ax is None else ax

        color, layer, vkey = colors[0], layers[0], vkeys[0]
        ix_choice = np.random.choice(adata.n_obs, size=int(density * adata.n_obs), replace=False)

        x, y = None if X is None else X[:, 0], None if X is None else X[:, 1]
        X = adata.obsm['X_' + basis][:, get_components(components, basis)][ix_choice] if X is None else X[:, :2][ix_choice]
        V = adata.obsm[vkey + '_' + basis][:, get_components(components, basis)][ix_choice] if V is None else V[:, :2][ix_choice]

        quiver_kwargs = {"scale": scale, "cmap": color_map, "angles": 'xy', "scale_units": 'xy', "width": .0005,
                         "edgecolors": 'k', "headlength": 12, "headwidth": 10, "headaxislength": 8, "linewidth": .1}
        quiver_kwargs.update(kwargs)

        c = interpret_colorkey(adata, color, layer, perc)
        c = c[ix_choice] if len(c) == adata.n_obs else c

        if is_color_like(c[0]): pl.quiver(X[:, 0], X[:, 1], V[:, 0], V[:, 1], color=c, zorder=3, **quiver_kwargs)
        else: pl.quiver(X[:, 0], X[:, 1], V[:, 0], V[:, 1], c, zorder=3, **quiver_kwargs)

        size = default_size(adata)/2 if size is None else size
        ax = scatter(adata, x=x, y=y, basis=basis, layer=layer, color=color, xlabel=xlabel, ylabel=ylabel, color_map=color_map,
                     perc=perc, size=size, alpha=alpha, fontsize=fontsize, frameon=frameon, title=title, show=False,
                     colorbar=colorbar, components=components, figsize=figsize, dpi=dpi, save=None, ax=ax,
                     use_raw=use_raw, sort_order=sort_order, groups=groups, projection=projection,
                     legend_loc=legend_loc, legend_fontsize=legend_fontsize, legend_fontweight=legend_fontweight,
                     palette=palette, right_margin=right_margin, left_margin=left_margin)

        if isinstance(save, str): savefig('' if basis is None else basis, dpi=dpi, save=save, show=show)
        if show: pl.show()
        else: return ax
<commit_msg>allow groups visualization in velocity_embedding<commit_after>from ..tools.velocity_embedding import velocity_embedding as compute_velocity_embedding
from ..tools.utils import groups_to_bool
from .utils import interpret_colorkey, default_basis, default_size, get_components, savefig, default_color
from .scatter import scatter
from .docs import doc_scatter, doc_params

from matplotlib import rcParams
from matplotlib.colors import is_color_like
import matplotlib.pyplot as pl
import numpy as np
import pandas as pd

@doc_params(scatter=doc_scatter)
def velocity_embedding(adata, basis=None, vkey='velocity', density=1, scale=1, X=None, V=None, color=None, use_raw=None,
                       layer=None, color_map=None, colorbar=False, palette=None, size=None, alpha=.2, perc=None,
                       sort_order=True, groups=None, components=None, projection='2d', legend_loc='none', legend_fontsize=None,
                       legend_fontweight=None, right_margin=None, left_margin=None, xlabel=None, ylabel=None, title=None,
                       fontsize=None, figsize=None, dpi=None, frameon=None, show=True, save=None, ax=None, **kwargs):
    """\
    Scatter plot of velocities on the embedding

    Arguments
    ---------
    adata: :class:`~anndata.AnnData`
        Annotated data matrix.
    x: `str`, `np.ndarray` or `None` (default: `None`)
        x coordinate
    y: `str`, `np.ndarray` or `None` (default: `None`)
        y coordinate
    vkey: `str` or `None` (default: `None`)
        Key for annotations of observations/cells or variables/genes.
    density: `float` (default: 1)
        Amount of velocities to show - 0 none to 1 all
    scale: `float` (default: 1)
        Length of velocities in the embedding.
    {scatter}

    Returns
    -------
        `matplotlib.Axis` if `show==False`
    """
    basis = default_basis(adata) if basis is None else basis
    colors = pd.unique(color) if isinstance(color, (list, tuple, np.record)) else [color]
    layers = pd.unique(layer) if isinstance(layer, (list, tuple, np.ndarray, np.record)) else [layer]
    vkeys = pd.unique(vkey) if isinstance(vkey, (list, tuple, np.ndarray, np.record)) else [vkey]
    for key in vkeys:
        if key + '_' + basis not in adata.obsm_keys() and V is None:
            compute_velocity_embedding(adata, basis=basis, vkey=key)

    scatter_kwargs = {"basis": basis, "perc": perc, "use_raw": use_raw, "sort_order": sort_order, "alpha": alpha,
                      "components": components, "projection": projection, "legend_loc": legend_loc, "groups": groups,
                      "legend_fontsize": legend_fontsize, "legend_fontweight": legend_fontweight, "palette": palette,
                      "color_map": color_map, "frameon": frameon, "title": title, "xlabel": xlabel, "ylabel": ylabel,
                      "right_margin": right_margin, "left_margin": left_margin, "colorbar": colorbar, "dpi": dpi,
                      "fontsize": fontsize, "show": False, "save": None}

    multikey = colors if len(colors) > 1 else layers if len(layers) > 1 else vkeys if len(vkeys) > 1 else None
    if multikey is not None:
        figsize = rcParams['figure.figsize'] if figsize is None else figsize
        for i, gs in enumerate(pl.GridSpec(1, len(multikey), pl.figure(None, (figsize[0]*len(multikey), figsize[1]), dpi=dpi))):
            velocity_embedding(adata, X=X, V=V, density=density, scale=scale, size=size, ax=pl.subplot(gs),
                               color=colors[i] if len(colors) > 1 else color,
                               layer=layers[i] if len(layers) > 1 else layer,
                               vkey=vkeys[i] if len(vkeys) > 1 else vkey, **scatter_kwargs, **kwargs)
        if isinstance(save, str): savefig('' if basis is None else basis, dpi=dpi, save=save, show=show)
        if show:
            pl.show()
        else:
            return ax

    else:
        if projection == '3d':
            from mpl_toolkits.mplot3d import Axes3D
            ax = pl.figure(None, figsize, dpi=dpi).gca(projection=projection) if ax is None else ax
        else:
            ax = pl.figure(None, figsize, dpi=dpi).gca() if ax is None else ax

        color, layer, vkey = colors[0], layers[0], vkeys[0]
        color = default_color(adata) if color is None else color
        _adata = adata[groups_to_bool(adata, groups, groupby=color)] if groups is not None and color in adata.obs.keys() else adata

        ix_choice = np.random.choice(_adata.n_obs, size=int(density * _adata.n_obs), replace=False)

        x, y = None if X is None else X[:, 0], None if X is None else X[:, 1]
        X = _adata.obsm['X_' + basis][:, get_components(components, basis)][ix_choice] if X is None else X[:, :2][ix_choice]
        V = _adata.obsm[vkey + '_' + basis][:, get_components(components, basis)][ix_choice] if V is None else V[:, :2][ix_choice]

        quiver_kwargs = {"scale": scale, "cmap": color_map, "angles": 'xy', "scale_units": 'xy', "width": .0005,
                         "edgecolors": 'k', "headlength": 12, "headwidth": 10, "headaxislength": 8, "linewidth": .1}
        quiver_kwargs.update(kwargs)

        c = interpret_colorkey(_adata, color, layer, perc)
        c = c[ix_choice] if len(c) == _adata.n_obs else c

        if is_color_like(c[0]): pl.quiver(X[:, 0], X[:, 1], V[:, 0], V[:, 1], color=c, zorder=3, **quiver_kwargs)
        else: pl.quiver(X[:, 0], X[:, 1], V[:, 0], V[:, 1], c, zorder=3, **quiver_kwargs)

        size = default_size(adata) / 2 if size is None else size,
        ax = scatter(adata, x=x, y=y, layer=layer, color=color, size=size, ax=ax, **scatter_kwargs)

        if isinstance(save, str): savefig('' if basis is None else basis, dpi=dpi, save=save, show=show)
        if show: pl.show()
        else: return ax
<|endoftext|>


<commit_before>import argparse
import os
from pkg_resources import find_distributions

from .checker import Checker
from .loader import Loader

def make_local_settings(argv=None):
    """Generate a local settings file."""
    parser = argparse.ArgumentParser(description=make_local_settings.__doc__)

    parser.add_argument('env', help='Environment name (e.g., dev or prod)')
    parser.add_argument(
        '-b', '--base-settings-module', default=None,
        help='Base settings module as a dotted path')
    parser.add_argument(
        '-f', '--file-name', default=None,
        help='Name of file to write settings into')
    parser.add_argument(
        '-s', '--section', default=None,
        help='Section to read/write settings from/to (default = <env>)')
    parser.add_argument('-o', '--overwrite', action='store_true', default=False)

    args = parser.parse_args(argv)

    os.environ['DISABLE_LOCAL_SETTINGS'] = '1'
    if args.base_settings_module is None:
        dist = next(find_distributions('.', only=True), None)
        if dist is not None:
            args.base_settings_module = '{0.project_name}.settings'.format(dist)
        else:
            msg = 'Could not guess which base settings module to use; specify with -b'
            parser.exit(1, msg)

    module = __import__(args.base_settings_module, fromlist=[''])
    base_settings = vars(module)

    if args.file_name is None:
        file_name = 'local.{0.env}.cfg'.format(args)
        section = args.env
    elif '#' in args.file_name:
        file_name, section = args.file_name.rsplit('#', 1)
    else:
        file_name = args.file_name
        section = args.env

    file_name = os.path.normpath(os.path.abspath(file_name))

    if args.section:
        section = args.section

    registry = None

    if os.path.exists(file_name):
        if args.overwrite:
            print('Overwriting {0.file_name}'.format(args))
            with open(file_name, 'w'):
                pass
        else:
            loader = Loader(file_name, section)
            loader.load(base_settings)
            registry = loader.registry

    checker = Checker(file_name, section, registry=registry)
    try:
        checker.check(base_settings)
    except KeyboardInterrupt:
        print('\nAborted')

if __name__ == '__main__':
    make_local_settings()
<commit_msg>Fix name of environment variable in make_local_settings<commit_after>import argparse
import os
from pkg_resources import find_distributions

from .checker import Checker
from .loader import Loader

def make_local_settings(argv=None):
    """Generate a local settings file."""
    parser = argparse.ArgumentParser(description=make_local_settings.__doc__)

    parser.add_argument('env', help='Environment name (e.g., dev or prod)')
    parser.add_argument(
        '-b', '--base-settings-module', default=None,
        help='Base settings module as a dotted path')
    parser.add_argument(
        '-f', '--file-name', default=None,
        help='Name of file to write settings into')
    parser.add_argument(
        '-s', '--section', default=None,
        help='Section to read/write settings from/to (default = <env>)')
    parser.add_argument('-o', '--overwrite', action='store_true', default=False)

    args = parser.parse_args(argv)

    os.environ['LOCAL_SETTINGS_DISABLE'] = '1'
    if args.base_settings_module is None:
        dist = next(find_distributions('.', only=True), None)
        if dist is not None:
            args.base_settings_module = '{0.project_name}.settings'.format(dist)
        else:
            msg = 'Could not guess which base settings module to use; specify with -b'
            parser.exit(1, msg)

    module = __import__(args.base_settings_module, fromlist=[''])
    base_settings = vars(module)

    if args.file_name is None:
        file_name = 'local.{0.env}.cfg'.format(args)
        section = args.env
    elif '#' in args.file_name:
        file_name, section = args.file_name.rsplit('#', 1)
    else:
        file_name = args.file_name
        section = args.env

    file_name = os.path.normpath(os.path.abspath(file_name))

    if args.section:
        section = args.section

    registry = None

    if os.path.exists(file_name):
        if args.overwrite:
            print('Overwriting {0.file_name}'.format(args))
            with open(file_name, 'w'):
                pass
        else:
            loader = Loader(file_name, section)
            loader.load(base_settings)
            registry = loader.registry

    checker = Checker(file_name, section, registry=registry)
    try:
        checker.check(base_settings)
    except KeyboardInterrupt:
        print('\nAborted')

if __name__ == '__main__':
    make_local_settings()
<|endoftext|>