Example #1
0
	def init_main_st(self, water_pts):
		(x1, y1) = random.choice(water_pts)
		n = self.array[x1][y1]
		n1_options = list(set(n.range()) - set(n.local()))
		n1 = np.random.choice(n1_options, replace=False)
		while Type.WATER in n1.type:
			n1 = np.random.choice(n1_options, replace=False)
		n2_options = list(set(n1.range()) - set(n1.local()))
		n2 = np.random.choice(n2_options, replace=False)
		points = get_line((n1.x, n1.y), (n2.x, n2.y))
		while any(Type.WATER in self.array[x][y].type for (x, y) in points):
			n2 = np.random.choice(n2_options, replace=False)
			points = get_line((n1.x, n1.y), (n2.x, n2.y))

		(x1, y1) = points[0]
		(x2, y2) = points[len(points)-1]
		self.set_type_road(points, Type.MAJOR_ROAD)
		middle_nodes = []
		if len(points) > 2:
			middle_nodes = points[1:len(points)-1]
		self.roadsegments.add(RoadSegment(self.array[x1][y1], self.array[x2][y2], middle_nodes, Type.MAJOR_ROAD, self.roadsegments))
		for (x, y) in points:
			adjacent = self.array[x][y].adjacent
			adjacent = [s for n in adjacent for s in n.adjacent]
			for pt in adjacent:
				if pt not in points:
					self.set_type_building([self.array[pt.x][pt.y]])
		self.init_lots(x1, y1, x2, y2)
Example #2
0
def complete_paths():
    line = util.get_line()[0:util.get_cursor_col_num() - 1]
    m = re.search("res://(((\w|-)+/)*)$", line)
    if m:
        project_dir = util.get_project_dir()
        if not project_dir:
            return
        # Directories and files are grouped and sorted separately.
        dirs = []
        files = []
        base_dir = "{}/{}".format(project_dir, m.group(1))
        if not os.path.isdir(base_dir):
            return
        for entry in os.listdir(base_dir):
            if not entry.startswith("."):
                if not util.filter(entry):
                    continue
                if os.path.isdir("{}/{}".format(base_dir, entry)):
                    dirs.append("{}/".format(entry))
                else:
                    files.append(entry)
        for d in sorted(dirs):
            append_completion(d)
        for f in sorted(files):
            append_completion(f)
Example #3
0
def get_extended_class(start_line=None):
    # Figure out if we're in an inner class and return its extended type if so.
    if not start_line:
        start_line = util.get_cursor_line_num()
    start_indent = util.get_indent(start_line)
    if start_indent > 0:
        for decl in iter_decls(start_line, -1, FUNC_DECLS | CLASS_DECLS):
            indent = util.get_indent(decl.line)
            if indent == start_indent:
                continue
            decl_type = type(decl)
            if decl_type is FuncDecl:
                start_indent = indent
            elif decl_type is ClassDecl:
                if decl.extends:
                    return decl.extends
                else:
                    return None
            if indent == 0:
                break

    # Search for 'extends' at the top of the file.
    for lnum in range(1, util.get_line_count()):
        line = util.get_line(lnum)
        m = re.match("extends\s+(\w+)", line)
        if m:
            return m.group(1)
        # Only 'tool' can appear before 'extends', so stop searching if any other
        # text is encountered.
        elif not re.match("tool\s+$", line):
            return None
def extractGrid(veins, out_img, fuzz_cell_scale, drawLines=False):
	new_x = float(out_img.shape[1]) / float(fuzz_cell_scale)
	new_y = float(out_img.shape[0]) / float(fuzz_cell_scale)
	grid_old_to_new = {} # MAPPING OF OLD CELLS TO NEW CELLS, ACCESS AS 2D ARRAY
	new_cell_counts = {} # MAPPING OF NEW CELLS TO COUNT OF VEIN OCCURRENCES PER CELL
	print 'VEINS! %d' % len(veins)

	for x in range(0, out_img.shape[1]):
		for y in range(0, out_img.shape[0]):
			new_x_cell = math.floor(float(x) / float(fuzz_cell_scale))
			new_y_cell = math.floor(float(y) / float(fuzz_cell_scale))
			if drawLines:
				cv2.line(out_img, (int(new_x_cell*fuzz_cell_scale),0), (int(new_x_cell*fuzz_cell_scale), out_img.shape[0]), colors[4], 1)
				cv2.line(out_img, (0,int(new_y_cell*fuzz_cell_scale)), (out_img.shape[1], int(new_y_cell*fuzz_cell_scale)), colors[4], 1)
			if x not in grid_old_to_new:
				grid_old_to_new[x] = {}
			grid_old_to_new[x][y] = (new_x_cell, new_y_cell)
			new_cell_counts[(new_x_cell, new_y_cell)] = 0

	for vi, vein in enumerate(veins):
		used_grid_points = set()
		for line in vein:
			points = util.get_line(line[0][0], line[0][1], line[1][0], line[1][1])
			for point in points:
				new_cell = grid_old_to_new[point[0]][point[1]]
				if new_cell not in used_grid_points:
					new_cell_counts[new_cell] += 1
					used_grid_points.add(new_cell)

	vein_features['fuzzy_grid_heatmap'] = new_cell_counts
Example #5
0
def _iter_decls_down(start_line, flags):
    # Check whether the starting line is a class decl.
    # If so, the indent of the next line is used as a baseline to determine
    # which items are direct children of the inner class.
    in_class = False
    class_decl = _get_decl(start_line, CLASS_DECLS)
    if class_decl:
        in_class = True
        class_indent = util.get_indent(start_line)
        inner_indent = None
        if flags & CLASS_DECLS:
            yield class_decl

    for lnum in range(start_line + 1, util.get_line_count()):
        if not util.get_line(lnum):
            continue
        indent = util.get_indent(lnum)
        if in_class:
            if indent <= class_indent:
                return
            if not inner_indent:
                inner_indent = indent
            elif indent > inner_indent:
                continue
        else:
            if indent > 0:
                continue
        decl = _get_decl(lnum, flags)
        if decl:
            yield decl
Example #6
0
def _get_decl(lnum, flags):
    line = util.get_line(lnum)

    if flags & VAR_DECLS:
        m = re.match(_VAR_PATTERN, line)
        if m:
            return VarDecl(lnum, m.group(1), None)

    if flags & CONST_DECLS:
        m = re.match(_CONST_PATTERN, line)
        if m:
            return ConstDecl(lnum, m.group(1), m.group(2))

    if flags & FUNC_DECLS:
        m = re.match(_FUNC_PATTERN, line)
        if m:
            static = m.group(1) != None
            name = m.group(2)
            args = m.group(3)
            if args:
                args = [a.strip() for a in args.split(",")]
            return FuncDecl(lnum, static, name, args)

    if flags & ENUM_DECLS:
        m = re.match(_ENUM_PATTERN, line)
        if m:
            return EnumDecl(lnum, m.group(1))

    if flags & CLASS_DECLS:
        m = re.match(_CLASS_PATTERN, line)
        if m:
            return ClassDecl(lnum, m.group(1), m.group(2))
