Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
keeleliin
word_cloud_est
Commits
5a380ef2
Commit
5a380ef2
authored
Nov 04, 2012
by
Andreas Mueller
Browse files
fast with cython
parent
ca740b8a
Changes
3
Hide whitespace changes
Inline
Side-by-side
query_integral_image.pyx
0 → 100644
View file @
5a380ef2
# cython: boundscheck=False
# cython: wraparound=False
import
array
import
numpy
as
np
def
query_integral_image
(
unsigned
int
[:,:]
integral_image
,
int
size_x
,
int
size_y
):
#result_x = array.array("i")
#result_y = array.array("i")
cdef
int
x
=
integral_image
.
shape
[
0
]
cdef
int
y
=
integral_image
.
shape
[
1
]
cdef
int
area
,
i
,
j
cdef
int
hits
=
0
for
i
in
xrange
(
x
-
size_x
):
for
j
in
xrange
(
y
-
size_y
):
area
=
integral_image
[
i
,
j
]
+
integral_image
[
i
+
size_x
,
j
+
size_y
]
area
-=
integral_image
[
i
+
size_x
,
j
]
+
integral_image
[
i
,
j
+
size_y
]
if
not
area
:
hits
+=
1
if
not
hits
:
return
None
cdef
int
goal
=
np
.
random
.
randint
(
hits
)
hits
=
0
for
i
in
xrange
(
x
-
size_x
):
for
j
in
xrange
(
y
-
size_y
):
area
=
integral_image
[
i
,
j
]
+
integral_image
[
i
+
size_x
,
j
+
size_y
]
area
-=
integral_image
[
i
+
size_x
,
j
]
+
integral_image
[
i
,
j
+
size_y
]
if
not
area
:
hits
+=
1
if
hits
==
goal
:
return
i
,
j
setup.py
0 → 100644
View file @
5a380ef2
from
distutils.core
import
setup
from
Cython.Build
import
cythonize
setup
(
name
=
'integral'
,
ext_modules
=
cythonize
(
"*.pyx"
),
)
wordcloud.py
View file @
5a380ef2
...
...
@@ -11,7 +11,7 @@ plt
#from scipy.ndimage import uniform_filter
#from scipy.misc import imsave
@
profile
def
make_wordcloud
(
words
,
counts
,
width
=
400
,
height
=
200
):
# sort words by counts
inds
=
np
.
argsort
(
counts
)[::
-
1
]
...
...
@@ -44,29 +44,29 @@ def make_wordcloud(words, counts, width=400, height=200):
# find possible places using convolution:
off_x
=
size
[
0
]
off_y
=
size
[
1
]
mask2
=
(
integral
[:
-
off_y
,
:
-
off_x
]
+
integral
[
off_y
:,
off_x
:]
-
integral
[:
-
off_y
,
off_x
:]
-
integral
[
off_y
:,
:
-
off_x
])
mask2
=
mask2
<=
0
where
=
np
.
where
(
mask2
)
res
_x
,
res_y
=
query_integral_image
(
integral
,
off_y
,
off_x
)
if
len
(
where
[
0
])
:
#
mask2 = (integral[:-off_y, :-off_x]
#
+ integral[off_y:, off_x:]
#
- integral[:-off_y, off_x:]
#
- integral[off_y:, :-off_x])
#
mask2 = mask2 <= 0
#
where = np.where(mask2)
res
ult
=
query_integral_image
(
integral
,
off_y
,
off_x
)
if
result
is
not
None
:
break
font_size
-=
1
runs
+=
1
idx
=
random
.
randint
(
0
,
len
(
where
[
0
]
)
-
1
)
x
,
y
=
res_x
[
idx
],
res_y
[
idx
]
asdf
=
np
.
zeros
(
integral
.
shape
)
asdf
[
res_x
,
res_y
]
=
1
asdf2
=
np
.
zeros
(
integral
.
shape
)
asdf2
[
where
[
0
],
where
[
1
]]
=
1
if
not
(
asdf
==
asdf2
).
all
():
from
IPython.core.debugger
import
Tracer
Tracer
()()
#
idx = random.randint(0, len(
res_x
) - 1)
#
x, y = res_x[idx], res_y[idx]
#
asdf = np.zeros(integral.shape)
#
asdf[res_x, res_y] = 1
#
asdf2 = np.zeros(integral.shape)
#
asdf2[where[0], where[1]] = 1
#
if not (asdf == asdf2).all():
#
from IPython.core.debugger import Tracer
#
Tracer()()
#draw.text((y, x), word,
#fill=random.choice(['red', 'green', 'blue']))
draw
.
text
((
y
,
x
),
word
,
fill
=
"white"
)
draw
.
text
((
result
[
1
],
result
[
0
]
),
word
,
fill
=
"white"
)
#img_array = np.asarray(img, dtype=np.float) / 255.
img_array
=
np
.
asarray
(
img
)
#partial_integral = np.cumsum(np.cumsum(img_array[x:, y:], axis=0),
...
...
@@ -102,4 +102,4 @@ if __name__ == "__main__":
words
=
words
[
counts
>
1
]
counts
=
counts
[
counts
>
1
]
#make_wordcloud(words, counts, width=800, height=600)
make_wordcloud
(
words
,
counts
,
width
=
2
00
,
height
=
1
00
)
make_wordcloud
(
words
,
counts
,
width
=
4
00
,
height
=
2
00
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment