<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>cowlicks (Posts about gsoc)</title><link>https://cowlicks.website/</link><description></description><atom:link href="https://cowlicks.website/categories/gsoc.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Sat, 23 Nov 2024 17:14:27 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Wrapping Up Google Summer of Code</title><link>https://cowlicks.website/posts/%20wrapping-up-google-summer-of-code.html</link><dc:creator>Blake Griffith</dc:creator><description>&lt;div class="cell border-box-sizing text_cell rendered" id="cell-id=3d421e12"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;My Summer has been over for a while, but GSoC is officially ending this week. And what do I have to show?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SciPy's sparse matrices now support boolean data.&lt;/li&gt;
&lt;li&gt;Sparse matrices now support boolean operations (!=, &amp;lt;=, etc.).&lt;/li&gt;
&lt;li&gt;NumPy universal functions (ufuncs) can now be overridden for compatibility.&lt;/li&gt;
&lt;li&gt;Sparse matrices uses this ufunc override functionality to do sensible things when acted on by ufuncs.&lt;/li&gt;
&lt;li&gt;Support fancy indexing.&lt;/li&gt;
&lt;li&gt;Axis arguments for &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; methods.&lt;/li&gt;
&lt;li&gt;various other things: speed ups, small features, test coverage, bugs fixes, code clean ups, and a few backports.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But I think that I have benefited personally from this experience much more than SciPy has. I learned &lt;em&gt;so&lt;/em&gt; much this summer, a short list of things I know vastly more about than I did before this summer:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PYTHON. Jeez looking at code I wrote before this summer is painful.&lt;/li&gt;
&lt;li&gt;The Python and NumPy C API, before this summer I was a total C noob. Now I can scratch together a python API in C.&lt;/li&gt;
&lt;li&gt;SciPy.&lt;/li&gt;
&lt;li&gt;NumPy, &lt;code&gt;numpy.frompyfunc&lt;/code&gt; is awesome.&lt;/li&gt;
&lt;li&gt;Git, before this summer I could commit thing to my own repos and that was pretty much it. Git hooks are awesome.&lt;/li&gt;
&lt;li&gt;In general, development workflow.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I grown attached to SciPy and am excited I can make meaningful contributions to that are actually helping people. But contributing more will have to wait untill after the GRE, grad school applications, and my senior thesis. There are a few things I would like to add down the road to incorporate into SciPy 1.0. e.g. 64bit indices, change spares matrices to emulate &lt;code&gt;numpy.array&lt;/code&gt; rather than &lt;code&gt;numpy.matrix&lt;/code&gt;. I would also like to see if I can apply ufunc overrides in a few other places that mix &lt;code&gt;numpy.array&lt;/code&gt; and &lt;code&gt;scipy.sparse&lt;/code&gt; like &lt;a href="http://scikit-learn.org/stable/"&gt;scikit-learn&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here is a link to &lt;code&gt;git log -p upstream/master --author="Blake Griffith"&lt;/code&gt; in &lt;a href="https://cowlicks.website/numpy-codesample.txt"&gt;numpy&lt;/a&gt; and &lt;a href="https://cowlicks.website/scipy-codesample.txt"&gt;scipy&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description><category>gsoc</category><category>numpy</category><category>scipy</category><guid>https://cowlicks.website/posts/%20wrapping-up-google-summer-of-code.html</guid><pubDate>Mon, 23 Sep 2013 21:52:13 GMT</pubDate></item><item><title>week 4 - Sparse Matrix Inequalities</title><link>https://cowlicks.website/posts/week-4-sparse-matrix-inequalities.html</link><dc:creator>Blake Griffith</dc:creator><description>&lt;p&gt;This week I worked on adding support for inequalities to sparse
matrices. The pull request is
&lt;a href="https://github.com/scipy/scipy/pull/2586"&gt;here&lt;/a&gt; (still pending).
I also modified, the c++ routines used to produce a boolean output, pull
request &lt;a href="https://github.com/scipy/scipy/pull/2596"&gt;here&lt;/a&gt;. This is so
these inequalities don't have to be cast to bool in python. Once both of
these are accepted the inequalities will be using the c++ routines that
produce boolean output. &lt;/p&gt;
&lt;p&gt;These are separate pull requests but they are related and one will need
to be rebased once the other is accepted. Or I may combine them.&lt;/p&gt;
&lt;p&gt;Each of the four basic inequalities has its own particular quirks which
make it more or less efficient with sparse matrices. Here we consider
&lt;code&gt;A&lt;/code&gt; to be a sparse matrix. The cases where &lt;code&gt;B&lt;/code&gt; is a scalar, or sparse
are considered. (By scalar we effectively mean a matrix the same size as
&lt;code&gt;A&lt;/code&gt; with every element as the scalar value &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;&lt;code&gt;A &amp;lt; B&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;For scalar B, this operation is only efficient if &lt;code&gt;B &amp;lt; 0&lt;/code&gt; otherwise the
resulting matrix will be dense.&lt;/p&gt;
&lt;p&gt;For sparse &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; this is also efficient.&lt;/p&gt;
&lt;h4&gt;&lt;code&gt;A &amp;gt; B&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;For scalar &lt;code&gt;B&lt;/code&gt; the efficiency is the opposite of the less than operator.
That is &lt;code&gt;B &amp;gt; 0&lt;/code&gt; has efficient output. Similarly to &lt;code&gt;&amp;lt;&lt;/code&gt; this operation is
efficient with other sparse matrices.&lt;/p&gt;
&lt;h4&gt;&lt;code&gt;A &amp;lt;= B&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;This operation is pretty much useless. The only case which make this
efficient is when &lt;code&gt;B &amp;lt; 0&lt;/code&gt;. But this case is already covered by &lt;code&gt;&amp;lt;&lt;/code&gt;, and
&lt;code&gt;&amp;lt;&lt;/code&gt; can be used efficiently with sparse matrices. So why use &lt;code&gt;&amp;lt;=&lt;/code&gt;? I'll
discuss the pros and cons of having these non-strict inequalities in a
moment.&lt;/p&gt;
&lt;p&gt;Another disadvantage of non strict inequalities like these is that they
raies &lt;code&gt;NotImplementedError&lt;/code&gt; when comparing with 0. This is because of
the nature of the c++ routines. When comparing every pair of elements
they don't consider cases where both are zero. &lt;/p&gt;
&lt;h4&gt;&lt;code&gt;A &amp;gt;= B&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;Like &lt;code&gt;&amp;lt;=&lt;/code&gt; this operation is only efficient for one case, when &lt;code&gt;B &amp;gt; 0&lt;/code&gt;.
And also like &lt;code&gt;&amp;lt;=&lt;/code&gt;, it is not very useful.&lt;/p&gt;
&lt;h3&gt;Why have non strict inequalities?&lt;/h3&gt;
&lt;p&gt;I can't really think of uses for &lt;code&gt;&amp;gt;=&lt;/code&gt; and &lt;code&gt;&amp;lt;=&lt;/code&gt; but they might exist, I
added them because they were easy to implement once I had done &lt;code&gt;&amp;gt;&lt;/code&gt; and
&lt;code&gt;&amp;lt;&lt;/code&gt;. In practice, they don't slow the usage down. But removing non -
strict inequalities removes &lt;em&gt;24,990&lt;/em&gt; lines of code.&lt;/p&gt;
&lt;h2&gt;Implementation&lt;/h2&gt;
&lt;p&gt;The inequality operations are implemented as c++ routines, these are
wrapped to provide a python interface using SWIG, then the various
inequalities are added appropriately. &lt;/p&gt;
&lt;h3&gt;C++&lt;/h3&gt;
&lt;p&gt;The inequalities routines were implemented by reusing existing routines
like &lt;a href="https://cowlicks.website/posts/week-4-sparse-matrix-inequalities.html"&gt;&lt;code&gt;csr_binop_csr&lt;/code&gt;&lt;/a&gt;. Overrides for inequalities had to be added to
&lt;a href="https://github.com/cowlicks/scipy/blob/master/scipy/sparse/sparsetools/complex_ops.h"&gt;&lt;code&gt;complex_wrapper.h&lt;/code&gt;&lt;/a&gt; too.&lt;/p&gt;
&lt;p&gt;The binop routines had to altered, another class was added to their
templates, &lt;code&gt;T2&lt;/code&gt;, for the type of data out. In theory this could be
something other than bool.&lt;/p&gt;
&lt;h3&gt;SWIG&lt;/h3&gt;
&lt;p&gt;To handle boolean output data a new &lt;a href="https://github.com/cowlicks/scipy/blob/binop-output/scipy/sparse/sparsetools/sparsetools.i#L203"&gt;swig
macro&lt;/a&gt;
was added to create typemaps that have &lt;code&gt;npy_bool_wrapper&lt;/code&gt; as this output
class. I then use these to instantiate the boolean operations.&lt;/p&gt;
&lt;h3&gt;Python&lt;/h3&gt;
&lt;p&gt;Here the associated python special methods corresponding to the
inequalities were added in
&lt;a href="https://github.com/cowlicks/scipy/blob/inequalities-override/scipy/sparse/compressed.py#L231"&gt;&lt;code&gt;compressed.py&lt;/code&gt;&lt;/a&gt;.
These operations can be used with a scalar, dense, or sparse matrix.
Numpy style broadcasting is not implemented.&lt;/p&gt;
&lt;p&gt;To produce a bool output by default for boolean comparisons, I altered
the
&lt;a href="https://github.com/cowlicks/scipy/blob/binop-output/scipy/sparse/compressed.py#L755"&gt;&lt;code&gt;_binopt&lt;/code&gt;&lt;/a&gt;
function to pass the c++ routines a matrix to use for output with a bool
dtype.&lt;/p&gt;
&lt;h2&gt;Problems&lt;/h2&gt;
&lt;p&gt;When testing the inequalities with dense matrices, I could not get the proper
behavior with a dense matrix on the left hand side. Previously, with &lt;code&gt;!=&lt;/code&gt; and
&lt;code&gt;==&lt;/code&gt; I modified the &lt;code&gt;__bool__&lt;/code&gt; method but that did not work in this case. This
is because of the same problem I will be dealing with in the next stage of my
proposal, interactions with numpy ufuncs.&lt;/p&gt;</description><category>bool</category><category>boolean comparisons</category><category>gsoc</category><category>scipy</category><guid>https://cowlicks.website/posts/week-4-sparse-matrix-inequalities.html</guid><pubDate>Fri, 21 Jun 2013 10:32:39 GMT</pubDate></item><item><title>Week 1 - bool dtype support</title><link>https://cowlicks.website/posts/week-1-bool-dtype-support.html</link><dc:creator>Blake Griffith</dc:creator><description>&lt;p&gt;I got accepted to Google Summer of Code! Thank you everyone who has helped me
get this far, and Amber for making me a congratulatory google cake!&lt;/p&gt;
&lt;p&gt;&lt;img alt="google cake" src="https://cowlicks.website/google_cake.jpg"&gt;&lt;/p&gt;
&lt;p&gt;On my proposal timeline I gave myself 2 weeks to add support to sparse matrices
for &lt;code&gt;dtype=bool&lt;/code&gt;. So far, &lt;code&gt;dtype=bool&lt;/code&gt; works implementation was not too hard,
and most of my time was spent figuring out how &lt;a href="http://swig.org/"&gt;SWIG&lt;/a&gt; works.
However there is still a lot of work to do in testing bool support. This will
be more time consuming than it should be because the sparse test suite is
messy.&lt;/p&gt;
&lt;h2&gt;bool dtype implementation&lt;/h2&gt;
&lt;h3&gt;sparsetools&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;sparsetools/&lt;/code&gt; directory contains several &lt;code&gt;*.cxx&lt;/code&gt;, &lt;code&gt;*.h&lt;/code&gt;, &lt;code&gt;*.i&lt;/code&gt;, and &lt;code&gt;*.py&lt;/code&gt;
files. The header files (&lt;code&gt;*.h&lt;/code&gt;) contain C++ routines these are wrapped by SWIG
according to instructions in the &lt;code&gt;*.i&lt;/code&gt;  interface files. SWIG uses the header
and interface files to generate python &lt;code&gt;*.py&lt;/code&gt; files C++ code in &lt;code&gt;*.cxx&lt;/code&gt; files.
Once these &lt;code&gt;*.cxx&lt;/code&gt; files are compiled and linked the functions they define can
be called by the generated &lt;code&gt;*.py&lt;/code&gt; files.&lt;/p&gt;
&lt;h3&gt;Where do I add bool support?&lt;/h3&gt;
&lt;p&gt;Contained in &lt;code&gt;sparsetools/&lt;/code&gt; is a file called &lt;code&gt;complex_ops.h&lt;/code&gt; which translates
numpy's complex types to C++ classes. Well, that is what I want to do, except
for numpy's bool types instead! My new file &lt;code&gt;bool_ops.h&lt;/code&gt; is much simpler
though. It basically does&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="nx"&gt;typedef&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;npy_int8&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;npy_bool_wrapper&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Yep that's it, (for now anyway). This file basically says to treat the 
&lt;code&gt;npy_bool_wrapper&lt;/code&gt; type like the &lt;code&gt;npy_int8&lt;/code&gt;. Because boolean algebra is &lt;em&gt;almost&lt;/em&gt; 
integer algebra.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;npy_bool_wrapper&lt;/code&gt; is used in &lt;code&gt;numpy.i&lt;/code&gt; to define a
&lt;a href="http://www.swig.org/Doc1.3/Python.html#Python_nn53"&gt;typemap&lt;/a&gt; from numpy's
&lt;code&gt;npy_bool&lt;/code&gt; type to our &lt;code&gt;npy_bool_wrapper&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;sparsetools.i&lt;/code&gt; we have to declare the new data type and add it to the
&lt;code&gt;INSTANTIATE_ALL&lt;/code&gt; macro which is used in all the *.i files to call the 
functions we want from the *.h files. &lt;/p&gt;
&lt;h2&gt;What else?&lt;/h2&gt;
&lt;p&gt;Adding &lt;code&gt;bool&lt;/code&gt; to &lt;code&gt;supported_dtypes&lt;/code&gt; in &lt;code&gt;sputils.py&lt;/code&gt; prevents it from upcast.
Then tests need to be added, but this isn't too easy. The test suite does not
have a way to take some general data or dtype. So I am going through adding 
tests one-by-one where it seems appropriate. I'm also trying to add tests for
other dtypes because there don't seem to be any tests for anything other than
float64 and int64. &lt;/p&gt;
&lt;p&gt;While I was doing this I discovered a few
&lt;a href="https://github.com/scipy/scipy/issues/2534"&gt;bugs&lt;/a&gt; relating to how sparse
handles &lt;code&gt;uint&lt;/code&gt; dtypes.&lt;/p&gt;
&lt;p&gt;Once I'm done adding tests and fixing what ever does not pass, I'll move on to
the next stage in my proposal. Adding support for boolean operations.&lt;/p&gt;</description><category>bool</category><category>dtype</category><category>gsoc</category><category>scipy</category><guid>https://cowlicks.website/posts/week-1-bool-dtype-support.html</guid><pubDate>Sun, 02 Jun 2013 06:05:49 GMT</pubDate></item></channel></rss>