Example #7
0
def get_enum_values(line_num):
    lines = [util.strip_line(line_num, util.get_line(line_num))]
    line_count = util.get_line_count()
    while not lines[-1].endswith("}"):
        line_num += 1
        if line_num > line_count:
            return
        lines.append(util.strip_line(line_num, util.get_line(line_num)))
    m = re.match(_ENUM_VALUES_PATTERN, "\n".join(lines), re.DOTALL)
    if m:
        values = [v.strip() for v in m.group(1).replace("\n", ",").split(",")]

        def map_value(v):
            m = re.match("(\w+)(?:\s*=\s*(.*))?", v)
            if m:
                return ConstDecl(-1, m.group(1), m.group(2))

        return list(filter(lambda v: v, map(map_value, values)))
Example #8
0
def list_all_sessions():
    """print a list of all backuped sessions (with short info)"""
    if not len(tmux_dict.keys()):
        #name is empty, show all list(short info)
        l = util.all_backups()
        if not l or len(l) == 0:
            LOG.info("No backup was created yet.\nretmux -b [name] to create backup" )
            sys.exit()

        last = util.latest_backup().split('/')[-1]
        latest_tmux = util.get_tmux_by_id(last)
        #add into dict
        i=1
        tmux_dict[str(i)]=latest_tmux

        bk_list = [ b for b in l if b != last]
        for tmux_id in bk_list:
            tmux = util.get_tmux_by_id(tmux_id)
            i+=1
            tmux_dict[str(i)]=tmux

    list_fmt = '%s%2s %s'#fmt for list all, the first %s is the '*' place
    print util.get_line('=')
    #header
    print  list_fmt %(' ', 'No.',tmux_obj.Tmux.short_format%('Name','Sessions','Created on'))
    print util.get_line('=')
    keys = tmux_dict.keys()
    keys.sort()
    for idx in keys:
        tmux = tmux_dict[idx]
        latest_flag = '*' if idx == '1' else ' '
        print list_fmt%(latest_flag, idx, tmux.short_info())
    print util.get_line('-')
    print '%72s'%'Latest default backup with (*)'
Example #9
0
	def render(self, t):
		a = self.m1.length()
		b = self.m2.length()

		x = self.m1.x + (a**2 - b**2 + self.separation**2)/(2*self.separation)
		inside = (-(a - b - self.separation)*(a + b - self.separation)*(a + b + self.separation)/self.separation**2)
		y = self.m1.y + cmath.sqrt(inside).real/2

		ln1 = util.get_line(self.m1.x, self.m1.y, int(x), int(y))
		ln2 = util.get_line(self.m2.x, self.m2.y, int(x), int(y))

		for (lx, ly) in ln1:
			with t.location(int(lx), int(ly)):
				print "*"
		for (lx, ly) in ln2:
			with t.location(int(lx), int(ly)):
				print "*"
		with t.location(int(x), int(y)):
			print "X"

		self.m1.render(t)
		self.m2.render(t)
Example #10
0
def complete_dot():
    line_num = util.get_cursor_line_num()
    line = util.get_line(line_num)
    col_num = util.get_cursor_col_num()
    token_chain = script.get_token_chain(line, line_num, col_num - 2)
    if token_chain:
        last_token = token_chain[-1]
        last_token_type = type(last_token)

        # Complete statically accessible items in classes.
        if last_token_type is script.ClassToken:
            # Class is defined by Godot.
            if last_token.line == -1:
                c = classes.get_class(last_token.name)
                # Manually add an entry for 'new()' for core types.
                if not c.is_built_in():
                    new_func = classes.GodotMethod("new", c.get_name(), [],
                                                   None)
                    append_completion(build_completion(new_func, c.get_name()))
                _add_class_items(c, _CONSTANTS)
            else:
                for decl in script.iter_static_decls(last_token.line,
                                                     script.ANY_DECLS):
                    append_completion(build_completion(decl))
            return

        # Treat 'self' like we're accessing script variables, but exclude globals.
        if last_token_type is script.VariableToken and last_token.name == "self":
            complete_script(include_globals=False)
            return

        # Complete enum values.
        if last_token_type is script.EnumToken:
            values = script.get_enum_values(last_token.line)
            if values:
                for value in values:
                    append_completion(build_completion(value))
            return

        c_name = None
        flags = None
        if len(token_chain
               ) == 1 and last_token_type is script.SuperAccessorToken:
            c_name = script.get_extended_class(line_num)
            flags = _METHODS
        elif last_token_type is script.MethodToken:
            c_name = last_token.returns
        elif last_token_type is script.VariableToken:
            c_name = last_token.type
        if c_name:
            _add_class_items(classes.get_class(c_name), flags)
Example #11
0
	def set_new_road(self, x1, y1, road_type, leave_lot=False, correction=5):
		check1 = True #?
		check2 = True
	
		node = self.array[x1][y1]
		point, points = get_closest_point(node, self.lots, self.roads, road_type, leave_lot, correction=correction)
		if point is None:
			return 
		(x2, y2) = point

		point2 = None
		if road_type == Type.MINOR_ROAD:
			point2 = get_point_to_close_gap_minor(x1, y1, self, points)
		elif road_type == Type.MAJOR_ROAD:
			point2 = get_point_to_close_gap_major(node, x1, y1, self, points)

		if point2 is not None:
			(x1, y1) = point2
			points.extend(get_line((x2, y2), (x1, y1)))
			check1 = True

		self.roadnodes.append(self.array[x1][y1])
		self.roadnodes.append(self.array[x2][y2])
		
		middle_nodes = []
		if len(points) > 2:
			middle_nodes = points[1:len(points)-1]
		
		if check1:
			n1 = self.array[x1][y1]
			for rs in self.roadsegments:
				if (x1, y1) in rs.nodes:
					rs.split(n1, self.roadsegments, self.roadnodes)
					break
		if check2:
			n2 = self.array[x2][y2]
			for rs in self.roadsegments:
				if (x2, y2) in rs.nodes:
					rs.split(n2, self.roadsegments, self.roadnodes)
					break
		
		self.roadsegments.add(RoadSegment(self.array[x1][y1], self.array[x2][y2], middle_nodes, road_type, self.roadsegments))
				
		self.set_type_road(points, road_type)
