Templates and guidelines for coders¶
Thank you for contributing your work and time to the ORMIR community!
On this page, you will find templates, coding guidelines, and tips and tricks.
Please, follow them to ensure that your code aligns with the existing code in the community and future contributions.
Don't forget to write code documentation! Code without documentation is rarely reused and hard to understand. Documentation is just as important as code itself!
Templates¶
Download the templates for coding and documentation. We are working on creating more and more!
Guidelines¶
Here are the coding guidelines of the ORMIR community:
Naming Style¶
-
Variables, functions, methods, modules, packages:
lower_case_with_underscores
-
Constants:
ALL_CAPS_WITH_UNDERSCORES
-
Classes and exceptions:
CapWords
Variables, functions, methods, modules, packages:
lower_case_with_underscores
Constants:
ALL_CAPS_WITH_UNDERSCORES
Classes and exceptions:
CapWords
Indentation¶
-
4 spaces or 1 tab
4 spaces or 1 tab
Space¶
-
Leave space between operators in case of assignment and arithmetic
operations:
var = 4 result = var * 5
-
Leave space after a comma:
var1, var2 = get_values(num1, num2)
Leave space between operators in case of assignment and arithmetic operations:
var = 4 result = var * 5
Leave space after a comma:
var1, var2 = get_values(num1, num2)
Example: docstring for functions (for modules, classes, and methods, refer to the NumPy guidelines):
def my_function (input1, input2):
"""This functions does ... .
Parameters
----------
input_1 : type
Description of the parameter `input_1`.
input_2 : type
Description of the parameter `input_2`.
Returns
-------
output: int
Description of the variable `output`
"""
# function body
return output
Explanation: The function documentation is right below the function header, and it is
between three opening and three closing single quotes '''
or double quotes: """
.
It is composed of 3 parts:
-
Description of what the function does. It is written right next to the opening quotes
-
Parameters, i.e. inputs of the function. It contains:
-
Parameters as a title, underlined by minuses
-
A list of parameters. For each parameter, we write:
-
Parameter name, colon (:), and parameter type (string, list, ITKimage, etc.)
-
On the following line, indented description of the parameter
-
-
-
Returns, i.e. outputs of the function. It contains:
-
Returns as a title, underlined by minuses
-
The list of returns, using the same structure as for parameters
-
Special cases:
-
It the function does not return any variable, we omit the whole Returns section
-
If the function does not return a variable but the output of another function, we omit the variable name, as in this example:
-
-
Returns ------- int
Integer component of the variable float_variable
-
Why is documentation important? Because docstrings are used:
-
By users to know what the function does by typing
help (my_function)
-
To create whole code documentation using, for example,
pydoc
orSphinx
, where you get a list of all the functions in a package, and their documentation
-
write docstrings in Numpy style for the following function: [paste your function]
Important: Double-check the content of the ChatGPT output!
- You can choose a permissive license (e.g. BDS, MIT, Apache) or a copyleft license (e.g. GNU GPL, Mozilla). If you are not familiar with open-source licenses, you can browse choosealicense.com, read the paper A Quick Guide to Software Licensing for the Scientist-Programmer, or watch this video (from minute 4)
- You can use a Creative Commons license. Possibilities are: CC0, CC-BY, CC-BY-NC, CC-BY-NC-SA, and their combinations. If you are not familiar with Creative Commons licenses, you can browse the Creative Commons website or watch this video (up to minute 4)
Why should I use the template? We usually use notebooks to create use-cases for our Python packages. Using the template allows us to harmonize our use-cases and to make sure we remember to include authors, licenses, references, etc.
translate the following code from Matlab to Python: [paste your code]
Important: Double-check the content of the ChatGPT output, preferably with test functions.
If you don't know Python: team up with some community members who know Python and work on the translation together!
-
Our pipelines usually start with
.dcm
images for CT/MR acquisitions or.isq
images for µCT/HR-pQCT acquisitions. - Images are then transformed into other file formats for simplicity (e.g. nifti, metafile, etc.)
Comments¶-
Comments are introduced by
#
, positioned above the code they refer to, and indented to align with the code are commenting on:Why are comments important? As part of a community we want to write code that can be readable and reusable by others. Be generous and precise with your comments!