Differences between revisions 24 and 37 (spanning 13 versions)
Revision 24 as of 2017-04-24 18:59:22
Size: 7033
Editor: LeeTirrell
Comment:
Revision 37 as of 2023-10-18 18:17:25
Size: 4244
Editor: PaulRaines
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Scientific Python Mode in the NMR Center = = Using Python at the Martinos Center =
Line 4: Line 4:
There are currently three scientific Python installations available at the Martinos Center. The two more recent installations are the [[https://store.continuum.io/cshop/anaconda/|Anaconda]] and the [[http://enthought.com/products/epd.php|Enthought Python Distribution (EPD)]]. The third installation is the legacy Python Installation; users of this installation are encouraged to switch to the Anaconda based installation, as it is more actively supported and contains more up to date packages. There is no longer a centrally maintained Python distribution at the Martinos Center. Each group (or user) is encouraged to create their own Python installation. This is most easily done using the Anaconda installer and conda tool to install the versions of modules that work for their group. But be aware of major "gotchas" as discussed below.
Line 6: Line 6:
If you use Python, make sure to sign up for the [[https://mail.nmr.mgh.harvard.edu/mailman/listinfo/martinos-python|martinos-python mailing list]] to receive updates regarding the Python installation and discuss Python related issues. For generic python scripts, one can try the OS installed python. Be aware the default python in CentOS7 is python2 and in CentOS8/Rocky8 no binary named just "python" exists -- you must explictly run python3 or python2. This may mean you need to change the first line of python scripts you download from the internet that just have "python".
Line 8: Line 8:
=== Resources for Learning Python === If you use Python at the Center, make sure to sign up for the [[https://mail.nmr.mgh.harvard.edu/mailman/listinfo/martinos-python|martinos-python mailing list]] to receive updates regarding Python related issues at the center.

== Resources for Learning Python ==
Line 15: Line 17:
=== Available Python Modules ===
The following neuroimaging related modules are installed:
== Installing a Custom Python for Yourself with Anaconda ==
Follow the direction at [[https://docs.anaconda.com/anaconda/install/linux/|Installing Ananaconda]]. You can skip the Prerequisites part as it is done on all our systems.

Make sure NOT to install the distribution into your home directory as your home directory quota is too small. Various things conda/pip do will also write lots of data to your ~/.conda, ~/.cache/pip and ~/.local/share/python* directories so you may need to symlink these to group storage volumes with more space.

Also I strongly suggest NOT having your ~/.cshrc or ~/.bashrc initialize your Anaconda install immediately on login. Instead setup a shell alias to do that on demand in each shell/Terminal when you need it. Anaconda installs in PATHs can interfer with other Linux systems program or even prevent GUI login from working. !NoMachine, VNC and !OpenOffice are known to break when sourcing anaconda automatically depending on the packages installed in your environment.

Another option is set "auto_activate_base: false" in your ~/.condarc file.

== Using venv module with OS installed python ==

Using the '''venv''' module with the system OS installed python is a simple and quick way to install modules with '''pip''' you need that have no complicated 3rd party dependencies. Here is an example of using this method to install jupyter:

{{{
 cd /vast/itgroup/raines
 mkdir jupyter
 python3 -m venv /vast/itgroup/raines/jupyter
 source /vast/itgroup/raines/jupyter/bin/activate
 pip3 install --upgrade pip
 pip3 install jupyterlab
 /vast/itgroup/raines/jupyter/bin/jupyter lab --no-browser
}}}

== Neuroimaging Python Modules ==
The following neuroimaging related modules are ones many users will want to install in their Anaconda distribution:
Line 26: Line 51:
* [[http://pysurfer.github.com|PySurfer]] for visualization and interaction with cortical surface representations of neuroimaging data from FreeSurfer * [[http://pysurfer.github.com|PySurfer]] for visualization and interaction with cortical surface representations of neuroimaging data from FreeSurfer (Requires using environment '''pysurfer''' in anaconda2.7)
Line 28: Line 53:
If you know of other modules that should be made available to all users, send an e-mail to the [[https://mail.nmr.mgh.harvard.edu/mailman/listinfo/martinos-python|martinos-python mailing list]] * [[http://tensorflow.org|TensorFlow]] for machine learning (Requires using either the '''tensorflow''' or '''tensorflow-gpu''' environment in anaconda2.7)
Line 30: Line 55:
== Anaconda Based Scientific Python Installation ==
<<Anchor(anaconda_python)>>
Always prefer installing a module using the '''conda''' installer with a prebuilt package. If you cannot find one that then you can resort to building it with '''pip'''.
Line 33: Line 57:
=== Getting Started ===
On CentOS 5/6 (64-bit) do the following to enable the Anaconda Python installation
Run '''pip list''' to see a full list of modules
Line 36: Line 59:
'''For Python 2.7:'''
{{{
setenv PATH /usr/pubsw/packages/python/anaconda2.7/bin:${PATH}
}}}

'''For Python 3.6:'''
{{{
setenv PATH /usr/pubsw/packages/python/anaconda3.6/bin:${PATH}
}}}

Notes for the python maintainer (person managing anaconda, not users) on saving old environments and creating new ones are [[managing_python|here]]

== EPD Based Scientific Python Installation ==
<<Anchor(epd_python)>>

=== Getting Started ===
On CentOS 5/6 (64-bit) do the following to enable the EPD based Python installation

{{{
setenv PATH /usr/pubsw/packages/python/epd/bin:${PATH}
}}}
== Legacy Scientific Python Installation ==
<<Anchor(legacy_python)>>

Scientific Python mode is now available in the following platform/os combinations in the NMR center

 * CentOS 64-bit
 * CentOS 32-bit
 * MacOSX Leopard (Intel)

The "system Python" ( the Python version which already exists in the system ) is not tampered with due to the following reasons:

 1. In CentOS systems, the Python version is very old ( 2.3.4 ) which prevents installation of many new modules essential for scientific computing in Python. However, a number of other system modules and programs are dependent on this version and it's best not to disturb this version.

 1. Leopard systems have a customized Python 2.5.x, which, again is a necessity for a number of modules. Again, best not to tamper with it.

 1. '''Isolation:''' The latest Python ( 2.6.3 ) and its associated modules are installed in /usr/pubsw/packages which are isolated from the system python and its modules. This configuration enables NMR center users to basically access both the versions of the Python and since both exist independently, they don't mess with each other.

=== Enabling the Scientific Python mode ===
By default, typing "python" to invoke the Python interpreter brings up the system Python interpreter which can be verified by looking at the version.

To enable the scientific python mode, users source the scientific python mode script in the following way :

{{{
source /usr/pubsw/packages/python/2.6/scientificpython/bin/tcsh_activate
}}}
'''Note:''' strongly recommended. this line can/should be put as an alias in one's ~/.cshrc in the following way:

{{{
alias pyactivate "source /usr/pubsw/packages/python/2.6/scientificpython/bin/tcsh_activate"
}}}
Once, this is done, the user can just type '''`pyactivate`''' to activate the scientific python mode and type '''`deactivate`''' to get back to the default ( normal system python mode )

In the scientific python mode, the prompt is preceded by the text `(scientificpython)`, which lets the user know that he/she is in the scientific python mode. The user can invoke the python interpreter by typing `python` and notice that the interpreter is now the latest Python's interpreter. Once the user deactivates the mode, the prompt is changed back to the default prompt.

'''Note:''' The sourcing script works under tcsh shell. Bash is currently unsupported but will be if there are enough users using bash.

=== List of installed modules ===
 * Python 2.6.3
 * Numpy 1.3.0
 * Scipy 0.7.1 (c)
 * matplotlib 0.99.1.1 ( `TkInter` backend in Mac platform and wxPython backend in CentOS platforms ) (c)
 * wxPython 2.8.10.1 ( only on CentOS platforms ) (c)
 * IPython 0.10
 * NetworkX 1.0rc1
 * nose 0.11.1
 * Sphinx 0.6.3
 * Cython 0.11.3
 * pynifti 0.20090303.1 (c)
 * SQLAlchemy 0.6.3
 * argparse 1.1
 * xlrd 0.7.1
 * xlwt 0.7.2
 * pysqlite 2.6.0 ( except CentOS 32-bit )
 * PyXML 0.8.4 (c)
 * pydicom 0.9.4-1
 * nibabel 1.0.0 ( cutting edge master branch Aug 27, 2010 )

'''Note :''' The list of modules indicated by (c) are compiled modules and the rest were installed using easy_install script.

=== Troubleshooting ===
 1. '''`pylab`''' doesn't start: This might be because there might be a stray `.matplotlibrc` file, which is already existing in your path preventing the standard backend to load. As an example, pylab can't be imported if the backend is `WXAgg` for Mac platforms or `TkAgg` for CentOS platforms. The fix is to comment out the line starting with `backend` in your `.matplotlibrc` ( usually in ~/matplotlib/matplotlibrc )

=== Behind the Scenes ===
( For advanced users )

A program called `virtualenv` is made use of to isolate the Pythons. virtualenv enables one to totally switch to a different Python with its own modules ( enabled by the switch --no-site-packages). So when the user sources the `tcsh_activate` script, what he/she is effectively doing is to prefix the system PATH, PYTHONPATH, module PATHs with the custom Python's paths and when the user `deactivate`s he/she gets back all the default paths. virtualenv takes care of this complex paths manipulation behind the scenes. It also makes the modules installation a breeze for the administrator.
Anaconda is also a good way to [[https://docs.anaconda.com/anaconda/packages/r-language-pkg-docs/|install and run R]]

Using Python at the Martinos Center

There is no longer a centrally maintained Python distribution at the Martinos Center. Each group (or user) is encouraged to create their own Python installation. This is most easily done using the Anaconda installer and conda tool to install the versions of modules that work for their group. But be aware of major "gotchas" as discussed below.

For generic python scripts, one can try the OS installed python. Be aware the default python in CentOS7 is python2 and in CentOS8/Rocky8 no binary named just "python" exists -- you must explictly run python3 or python2. This may mean you need to change the first line of python scripts you download from the internet that just have "python".

If you use Python at the Center, make sure to sign up for the martinos-python mailing list to receive updates regarding Python related issues at the center.

Resources for Learning Python

* Python Scientific Lecture Notes

* NumPy for Matlab Users

* The Boston Python User Group regularly holds meetings and organizes introductory Python classes

Installing a Custom Python for Yourself with Anaconda

Follow the direction at Installing Ananaconda. You can skip the Prerequisites part as it is done on all our systems.

Make sure NOT to install the distribution into your home directory as your home directory quota is too small. Various things conda/pip do will also write lots of data to your ~/.conda, ~/.cache/pip and ~/.local/share/python* directories so you may need to symlink these to group storage volumes with more space.

Also I strongly suggest NOT having your ~/.cshrc or ~/.bashrc initialize your Anaconda install immediately on login. Instead setup a shell alias to do that on demand in each shell/Terminal when you need it. Anaconda installs in PATHs can interfer with other Linux systems program or even prevent GUI login from working. NoMachine, VNC and OpenOffice are known to break when sourcing anaconda automatically depending on the packages installed in your environment.

Another option is set "auto_activate_base: false" in your ~/.condarc file.

Using venv module with OS installed python

Using the venv module with the system OS installed python is a simple and quick way to install modules with pip you need that have no complicated 3rd party dependencies. Here is an example of using this method to install jupyter:

 cd /vast/itgroup/raines
 mkdir jupyter
 python3 -m venv /vast/itgroup/raines/jupyter
 source /vast/itgroup/raines/jupyter/bin/activate
 pip3 install --upgrade pip
 pip3 install jupyterlab
 /vast/itgroup/raines/jupyter/bin/jupyter lab --no-browser 

Neuroimaging Python Modules

The following neuroimaging related modules are ones many users will want to install in their Anaconda distribution:

* mne-python for M/EEG analysis

* nibabel provides access to a large number of neuro-imaging file formats

* nitime for time-series analysis

* pydicom enables you to read, modify and write DICOM files with python code

* PySurfer for visualization and interaction with cortical surface representations of neuroimaging data from FreeSurfer (Requires using environment pysurfer in anaconda2.7)

* TensorFlow for machine learning (Requires using either the tensorflow or tensorflow-gpu environment in anaconda2.7)

Always prefer installing a module using the conda installer with a prebuilt package. If you cannot find one that then you can resort to building it with pip.

Run pip list to see a full list of modules

Anaconda is also a good way to install and run R

DevelopersGuide/NMRCenterPython/UsersGuide (last edited 2023-11-01 16:09:01 by PaulRaines)