Example #12
0
def show_and_action(name=None, action=None):
    """list backups info. if name was given, show detailed info for given
        backup item. otherwise show short_info for all backups as list
        if there is action, will do the action after the details was displayed.
        the action should be a function with single argument of tmux_obj.Tmux instance
        """

    #using restore check function to validate the given name
    name = tmux_id_4_show(name)

    if not name:
        #interactively show details
        while 1:
            list_all_sessions()
            idx = raw_input(
                "retmux> Please give backup No. (press q to exit):")

            if not idx:
                log.print_err("Invalid index: (empty)")
            elif idx.lower() == 'q':
                break
            elif not tmux_dict.has_key(idx):
                log.print_err("Invalid index: " + idx)
            else:
                tmux = tmux_dict[idx]
                print util.get_line('>')
                print log.hl('Details of backup:', 'bold') + '%s' % tmux.tid
                print util.get_line('>')
                print '\n'.join(tmux.long_info())
                print util.get_line('<')
                if action:
                    action(tmux)
    else:
        #till here, the name should be validated, exists
        print log.hl('Details of backup:', 'bold') + '%s' % name
        print util.get_line('=')
        tmux = util.get_tmux_by_id(name)
        print '\n'.join(tmux.long_info())
        if action:
            action(tmux)
Example #13
0
def show_and_action(name=None, action=None):
    """list backups info. if name was given, show detailed info for given
        backup item. otherwise show short_info for all backups as list
        if there is action, will do the action after the details was displayed.
        the action should be a function with single argument of tmux_obj.Tmux instance
        """

    #using restore check function to validate the given name
    name = tmux_id_4_show(name)
    
    if not name:
        #interactively show details
        while 1:
            list_all_sessions()
            idx = raw_input("retmux> Please give backup No. (press q to exit):")

            if not idx:
                log.print_err("Invalid index: (empty)")
            elif idx.lower() == 'q':
                break
            elif not tmux_dict.has_key(idx):
               log.print_err("Invalid index: " + idx)
            else:
                tmux = tmux_dict[idx]
                print util.get_line('>')
                print log.hl('Details of backup:','bold') +'%s'% tmux.tid
                print util.get_line('>')
                print '\n'.join(tmux.long_info())
                print util.get_line('<')
                if action:
                    action(tmux)
    else:
        #till here, the name should be validated, exists
        print log.hl('Details of backup:','bold') +'%s'% name
        print util.get_line('=')
        tmux = util.get_tmux_by_id(name)
        print '\n'.join(tmux.long_info()) 
        if action:
            action(tmux)
Example #14
0
def gdscript_complete():
    util.clear_cache()
    completer.clear_completions()

    line = util.get_line()[0:util.get_cursor_col_num()]
    syn_attr = util.get_syn_attr()
    if syn_attr == "gdComment":
        return
    elif syn_attr == "gdString":
        completer.complete_paths()
    elif re.match("(\s*class\s+\w+\s+)?extends\s*", line):
        completer.complete_class_names(classes.EXTENDABLE)
    elif re.match("export\(\s*", line):
        completer.complete_class_names(classes.EXPORTABLE)
    elif re.match("\s*func", line):
        completer.complete_method_signatures()
    elif line and line[-1] == ".":
        completer.complete_dot()
    else:
        completer.complete_script(include_globals=True)

    completions = completer.get_completions()
    vim.command("let gdscript_completions = " + str(completions))
Example #15
0
def can_see_player(monster, game_data, visibility=10):

    visible = tcod.map_is_in_fov(game_data.fov_map, monster.pos.x,
                                 monster.pos.y)
    return visible
    """
    this doesn't work nicely yet for some corners, player can see monster but monster can't see player
    in situations like this, M=monster, P=player, = are walls 
    
    M
    = =
     P
     
    This is because the lines goes directly down first, crossing the wall and thus being blocked 
    """
    vec = monster.pos - game_data.player.pos
    if vec.length() > visibility:
        return False
    points = get_line(monster.pos.tuple(), game_data.player.pos.tuple())
    for p in points:
        x, y = p
        if game_data.map.tiles[x][y].block_sight:
            return False
    return True
Example #16
0
def list_all_sessions():
    """print a list of all backuped sessions (with short info)"""
    if not len(tmux_dict.keys()):
        #name is empty, show all list(short info)
        l = util.all_backups()
        if not l or len(l) == 0:
            LOG.info(
                "No backup was created yet.\nretmux -b [name] to create backup"
            )
            sys.exit()

        last = util.latest_backup().split('/')[-1]
        latest_tmux = util.get_tmux_by_id(last)
        #add into dict
        i = 1
        tmux_dict[str(i)] = latest_tmux

        bk_list = [b for b in l if b != last]
        for tmux_id in bk_list:
            tmux = util.get_tmux_by_id(tmux_id)
            i += 1
            tmux_dict[str(i)] = tmux

    list_fmt = '%s%2s %s'  #fmt for list all, the first %s is the '*' place
    print util.get_line('=')
    #header
    print list_fmt % (' ', 'No.', tmux_obj.Tmux.short_format %
                      ('Name', 'Sessions', 'Created on'))
    print util.get_line('=')
    keys = tmux_dict.keys()
    keys.sort()
    for idx in keys:
        tmux = tmux_dict[idx]
        latest_flag = '*' if idx == '1' else ' '
        print list_fmt % (latest_flag, idx, tmux.short_info())
    print util.get_line('-')
    print '%72s' % 'Latest default backup with (*)'
