Python#

Installation#

IDEs#

Below are popular IDE that are used at NeuroPoly:

  • Visual Studio Code (VSCode)

    • Since VSCode is a “general” IDE, you will need to install extensions to work with specific languages. For Python, you will need to install the Python extension.

  • JetBrains PyCharm

Here are some useful tips/modules/extensions to get the most out of your IDE:

1. Run tests by changing the Python interpreter to an existing Virtual Environment#

If you are working on a project that installs itself into a Python/Conda virtual environment, then it is a good idea to attach the virtual environment to your IDE. (This will let you run tests directly from IDE, using the packages installed in the project’s virtual envrionment.)

First, make sure you have run the installation instructions for your project, and that you have opened the project folder in your IDE. Next, follow the instructions corresponding to your IDE:

VSCode
  1. First, select the correct interpreter by first opening up the Command Palette (SHIFT+CMD+P or CTRL+SHIFT+P), typing “Python: Select Interpreter”, then selecting the virtual environment corresponding to the project. (For SCT, this is venv_sct) image

  2. After selecting the correct interpreter, click on the flask icon in the sidebar, then click “Configure Python Tests”, then “pytest”. Then, select the directory containing the test files (“testing”, “tests”, etc.) image

  3. Once tests are configured, you will now be able to Run and Debug tests directly from the Python test files by clicking (or right clicking) on the green arrow next to each test.

    image

PyCharm
  1. File > Settings > Project: spinalcordtoolbox > Python interpreter

  2. Gear in top-right > Show all

    image

  3. + in top-right > Add Local Interpreter…

    image

  4. “Conda environment” in top-left > Check “Existing environment”, then set Interpreter: to ${SCT_DIR}/python/envs/venv_sct/bin/python. (NB: Replace ${SCT_DIR} with the location of the SCT installation directory.)

2. Opening lines of code on GitHub#

If you are looking at code in your text editor and want to share a link to specific lines on GitHub, normally you would have to go to GitHub, find the same file, select the lines (by clicking with “shift” held down), right click, click “Copy permalink”. (“Permalinks” are links that reference a specific commit, so that the links will not change even if the git branch is updated in the future.)

However, you can skip much of this work by going right from your IDE to GitHub using a single click:

VS Code
  1. First, install the Open In GitHub extension.

  2. Next, highlight a snippet, then right click and select “Open In GitHub: Copy File URL” and it will take you directly to a permalink to those lines on GitHub.

image

PyCharm

PyCharm has this feature baked directly into the IDE. Just highlight a snippet, then right click and select “Open In -> GitHub” and it will take you directly to a permalink to those lines on GitHub.

image

If you then paste the permalink in a GitHub comment on issues/PRs, GitHub will automatically render those lines in your comment. (This is super useful for when you’re debugging an issue locally, and you want to communicate exactly which lines you’re looking at locally.)

image

Virtual Environments#

1. Create a virtual environment for a project:

$ cd my_project_folder
$ virtualenv venv

Or to specify a Python version:

$ virtualenv venv -p <PATH_TO_PYTHON>

2. To begin using the virtual environment, it needs to be activated:

 
 $ source my_project/bin/activate

3. If you are done working in the virtual environment for the moment, you can deactivate it:

 $ deactivate

Packaging Code#

Suggest workflow via Github Actions:

  • on Pypi, create a Token for the “organization” with “global” scope

  • on Github > Settings > Secrets > Add a secrets.

    • Warning: if you created the token at the organization level, you should create a secrets for the organization (not the repos)

  • Create a Github action template for “publishing”

  • Github uses twine to publish the package, everytime a release is created (does not work for draft releases)

  • Edit the .yml file with the credentials for connecting to Pypi

  • To debug, you can create the package locally and verify its integrity:

    • Install twine: pip install twine

    • Build the distribution: python setup.py sdist bdist_wheel

    • Check integrity: twine check dist/*

Useful Packages#

NeuroImaging#

Visualization#

Courses and Tutorials#

Resources#

Tips and Tricks#

Nice Packages#

- pytest-sugar: Nice visualization for pytestEdit

Matplotlib#

Display 2d array:

from matplotlib.pylab import *
matshow(data2d, fignum=1, cmap=cm.gray), plt.colorbar(), show()

Interactive mode (can display several figures without freezing the process)

import matplotlib.pyplot as plt
plt.ion()
plt.plot([1.6, 2.7])
plt.draw()
# Turn interactive mode off
plt.ioff()

Using oriented-object approach (force backend)

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure()
FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.matshow(data2d, cmap='gray')
fig.savefig('myfig.png')