Deployed 71e6d40 with MkDocs version: 0.15.3

This commit is contained in:
Peter Bull
2016-04-23 15:56:31 -05:00
parent 65e7f8219f
commit d70997d004
2 changed files with 50 additions and 28 deletions
+32 -15
View File
@@ -108,7 +108,7 @@
<li class="first-level active"><a href="#cookiecutter-data-science">Cookiecutter Data Science</a></li>
<li class="second-level"><a href="#why">Why?</a></li>
<li class="second-level"><a href="#why-use-this-project-structure">Why use this project structure?</a></li>
<li class="third-level"><a href="#other-people-will-thank-you">Other people will thank you</a></li>
@@ -131,12 +131,14 @@
<li class="third-level"><a href="#data-is-immutable">Data is immutable</a></li>
<li class="third-level"><a href="#notebooks-are-for-exploration">Notebooks are for exploration</a></li>
<li class="third-level"><a href="#notebooks-are-for-exploration-and-communication">Notebooks are for exploration and communication</a></li>
<li class="third-level"><a href="#analysis-is-a-dag">Analysis is a DAG</a></li>
<li class="third-level"><a href="#build-from-the-environment-up">Build from the environment up</a></li>
<li class="third-level"><a href="#keep-secrets-out-of-version-control">Keep secrets out of version control</a></li>
<li class="second-level"><a href="#contributing">Contributing</a></li>
@@ -150,21 +152,34 @@
<div class="col-md-9" role="main">
<h1 id="cookiecutter-data-science">Cookiecutter Data Science</h1>
<h2 id="why">Why?</h2>
<p>We often think data analysis is a report, some visualizations and or some insights. While these end products are generated by code, it's easy to focus on making to products look <em>real good</em> and ignore the quality of the code that generates them.</p>
<p>On top of that, it's no secret that good analyses are often the result of exploration, experimentation, and digging into the data to see what works. This is not a process that lends itself to thinking carefully about the structure of your code or your project beforehand. So, let someone else do that thinking and the setup for you. Here's why:</p>
<p><em>A logical, reasonably standardized, but flexible project structure for doing and sharing data science work.</em></p>
<h2 id="why-use-this-project-structure">Why use this project structure?</h2>
<p>We often think of data analysis as just the resulting report, insights, or visualizations. Even though these end products are generated by code, it's easy to focus on making the products <em>look nice</em> and ignore the <em>quality of the code that generates them</em>. While these end products are generally the main event, <strong>code quality is still important</strong>! And we're not talking about bikeshedding the aesthetics or pedantic formatting standards — it's ultimately about correctness and reproducibility.</p>
<p>It's no secret that good analyses are often the result of very scattershot and serendipitous explorations, tentative experiments, and rapidly testing what works and what doesn't. That is all part of the process for getting to the good stuff, and there is no magic bullet to turn data exploration into a simple, linear progression. That being said, once started it is not a process that lends itself to thinking carefully about the structure of your code or project layout.</p>
<p>We think it's a pretty big win all around to let someone else do that up-front thinking and setup for you. Here's why:</p>
<h3 id="other-people-will-thank-you">Other people will thank you</h3>
<p>A well-defined project structure means that a newcomer can begin to understand an analysis without digging in to extensive documentation. Well organized code is self-documenting and provides a lot of context for your code without much overhead. People will thank you for this because they can:</p>
<blockquote>
<p>Nobody sits around before creating a new Rails project to figure out where they want to put their views; they just run <code>rails new</code> to get a standard project skeleton like everybody else.</p>
</blockquote>
<p>A well-defined, standard project structure means that a newcomer can begin to understand an analysis without digging in to extensive documentation. Well organized code is self-documenting and provides a lot of context for your code without much overhead. People will thank you for this because they can:</p>
<ul>
<li>Collaborate easily with you on this analysis</li>
<li>Easily learn from your analysis about the process and the domain</li>
<li>Feel confident in the conclusions the analysis presents</li>
</ul>
<p>A consistent project structure means that the</p>
<p>A consistent project structure means less random searching for what gets called where. A good example of this can be found in web development frameworks like Ruby on Rails, Django, and most others. Nobody sits around before creating a new Rails project to figure out where they want to put their views; they just run <code>rails new</code> to get a standard project skeleton like everybody else. And because the default project structure is <em>reasonably logical</em> and <em>standard across most projects</em>, it takes almost no time at all for somebody who has never seen a particular project to figure out where they would find the various moving parts.</p>
<p>Ideally, that's how it should be when a colleague opens up your data science project.</p>
<h3 id="you-will-thank-you">You will thank you</h3>
<p>Ever tried to reproduce an analysis that you did a few months ago or even a few years ago? You may have written the code, but it's now impossible to decipher whether you should use <code>make_figures.py.old</code>, <code>make_figures_working.py</code> or <code>new_make_figures01.py</code> to get things done. A good project structure encourages practices that make it easier to come back to old work, for example separation of concerns, abstracting analysis as a <a href="https://en.wikipedia.org/wiki/Directed_acyclic_graph">DAG</a>, and engineering best practices like version control.</p>
<p>Ever tried to reproduce an analysis that you did a few months ago or even a few years ago? You may have written the code, but it's now impossible to decipher whether you should use <code>make_figures.py.old</code>, <code>make_figures_working.py</code> or <code>new_make_figures01.py</code> to get things done. Here are some questions we've learned to ask with a sense of existential dread:</p>
<ul>
<li>Are we supposed to go in and join the "region" column to the data before we get started or did that come from one of the notebooks?</li>
<li>Come to think of it, which notebook do we have to run first before running the plotting code: was it "process data" or "clean data"?</li>
<li>Where did the shapefiles get downloaded from for the geographic plots you made?</li>
<li><em>Et cetera, times infinity.</em></li>
</ul>
<p>These types of questions are painful and are symptoms of a disorganized project. A good project structure encourages practices that make it easier to come back to old work, for example separation of concerns, abstracting analysis as a <a href="https://en.wikipedia.org/wiki/Directed_acyclic_graph">DAG</a>, and engineering best practices like version control.</p>
<h2 id="getting-started">Getting started</h2>
<p>With this in mind, we've created a Cookiecutter Data Science template for projects in Python. Your analysis doesn't have to be in Python, but the template does provide some Python boilerplate that you'd want to remove (exclusively in the <code>src</code> folder).</p>
<p>With this in mind, we've created a data science cookiecutter template for projects in Python. Your analysis doesn't have to be in Python, but the template does provide some Python boilerplate that you'd want to remove (in the <code>src</code> folder for example, and the Sphinx documentation skeleton in <code>docs</code>).</p>
<h3 id="requirements">Requirements</h3>
<ul>
<li>Python 2.7 or 3.5</li>
@@ -221,12 +236,12 @@
</code></pre>
<h2 id="opinions">Opinions</h2>
<p>There are some opinions implicit in the project structure that have grown out of learning what works and what doesn't when collaborating on data science projects. Some of the opinions are about workflows, and some of the opinions are about tools that make life easier. Here are some of the beliefs which this project is built on--if you've got thoughts, please <a href="#contributing">contribute or share them</a>.</p>
<p>There are some opinions implicit in the project structure that have grown out of our experience with what works and what doesn't when collaborating on data science projects. Some of the opinions are about workflows, and some of the opinions are about tools that make life easier. Here are some of the beliefs which this project is built onif you've got thoughts, please <a href="#contributing">contribute or share them</a>.</p>
<h3 id="data-is-immutable">Data is immutable</h3>
<p>Don't edit your raw data in Excel. Don't overwrite your raw data. Don't save multiple versions of the raw data. Treat the data (and its format) as immutable. The code you write should move the raw data through a pipeline to your final analysis. You shouldn't have to run all of the steps every time you want to make a new figure (q.v. <a href="#analysis-is-a-dag">Analysis is a DAG</a>), but anyone should be able to reproduce the final products with only the code in <code>src</code> and the data in <code>data/raw</code>.</p>
<p>Also, if data is immutable, it doesn't need source control in the same way that code does. Therefore, <strong><em>by default, the data folder is included in the .gitignore file.</em></strong> If you have a small amount of data that rarely changes, you may want to include the data in the repository. Github currently warns if files are over 50MB and rejects files over 100MB. Some other options for storing/syncing large data include <a href="https://aws.amazon.com/s3/">AWS S3</a> with a syncing tool (e.g., <a href="http://s3tools.org/s3cmd"><code>s3cmd</code></a>), <a href="https://git-lfs.github.com/">Git Large File Storage</a>, <a href="https://git-annex.branchable.com/">Git Annex</a>, and <a href="http://dat-data.com/">dat</a>. Currently by default, we ask for an S3 bucket and use <code>s3cmd</code> to sync data in the <code>data</code> folder with the server.</p>
<h3 id="notebooks-are-for-exploration">Notebooks are for exploration</h3>
<p>Notebooks such as the <a href="http://jupyter.org/">Jupyter notebook</a> and other literate programming tools are very effective for exploratory data analysis. However, these tools can be less effective for reproducing an analysis. When we use notebooks in our work, we often subdivide the <code>notebooks</code> folder. For example, <code>notebooks/exploratory</code> contains initial explorations, whereas <code>notebooks/reports</code> is more polished work that can be exported as html to the <code>reports</code> directory.</p>
<p>Don't ever edit your raw data, especially not manually, and especially not in Excel. Don't overwrite your raw data. Don't save multiple versions of the raw data. Treat the data (and its format) as immutable. The code you write should move the raw data through a pipeline to your final analysis. You shouldn't have to run all of the steps every time you want to make a new figure (see <a href="#analysis-is-a-dag">Analysis is a DAG</a>), but anyone should be able to reproduce the final products with only the code in <code>src</code> and the data in <code>data/raw</code>.</p>
<p>Also, if data is immutable, it doesn't need source control in the same way that code does. Therefore, <strong><em>by default, the data folder is included in the <code>.gitignore</code> file.</em></strong> If you have a small amount of data that rarely changes, you may want to include the data in the repository. Github currently warns if files are over 50MB and rejects files over 100MB. Some other options for storing/syncing large data include <a href="https://aws.amazon.com/s3/">AWS S3</a> with a syncing tool (e.g., <a href="http://s3tools.org/s3cmd"><code>s3cmd</code></a>), <a href="https://git-lfs.github.com/">Git Large File Storage</a>, <a href="https://git-annex.branchable.com/">Git Annex</a>, and <a href="http://dat-data.com/">dat</a>. Currently by default, we ask for an S3 bucket and use <code>s3cmd</code> to sync data in the <code>data</code> folder with the server.</p>
<h3 id="notebooks-are-for-exploration-and-communication">Notebooks are for exploration and communication</h3>
<p>Notebook packages like the <a href="http://jupyter.org/">Jupyter notebook</a>, <a href="http://beakernotebook.com/">Beaker notebook</a>, <a href="http://zeppelin-project.org/">Zeppelin</a>, and other literate programming tools are very effective for exploratory data analysis. However, these tools can be less effective for reproducing an analysis. When we use notebooks in our work, we often subdivide the <code>notebooks</code> folder. For example, <code>notebooks/exploratory</code> contains initial explorations, whereas <code>notebooks/reports</code> is more polished work that can be exported as html to the <code>reports</code> directory.</p>
<p>Since notebooks are challenging objects for source control (e.g., diffs of the <code>json</code> are often not human-readable and merging is near impossible), we recommended not collaborating directly with others on Jupyter notebooks. There are two steps we recommend for using notebooks effectively:</p>
<ol>
<li>
@@ -267,6 +282,8 @@ from preprocess.build_features import remove_invalid_data
<li>If you find you need to install another package, run <code>pip freeze &gt;&gt; requirements.txt</code> again and commit the changes to version control.</li>
</ol>
<p>If you have more complex requirements for recreating your environment, consider a virtual machine based approach such as <a href="https://www.docker.com/">Docker</a> or <a href="https://www.vagrantup.com/">Vagrant</a>. Both of these tools use text-based formats (Dockerfile and Vagrantfile respectively) you can easily add to source control to describe how to create a virtual machine with the requirements you need.</p>
<h3 id="keep-secrets-out-of-version-control">Keep secrets out of version control</h3>
<p>You <em>really</em> don't want to leak your AWS secret key or Postgres username and password on Github. Enough said, mostly — see the <a href="http://12factor.net/">Twelve Factor App</a> principles on this point. We generally use a <code>.env</code> file that, thanks to the <code>.gitignore</code>, never makes it into the repository (secrets should be shared via other means with contributors). The <code>.env</code> file defines secrets as environment variables, and is read in automatically by a package like <code>dotenv</code> in Python.</p>
<h2 id="contributing">Contributing</h2>
<p>The Cookiecutter Data Science project is opinionated, but not afraid to be wrong. Best practices change, tools evolve, and lessons are learned. <strong>The goal of this project is to make it easier to start, structure, and share an analysis.</strong> <a href="https://github.com/drivendata/cookiecutter-data-science/pulls">Pull requests</a> and <a href="https://github.com/drivendata/cookiecutter-data-science/issues">filing issues</a> is encouraged. We'd love to hear what works for you, and what doesn't.</p>
<p>If you use the Cookiecutter Data Science project, link back to this page or <a href="https://twitter.com/drivendataorg">give us a holler</a> and <a href="mailto:info@drivendata.org">let us know</a>!</p>
@@ -333,5 +350,5 @@ from preprocess.build_features import remove_invalid_data
<!--
MkDocs version : 0.15.3
Build Date UTC : 2016-04-23 19:22:33.049193
Build Date UTC : 2016-04-23 20:56:31.249855
-->
File diff suppressed because one or more lines are too long