Example #17
0
def parse_trace():
    ''' Parse McStas trace output from stdin and write results
        to file objects csv_comps and csv_lines '''

    mpl.rcParams['legend.fontsize'] = 10

    fig = plt.figure(figsize=plt.figaspect(0.5)*1.5)
    ax = fig.gca(projection='3d')
    ax.set_xlabel("z")
    ax.set_ylabel("x")
    ax.set_zlabel("y")
    ax.set_aspect('equal')
    #    ax.autoscale_view(scalex=False, scaley=False, scalez=False)
    color = 0

    # map from component name to (position, rotation matrix)
    comps = {}

    # active (position, rotation matrix)
    comp = (np.array([0, 0, 0]),
            np.array([1, 0, 0,
                      0, 1, 0,
                      0, 0, 1]).reshape(3,3))

    # previous neutron position
    prev = None
    skip = False
    # we are drawing a neutron
    active = False
    xstate=[]
    ystate=[]
    zstate=[]
    
    while True:
        # read line
        line = get_line()
        if not line:
            break

        # register components
        if line.startswith(UC_COMP):
            # grab info line
            info = get_line()
            assert info[:4] == 'POS:'
            nums = [x.strip() for x in info[4:].split(',')]
            # extract fields
            name = line[len(UC_COMP):].strip(' "\n')
            pos = np.array([float(x) for x in nums[:3]])
            # read flat 3x3 rotation matrix
            rot = np.array([float(x) for x in nums[3:3+9]]).reshape(3, 3)
            comps[name] = (pos, rot)

        # switch perspective
        elif line.startswith(MC_COMP):
            color += 1
            comp = comps[line[len(MC_COMP) + 1:]]

        elif line.startswith(MC_COMP_SHORT):
            name = line[len(MC_COMP_SHORT) + 1:].strip('"')
            comp = comps[name]
            skip = True

        # process multiline
        elif line.startswith(MC_LINE):
            points = parse_multiline(line[len(MC_LINE):].strip('()'))
            start = points.pop(0)
            (x, y, z) = rotate_points(points, comp)
            ax.plot(z, x, y)

        # process circle
        elif line.startswith(MC_CIRCLE):
            xyz = 'xyz'
            items = line[len(MC_CIRCLE):].strip('()').split(',')
            # plane
            pla = [xyz.find(a) for a in items[0].strip("''")]
            # center and radius
            pos = [float(x) for x in items[1:4]]
            rad = float(items[4])
            (x,y,z) = draw_circle(pla, pos, rad, comp)
            ax.plot(z, x, y)
            
        # activate neutron when it enters
        elif line.startswith(MC_ENTER):
            prev = None
            skip = True
            active = True
            color = 0
            xstate=[]
            ystate=[]
            zstate=[]

        # deactivate neutron when it leaves
        elif line.startswith(MC_LEAVE):
            ax.plot(zstate, xstate, ystate)
            active = False
            prev = None
            
        elif line.startswith(MC_ABSORB):
            pass

        # register state and scatter
        elif line.startswith(MC_STATE) or line.startswith(MC_SCATTER):
            
            if not active:
                continue

            if skip:
                skip = False
                continue
            
            xyz = [float(x) for x in line[line.find(':')+1:].split(',')[:3]]
            xyz = rotate(xyz, comp)
            if prev is not None:
                xstate.append(xyz[0])
                ystate.append(xyz[1])
                zstate.append(xyz[2])
            prev = xyz
            xstate.append(prev[0])
            ystate.append(prev[1])
            zstate.append(prev[2])

        # kick out legacy "junk"
        elif line.startswith(MC_MAGNIFY) or line.startswith(MC_START) or line.startswith(MC_END) or line.startswith(MC_STOP):
            continue
        else:
            print line


    # A little bit of logic for controlling the aspect ratios/view
    (xmin, xmax)=ax.get_xlim3d()
    (ymin, ymax)=ax.get_ylim3d()
    (zmin, zmax)=ax.get_zlim3d()
    dx = xmax - xmin
    dy = ymax - ymin
    dz = zmax - zmin
    dmax=max(dx,dy,dz)

    # Check ranges and define axis box of max length cubed
    if dmax > dx:
        mean=(xmax+xmin)/2
        xmin=mean-dmax/2
        xmax=mean+dmax/2
    if dmax > dy:
        mean=(ymax+ymin)/2
        ymin=mean-dmax/2
        ymax=mean+dmax/2
    if dmax > dz:
        mean=(zmax+zmin)/2
        zmin=mean-dmax/2
        zmax=mean+dmax/2
        
    # Set new axis limits
    ax.set_xlim3d(xmin,xmax)
    ax.set_ylim3d(ymin,ymax)
    ax.set_zlim3d(zmin,zmax)
 
    plt.show()
Example #18
0
def parse_trace(csv_comps, csv_lines):
    ''' Prase McStas trace output from stdin and write results
        to file objects csv_comps and csv_lines '''

    color = 0
    def out_point((p_x, p_y, p_z)):
        ''' Write a line to csv_lines '''
        csv_lines.write('%s, %s, %s, %s\n' % (p_x, p_y, p_z, color))

    # print headers
    csv_comps.write('name, x, y, z\n')
    csv_lines.write('x, y, z, c\n')

    # map from component name to (position, rotation matrix)
    comps = {}

    # active (position, rotation matrix)
    comp = (np.array([0, 0, 0]),
            np.array([1, 0, 0,
                      0, 1, 0,
                      0, 0, 1]).reshape(3,3))

    # previous neutron position
    prev = None
    skip = False
    # we are drawing a neutron
    active = False

    while True:
        # read line
        line = get_line()
        if not line:
            break

        # register components
        if line.startswith(UC_COMP):
            # grab info line
            info = get_line()
            assert info[:4] == 'POS:'
            nums = [x.strip() for x in info[4:].split(',')]
            # extract fields
            name = line[len(UC_COMP):].strip(' "\n')
            pos = np.array([float(x) for x in nums[:3]])
            # read flat 3x3 rotation matrix
            rot = np.array([float(x) for x in nums[3:3+9]]).reshape(3, 3)
            comps[name] = (pos, rot)
            csv_comps.write('%s, %s, %s, %s\n' % ((name,) + tuple(pos)))

        # switch perspective
        elif line.startswith(MC_COMP):
            color += 1
            comp = comps[line[len(MC_COMP) + 1:]]

        elif line.startswith(MC_COMP_SHORT):
            name = line[len(MC_COMP_SHORT) + 1:].strip('"')
            comp = comps[name]
            skip = True

        # process multiline
        elif line.startswith(MC_LINE):
            points = parse_multiline(line[len(MC_LINE):].strip('()'))
            start = points.pop(0)
            while points:
                end = points.pop(0)
                for point in (start, end):
                    out_point(rotate(point, comp))
                start = end

        # process circle
        elif line.startswith(MC_CIRCLE):
            xyz = 'xyz'
            items = line[len(MC_CIRCLE):].strip('()').split(',')
            # plane
            pla = [xyz.find(a) for a in items[0].strip("''")]
            # center and radius
            pos = [float(x) for x in items[1:4]]
            rad = float(items[4])
            draw_circle(pla, pos, rad, comp, out_point)

        # activate neutron when it enters
        elif line.startswith(MC_ENTER):
            prev = None
            skip = True
            active = True
            color += 1

        # deactivate neutron when it leaves
        elif line.startswith(MC_LEAVE):
            active = False

        elif line.startswith(MC_ABSORB):
            pass

        # register state and scatter
        elif line.startswith(MC_STATE) or line.startswith(MC_SCATTER):

            if not active:
                continue

            if skip:
                skip = False
                continue

            xyz = [float(x) for x in line[line.find(':')+1:].split(',')[:3]]
            xyz = rotate(xyz, comp)
            if prev is not None:
                out_point(prev)
                out_point(xyz)

            prev = xyz
