annotate hexagram-6ae12361157c/hexagram/hexagram.py~ @ 0:1407e3634bcf draft default tip

Uploaded r11 from test tool shed.
author adam-novak
date Tue, 22 Oct 2013 14:17:59 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1 #!/usr/bin/env python2.7
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
2 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
3 hexagram.py: Given a matrix of similarities, produce a hexagram visualization.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
4
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
5 This script takes in the filename of a tab-separated value file containing a
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
6 sparse similarity matrix (with string labels) and several matrices of
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
7 layer/score data. It produces an HTML file (and several support files) that
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
8 provide an interactive visualization of the items clustered on a hexagonal grid.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
9
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
10 This script depends on the DrL graph alyout package, binaries for which must be
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
11 present in your PATH.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
12
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
13 Re-uses sample code and documentation from
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
14 <http://users.soe.ucsc.edu/~karplus/bme205/f12/Scaffold.html>
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
15 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
16
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
17 import argparse, sys, os, itertools, math, numpy, subprocess, shutil, tempfile
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
18 import collections, scipy.stats, multiprocessing, traceback, numpy.ma
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
19 import os.path
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
20 import tsv
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
21
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
22 def parse_args(args):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
23 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
24 Takes in the command-line arguments list (args), and returns a nice argparse
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
25 result with fields for all the options.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
26 Borrows heavily from the argparse documentation examples:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
27 <http://docs.python.org/library/argparse.html>
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
28 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
29
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
30 # The command line arguments start with the program name, which we don't
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
31 # want to treat as an argument for argparse. So we remove it.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
32 args = args[1:]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
33
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
34 # Construct the parser (which is stored in parser)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
35 # Module docstring lives in __doc__
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
36 # See http://python-forum.com/pythonforum/viewtopic.php?f=3&t=36847
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
37 # And a formatter class so our examples in the docstring look good. Isn't it
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
38 # convenient how we already wrapped it to 80 characters?
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
39 # See http://docs.python.org/library/argparse.html#formatter-class
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
40 parser = argparse.ArgumentParser(description=__doc__,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
41 formatter_class=argparse.RawDescriptionHelpFormatter)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
42
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
43 # Now add all the options to it
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
44 # Options match the ctdHeatmap tool options as much as possible.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
45 parser.add_argument("similarities", type=argparse.FileType("r"),
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
46 help="the TSV file with the similarities for signatures we're using")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
47 parser.add_argument("--scores", type=str,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
48 action="append", default=[],
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
49 help="a TSV to read scores for each signature from")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
50 parser.add_argument("--colormaps", type=argparse.FileType("r"),
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
51 default=None,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
52 help="a TSV defining coloring and value names for discrete scores")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
53 parser.add_argument("--html", "-H", type=str,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
54 default="index.html",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
55 help="where to write HTML report")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
56 parser.add_argument("--directory", "-d", type=str, default=".",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
57 help="directory in which to create other output files")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
58 parser.add_argument("--query", type=str, default=None,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
59 help="Galaxy-escaped name of the query signature")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
60 parser.add_argument("--window_size", type=int, default=20,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
61 help="size of the window to use when looking for clusters")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
62 parser.add_argument("--no-stats", dest="stats", action="store_false",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
63 default=True,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
64 help="disable cluster-finding statistics")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
65
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
66 return parser.parse_args(args)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
67
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
68 def hexagon_center(x, y, scale=1.0):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
69 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
70 Given a coordinate on a grid of hexagons (using wiggly rows in x), what is
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
71 the 2d Euclidian coordinate of its center?
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
72
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
73 x and y are integer column and row coordinates of the hexagon in the grid.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
74
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
75 scale is a float specifying hexagon side length.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
76
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
77 The origin in coordinate space is defined as the upper left corner of the
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
78 bounding box of the hexagon wityh indices x=0 and y=0.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
79
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
80 Returns a tuple of floats.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
81 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
82 # The grid looks like this:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
83 #
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
84 # /-\ /-\ /-\ /-\
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
85 # /-\-/-\-/-\-/-\-/-\
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
86 # \-/-\-/-\-/-\-/-\-/
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
87 # /-\-/-\-/-\-/-\-/-\
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
88 # \-/-\-/-\-/-\-/-\-/
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
89 # /-\-/-\-/-\-/-\-/-\
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
90 # \-/ \-/ \-/ \-/ \-/
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
91 #
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
92 # Say a hexagon side has length 1
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
93 # It's 2 across corner to corner (x), and sqrt(3) across side to side (y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
94 # X coordinates are 1.5 per column
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
95 # Y coordinates (down from top) are sqrt(3) per row, -1/2 sqrt(3) if you're
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
96 # in an odd column.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
97
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
98 center_y = math.sqrt(3) * y
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
99 if x % 2 == 1:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
100 # Odd column: shift up
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
101 center_y -= 0.5 * math.sqrt(3)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
102
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
103 return (1.5 * x * scale + scale, center_y * scale + math.sqrt(3.0) / 2.0 *
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
104 scale)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
105
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
106 def hexagon_pick(x, y, scale=1.0):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
107 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
108 Given floats x and y specifying coordinates in the plane, determine which
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
109 hexagon grid cell that point is in.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
110
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
111 scale is a float specifying hexagon side length.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
112
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
113 See http://blog.ruslans.com/2011/02/hexagonal-grid-math.html
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
114 But we flip the direction of the wiggle. Odd rows are up (-y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
115 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
116
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
117 # How high is a hex?
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
118 hex_height = math.sqrt(3) * scale
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
119
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
120 # First we pick a rectangular tile, from the point of one side-traingle to
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
121 # the base of the other in width, and the whole hexagon height in height.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
122
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
123 # How wide are these tiles? Corner to line-between-far-corners distance
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
124 tile_width = (3.0 / 2.0 * scale)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
125
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
126 # Tile X index is floor(x / )
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
127 tile_x = int(math.floor(x / tile_width))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
128
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
129 # We need this intermediate value for the Y index and for tile-internal
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
130 # picking
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
131 corrected_y = y + (tile_x % 2) * hex_height / 2.0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
132
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
133 # Tile Y index is floor((y + (x index mod 2) * hex height/2) / hex height)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
134 tile_y = int(math.floor(corrected_y / hex_height))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
135
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
136 # Find coordinates within the tile
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
137 internal_x = x - tile_x * tile_width
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
138 internal_y = corrected_y - tile_y * hex_height
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
139
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
140 # Do tile-scale picking
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
141 # Are we in the one corner, the other corner, or the bulk of the tile?
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
142 if internal_x > scale * abs(0.5 - internal_y / hex_height):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
143 # We're in the bulk of the tile
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
144 # This is the column (x) of the picked hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
145 hexagon_x = tile_x
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
146
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
147 # This is the row (y) of the picked hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
148 hexagon_y = tile_y
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
149 else:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
150 # We're in a corner.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
151 # In an even column, the lower left is part of the next row, and the
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
152 # upper left is part of the same row. In an odd column, the lower left
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
153 # is part of the same row, and the upper left is part of the previous
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
154 # row.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
155 if internal_y > hex_height / 2.0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
156 # It's the lower left corner
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
157 # This is the offset in row (y) that being in this corner gives us
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
158 # The lower left corner is always 1 row below the upper left corner.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
159 corner_y_offset = 1
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
160 else:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
161 corner_y_offset = 0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
162
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
163 # TODO: verify this for correctness. It seems to be right, but I want a
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
164 # unit test to be sure.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
165 # This is the row (y) of the picked hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
166 hexagon_y = tile_y - tile_x % 2 + corner_y_offset
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
167
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
168 # This is the column (x) of the picked hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
169 hexagon_x = tile_x - 1
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
170
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
171 # Now we've picked the hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
172 return (hexagon_x, hexagon_y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
173
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
174 def radial_search(center_x, center_y):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
175 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
176 An iterator that yields coordinate tuples (x, y) in order of increasing
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
177 hex-grid distance from the specified center position.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
178 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
179
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
180 # A hexagon has neighbors at the following relative coordinates:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
181 # (-1, 0), (1, 0), (0, -1), (0, 1)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
182 # and ((-1, 1) and (1, 1) if in an even column)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
183 # or ((-1, -1) and (1, -1) if in an odd column)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
184
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
185 # We're going to go outwards using breadth-first search, so we need a queue
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
186 # of hexes to visit and a set of already visited hexes.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
187
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
188 # This holds a queue (really a deque) of hexes waiting to be visited.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
189 # A list has O(n) pop/insert at left.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
190 queue = collections.deque()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
191 # This holds a set of the (x, y) coordinate tuples of already-seen hexes,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
192 # so we don't enqueue them again.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
193 seen = set()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
194
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
195 # First place to visit is the center.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
196 queue.append((center_x, center_y))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
197
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
198 while len(queue) > 0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
199 # We should in theory never run out of items in the queue.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
200 # Get the current x and y to visit.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
201 x, y = queue.popleft()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
202
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
203 # Yield the location we're visiting
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
204 yield (x, y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
205
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
206 # This holds a list of all relative neighbor positions as (x, y) tuples.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
207 neighbor_offsets = [(-1, 0), (1, 0), (0, -1), (0, 1)]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
208 if y % 2 == 0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
209 # An even-column hex also has these neighbors
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
210 neighbor_offsets += [(-1, 1), (1, 1)]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
211 else:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
212 # An odd-column hex also has these neighbors
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
213 neighbor_offsets += [(-1, -1), (1, -1)]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
214
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
215 for x_offset, y_offset in neighbor_offsets:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
216 # First calculate the absolute position of the neighbor in x
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
217 neighbor_x = x + x_offset
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
218 # And in y
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
219 neighbor_y = y + y_offset
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
220
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
221 if (neighbor_x, neighbor_y) not in seen:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
222 # This is a hex that has never been in the queue. Add it.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
223 queue.append((neighbor_x, neighbor_y))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
224
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
225 # Record that it has ever been enqueued
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
226 seen.add((neighbor_x, neighbor_y))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
227
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
228
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
229
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
230
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
231 def assign_hexagon(hexagons, node_x, node_y, node, scale=1.0):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
232 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
233 This function assigns the given node to a hexagon in hexagons. hexagons is a
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
234 defaultdict from tuples of hexagon (x, y) integer indices to assigned nodes,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
235 or None if a hexagon is free. node_x and node_y are the x and y coordinates
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
236 of the node, adapted so that the seed node lands in the 0, 0 hexagon, and
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
237 re-scaled to reduce hexagon conflicts. node is the node to be assigned.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
238 scale, if specified, is the hexagon side length in node space units.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
239
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
240 This function assigns nodes to their closest hexagon, reprobing outwards if
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
241 already occupied.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
242
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
243 When the function completes, node is stored in hexagons under some (x, y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
244 tuple.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
245
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
246 Returns the distance this hexagon is from its ideal location.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
247 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
248
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
249 # These hold the hexagon that the point falls in, which may be taken.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
250 best_x, best_y = hexagon_pick(node_x, node_y, scale=scale)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
251
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
252 for x, y in radial_search(best_x, best_y):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
253 # These hexes are enumerated in order of increasign distance from the
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
254 # best one, starting with the best hex itself.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
255
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
256 if hexagons[(x, y)] is None:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
257 # This is the closest free hex. Break out of the loop, leaving x and
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
258 # y pointing here.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
259 break
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
260
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
261 # Assign the node to the hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
262 hexagons[(x, y)] = node
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
263
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
264 return math.sqrt((x - best_x) ** 2 + (y - best_y) ** 2)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
265
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
266
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
267
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
268 def assign_hexagon_local_radial(hexagons, node_x, node_y, node, scale=1.0):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
269 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
270 This function assigns the given node to a hexagon in hexagons. hexagons is a
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
271 defaultdict from tuples of hexagon (x, y) integer indices to assigned nodes,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
272 or None if a hexagon is free. node_x and node_y are the x and y coordinates
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
273 of the node, adapted so that the seed node lands in the 0, 0 hexagon, and
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
274 re-scaled to reduce hexagon conflicts. node is the node to be assigned.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
275 scale, if specified, is the hexagon side length in node space units.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
276
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
277 This function assigns nodes to their closest hexagon. If thast hexagon is
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
278 full, it re-probes in the direction that the node is from the closest
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
279 hexagon's center.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
280
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
281 When the function completes, node is stored in hexagons under some (x, y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
282 tuple.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
283
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
284 Returns the distance this hexagon is from its ideal location.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
285 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
286
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
287 # These hold the hexagon that the point falls in, which may be taken.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
288 best_x, best_y = hexagon_pick(node_x, node_y, scale=scale)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
289
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
290 # These hold the center of that hexagon in float space
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
291 center_x, center_y = hexagon_center(best_x, best_y, scale=scale)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
292
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
293 # This holds the distance from this point to the center of that hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
294 node_distance = math.sqrt((node_x - center_x) ** 2 + (node_y - center_y) **
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
295 2)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
296
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
297 # These hold the normalized direction of this point, relative to the center
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
298 # of its best hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
299 direction_x = (node_x - center_x) / node_distance
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
300 direction_y = (node_y - center_y) / node_distance
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
301
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
302 # Do a search in that direction, starting at the best hex.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
303
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
304 # These are the hexagon indices we're considering
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
305 x, y = best_x, best_y
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
306
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
307 # These are the Cartesian coordinates we're probing. Must be in the x, y hex
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
308 # as a loop invariant.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
309 test_x, test_y = center_x, center_y
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
310
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
311 while hexagons[(x, y)] is not None:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
312 # Re-probe outwards from the best hex in scale/2-sized steps
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
313 # TODO: is that the right step size? Scale-sized steps seemed slightly
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
314 # large.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
315 test_x += direction_x * scale
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
316 test_y += direction_y * scale
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
317
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
318 # Re-pick x and y for the hex containing our test point
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
319 x, y = hexagon_pick(test_x, test_y, scale=scale)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
320
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
321 # We've finally reached the edge of the cluster.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
322 # Drop our hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
323 hexagons[(x, y)] = node
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
324
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
325 return math.sqrt((x - best_x) ** 2 + (y - best_y) ** 2)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
326
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
327 def assign_hexagon_radial(hexagons, node_x, node_y, node, scale=1.0):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
328 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
329 This function assigns the given node to a hexagon in hexagons. hexagons is a
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
330 defaultdict from tuples of hexagon (x, y) integer indices to assigned nodes,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
331 or None if a hexagon is free. node_x and node_y are the x and y coordinates
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
332 of the node, adapted so that the seed node lands in the 0, 0 hexagon, and
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
333 re-scaled to reduce hexagon conflicts. node is the node to be assigned.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
334 scale, if specified, is the hexagon side length in node space units.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
335
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
336 This function assigns nodes to hexagons based on radial distance from 0, 0.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
337 This makes hexagon assignment much more dense, but can lose spatial
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
338 structure.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
339
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
340 When the function completes, node is stored in hexagons under some (x, y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
341 tuple.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
342
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
343 Returns the distance this hexagon is from its ideal location. Unfortunately,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
344 this doesn't really make sense for this assignment scheme, so it is always
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
345 0.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
346 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
347
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
348 # Compute node's distance from the origin
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
349 node_distance = math.sqrt(node_x ** 2 + node_y ** 2)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
350
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
351 # Compute normalized direction from the origin for this node
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
352 direction_x = node_x / node_distance
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
353 direction_y = node_y / node_distance
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
354
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
355 # These are the coordinates we are testing
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
356 test_x = 0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
357 test_y = 0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
358
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
359 # These are the hexagon indices that correspond to that point
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
360 x, y = hexagon_pick(test_x, test_y, scale=scale)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
361
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
362 while hexagons[(x, y)] is not None:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
363 # Re-probe outwards from the origin in scale-sized steps
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
364 # TODO: is that the right step size?
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
365 test_x += direction_x * scale
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
366 test_y += direction_y * scale
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
367
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
368 # Re-pick
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
369 x, y = hexagon_pick(test_x, test_y, scale=scale)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
370
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
371 # We've finally reached the edge of the cluster.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
372 # Drop our hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
373 # TODO: this has to be N^2 if we line them all up in a line
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
374 hexagons[(x, y)] = node
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
375
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
376 return 0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
377
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
378 def hexagons_in_window(hexagons, x, y, width, height):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
379 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
380 Given a dict from (x, y) position to signature names, return the list of all
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
381 signatures in the window starting at hexagon x, y and extending width in the
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
382 x direction and height in the y direction on the hexagon grid.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
383 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
384
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
385 # This holds the list of hexagons we've found
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
386 found = []
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
387
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
388 for i in xrange(x, x + width):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
389 for j in xrange(y, y + height):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
390 if hexagons.has_key((i, j)):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
391 # This position in the window has a hex.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
392 found.append(hexagons[(i, j)])
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
393
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
394 return found
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
395
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
396 class ClusterFinder(object):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
397 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
398 A class that can be invoked to find the p value of the best cluster in its
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
399 layer. Instances are pickleable.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
400 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
401
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
402 def __init__(self, hexagons, layer, window_size=5):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
403 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
404 Keep the given hexagons dict (from (x, y) to signature name) and the
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
405 given layer (a dict from signature name to a value), and the given
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
406 window size, in a ClusterFinder object.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
407 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
408
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
409 # TODO: This should probably all operate on numpy arrays that we can
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
410 # slice efficiently.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
411
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
412 # Store the layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
413 self.hexagons = hexagons
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
414 # Store the hexagon assignments
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
415 self.layer = layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
416
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
417 # Store the window size
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
418 self.window_size = window_size
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
419
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
420 @staticmethod
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
421 def continuous_p(in_values, out_values):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
422 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
423 Get the p value for in_values and out_values being distinct continuous
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
424 distributions.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
425
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
426 in_values and out_values are both Numpy arrays. Returns the p value, or
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
427 raises a ValueError if the statistical test cannot be run for some
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
428 reason.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
429
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
430 Uses the Mann-Whitney U test.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
431 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
432
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
433 # Do a Mann-Whitney U test to see how different the data
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
434 # sets are.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
435 u_statistic, p_value = scipy.stats.mannwhitneyu(in_values,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
436 out_values)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
437
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
438 return p_value
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
439
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
440 @staticmethod
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
441 def dichotomous_p(in_values, out_values):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
442 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
443 Given two one-dimensional Numpy arrays of 0s and 1s, compute a p value
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
444 for the in_values having a different probability of being 1 than the
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
445 frequency of 1s in the out_values.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
446
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
447 This test uses the scipy.stats.binom_test function, which does not claim
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
448 to use the normal approximation. Therefore, this test should be valid
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
449 for arbitrarily small frequencies of either 0s or 1s in in_values.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
450
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
451 TODO: What if out_values is shorter than in_values?
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
452 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
453
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
454 if len(out_values) == 0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
455 raise ValueError("Background group is empty!")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
456
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
457 # This holds the observed frequency of 1s in out_values
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
458 frequency = numpy.sum(out_values) / len(out_values)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
459
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
460 # This holds the number of 1s in in_values
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
461 successes = numpy.sum(in_values)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
462
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
463 # This holds the number of "trials" we got that many successes in
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
464 trials = len(in_values)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
465
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
466 # Return how significantly the frequency inside differs from that
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
467 # outside.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
468 return scipy.stats.binom_test(successes, trials, frequency)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
469
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
470 @staticmethod
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
471 def categorical_p(in_values, out_values):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
472 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
473 Given two one-dimensional Numpy arrays of integers (which may be stored
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
474 as floats), which represent items being assigned to different
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
475 categories, return a p value for the distribution of categories observed
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
476 in in_values differing from that observed in out_values.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
477
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
478 The normal way to do this is with a chi-squared goodness of fit test.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
479 However, that test has invalid assumptions when there are fewer than 5
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
480 expected and 5 observed observations in every category.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
481 See http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chis
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
482 quare.html
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
483
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
484 However, we will use it anyway, because the tests that don't break down
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
485 are prohibitively slow.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
486 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
487
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
488 # Convert our inputs to integer arrays
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
489 in_values = in_values.astype(int)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
490 out_values = out_values.astype(int)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
491
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
492 # How many categories are there (count 0 to the maximum value)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
493 num_categories = max(numpy.max(in_values), numpy.max(out_values)) + 1
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
494
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
495 # Count the number of in_values and out_values in each category
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
496 in_counts = numpy.array([len(in_values[in_values == i]) for i in
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
497 xrange(num_categories)])
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
498 out_counts = numpy.array([len(out_values[out_values == i]) for i in
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
499 xrange(num_categories)])
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
500
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
501 # Get the p value for the window being from the estimated distribution
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
502 # None of the distribution parameters count as "estimated from data"
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
503 # because they aren't estimated from the data under test.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
504 _, p_value = scipy.stats.chisquare(in_counts, out_counts)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
505
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
506 return p_value
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
507
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
508 def __call__(self):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
509 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
510 Find the best p value for any window of size window_size. Return it.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
511 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
512
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
513 # Calculate the bounding box where we want to look for windows.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
514 # TODO: This would just be all of a numpy array
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
515 min_x = min(coords[0] for coords in self.hexagons.iterkeys())
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
516 min_y = min(coords[1] for coords in self.hexagons.iterkeys())
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
517 max_x = max(coords[0] for coords in self.hexagons.iterkeys())
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
518 max_y = max(coords[1] for coords in self.hexagons.iterkeys())
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
519
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
520 # This holds a Numpy array of all the data by x, y
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
521 layer_data = numpy.empty((max_x - min_x + 1, max_y - min_y + 1))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
522
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
523 # Fill it with NaN so we can mask those out later
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
524 layer_data[:] = numpy.NAN
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
525
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
526 for (hex_x, hex_y), name in self.hexagons.iteritems():
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
527 # Copy the layer values into the Numpy array
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
528 if self.layer.has_key(name):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
529 layer_data[hex_x - min_x, hex_y - min_y] = self.layer[name]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
530
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
531 # This holds a masked version of the layer data
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
532 layer_data_masked = numpy.ma.masked_invalid(layer_data, copy=False)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
533
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
534 # This holds the smallest p value we have found for this layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
535 best_p = float("+inf")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
536
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
537 # This holds the statistical test to use (a function from two Numpy
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
538 # arrays to a p value)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
539 # The most specific test is the dichotomous test (0 or 1)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
540 statistical_test = self.dichotomous_p
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
541
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
542 if numpy.sum(~layer_data_masked.mask) == 0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
543 # There is actually no data in this layer at all.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
544 # nditer complains if we try to iterate over an empty thing.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
545 # So quit early and say we couldn't find anything.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
546 return best_p
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
547
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
548 for value in numpy.nditer(layer_data_masked[~layer_data_masked.mask]):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
549 # Check all the values in the layer.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
550 # If this value is out of the domain of the current statistical
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
551 # test, upgrade to a more general test.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
552
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
553 if statistical_test == self.dichotomous_p and (value > 1 or
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
554 value < 0):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
555
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
556 # We can't use a dichotomous test on things outside 0 to 1
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
557 # But we haven't yet detected any non-integers
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
558 # Use categorical
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
559 statistical_test = self.categorical_p
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
560
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
561 if value % 1 != 0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
562 # This is not an integer value
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
563 # So, we must use a continuous statistical test
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
564 statistical_test = self.continuous_p
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
565
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
566 # This is the least specific test, so we can stop now
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
567 break
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
568
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
569
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
570 for i in xrange(min_x, max_x - self.window_size):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
571 for j in xrange(min_y, max_y - self.window_size):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
572
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
573 # Get the layer values for hexes in the window, as a Numpy
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
574 # masked array.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
575 in_region = layer_data_masked[i:i + self.window_size,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
576 j:j + self.window_size]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
577
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
578 # And as a 1d Numpy array
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
579 in_values = numpy.reshape(in_region[~in_region.mask], -1).data
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
580
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
581 # And out of the window (all the other hexes) as a masked array
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
582 out_region = numpy.ma.copy(layer_data_masked)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
583 # We get this by masking out everything in the region
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
584 out_region.mask[i:i + self.window_size,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
585 j:j + self.window_size] = True
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
586
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
587 # And as a 1d Numpy array
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
588 out_values = numpy.reshape(out_region[~out_region.mask],
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
589 -1).data
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
590
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
591
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
592 if len(in_values) == 0 or len(out_values) == 0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
593 # Can't do any stats on this window
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
594 continue
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
595
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
596 if len(in_values) < 0.5 * self.window_size ** 2:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
597 # The window is less than half full. Skip it.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
598 # TODO: Make this threshold configurable.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
599 continue
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
600
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
601 try:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
602
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
603 # Get the p value for this window under the selected
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
604 # statistical test
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
605 p_value = statistical_test(in_values, out_values)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
606
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
607 # If this is the best p value so far, record it
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
608 best_p = min(best_p, p_value)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
609 except ValueError:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
610 # Probably an all-zero layer, or something else the test
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
611 # can't handle.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
612 # But let's try all the other windows to be safe.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
613 # Maybe one will work.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
614 pass
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
615
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
616
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
617
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
618 # We have now found the best p for any window for this layer.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
619 print "Best p found: {}".format(best_p)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
620 sys.stdout.flush()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
621
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
622 return best_p
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
623
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
624 def run_functor(functor):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
625 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
626 Given a no-argument functor (like a ClusterFinder), run it and return its
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
627 result. We can use this with multiprocessing.map and map it over a list of
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
628 job functors to do them.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
629
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
630 Handles getting more than multiprocessing's pitiful exception output
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
631 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
632
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
633 try:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
634 return functor()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
635 except:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
636 # Put all exception text into an exception and raise that
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
637 raise Exception(traceback.format_exc())
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
638
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
639 def main(args):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
640 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
641 Parses command line arguments, and makes visualization.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
642 "args" specifies the program arguments, with args[0] being the executable
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
643 name. The return value should be used as the program's exit code.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
644 """
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
645
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
646 options = parse_args(args) # This holds the nicely-parsed options object
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
647
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
648 # Test our picking
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
649 x, y = hexagon_center(0, 0)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
650 if hexagon_pick(x, y) != (0, 0):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
651 raise Exception("Picking is broken!")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
652
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
653 # First bit of stdout becomes annotation in Galaxy
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
654
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
655 # Make sure our output directory exists.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
656 if not os.path.exists(options.directory):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
657 # makedirs is the right thing to use here: recursive
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
658 os.makedirs(options.directory)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
659
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
660 # Work in a temporary directory
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
661 drl_directory = tempfile.mkdtemp()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
662
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
663 # This is the base name for all the files that DrL uses to do the layout
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
664 # We're going to put it in a temporary directory.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
665 drl_basename = os.path.join(drl_directory, "layout")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
666
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
667 # We can just pass our similarity matrix to DrL's truncate
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
668 # But we want to run it through our tsv parser to strip comments and ensure
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
669 # it's valid
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
670
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
671 # This holds a reader for the similarity matrix
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
672 sim_reader = tsv.TsvReader(options.similarities)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
673
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
674 # This holds a writer for the sim file
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
675 sim_writer = tsv.TsvWriter(open(drl_basename + ".sim", "w"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
676
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
677 print "Regularizing similarity matrix..."
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
678 sys.stdout.flush()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
679
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
680 for parts in sim_reader:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
681 sim_writer.list_line(parts)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
682
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
683 sim_reader.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
684 sim_writer.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
685
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
686 # Now our input for DrL is prepared!
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
687
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
688 # Do DrL truncate.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
689 # TODO: pass a truncation level
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
690 print "DrL: Truncating..."
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
691 sys.stdout.flush()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
692 subprocess.check_call(["truncate", drl_basename])
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
693
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
694 # Run the DrL layout engine.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
695 print "DrL: Doing layout..."
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
696 sys.stdout.flush()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
697 subprocess.check_call(["layout", drl_basename])
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
698
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
699 # Put the string names back
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
700 print "DrL: Restoring names..."
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
701 sys.stdout.flush()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
702 subprocess.check_call(["recoord", drl_basename])
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
703
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
704 # Now DrL has saved its coordinates as <signature name>\t<x>\t<y> rows in
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
705 # <basename>.coord
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
706
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
707 # We want to read that.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
708 # This holds a reader for the DrL output
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
709 coord_reader = tsv.TsvReader(open(drl_basename + ".coord", "r"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
710
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
711 # This holds a dict from signature name string to (x, y) float tuple
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
712 nodes = {}
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
713
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
714 print "Reading DrL output..."
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
715 sys.stdout.flush()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
716 for parts in coord_reader:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
717 nodes[parts[0]] = (float(parts[1]), float(parts[2]))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
718
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
719 coord_reader.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
720
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
721 # Save the DrL coordinates in our bundle, to be displayed client-side for
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
722 # debugging.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
723 coord_writer = tsv.TsvWriter(open(
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
724 os.path.join(options.directory, "drl.tab"), "w"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
725
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
726 for signature_name, (x, y) in nodes.iteritems():
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
727 # Write a tsv with names instead of numbers, like what DrL recoord would
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
728 # have written. This is what the Javascript on the client side wants.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
729 coord_writer.line(signature_name, x, y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
730
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
731 coord_writer.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
732
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
733 # Do the hexagon layout
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
734 # We do the squiggly rows setup, so express everything as integer x, y
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
735
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
736 # This is a defaultdict from (x, y) integer tuple to id that goes there, or
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
737 # None if it's free.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
738 hexagons = collections.defaultdict(lambda: None)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
739
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
740 # This holds the side length that we use
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
741 side_length = 1.0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
742
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
743 # This holds what will be a layer of how badly placed each hexagon is
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
744 # A dict from node name to layer value
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
745 placement_badnesses = {}
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
746
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
747 for node, (node_x, node_y) in nodes.iteritems():
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
748 # Assign each node to a hexagon
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
749 # This holds the resulting placement badness for that hexagon (i.e.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
750 # distance from ideal location)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
751 badness = assign_hexagon(hexagons, node_x, node_y, node,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
752 scale=side_length)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
753
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
754 # Put the badness in the layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
755 placement_badnesses[node] = float(badness)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
756
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
757 # Normalize the placement badness layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
758 # This holds the max placement badness
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
759 max_placement_badness = max(placement_badnesses.itervalues())
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
760 print "Max placement badness: {}".format(max_placement_badness)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
761
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
762 if max_placement_badness != 0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
763 # Normalize by the max if possible.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
764 placement_badnesses = {node: value / max_placement_badness for node,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
765 value in placement_badnesses.iteritems()}
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
766
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
767 # The hexagons have been assigned. Make hexagons be a dict instead of a
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
768 # defaultdict, so it pickles.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
769 # TODO: I should change it so I don't need to do this.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
770 hexagons = dict(hexagons)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
771
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
772 # Now dump the hexagon assignments as an id, x, y tsv. This will be read by
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
773 # the JavaScript on the static page and be used to produce the
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
774 # visualization.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
775 hexagon_writer = tsv.TsvWriter(open(os.path.join(options.directory,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
776 "assignments.tab"), "w"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
777
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
778 # First find the x and y offsets needed to make all hexagon positions
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
779 # positive
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
780 min_x = min(coords[0] for coords in hexagons.iterkeys())
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
781 min_y = min(coords[1] for coords in hexagons.iterkeys())
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
782
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
783 for coords, name in hexagons.iteritems():
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
784 # Write this hexagon assignment, converted to all-positive coordinates.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
785 hexagon_writer.line(name, coords[0] - min_x, coords[1] - min_y)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
786 hexagon_writer.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
787
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
788 # Now that we have hex assignments, compute layers.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
789
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
790 # In addition to making per-layer files, we're going to copy all the score
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
791 # matrices to our output directoy. That way, the client can download layers
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
792 # in big chunks when it wants all layer data for statistics. We need to
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
793 # write a list of matrices that the client can read, which is written by
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
794 # this TSV writer.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
795 matrix_index_writer = tsv.TsvWriter(open(os.path.join(options.directory,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
796 "matrices.tab"), "w"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
797
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
798 # Read in all the layer data at once
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
799 # TODO: Don't read in all the layer data at once
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
800
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
801 # This holds a dict from layer name to a dict from signature name to
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
802 # score.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
803 layers = {}
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
804
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
805 # This holds the names of all layers
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
806 layer_names = []
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
807
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
808 for matrix_number, score_filename in enumerate(options.scores):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
809 # First, copy the whole matrix into our output. This holds its filename.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
810 output_filename = "matrix_{}.tab".format(matrix_number)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
811 shutil.copy2(score_filename, os.path.join(options.directory,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
812 output_filename))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
813
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
814 # Record were we put it
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
815 matrix_index_writer.line(output_filename)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
816
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
817 # This holds a reader for the scores TSV
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
818 scores_reader = tsv.TsvReader(open(score_filename, "r"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
819
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
820 # This holds an iterator over lines in that file
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
821 # TODO: Write a proper header/data API
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
822 scores_iterator = scores_reader.__iter__()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
823
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
824 try:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
825 # This holds the names of the columns (except the first, which is
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
826 # labels). They also happen to be layer names
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
827 file_layer_names = scores_iterator.next()[1:]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
828
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
829 # Add all the layers in this file to the complete list of layers.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
830 layer_names += file_layer_names
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
831
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
832 # Ensure that we have a dict for every layer mentioned in the file
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
833 # (even the ones that have no data below). Doing it this way means
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
834 # all score matrices need disjoint columns, or the last one takes
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
835 # precedence.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
836 for name in file_layer_names:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
837 layers[name] = {}
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
838
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
839 for parts in scores_iterator:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
840 # This is the signature that this line is about
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
841 signature_name = parts[0]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
842
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
843 # These are the scores for all the layers for this signature
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
844 layer_scores = parts[1:]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
845
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
846 for (layer_name, score) in itertools.izip(file_layer_names,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
847 layer_scores):
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
848
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
849 # Store all the layer scores in the appropriate
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
850 # dictionaries.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
851 try:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
852 layers[layer_name][signature_name] = float(score)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
853 except ValueError:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
854 # This is not a float.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
855 # Don't set that entry for this layer.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
856 # TODO: possibly ought to complain to the user? But then
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
857 # things like "N/A" won't be handled properly.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
858 continue
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
859
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
860 except StopIteration:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
861 # We don't have any real data here. Couldn't read the header line.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
862 # Skip to the next file
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
863 pass
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
864
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
865 # We're done with this score file now
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
866 scores_reader.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
867
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
868 # We're done with all the input score matrices, so our index is done too.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
869 matrix_index_writer.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
870
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
871 # We have now loaded all layer data into memory as Python objects. What
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
872 # could possibly go wrong?
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
873
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
874 # Stick our placement badness layer on the end
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
875 layer_names.append("Placement Badness")
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
876 layers["Placement Badness"] = placement_badnesses
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
877
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
878 # Now we need to write layer files.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
879
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
880 # Generate some filenames for layers that we can look up by layer name.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
881 # We do this because layer names may not be valid filenames.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
882 layer_files = {name: os.path.join(options.directory,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
883 "layer_{}.tab".format(number)) for (name, number) in itertools.izip(
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
884 layer_names, itertools.count())}
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
885
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
886 for layer_name, layer in layers.iteritems():
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
887 # Write out all the individual layer files
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
888 # This holds the writer for this layer file
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
889 scores_writer = tsv.TsvWriter(open(layer_files[layer_name], "w"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
890 for signature_name, score in layer.iteritems():
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
891 # Write the score for this signature in this layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
892 scores_writer.line(signature_name, score)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
893 scores_writer.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
894
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
895 # We need something to sort layers by. We have "priority" (lower is
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
896 # better)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
897
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
898 if len(layer_names) > 0 and options.stats:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
899 # We want to do this fancy parallel stats thing.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
900 # We skip it when there are no layers, so we don't try to join a
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
901 # never-used pool, which seems to hang.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
902
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
903 print "Running statistics..."
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
904
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
905 # This holds an iterator that makes ClusterFinders for all out layers
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
906 cluster_finders = [ClusterFinder(hexagons, layers[layer_name],
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
907 window_size=options.window_size) for layer_name in layer_names]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
908
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
909 print "{} jobs to do.".format(len(cluster_finders))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
910
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
911 # This holds a multiprocessing pool for parallelization
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
912 pool = multiprocessing.Pool()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
913
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
914 # This holds all the best p values in the same order
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
915 best_p_values = pool.map(run_functor, cluster_finders)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
916
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
917 # Close down the pool so multiprocessing won't die sillily at the end
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
918 pool.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
919 pool.join()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
920
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
921 # This holds a dict from layer name to priority (best p value)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
922 # We hope the order of the dict items has not changed
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
923 layer_priorities = {layer_name: best_p_value for layer_name,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
924 best_p_value in itertools.izip(layer_names, best_p_values)}
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
925 else:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
926 # We aren't doing any stats.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
927
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
928 print "Skipping statistics."
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
929
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
930 # Make up priorities.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
931 layer_priorities = {name: float("+inf") for name in layer_names}
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
932
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
933 # Count how many layer entries are greater than 0 for each binary layer, and
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
934 # store that number in this dict by layer name. Things with the default
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
935 # empty string instead of a number aren't binary layers, but they can use
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
936 # the empty string as their TSV field value, so we can safely pull any layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
937 # out of this by name.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
938 layer_positives = collections.defaultdict(str)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
939
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
940 for layer_name in layer_names:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
941 # Assume it's a binary layer until proven otherwise
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
942 layer_positives[layer_name] = 0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
943 for value in layers[layer_name].itervalues():
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
944 if value == 1:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
945 # Count up all the 1s in the layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
946 layer_positives[layer_name] += 1
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
947 elif value != 0:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
948 # It has something that isn't 1 or 0, so it can't be a binary
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
949 # layer. Throw it out and try the next layer.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
950 layer_positives[layer_name] = ""
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
951 continue
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
952
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
953 # Write an index of all the layers we have, in the form:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
954 # <layer>\t<file>\t<priority>\t<number of signatures with data>\t<number of
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
955 # signatures that are 1 for binary layers>
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
956 # This is the writer to use.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
957 index_writer = tsv.TsvWriter(open(os.path.join(options.directory,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
958 "layers.tab"), "w"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
959
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
960 for layer_name, layer_file in layer_files.iteritems():
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
961 # Write the index entry for this layer
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
962 index_writer.line(layer_name, os.path.basename(layer_file),
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
963 layer_priorities[layer_name], len(layers[layer_name]),
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
964 layer_positives[layer_name])
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
965
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
966 index_writer.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
967
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
968 # Copy over the user-specified colormaps file, or make an empty TSV if it's
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
969 # not specified.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
970
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
971
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
972
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
973 # This holds a writer for the sim file. Creating it creates the file.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
974 colormaps_writer = tsv.TsvWriter(open(os.path.join(options.directory,
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
975 "colormaps.tab"), "w"))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
976
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
977 if options.colormaps is not None:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
978 # The user specified colormap data, so copy it over
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
979 # This holds a reader for the colormaps file
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
980 colormaps_reader = tsv.TsvReader(options.colormaps)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
981
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
982 print "Regularizing colormaps file..."
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
983 sys.stdout.flush()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
984
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
985 for parts in colormaps_reader:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
986 colormaps_writer.list_line(parts)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
987
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
988 colormaps_reader.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
989
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
990 # Close the colormaps file we wrote. It may have gotten data, or it may
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
991 # still be empty.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
992 colormaps_writer.close()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
993
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
994 # Now copy any static files from where they live next to this Python file
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
995 # into the web page bundle.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
996 # This holds the directory where this script lives, which also contains
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
997 # static files.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
998 tool_root = os.path.dirname(os.path.realpath(__file__))
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
999
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1000 # Copy over all the static files we need for the web page
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1001 # This holds a list of them
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1002 static_files = [
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1003 # Static images
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1004 "drag.svg",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1005 "filter.svg",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1006 "statistics.svg",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1007 "right.svg",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1008 "throbber.svg",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1009
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1010 # jQuery itself is pulled from a CDN.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1011 # We can't take everything offline since Google Maps needs to be sourced
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1012 # from Google, so we might as well use CDN jQuery.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1013
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1014 # Select2 scripts and resources:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1015 "select2.css",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1016 "select2.js",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1017 "select2.png",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1018 "select2-spinner.gif",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1019 "select2x2.png",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1020
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1021 # The jQuery.tsv plugin
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1022 "jquery.tsv.js",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1023 # The color library
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1024 "color-0.4.1.js",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1025 # The jStat statistics library
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1026 "jstat-1.0.0.js",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1027 # The Google Maps MapLabel library
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1028 "maplabel-compiled.js",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1029 # The main CSS file
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1030 "hexagram.css",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1031 # The main JavaScript file that runs the page
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1032 "hexagram.js",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1033 # Web Worker for statistics
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1034 "statistics.js",
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1035 # File with all the tool code
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1036 "tools.js"
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1037 ]
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1038
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1039 # We'd just use a directory of static files, but Galaxy needs single-level
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1040 # output.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1041 for filename in static_files:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1042 shutil.copy2(os.path.join(tool_root, filename), options.directory)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1043
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1044 # Copy the HTML file to our output file. It automatically knows to read
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1045 # assignments.tab, and does its own TSV parsing
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1046 shutil.copy2(os.path.join(tool_root, "hexagram.html"), options.html)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1047
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1048 # Delete our temporary directory.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1049 shutil.rmtree(drl_directory)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1050
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1051 print "Visualization generation complete!"
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1052
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1053 return 0
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1054
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1055 if __name__ == "__main__" :
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1056 try:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1057 # Get the return code to return
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1058 # Don't just exit with it because sys.exit works by exceptions.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1059 return_code = main(sys.argv)
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1060 except:
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1061 traceback.print_exc()
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1062 # Return a definite number and not some unspecified error code.
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1063 return_code = 1
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1064
1407e3634bcf Uploaded r11 from test tool shed.
adam-novak
parents:
diff changeset
1065 sys.exit(return_code)