As project size increases, consistency of the code base and documentation becomes more important. We therefore provide guidelines for code and documentation that is contributed to scikit-bio. Our goal is to create a consistent code base where:
As scikit-bio is in beta, our coding guidelines are presented here as a working draft. These guidelines are requirements for all code submitted to scikit-bio, but at this stage the guidelines themselves are malleable. If you disagree with something, or have a suggestion for something new to include, you should create an issue to initiate a discussion.
We adhere to the PEP 8 python coding guidelines for code and documentation standards. Before submitting any code to scikit-bio, you should read these carefully and apply the guidelines in your code.
curr_recordis better than
self.nameto hold something like a single string, but
self.namesto hold something that you could loop through like a list or dictionary. Sometimes the decision can be tricky: is
self.indexan integer holding a positon, or a dictionary holding records keyed by name for easy lookup? If you find yourself wondering these things, the name should probably be changed to avoid the problem: try
RecordList, etc. Don’t use Hungarian Notation either (i.e. where you prefix the name with the type).
file(which you shouldn’t use anyway, since they’re keywords), and not
infile(because that looks like it should be a file object, not just its name).
resultto store the value that will be returned from a method or function. Use
datafor input in cases where the function or method acts on arbitrary data (e.g. sequence data, or a list of numbers, etc.) unless a more descriptive name is appropriate.
for k in keys: print k, where
ksurvives only a line or two. Loop iterators should refer to the variable that they’re looping through:
for k in keys, i in items, or
for key in keys, item in items. If the loop is long or there are several 1-letter variables active in the same scope, rename them.
sptxck2is. It’s worth it to spend the extra time typing
species_taxon_check_2, but that’s still a horrible name: what’s check number 1? Far better to go with something like
taxon_is_species_rankthat needs no explanation, especially if the variable is only used once or twice.
The following list of abbreviations can be considered well-known and used with impunity within mixed name variables, but some should not be used by themselves as they would conflict with common functions, python built-in’s, or raise an exception. Do not use the following by themselves as variable names:
exp (a common
math module function),
min. They can, however, be used as part of a name, eg
|end of file||eof|
obj.__doc__) or when generating documentation with automated tools.
from module import *, instead use
from module import Name, Name2, Name3...or possibly
import module. This makes it much easier to see name collisions and to replace implementations.
import numpy as np import numpy.testing as npt import pandas as pd from matplotlib import pyplot as plt
The structure of your module should be similar to the example below. scikit-bio uses the NumPy doc standard for documentation. Our doc/README.md explains how to write your docstrings using the NumPy doc standards for scikit-bio:
r""" Numbers (:mod:`skbio.numbers`) ============================== .. currentmodule:: skbio.numbers Numbers holds a sequence of numbers, and defines several statistical operations (mean, stdev, etc.) FrequencyDistribution holds a mapping from items (not necessarily numbers) to counts, and defines operations such as Shannon entropy and frequency normalization. Classes ------- .. autosummary:: :toctree: generated/ Numbers """ # ---------------------------------------------------------------------------- # Copyright (c) 2013--, scikit-bio development team. # # Distributed under the terms of the Modified BSD License. # # The full license is in the file COPYING.txt, distributed with this software. # ---------------------------------------------------------------------------- from random import choice, random import numpy as np from utils import indices class Numbers(list): pass class FrequencyDistribution(dict): pass
Always update the comments when the code changes. Incorrect comments are far worse than no comments, since they are actively misleading.
Comments should say more than the code itself. Examine your comments carefully: they may indicate that you’d be better off rewriting your code (especially if renaming your variables would allow you to get rid of the comment.) In particular, don’t scatter magic numbers and other constants that have to be explained through your code. It’s far better to use variables whose names are self-documenting, especially if you use the same constant more than once. Also, think about making constants into class or instance data, since it’s all too common for ‘constants’ to need to change or to be needed in several methods.
win_size -= 20 # decrement win_size by 20
win_size -= 20 # leave space for the scroll bar
self._scroll_bar_size = 20
win_size -= self._scroll_bar_size
Use comments starting with #, not strings, inside blocks of code.
Start each method, class and function with a docstring using triple double quotes (“””). Make sure the docstring follows the NumPy doc standard.
Always update the docstring when the code changes. Like outdated comments, outdated docstrings can waste a lot of time. “Correct examples are priceless, but incorrect examples are worse than worthless.” Jim Fulton.
There are several different approaches for testing code in python:
numpy.testing. Their purpose is the same, to check that execution of code given some input produces a specified output. The cases to which the approaches lend themselves are different.
Whatever approach is employed, the general principle is every line of code should be tested. It is critical that your code be fully tested before you draw conclusions from results it produces. For scientific work, bugs don’t just mean unhappy users who you’ll never actually meet: they may mean retracted publications.
Tests are an opportunity to invent the interface(s) you want. Write the test for a method before you write the method: often, this helps you figure out what you would want to call it and what parameters it should take. It’s OK to write the tests a few methods at a time, and to change them as your ideas about the interface change. However, you shouldn’t change them once you’ve told other people what the interface is. In the spirit of this, your tests should also import the functionality that they test from the shortest alias possible. This way any change to the API will cause your tests to break, and rightly so!
Never treat prototypes as production code. It’s fine to write prototype code without tests to try things out, but when you’ve figured out the algorithm and interfaces you must rewrite it with tests to consider it finished. Often, this helps you decide what interfaces and functionality you actually need and what you can get rid of.
“Code a little test a little”. For production code, write a couple of tests, then a couple of methods, then a couple more tests, then a couple more methods, then maybe change some of the names or generalize some of the functionality. If you have a huge amount of code where all you have to do is write the tests’, you’re probably closer to 30% done than 90%. Testing vastly reduces the time spent debugging, since whatever went wrong has to be in the code you wrote since the last test suite. And remember to use python’s interactive interpreter for quick checks of syntax and ideas.
Run the test suite when you change anything. Even if a change seems trivial, it will only take a couple of seconds to run the tests and then you’ll be sure. This can eliminate long and frustrating debugging sessions where the change turned out to have been made long ago, but didn’t seem significant at the time. Note that tests are executed using Travis CI, see this document’s section for further discussion.
noseframework with tests in a separate file for each module. Name the test file
test_module_name.pyand include it inside the tests folder of the module. Keeping the tests separate from the code reduces the temptation to change the tests when the code doesn’t work, and makes it easy to verify that a completely new implementation presents the same interface (behaves the same) as the old.
__init__.pyfile in your tests directory. This is required for the module to be included when the package is built and installed via
from skbio import DistanceMatrixinstead of
from skbio.stats.distance import DistanceMatrix. This allows us prevent most cases of accidental regression in our API.
numpy.testingif you are doing anything with floating point numbers, arrays or permutations (use
numpy.testing.assert_almost_equal). Do not try to compare floating point numbers using
assertEqualif you value your sanity.
TestCasewith the name
ClassNameTests. This should contain tests for everything in the public interface.
ClassNameTests_test_type. These might subclass
ClassNameTestsin order to share
ClassNameTests_private. Private methods may change if you change the implementation. It is not required that test cases for private methods pass when you change things (that’s why they’re private, after all), though it is often useful to have these tests for debugging.
test_method_name. Any leading and trailing underscores on the method name can be ignored for the purposes of the test; however, all tests must start with the literal substring
noseto find them. If the method is particularly complex, or has several discretely different cases you need to check, use
test_init_wrong_type, etc. for testing
$ python -c "import skbio; skbio.test(verbose=True)" skbio.maths.diversity.alpha.tests.test_ace.test_ace ... ok test_berger_parker_d (skbio.maths.diversity.alpha.tests.test_base.BaseTests) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.1234s OK
modulenameTests. Even if these functions are simple, it’s important to check that they work as advertised.
nosetest module structure¶
# ---------------------------------------------------------------------------- # Copyright (c) 2013--, scikit-bio development team. # # Distributed under the terms of the Modified BSD License. # # The full license is in the file COPYING.txt, distributed with this software. # ---------------------------------------------------------------------------- import numpy as np from nose.tools import assert_almost_equal, assert_raises from skbio.math.diversity.alpha.ace import ace def test_ace(): assert_almost_equal(ace(np.array([2, 0])), 1.0) assert_almost_equal(ace(np.array([12, 0, 9])), 2.0) assert_almost_equal(ace(np.array([12, 2, 8])), 3.0) assert_almost_equal(ace(np.array([12, 2, 1])), 4.0) assert_almost_equal(ace(np.array([12, 1, 2, 1])), 7.0) assert_almost_equal(ace(np.array([12, 3, 2, 1])), 4.6) assert_almost_equal(ace(np.array([12, 3, 6, 1, 10])), 5.62749672) # Just returns the number of OTUs when all are abundant. assert_almost_equal(ace(np.array([12, 12, 13, 14])), 4.0) # Border case: only singletons and 10-tons, no abundant OTUs. assert_almost_equal(ace([0, 1, 1, 0, 0, 10, 10, 1, 0, 0]), 9.35681818182) def test_ace_only_rare_singletons(): with assert_raises(ValueError): ace([0, 0, 43, 0, 1, 0, 1, 42, 1, 43]) if __name__ == '__main__': import nose nose.runmodule()
Commit messages are a useful way to document the changes being made to a project, it additionally documents who is making these changes and when are these changes being made, all of which are relevant when tracing back problems.
In general the writing of a commit message should adhere to NumPy’s guidelines which if followed correctly will help you structure your changes better i. e. bug fixes will be in a commit followed by a commit updating the test suite and with one last commit that update the documentation as needed.
GitHub provides a set of handy features that will link together a commit message to a ticket in the issue tracker, this is specially helpful because you can close an issue automatically when the change is merged into the main repository, this reduces the amount of work that has to be done making sure outdated issues are not open.