Example #19
0
def build_donor(app_ctx):

    click.echo('in build_donor...')
    schema = app_ctx['input_schemas']['donor']

    fname = os.path.join(schema['data_path'], schema['main_file_name'])

    annotations = {}
    util.read_annotations(annotations, 'pcawg_donorlist', schema['annotations_path']+'pc_annotation-pcawg_final_list.tsv')
    util.read_annotations(annotations, 'blacklist',  schema['annotations_path']+'blacklist/pc_annotation-donor_blacklist.tsv')
    util.read_annotations(annotations, 'graylist', schema['annotations_path']+'graylist/pc_annotation-donor_graylist.tsv')
    util.read_annotations(annotations, 'uuid_to_barcode', schema['annotations_path']+'pc_annotation-tcga_uuid2barcode.tsv')
    util.read_annotations(annotations, 'icgc_donor_id', schema['annotations_path']+'icgc_bioentity_ids/pc_annotation-icgc_donor_ids.csv')
    util.read_annotations(annotations, 'icgc_specimen_id', schema['annotations_path']+'icgc_bioentity_ids/pc_annotation-icgc_specimen_ids.csv')
    util.read_annotations(annotations, 'icgc_sample_id', schema['annotations_path']+'icgc_bioentity_ids/pc_annotation-icgc_sample_ids.csv')
    util.read_annotations(annotations, 'specimen_type_to_cv', schema['annotations_path']+'pc_annotation-tcga_specimen_type2icgc_cv.tsv')
    util.read_annotations(annotations, 'specimen_uuid_to_type', schema['annotations_path']+'pc_annotation-tcga_specimen_uuid2type.tsv')
    

    donor = {}
    specimen = {}
    # build the donor and specimen objects
    with open(fname, 'r') as f:
        reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
        for r in reader:
            donor_unique_id = r.get('disease.x')+'-US::'+r.get('participant_id')
            if not donor.get(donor_unique_id):
                #new donor: initial the donor
                donor[donor_unique_id] = create_obj(donor_unique_id, schema, r, annotations, 'donor')
            
            if not specimen.get('mirna'):
                specimen['mirna'] = {}
            specimen_id = r.get('sample_id')

            if not specimen['mirna'].get(specimen_id):
                specimen['mirna'][specimen_id] = create_obj(donor_unique_id, schema, r, annotations, 'mirna')

            aliquot = create_obj(donor_unique_id, schema, r, annotations, 'aliquot')
            specimen['mirna'][specimen_id]['aliquots'].append(copy.deepcopy(aliquot))
            specimen['mirna'][specimen_id]['donor_unique_id'] = donor_unique_id

    # build the specimen object from help file
    fname = os.path.join(schema['data_path'], schema['help_file_name'])
    with open(fname, 'r') as f:
        reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
        for r in reader:
            donor_unique_id = r.get('donor_unique_id')
            specimen_id = r.get('submitter_specimen_id')
            dtype = 'rna_seq' if r.get('library_strategy') == 'RNA-Seq' else 'wgs'
            if not specimen.get(dtype):
                specimen[dtype] = {}
            if not specimen[dtype].get(specimen_id):
                specimen[dtype][specimen_id] = create_obj(donor_unique_id, schema, r, annotations, dtype)
            specimen[dtype][specimen_id]['donor_unique_id'] = donor_unique_id  

    if os.path.exists(app_ctx['output_dir']):
        shutil.rmtree(app_ctx['output_dir'])
    os.makedirs(app_ctx['output_dir'])

    # add specimen to donor
    crossref_specimen_ids = {}
    crossref_specimen_ids['rna_seq'] = specimen['rna_seq'].keys()
    crossref_specimen_ids['wgs'] = specimen['wgs'].keys()

    json2tsv_fields_map = schema['json2tsv_fields_map'] 
    sample_sheet_fields_donor = schema['target_tsv_file']['mirna_sample_sheet']['donor']
    sample_sheet_fields_specimen = schema['target_tsv_file']['mirna_sample_sheet']['specimen']
    sample_sheet_fields_sample = schema['target_tsv_file']['mirna_sample_sheet']['sample']

    with open(os.path.join(app_ctx['output_dir'], 'mirna_sample_sheet.tsv'), 'a') as f:
        f.write('\t'.join(sample_sheet_fields_donor+sample_sheet_fields_specimen+sample_sheet_fields_sample) + '\n')

    for dtype in ['mirna', 'rna_seq', 'wgs']:
        #loop on the specimen to add it to donor
        for k, v in specimen[dtype].iteritems():
            donor_unique_id = v.get('donor_unique_id')            
            if not donor.get(donor_unique_id):
                continue
            if (not donor[donor_unique_id]['pcawg_donor']) and (not 'normal' in v.get('dcc_specimen_type').lower()):
                continue
            sample_sheet = OrderedDict()
            sample_sheet.update(util.get_dict_value(sample_sheet_fields_donor, donor[donor_unique_id], json2tsv_fields_map))
            # check whether wgs and rna_seq has the mirna specimen
            if dtype == 'mirna':
                v['has_matched_wgs_specimen'] = True if k in crossref_specimen_ids.get('wgs') else False
                v['has_matched_rna_seq_specimen'] = True if k in crossref_specimen_ids.get('rna_seq') else False
                
                sample_sheet.update(util.get_dict_value(sample_sheet_fields_specimen, v, json2tsv_fields_map))
                for aliquot in v.get('aliquots'):
                    sample_sheet.update(util.get_dict_value(sample_sheet_fields_sample, aliquot, json2tsv_fields_map))
                    line = util.get_line(sample_sheet)
                    # # generate mirna_sample_sheet
                    with open(os.path.join(app_ctx['output_dir'], 'mirna_sample_sheet.tsv'), 'a') as f:
                        f.write('\t'.join(line) + '\n')

            v.pop('donor_unique_id')
            if 'normal' in v.get('dcc_specimen_type').lower():
                donor[donor_unique_id]['normal'][dtype] = v
            else:
                donor[donor_unique_id]['tumor'][dtype].append(v)


    # get release_tsv fields
    release_fields = schema['target_tsv_file']['mirna_release_tsv']
    with open(os.path.join(app_ctx['output_dir'], 'mirna_release.tsv'), 'a') as f:
        f.write('\t'.join(release_fields) + '\n')
   
    # remove the donors only have tumor mirna specimen    
    for k, v in donor.iteritems():
        if (not v['pcawg_donor']) and (not v.get('normal').get('mirna')):
            continue
        for dtype in ['mirna', 'rna_seq', 'wgs']:
            v['tumor_'+dtype+'_specimen_count'] = len(v['tumor'][dtype]) if v['tumor'].get(dtype) else 0
        # generate the jsonl dump
        with open(os.path.join(app_ctx['output_dir'], 'mirna_release.jsonl'), 'a') as f:
            f.write(json.dumps(v) + '\n')

        # # generate mirna_release_tsv list
        tsv_obj = util.get_dict_value(release_fields, v, json2tsv_fields_map)
        line = util.get_line(tsv_obj)
        with open(os.path.join(app_ctx['output_dir'], 'mirna_release.tsv'), 'a') as f:
            f.write('\t'.join(line) + '\n')
    
    click.echo('complete in build_donor...')
