Show More
@@ -0,0 +1,90 | |||||
|
1 | from numpy import * | |||
|
2 | ||||
|
3 | def mandel(n, m, itermax, xmin, xmax, ymin, ymax): | |||
|
4 | ''' | |||
|
5 | Fast mandelbrot computation using numpy. | |||
|
6 | ||||
|
7 | (n, m) are the output image dimensions | |||
|
8 | itermax is the maximum number of iterations to do | |||
|
9 | xmin, xmax, ymin, ymax specify the region of the | |||
|
10 | set to compute. | |||
|
11 | ''' | |||
|
12 | # The point of ix and iy is that they are 2D arrays | |||
|
13 | # giving the x-coord and y-coord at each point in | |||
|
14 | # the array. The reason for doing this will become | |||
|
15 | # clear below... | |||
|
16 | ix, iy = mgrid[0:n, 0:m] | |||
|
17 | # Now x and y are the x-values and y-values at each | |||
|
18 | # point in the array, linspace(start, end, n) | |||
|
19 | # is an array of n linearly spaced points between | |||
|
20 | # start and end, and we then index this array using | |||
|
21 | # numpy fancy indexing. If A is an array and I is | |||
|
22 | # an array of indices, then A[I] has the same shape | |||
|
23 | # as I and at each place i in I has the value A[i]. | |||
|
24 | x = linspace(xmin, xmax, n)[ix] | |||
|
25 | y = linspace(ymin, ymax, m)[iy] | |||
|
26 | # c is the complex number with the given x, y coords | |||
|
27 | c = x+complex(0,1)*y | |||
|
28 | del x, y # save a bit of memory, we only need z | |||
|
29 | # the output image coloured according to the number | |||
|
30 | # of iterations it takes to get to the boundary | |||
|
31 | # abs(z)>2 | |||
|
32 | img = zeros(c.shape, dtype=int) | |||
|
33 | # Here is where the improvement over the standard | |||
|
34 | # algorithm for drawing fractals in numpy comes in. | |||
|
35 | # We flatten all the arrays ix, iy and c. This | |||
|
36 | # flattening doesn't use any more memory because | |||
|
37 | # we are just changing the shape of the array, the | |||
|
38 | # data in memory stays the same. It also affects | |||
|
39 | # each array in the same way, so that index i in | |||
|
40 | # array c has x, y coords ix[i], iy[i]. The way the | |||
|
41 | # algorithm works is that whenever abs(z)>2 we | |||
|
42 | # remove the corresponding index from each of the | |||
|
43 | # arrays ix, iy and c. Since we do the same thing | |||
|
44 | # to each array, the correspondence between c and | |||
|
45 | # the x, y coords stored in ix and iy is kept. | |||
|
46 | ix.shape = n*m | |||
|
47 | iy.shape = n*m | |||
|
48 | c.shape = n*m | |||
|
49 | # we iterate z->z^2+c with z starting at 0, but the | |||
|
50 | # first iteration makes z=c so we just start there. | |||
|
51 | # We need to copy c because otherwise the operation | |||
|
52 | # z->z^2 will send c->c^2. | |||
|
53 | z = copy(c) | |||
|
54 | for i in xrange(itermax): | |||
|
55 | if not len(z): break # all points have escaped | |||
|
56 | # equivalent to z = z*z+c but quicker and uses | |||
|
57 | # less memory | |||
|
58 | multiply(z, z, z) | |||
|
59 | add(z, c, z) | |||
|
60 | # these are the points that have escaped | |||
|
61 | rem = abs(z)>2.0 | |||
|
62 | # colour them with the iteration number, we | |||
|
63 | # add one so that points which haven't | |||
|
64 | # escaped have 0 as their iteration number, | |||
|
65 | # this is why we keep the arrays ix and iy | |||
|
66 | # because we need to know which point in img | |||
|
67 | # to colour | |||
|
68 | img[ix[rem], iy[rem]] = i+1 | |||
|
69 | # -rem is the array of points which haven't | |||
|
70 | # escaped, in numpy -A for a boolean array A | |||
|
71 | # is the NOT operation. | |||
|
72 | rem = -rem | |||
|
73 | # So we select out the points in | |||
|
74 | # z, ix, iy and c which are still to be | |||
|
75 | # iterated on in the next step | |||
|
76 | z = z[rem] | |||
|
77 | ix, iy = ix[rem], iy[rem] | |||
|
78 | c = c[rem] | |||
|
79 | return img | |||
|
80 | ||||
|
81 | if __name__=='__main__': | |||
|
82 | from pylab import * | |||
|
83 | import time | |||
|
84 | start = time.time() | |||
|
85 | I = mandel(400, 400, 100, -2, .5, -1.25, 1.25) | |||
|
86 | print 'Time taken:', time.time()-start | |||
|
87 | I[I==0] = 101 | |||
|
88 | img = imshow(I.T, origin='lower left') | |||
|
89 | img.write_png('mandel.png', noscale=True) | |||
|
90 | show() |
@@ -228,13 +228,13 class AppWithClusterDirArgParseConfigLoader(ArgParseConfigLoader): | |||||
228 | """Default command line options for IPython cluster applications.""" |
|
228 | """Default command line options for IPython cluster applications.""" | |
229 |
|
229 | |||
230 | def _add_other_arguments(self): |
|
230 | def _add_other_arguments(self): | |
231 |
self.parser.add_argument(' |
|
231 | self.parser.add_argument('--ipython-dir', | |
232 | dest='Global.ipythondir',type=str, |
|
232 | dest='Global.ipythondir',type=str, | |
233 | help='Set to override default location of Global.ipythondir.', |
|
233 | help='Set to override default location of Global.ipythondir.', | |
234 | default=NoConfigDefault, |
|
234 | default=NoConfigDefault, | |
235 | metavar='Global.ipythondir' |
|
235 | metavar='Global.ipythondir' | |
236 | ) |
|
236 | ) | |
237 |
self.parser.add_argument('-p', |
|
237 | self.parser.add_argument('-p', '--profile', | |
238 | dest='Global.profile',type=str, |
|
238 | dest='Global.profile',type=str, | |
239 | help='The string name of the profile to be used. This determines ' |
|
239 | help='The string name of the profile to be used. This determines ' | |
240 | 'the name of the cluster dir as: cluster_<profile>. The default profile ' |
|
240 | 'the name of the cluster dir as: cluster_<profile>. The default profile ' | |
@@ -243,25 +243,25 class AppWithClusterDirArgParseConfigLoader(ArgParseConfigLoader): | |||||
243 | default=NoConfigDefault, |
|
243 | default=NoConfigDefault, | |
244 | metavar='Global.profile' |
|
244 | metavar='Global.profile' | |
245 | ) |
|
245 | ) | |
246 |
self.parser.add_argument(' |
|
246 | self.parser.add_argument('--log-level', | |
247 | dest="Global.log_level",type=int, |
|
247 | dest="Global.log_level",type=int, | |
248 | help='Set the log level (0,10,20,30,40,50). Default is 30.', |
|
248 | help='Set the log level (0,10,20,30,40,50). Default is 30.', | |
249 | default=NoConfigDefault, |
|
249 | default=NoConfigDefault, | |
250 | metavar="Global.log_level" |
|
250 | metavar="Global.log_level" | |
251 | ) |
|
251 | ) | |
252 |
self.parser.add_argument(' |
|
252 | self.parser.add_argument('--cluster-dir', | |
253 | dest='Global.cluster_dir',type=str, |
|
253 | dest='Global.cluster_dir',type=str, | |
254 | help='Set the cluster dir. This overrides the logic used by the ' |
|
254 | help='Set the cluster dir. This overrides the logic used by the ' | |
255 | '--profile option.', |
|
255 | '--profile option.', | |
256 | default=NoConfigDefault, |
|
256 | default=NoConfigDefault, | |
257 | metavar='Global.cluster_dir' |
|
257 | metavar='Global.cluster_dir' | |
258 | ) |
|
258 | ) | |
259 |
self.parser.add_argument(' |
|
259 | self.parser.add_argument('--clean-logs', | |
260 | dest='Global.clean_logs', action='store_true', |
|
260 | dest='Global.clean_logs', action='store_true', | |
261 | help='Delete old log flies before starting.', |
|
261 | help='Delete old log flies before starting.', | |
262 | default=NoConfigDefault |
|
262 | default=NoConfigDefault | |
263 | ) |
|
263 | ) | |
264 |
self.parser.add_argument(' |
|
264 | self.parser.add_argument('--no-clean-logs', | |
265 | dest='Global.clean_logs', action='store_false', |
|
265 | dest='Global.clean_logs', action='store_false', | |
266 | help="Don't Delete old log flies before starting.", |
|
266 | help="Don't Delete old log flies before starting.", | |
267 | default=NoConfigDefault |
|
267 | default=NoConfigDefault |
@@ -50,12 +50,12 class IPClusterCLLoader(ArgParseConfigLoader): | |||||
50 | def _add_arguments(self): |
|
50 | def _add_arguments(self): | |
51 | # This has all the common options that all subcommands use |
|
51 | # This has all the common options that all subcommands use | |
52 | parent_parser1 = argparse.ArgumentParser(add_help=False) |
|
52 | parent_parser1 = argparse.ArgumentParser(add_help=False) | |
53 |
parent_parser1.add_argument(' |
|
53 | parent_parser1.add_argument('--ipython-dir', | |
54 | dest='Global.ipythondir',type=str, |
|
54 | dest='Global.ipythondir',type=str, | |
55 | help='Set to override default location of Global.ipythondir.', |
|
55 | help='Set to override default location of Global.ipythondir.', | |
56 | default=NoConfigDefault, |
|
56 | default=NoConfigDefault, | |
57 | metavar='Global.ipythondir') |
|
57 | metavar='Global.ipythondir') | |
58 |
parent_parser1.add_argument(' |
|
58 | parent_parser1.add_argument('--log-level', | |
59 | dest="Global.log_level",type=int, |
|
59 | dest="Global.log_level",type=int, | |
60 | help='Set the log level (0,10,20,30,40,50). Default is 30.', |
|
60 | help='Set the log level (0,10,20,30,40,50). Default is 30.', | |
61 | default=NoConfigDefault, |
|
61 | default=NoConfigDefault, | |
@@ -63,7 +63,7 class IPClusterCLLoader(ArgParseConfigLoader): | |||||
63 |
|
63 | |||
64 | # This has all the common options that other subcommands use |
|
64 | # This has all the common options that other subcommands use | |
65 | parent_parser2 = argparse.ArgumentParser(add_help=False) |
|
65 | parent_parser2 = argparse.ArgumentParser(add_help=False) | |
66 |
parent_parser2.add_argument('-p',' |
|
66 | parent_parser2.add_argument('-p','--profile', | |
67 | dest='Global.profile',type=str, |
|
67 | dest='Global.profile',type=str, | |
68 | default=NoConfigDefault, |
|
68 | default=NoConfigDefault, | |
69 | help='The string name of the profile to be used. This determines ' |
|
69 | help='The string name of the profile to be used. This determines ' | |
@@ -72,7 +72,7 class IPClusterCLLoader(ArgParseConfigLoader): | |||||
72 | 'if the --cluster-dir option is not used.', |
|
72 | 'if the --cluster-dir option is not used.', | |
73 | default=NoConfigDefault, |
|
73 | default=NoConfigDefault, | |
74 | metavar='Global.profile') |
|
74 | metavar='Global.profile') | |
75 |
parent_parser2.add_argument(' |
|
75 | parent_parser2.add_argument('--cluster-dir', | |
76 | dest='Global.cluster_dir',type=str, |
|
76 | dest='Global.cluster_dir',type=str, | |
77 | default=NoConfigDefault, |
|
77 | default=NoConfigDefault, | |
78 | help='Set the cluster dir. This overrides the logic used by the ' |
|
78 | help='Set the cluster dir. This overrides the logic used by the ' | |
@@ -124,22 +124,22 class IPClusterCLLoader(ArgParseConfigLoader): | |||||
124 | help='The number of engines to start.', |
|
124 | help='The number of engines to start.', | |
125 | metavar='Global.n' |
|
125 | metavar='Global.n' | |
126 | ) |
|
126 | ) | |
127 |
parser_start.add_argument(' |
|
127 | parser_start.add_argument('--clean-logs', | |
128 | dest='Global.clean_logs', action='store_true', |
|
128 | dest='Global.clean_logs', action='store_true', | |
129 | help='Delete old log flies before starting.', |
|
129 | help='Delete old log flies before starting.', | |
130 | default=NoConfigDefault |
|
130 | default=NoConfigDefault | |
131 | ) |
|
131 | ) | |
132 |
parser_start.add_argument(' |
|
132 | parser_start.add_argument('--no-clean-logs', | |
133 | dest='Global.clean_logs', action='store_false', |
|
133 | dest='Global.clean_logs', action='store_false', | |
134 | help="Don't delete old log flies before starting.", |
|
134 | help="Don't delete old log flies before starting.", | |
135 | default=NoConfigDefault |
|
135 | default=NoConfigDefault | |
136 | ) |
|
136 | ) | |
137 |
parser_start.add_argument('--daemon', |
|
137 | parser_start.add_argument('--daemon', | |
138 | dest='Global.daemonize', action='store_true', |
|
138 | dest='Global.daemonize', action='store_true', | |
139 | help='Daemonize the ipcluster program. This implies --log-to-file', |
|
139 | help='Daemonize the ipcluster program. This implies --log-to-file', | |
140 | default=NoConfigDefault |
|
140 | default=NoConfigDefault | |
141 | ) |
|
141 | ) | |
142 |
parser_start.add_argument('--nodaemon', |
|
142 | parser_start.add_argument('--nodaemon', | |
143 | dest='Global.daemonize', action='store_false', |
|
143 | dest='Global.daemonize', action='store_false', | |
144 | help="Dont't daemonize the ipcluster program.", |
|
144 | help="Dont't daemonize the ipcluster program.", | |
145 | default=NoConfigDefault |
|
145 | default=NoConfigDefault | |
@@ -150,9 +150,10 class IPClusterCLLoader(ArgParseConfigLoader): | |||||
150 | help='Stop a cluster.', |
|
150 | help='Stop a cluster.', | |
151 | parents=[parent_parser1, parent_parser2] |
|
151 | parents=[parent_parser1, parent_parser2] | |
152 | ) |
|
152 | ) | |
153 |
parser_start.add_argument(' |
|
153 | parser_start.add_argument('--signal-number', | |
154 | dest='Global.stop_signal', type=int, |
|
154 | dest='Global.stop_signal', type=int, | |
155 | help="The signal number to use in stopping the cluster (default=2).", |
|
155 | help="The signal number to use in stopping the cluster (default=2).", | |
|
156 | metavar="Global.stop_signal", | |||
156 | default=NoConfigDefault |
|
157 | default=NoConfigDefault | |
157 | ) |
|
158 | ) | |
158 |
|
159 | |||
@@ -339,7 +340,9 class IPClusterApp(ApplicationWithClusterDir): | |||||
339 | 'Cluster is already running with [pid=%s]. ' |
|
340 | 'Cluster is already running with [pid=%s]. ' | |
340 | 'use "ipcluster stop" to stop the cluster.' % pid |
|
341 | 'use "ipcluster stop" to stop the cluster.' % pid | |
341 | ) |
|
342 | ) | |
342 | sys.exit(9) |
|
343 | # Here I exit with a unusual exit status that other processes | |
|
344 | # can watch for to learn how I existed. | |||
|
345 | sys.exit(10) | |||
343 | # Now log and daemonize |
|
346 | # Now log and daemonize | |
344 | self.log.info('Starting ipcluster with [daemon=%r]' % config.Global.daemonize) |
|
347 | self.log.info('Starting ipcluster with [daemon=%r]' % config.Global.daemonize) | |
345 | if config.Global.daemonize: |
|
348 | if config.Global.daemonize: | |
@@ -359,7 +362,9 class IPClusterApp(ApplicationWithClusterDir): | |||||
359 | self.log.critical( |
|
362 | self.log.critical( | |
360 | 'Problem reading pid file, cluster is probably not running.' |
|
363 | 'Problem reading pid file, cluster is probably not running.' | |
361 | ) |
|
364 | ) | |
362 | sys.exit(9) |
|
365 | # Here I exit with a unusual exit status that other processes | |
|
366 | # can watch for to learn how I existed. | |||
|
367 | sys.exit(11) | |||
363 | sig = config.Global.stop_signal |
|
368 | sig = config.Global.stop_signal | |
364 | self.log.info( |
|
369 | self.log.info( | |
365 | "Stopping cluster [pid=%r] with [signal=%r]" % (pid, sig) |
|
370 | "Stopping cluster [pid=%r] with [signal=%r]" % (pid, sig) |
@@ -160,9 +160,13 cl_args = ( | |||||
160 | 'are deleted before the controller starts. This must be set if ' |
|
160 | 'are deleted before the controller starts. This must be set if ' | |
161 | 'specific ports are specified by --engine-port or --client-port.') |
|
161 | 'specific ports are specified by --engine-port or --client-port.') | |
162 | ), |
|
162 | ), | |
163 |
(('-ns', |
|
163 | (('--no-secure',), dict( | |
164 | action='store_false', dest='Global.secure', default=NoConfigDefault, |
|
164 | action='store_false', dest='Global.secure', default=NoConfigDefault, | |
165 | help='Turn off SSL encryption for all connections.') |
|
165 | help='Turn off SSL encryption for all connections.') | |
|
166 | ), | |||
|
167 | (('--secure',), dict( | |||
|
168 | action='store_true', dest='Global.secure', default=NoConfigDefault, | |||
|
169 | help='Turn off SSL encryption for all connections.') | |||
166 | ) |
|
170 | ) | |
167 | ) |
|
171 | ) | |
168 |
|
172 |
@@ -1,13 +1,20 | |||||
1 | """Count the frequencies of words in a string""" |
|
1 | """Count the frequencies of words in a string""" | |
2 |
|
2 | |||
|
3 | from __future__ import division | |||
|
4 | ||||
|
5 | import cmath as math | |||
|
6 | ||||
|
7 | ||||
3 | def wordfreq(text): |
|
8 | def wordfreq(text): | |
4 | """Return a dictionary of words and word counts in a string.""" |
|
9 | """Return a dictionary of words and word counts in a string.""" | |
5 |
|
10 | |||
6 | freqs = {} |
|
11 | freqs = {} | |
7 | for word in text.split(): |
|
12 | for word in text.split(): | |
8 | freqs[word] = freqs.get(word, 0) + 1 |
|
13 | lword = word.lower() | |
|
14 | freqs[lword] = freqs.get(lword, 0) + 1 | |||
9 | return freqs |
|
15 | return freqs | |
10 |
|
16 | |||
|
17 | ||||
11 | def print_wordfreq(freqs, n=10): |
|
18 | def print_wordfreq(freqs, n=10): | |
12 | """Print the n most common words and counts in the freqs dict.""" |
|
19 | """Print the n most common words and counts in the freqs dict.""" | |
13 |
|
20 | |||
@@ -17,7 +24,43 def print_wordfreq(freqs, n=10): | |||||
17 | for (count, word) in items[:n]: |
|
24 | for (count, word) in items[:n]: | |
18 | print word, count |
|
25 | print word, count | |
19 |
|
26 | |||
20 | if __name__ == '__main__': |
|
27 | ||
21 | import gzip |
|
28 | def wordfreq_to_weightsize(worddict, minsize=10, maxsize=50, minalpha=0.4, maxalpha=1.0): | |
22 | text = gzip.open('HISTORY.gz').read() |
|
29 | mincount = min(worddict.itervalues()) | |
23 | freqs = wordfreq(text) No newline at end of file |
|
30 | maxcount = max(worddict.itervalues()) | |
|
31 | weights = {} | |||
|
32 | for k, v in worddict.iteritems(): | |||
|
33 | w = (v-mincount)/(maxcount-mincount) | |||
|
34 | alpha = minalpha + (maxalpha-minalpha)*w | |||
|
35 | size = minsize + (maxsize-minsize)*w | |||
|
36 | weights[k] = (alpha, size) | |||
|
37 | return weights | |||
|
38 | ||||
|
39 | ||||
|
40 | def tagcloud(worddict, n=10, minsize=10, maxsize=50, minalpha=0.4, maxalpha=1.0): | |||
|
41 | from matplotlib import pyplot as plt | |||
|
42 | import random | |||
|
43 | ||||
|
44 | worddict = wordfreq_to_weightsize(worddict, minsize, maxsize, minalpha, maxalpha) | |||
|
45 | ||||
|
46 | fig = plt.figure() | |||
|
47 | ax = fig.add_subplot(111) | |||
|
48 | ax.set_position([0.0,0.0,1.0,1.0]) | |||
|
49 | plt.xticks([]) | |||
|
50 | plt.yticks([]) | |||
|
51 | ||||
|
52 | words = worddict.keys() | |||
|
53 | alphas = [v[0] for v in worddict.values()] | |||
|
54 | sizes = [v[1] for v in worddict.values()] | |||
|
55 | items = zip(alphas, sizes, words) | |||
|
56 | items.sort(reverse=True) | |||
|
57 | for alpha, size, word in items[:n]: | |||
|
58 | xpos = random.normalvariate(0.5, 0.3) | |||
|
59 | ypos = random.normalvariate(0.5, 0.3) | |||
|
60 | # xpos = random.uniform(0.0,1.0) | |||
|
61 | # ypos = random.uniform(0.0,1.0) | |||
|
62 | ax.text(xpos, ypos, word.lower(), alpha=alpha, fontsize=size) | |||
|
63 | ax.autoscale_view() | |||
|
64 | return ax | |||
|
65 | ||||
|
66 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now