Example #20
0
def get_closest_point(node,
                      lots,
                      road_segments,
                      road_type,
                      leave_lot,
                      correction=5):
    # check if road can leave lot
    (x, y) = (node.x, node.y)
    nodes = road_segments

    # if not leave_lot:
    # 	if node.lot is None:
    # 		return None
    # 	nodes = set(road_segments) & node.lot.get_nodes()

    # filter out bridges
    nodes = [n for n in nodes if Type.BRIDGE not in n.type]

    if len(nodes) == 0:
        print("leave_lot = {} no road segments".format(leave_lot))

        return None, None

    dists = [math.hypot(n.x - x, n.y - y) for n in nodes]
    node2 = nodes[dists.index(min(dists))]
    (x2, y2) = (node2.x, node2.y)

    # if node.lot is not None and road_type is not Type.MINOR_ROAD:
    # 	if abs(x - x2) < correction:
    # 		x = x2
    # 		node = node.landscape.array[x][y]
    # 	elif abs(y - y2) < correction:
    # 		y = y2
    # 		node = node.landscape.array[x][y]

    if node.lot is None:
        if road_type is not Type.MINOR_ROAD and abs(x2 - x) > 10 and abs(
                y2 - y) > 10:
            if node2.lot is not None:
                (cx2, cy2) = node2.lot.center
                (x, y) = (x + x - cx2, y + y - cy2)

                if x >= node.landscape.x:
                    x = node.landscape.x - 1
                if x < 0:
                    x = 0
                if y >= node.landscape.y:
                    y = node.landscape.y - 1
                if y < 0:
                    y = 0

        # else:
        # 	(x2, y2) = (node2.x, node2.y)

            if abs(x2 - x) > 10 and abs(
                    y2 - y) > 10:  # and road_type is Type.MAJOR_ROAD:
                if not node.landscape.add_lot([(x2, y2), (x, y)]):
                    print("leave_lot = {} add lot failed".format(leave_lot))

                    return None, None
        # else:
        # 	print("leave_lot = {} proposed lot is too small{} or road is not MAJOR_ROAD{}".format(leave_lot, abs(x2 - x) > 10 and abs(y2 - y) > 10, road_type is Type.MAJOR_ROAD))

        # 	return None
        else:
            return None, None

    points = get_line((x, y), (node2.x, node2.y))
    if len(points) <= 2:
        return None, None

    if not leave_lot:
        for (i, j) in points:
            if Type.WATER in node.landscape.array[i][j].type:
                return None, None

    return (node2.x, node2.y), points
Example #21
0
def parse_trace(csv_comps, csv_lines):
    ''' Prase McStas trace output from stdin and write results
        to file objects csv_comps and csv_lines '''

    color = 0

    def out_point((p_x, p_y, p_z)):
        ''' Write a line to csv_lines '''
        csv_lines.write('%s, %s, %s, %s\n' % (p_x, p_y, p_z, color))

    # print headers
    csv_comps.write('name, x, y, z\n')
    csv_lines.write('x, y, z, c\n')

    # map from component name to (position, rotation matrix)
    comps = {}

    # active (position, rotation matrix)
    comp = (np.array([0, 0, 0]), np.array([1, 0, 0, 0, 1, 0, 0, 0,
                                           1]).reshape(3, 3))

    # previous neutron position
    prev = None
    skip = False
    # we are drawing a neutron
    active = False

    while True:
        # read line
        line = get_line()
        if not line:
            break

        # register components
        if line.startswith(UC_COMP):
            # grab info line
            info = get_line()
            assert info[:4] == 'POS:'
            nums = [x.strip() for x in info[4:].split(',')]
            # extract fields
            name = line[len(UC_COMP):].strip(' "\n')
            pos = np.array([float(x) for x in nums[:3]])
            # read flat 3x3 rotation matrix
            rot = np.array([float(x) for x in nums[3:3 + 9]]).reshape(3, 3)
            comps[name] = (pos, rot)
            csv_comps.write('%s, %s, %s, %s\n' % ((name, ) + tuple(pos)))

        # switch perspective
        elif line.startswith(MC_COMP):
            color += 1
            comp = comps[line[len(MC_COMP) + 1:]]

        elif line.startswith(MC_COMP_SHORT):
            name = line[len(MC_COMP_SHORT) + 1:].strip('"')
            comp = comps[name]
            skip = True

        # process multiline
        elif line.startswith(MC_LINE):
            points = parse_multiline(line[len(MC_LINE):].strip('()'))
            start = points.pop(0)
            while points:
                end = points.pop(0)
                for point in (start, end):
                    out_point(rotate(point, comp))
                start = end

        # process circle
        elif line.startswith(MC_CIRCLE):
            xyz = 'xyz'
            items = line[len(MC_CIRCLE):].strip('()').split(',')
            # plane
            pla = [xyz.find(a) for a in items[0].strip("''")]
            # center and radius
            pos = [float(x) for x in items[1:4]]
            rad = float(items[4])
            draw_circle(pla, pos, rad, comp, out_point)

        # activate neutron when it enters
        elif line.startswith(MC_ENTER):
            prev = None
            skip = True
            active = True
            color += 1

        # deactivate neutron when it leaves
        elif line.startswith(MC_LEAVE):
            active = False

        elif line.startswith(MC_ABSORB):
            pass

        # register state and scatter
        elif line.startswith(MC_STATE) or line.startswith(MC_SCATTER):

            if not active:
                continue

            if skip:
                skip = False
                continue

            xyz = [float(x) for x in line[line.find(':') + 1:].split(',')[:3]]
            xyz = rotate(xyz, comp)
            if prev is not None:
                out_point(prev)
                out_point(xyz)

            prev = xyz
Example #22
0
def parse_trace(world, fp=sys.stdin, inspectComp=None):
    """ Prase McStas trace output from stdin and write result to output """

    color = 0

    # def out_point((p_x, p_y, p_z)):
    #     ''' Write a line to csv_lines '''
    #     csv_lines.write('%s, %s, %s, %s\n' % (p_x, p_y, p_z, color))

    # print headers
    # csv_comps.write('name, x, y, z\n')
    # csv_lines.write('x, y, z, c\n')

    # map from component name to (position, rotation matrix)
    comps = {}

    # active (position, rotation matrix)
    comp = (np.array([0, 0, 0]), np.array([1, 0, 0, 0, 1, 0, 0, 0, 1]).reshape(3, 3))
    compName = ""

    # we are following a neutron
    active = False
    # we need to draw the neutron (it passed the "check-point"/inspect component)
    inspect = False
    # list of observed neutron positions
    neutron = []
    # skip next neutron position
    skip = False
    # total count of drawed neutrons
    neutrons_drawed = 0

    while True:
        # read line
        line = get_line(fp)
        if line is None:
            break

        # register components
        if line.startswith(UC_COMP):
            # grab info line
            info = get_line(fp)
            assert info[:4] == "POS:"
            nums = [x.strip() for x in info[4:].split(",")]
            # extract fields
            name = line[len(UC_COMP) :].strip(' "\n')
            pos = np.array([float(x) for x in nums[:3]])
            # read flat 3x3 rotation matrix
            rot = np.array([float(x) for x in nums[3 : 3 + 9]]).reshape(3, 3)
            comps[name] = (pos, rot)
            # csv_comps.write('%s, %s, %s, %s\n' % ((name,) + tuple(pos)))

        # switch perspective
        elif line.startswith(MC_COMP):
            color += 1
            name = line[len(MC_COMP) + 1 :].strip()
            compName = name
            comp = comps[name]

        elif line.startswith(MC_COMP_SHORT):
            name = line[len(MC_COMP_SHORT) + 1 :].strip('"')
            compName = name
            comp = comps[name]
            skip = True

        # process multiline
        elif line.startswith(MC_LINE):
            points = parse_multiline(line[len(MC_LINE) :].strip("()"))
            world.drawLine((rotate(p, comp) for p in points), color=getColor(color))

        # process circle
        elif line.startswith(MC_CIRCLE):
            xyz = "xyz"
            items = line[len(MC_CIRCLE) :].strip("()").split(",")
            # plane
            pla = [xyz.find(a) for a in items[0].strip("''")]
            # center and radius
            pos = [float(x) for x in items[1:4]]
            rad = float(items[4])
            points = draw_circle(pla, pos, rad, comp)
            world.drawLine(points, color=getColor(color))

        # activate neutron when it enters
        elif line.startswith(MC_ENTER):
            neutron = []
            skip = True
            active = True
            inspect = False
            color += 1

        # deactivate neutron when it leaves
        elif line.startswith(MC_LEAVE) or line.startswith(MC_ABSORB):
            active = False
            if inspectComp is None or inspect:
                world.drawLine(neutron, color="1 0 0")
                neutrons_drawed += 1

        # register state and scatter
        elif line.startswith(MC_STATE) or line.startswith(MC_SCATTER):

            if not active:
                continue

            if skip:
                skip = False
                continue

            if inspectComp and inspectComp == compName:
                # We will draw this neutron!
                inspect = True

            # keep track of points the neutron passes through
            xyz = [float(x) for x in line[line.find(":") + 1 :].split(",")[:3]]
            xyz = rotate(xyz, comp)
            neutron.append(xyz)

    print "Neutrons drawed:", neutrons_drawed, (inspectComp and "(reaching %s)" % inspectComp or "(all)")
    return world
Example #23
0
def parse_trace(world, fp=sys.stdin, inspectComp=None):
    ''' Prase McStas trace output from stdin and write result to output '''

    color = 0

    # def out_point((p_x, p_y, p_z)):
    #     ''' Write a line to csv_lines '''
    #     csv_lines.write('%s, %s, %s, %s\n' % (p_x, p_y, p_z, color))

    # print headers
    # csv_comps.write('name, x, y, z\n')
    # csv_lines.write('x, y, z, c\n')

    # map from component name to (position, rotation matrix)
    comps = {}

    # active (position, rotation matrix)
    comp = (np.array([0, 0, 0]), np.array([1, 0, 0, 0, 1, 0, 0, 0,
                                           1]).reshape(3, 3))
    compName = ""

    # we are following a neutron
    active = False
    # we need to draw the neutron (it passed the "check-point"/inspect component)
    inspect = False
    # list of observed neutron positions
    neutron = []
    # skip next neutron position
    skip = False
    # total count of drawed neutrons
    neutrons_drawed = 0

    while True:
        # read line
        line = get_line(fp)
        if line is None:
            break

        # register components
        if line.startswith(UC_COMP):
            # grab info line
            info = get_line(fp)
            assert info[:4] == 'POS:'
            nums = [x.strip() for x in info[4:].split(',')]
            # extract fields
            name = line[len(UC_COMP):].strip(' "\n')
            pos = np.array([float(x) for x in nums[:3]])
            # read flat 3x3 rotation matrix
            rot = np.array([float(x) for x in nums[3:3 + 9]]).reshape(3, 3)
            comps[name] = (pos, rot)
            # csv_comps.write('%s, %s, %s, %s\n' % ((name,) + tuple(pos)))

        # switch perspective
        elif line.startswith(MC_COMP):
            color += 1
            name = line[len(MC_COMP) + 1:].strip()
            compName = name
            comp = comps[name]

        elif line.startswith(MC_COMP_SHORT):
            name = line[len(MC_COMP_SHORT) + 1:].strip('"')
            compName = name
            comp = comps[name]
            skip = True

        # process multiline
        elif line.startswith(MC_LINE):
            points = parse_multiline(line[len(MC_LINE):].strip('()'))
            world.drawLine((rotate(p, comp) for p in points),
                           color=getColor(color))

        # process circle
        elif line.startswith(MC_CIRCLE):
            xyz = 'xyz'
            items = line[len(MC_CIRCLE):].strip('()').split(',')
            # plane
            pla = [xyz.find(a) for a in items[0].strip("''")]
            # center and radius
            pos = [float(x) for x in items[1:4]]
            rad = float(items[4])
            points = draw_circle(pla, pos, rad, comp)
            world.drawLine(points, color=getColor(color))

        # activate neutron when it enters
        elif line.startswith(MC_ENTER):
            neutron = []
            skip = True
            active = True
            inspect = False
            color += 1

        # deactivate neutron when it leaves
        elif line.startswith(MC_LEAVE) or line.startswith(MC_ABSORB):
            active = False
            if inspectComp is None or inspect:
                world.drawLine(neutron, color="1 0 0")
                neutrons_drawed += 1

        # register state and scatter
        elif line.startswith(MC_STATE) or line.startswith(MC_SCATTER):

            if not active:
                continue

            if skip:
                skip = False
                continue

            if inspectComp and inspectComp == compName:
                # We will draw this neutron!
                inspect = True

            # keep track of points the neutron passes through
            xyz = [float(x) for x in line[line.find(':') + 1:].split(',')[:3]]
            xyz = rotate(xyz, comp)
            neutron.append(xyz)

    print('Neutrons drawed:', neutrons_drawed,
          (inspectComp and '(reaching %s)' % inspectComp or '(all)'))
    return world
Example #24
0
def parse_trace():
    ''' Parse McStas trace output from stdin and write results
        to file objects csv_comps and csv_lines '''

    mpl.rcParams['legend.fontsize'] = 10

    fig = plt.figure(figsize=plt.figaspect(0.5) * 1.5)
    ax = fig.gca(projection='3d')
    ax.set_xlabel("z")
    ax.set_ylabel("x")
    ax.set_zlabel("y")
    ax.set_aspect('equal')
    #    ax.autoscale_view(scalex=False, scaley=False, scalez=False)
    color = 0

    # map from component name to (position, rotation matrix)
    comps = {}

    # active (position, rotation matrix)
    comp = (np.array([0, 0, 0]), np.array([1, 0, 0, 0, 1, 0, 0, 0,
                                           1]).reshape(3, 3))

    # previous neutron position
    prev = None
    skip = False
    # we are drawing a neutron
    active = False
    xstate = []
    ystate = []
    zstate = []

    while True:
        # read line
        line = get_line()
        if not line:
            break

        # register components
        if line.startswith(UC_COMP):
            # grab info line
            info = get_line()
            assert info[:4] == 'POS:'
            nums = [x.strip() for x in info[4:].split(',')]
            # extract fields
            name = line[len(UC_COMP):].strip(' "\n')
            pos = np.array([float(x) for x in nums[:3]])
            # read flat 3x3 rotation matrix
            rot = np.array([float(x) for x in nums[3:3 + 9]]).reshape(3, 3)
            comps[name] = (pos, rot)

        # switch perspective
        elif line.startswith(MC_COMP):
            color += 1
            comp = comps[line[len(MC_COMP) + 1:]]

        elif line.startswith(MC_COMP_SHORT):
            name = line[len(MC_COMP_SHORT) + 1:].strip('"')
            comp = comps[name]
            skip = True

        # process multiline
        elif line.startswith(MC_LINE):
            points = parse_multiline(line[len(MC_LINE):].strip('()'))
            start = points.pop(0)
            (x, y, z) = rotate_points(points, comp)
            ax.plot(z, x, y)

        # process circle
        elif line.startswith(MC_CIRCLE):
            xyz = 'xyz'
            items = line[len(MC_CIRCLE):].strip('()').split(',')
            # plane
            pla = [xyz.find(a) for a in items[0].strip("''")]
            # center and radius
            pos = [float(x) for x in items[1:4]]
            rad = float(items[4])
            (x, y, z) = draw_circle(pla, pos, rad, comp)
            ax.plot(z, x, y)

        # activate neutron when it enters
        elif line.startswith(MC_ENTER):
            prev = None
            skip = True
            active = True
            color = 0
            xstate = []
            ystate = []
            zstate = []

        # deactivate neutron when it leaves
        elif line.startswith(MC_LEAVE):
            ax.plot(zstate, xstate, ystate)
            active = False
            prev = None

        elif line.startswith(MC_ABSORB):
            pass

        # register state and scatter
        elif line.startswith(MC_STATE) or line.startswith(MC_SCATTER):

            if not active:
                continue

            if skip:
                skip = False
                continue

            xyz = [float(x) for x in line[line.find(':') + 1:].split(',')[:3]]
            xyz = rotate(xyz, comp)
            if prev is not None:
                xstate.append(xyz[0])
                ystate.append(xyz[1])
                zstate.append(xyz[2])
            prev = xyz
            xstate.append(prev[0])
            ystate.append(prev[1])
            zstate.append(prev[2])

        # kick out legacy "junk"
        elif line.startswith(MC_MAGNIFY) or line.startswith(
                MC_START) or line.startswith(MC_END) or line.startswith(
                    MC_STOP):
            continue
        else:
            print(line)

    # A little bit of logic for controlling the aspect ratios/view
    (xmin, xmax) = ax.get_xlim3d()
    (ymin, ymax) = ax.get_ylim3d()
    (zmin, zmax) = ax.get_zlim3d()
    dx = xmax - xmin
    dy = ymax - ymin
    dz = zmax - zmin
    dmax = max(dx, dy, dz)

    # Check ranges and define axis box of max length cubed
    if dmax > dx:
        mean = (xmax + xmin) / 2
        xmin = mean - dmax / 2
        xmax = mean + dmax / 2
    if dmax > dy:
        mean = (ymax + ymin) / 2
        ymin = mean - dmax / 2
        ymax = mean + dmax / 2
    if dmax > dz:
        mean = (zmax + zmin) / 2
        zmin = mean - dmax / 2
        zmax = mean + dmax / 2

    # Set new axis limits
    ax.set_xlim3d(xmin, xmax)
    ax.set_ylim3d(ymin, ymax)
    ax.set_zlim3d(zmin, zmax)

    plt.show()
Example #25
0
def echodoc_search():
    util.clear_cache()

    text = vim.eval("a:text")
    text_len = len(text)
    if text_len == 0:
        return

    m = re.match("\w+", text)
    if not m:
        return
    method_name = m.group(0)

    chain_start = util.get_cursor_col_num() - text_len - 1
    line_num = util.get_cursor_line_num()
    line = util.get_line(line_num)[:chain_start]
    line = "{}{}()".format(line, method_name)

    method_args = None
    tokens = script.get_token_chain(line, line_num, len(line))
    if tokens and type(tokens[-1]) is script.MethodToken:
        method_args = tokens[-1].args
    else:
        return

    hl_identifier = vim.eval("g:echodoc#highlight_identifier")
    hl_arguments = vim.eval("g:echodoc#highlight_arguments")
    arg_hl_index = 0
    paren_count = 0
    for char in text[len(m.group(0)) + 1:]:
        if char == "(":
            paren_count += 1
        elif char == ")":
            paren_count -= 1
        elif char == "," and paren_count <= 0:
            arg_hl_index += 1

    echodoc = [{
        "text": method_name,
        "highlight": hl_identifier
    }, {
        "text": "("
    }]

    arg_count = len(method_args)
    for (i, arg) in enumerate(method_args):
        if arg.type:
            echodoc.append({
                "text": "{} ".format(arg.type),
                "highlight": "gdClass"
            })
        d = {"text": arg.name}
        if arg_hl_index == i:
            d["highlight"] = hl_arguments
        echodoc.append(d)
        if arg_count - 1 > i:
            echodoc.append({"text": ", "})
    if tokens[-1].qualifiers and "vararg" in tokens[-1].qualifiers:
        if arg_count > 0:
            echodoc.append({"text": ", "})
        d = {"text": "..."}
        if arg_hl_index >= arg_count:
            d["highlight"] = hl_arguments
        echodoc.append(d)
    echodoc.append({"text": ")"})

    vim.command("let echodoc_search_result = {}".format(str(echodoc)))