BMED365: Computational imaging, modeling and AI in biomedicine
v. 2024-01-17, (making a bmed365
conda environment) Arvid Lundervold
If you have a subscription to ChatGPT Pluss, you can also try out the Medical AI Assistant (UiBmed - ELMED219 & BMED365) and see if you can get it to answer some of your questions.
NOTE: ChatGPT can make mistakes. Consider checking important information.
- Are you using Windows?
- Are you using Mac?
- Are you using Linux?
- Notes on the command line (shell) and Notebooks
- Notes on GitHub
- Notes on Anaconda
- Notes on Colab
- Install and test your course environment
- Unix shell
- Programming with Python
- A note on R (tip for previous R users)
Then you must install Git for Windows (for free) before you start. See also here. Thereafter:
How to set up and use GitHub and Jupyter notebooks on Windows, by (i) using Anaconda, (ii) using Visual Studio Code, (iii) initially cloning the BMED365 course repository, (iv) activate its conda environment "bmed365" as specified in "environment.yml" , and (v) being able to experiment with my own notebooks (making a local copy) in this environment without getting in conflict with the original repository?
Setting up and using GitHub and Jupyter notebooks on Windows for the BMED365 course, with Anaconda and Visual Studio Code, involves several steps. I'll guide you through them:
-
Anaconda:
- Download Anaconda from the official website.
- Install Anaconda by following the instructions. It includes Python and Jupyter.
-
Visual Studio Code (VS Code):
- Download and install VS Code from here.
- In VS Code, install the Python extension available in the Extensions marketplace.
-
Git:
- Download and install Git for Windows from Git's official site.
- Open Git Bash or the terminal.
- Navigate to the directory where you want the repository.
- Clone the repository:
git clone https://github.com/MMIV-ML/BMED365.git
- Open Anaconda Prompt.
- Navigate to the cloned repository's directory.
- Create the
bmed365
environment from theenvironment.yml
file:conda env create -f environment.yml
- Activate the
bmed365
environment:conda activate bmed365
- With the
bmed365
environment active, launch Jupyter Notebook:jupyter notebook
- Jupyter will open in your web browser, where you can access and run notebooks.
- Open VS Code.
- Open the folder containing the cloned BMED365 repository.
- Make sure to select the
bmed365
environment in VS Code (bottom left corner). - Open or create Jupyter notebook files (
.ipynb
) within VS Code.
- Within the cloned repository, create a new folder, e.g.,
MyNotebooks
, for your experiments. - Make sure to add
MyNotebooks/
to the.gitignore
file to avoid conflicts with the original repository. - Develop and test your notebooks in this folder, using the
bmed365
environment.
- Regularly pull updates from the original BMED365 repository to stay updated.
- For your work in
MyNotebooks
, you can either:- Use Git to manage and track changes.
- Or, if you prefer not to use Git for personal experiments, just save your work locally.
- Familiarize Yourself with Tools: Explore Anaconda, VS Code, and Git functionalities.
- Backup Your Work: Consider other backup options for your
MyNotebooks
folder.
This setup should provide a robust environment for working with the BMED365 course materials while enabling you to experiment in a separate space without affecting the original repository. If you encounter any issues or have further questions, feel free to ask for assistance. Happy coding!
Can you help me with using the corresponding command line in Windows to work with the course material locally, using VS Code, and Git ? Please give me details on how to start and stop the command line (shell), and explain to me the use of important command line commands to navigate, manipulate, and display files and directories on my disk.
Certainly! Using the command line (shell) in Windows to work with course materials, especially with tools like Visual Studio Code (VS Code) and Git, can enhance your workflow significantly. Here's a detailed guide:
-
To Start:
- Press
Win + R
, typecmd
, and pressEnter
to open the Command Prompt. - Alternatively, you can search for "Command Prompt" in the start menu.
- For Git-specific commands, you can use Git Bash, which comes with Git for Windows. You can start it the same way as Command Prompt, by searching for "Git Bash".
- Press
-
To Stop:
- Simply type
exit
and pressEnter
, or just close the window.
- Simply type
-
cd [directory]
(Change Directory): Changes the current directory.- Example:
cd Documents
changes the current directory toDocuments
.
- Example:
-
dir
(List Directory Contents in Command Prompt) orls
(in Git Bash): Lists the files and folders in the current directory.- Example:
dir
orls
will display a list of files and folders in the current directory.
- Example:
-
mkdir [folder_name]
(Make Directory): Creates a new folder.- Example:
mkdir NewFolder
creates a new folder namedNewFolder
.
- Example:
-
del [file]
(Delete File) orrm [file]
(in Git Bash): Deletes a file.- Example:
del example.txt
orrm example.txt
deletes the file namedexample.txt
.
- Example:
-
rmdir [folder]
orrm -r [folder]
(in Git Bash) (Remove Directory): Removes a folder.- Example:
rmdir OldFolder
orrm -r OldFolder
removes the folder namedOldFolder
.
- Example:
-
Cloning a Repository:
- Use
git clone [repository URL]
to clone a repository. - Example:
git clone https://github.com/MMIV-ML/BMED365.git
clones the BMED365 repository.
- Use
-
Checking the Status:
git status
shows the status of changes as untracked, modified, or staged.
-
Adding Changes:
git add [file]
stages a file for commit.git add .
stages all changes.
-
Committing Changes:
git commit -m "[commit message]"
commits the staged changes with a message.
-
Pulling Updates:
git pull
fetches and merges updates from the remote repository.
-
Pushing Changes:
git push
uploads local repository content to a remote repository.
- Opening VS Code:
- Navigate to your project's directory using
cd
. - Type
code .
to open the current directory in VS Code.
- Navigate to your project's directory using
- Use the command line to navigate to your project directory and manage Git operations.
- Use VS Code for editing, running, and debugging your code. VS Code also has integrated Git support, which can be handy for committing and pushing changes without leaving the editor.
- Tab Completion: Press the
Tab
key while typing a command to auto-complete file and folder names. - Command History: Use the
Up
andDown
arrow keys to cycle through previously used commands. - Copying and Pasting: Right-click to copy/paste in the Command Prompt or Git Bash.
Understanding and using these command line tools can greatly enhance your efficiency and capability in handling course materials and development tasks. If you have any specific questions or need further assistance, feel free to ask.
Then you must install xcode (for free) before you start. See also here. Thereafter (see also here):
- Launch the
terminal.app
, found in /Applications/Utilities/ - Type the following command string:
xcode-select --install
How to set up and use GitHub and Jupyter notebooks on Mac, by (i) using Anaconda, (ii) using Visual Studio Code, (iii) initially cloning the BMED365 course repository, (iv) activate its conda environment "bmed365" as specified in "environment.yml" , and (v) being able to experiment with my own notebooks (making a local copy) in this environment without getting in conflict with the original repository?
Setting up and using GitHub and Jupyter notebooks on a Mac, particularly for working with the BMED365 course materials using Anaconda and Visual Studio Code, involves a series of steps. Here's a detailed guide to help you through the process:
-
Anaconda:
- Download Anaconda for Mac from the Anaconda website.
- Open the downloaded file and follow the installation instructions.
-
Visual Studio Code (VS Code):
- Download VS Code for Mac from the Visual Studio Code website.
- Open the downloaded file and drag the VS Code icon to the Applications folder.
-
Git:
- Git usually comes pre-installed on Mac. To check, open Terminal and type
git --version
. - If not installed, you can install it via Homebrew (a package manager for Mac) by running
brew install git
in the Terminal.
- Git usually comes pre-installed on Mac. To check, open Terminal and type
- Open Terminal.
- Navigate to the directory where you want to clone the repository, e.g.,
cd ~/Documents
. - Clone the repository:
git clone https://github.com/MMIV-ML/BMED365.git
- Move into the cloned directory:
cd BMED365
.
- Ensure Anaconda is properly installed by typing
conda --version
in Terminal. - In the BMED365 directory, create the
bmed365
environment using theenvironment.yml
file:conda env create -f environment.yml
- Activate the
bmed365
environment:conda activate bmed365
- With the
bnmed365
environment active, start Jupyter Notebook:jupyter notebook
- This will open Jupyter in your default web browser, where you can access and run the course notebooks.
- Open VS Code.
- Open the folder containing the BMED365 repository by going to "File" > "Open...".
- Ensure the
bmed365
environment is selected in VS Code (visible in the bottom left corner or the Python extension tab). - Open or create Jupyter notebook files (
.ipynb
) within VS Code.
- Inside the BMED365 directory, create a new folder, say
MyNotebooks
, for your personal experiments. - Add this folder to the
.gitignore
file (create one if it doesn't exist) to prevent conflicts with the original repository. - Work on your notebooks within this folder using the
bmed365
environment.
- Regularly pull updates from the BMED365 repository to stay updated:
git pull origin main
- Manage your work in
MyNotebooks
separately, ensuring not to track these files in Git if they're included in.gitignore
.
- Familiarize yourself with Terminal commands for navigating and managing files.
- Backup your work regularly, especially the contents of
MyNotebooks
.
Following these steps, you should be able to work with the BMED365 course materials using Jupyter notebooks, manage the course materials via Git, and experiment with your own notebooks in a managed environment. If you encounter any issues or need further assistance, feel free to reach out for help.
Can you help me with using the corresponding command line in MacOS to work with the course material locally, using VS Code, and Git ? Please give me details on how to start and stop the command line (shell), and explain to me the use of important command line commands to navigate, manipulate, and display files and directories on my disk.
Certainly! Working with course materials locally on macOS using the command line (shell), Visual Studio Code (VS Code), and Git involves a combination of terminal commands and applications. Here's a detailed guide:
-
To Start:
- You can find the Terminal application in the
Applications
folder, underUtilities
, or use Spotlight (Cmd + Space
) to search for "Terminal." - Open the Terminal app to start the command line interface.
- You can find the Terminal application in the
-
To Stop:
- You can exit the Terminal by typing
exit
and pressingEnter
. Alternatively, you can just close the Terminal window.
- You can exit the Terminal by typing
-
cd [directory]
(Change Directory): Changes the current directory.- Example:
cd Documents
moves you to the Documents directory.
- Example:
-
ls
(List Directory Contents): Lists the files and folders in the current directory.- Example:
ls
displays a list of files and folders in the current directory.
- Example:
-
mkdir [folder_name]
(Make Directory): Creates a new folder.- Example:
mkdir NewFolder
creates a new folder namedNewFolder
.
- Example:
-
rm [file]
(Remove File): Deletes a file.- Example:
rm example.txt
deletes the file namedexample.txt
.
- Example:
-
rm -r [folder]
(Remove Directory): Deletes a folder and its contents.- Example:
rm -r OldFolder
removes the folder namedOldFolder
.
- Example:
-
open [file]
(Open File): Opens a file with its default application.- Example:
open example.txt
opens the fileexample.txt
in the default text editor.
- Example:
-
open -a [application] [file]
(Open File with Application): Opens a file with a specified application.- Example:
open -a TextEdit example.txt
opensexample.txt
with TextEdit.
- Example:
-
Cloning a Repository:
- Use
git clone [repository URL]
to clone a repository. - Example:
git clone https://github.com/MMIV-ML/BMED365.git
clones the BMED365 repository.
- Use
-
Checking Status:
git status
shows the status of changes in the current repository.
-
Adding Changes:
git add [file]
stages a file for commit.git add .
stages all current changes.
-
Committing Changes:
git commit -m "[commit message]"
commits the staged changes with a message.
-
Pulling and Pushing Changes:
git pull
fetches and merges updates from the remote repository.git push
uploads your local repository content to a remote repository.
- Opening VS Code:
- Navigate to your project's directory using
cd
. - Type
code .
to open the current directory in VS Code.
- Navigate to your project's directory using
- Use the Terminal for navigating to your project directory and managing Git operations.
- Use VS Code for editing, running, and debugging your code. VS Code has integrated Git support, which is handy for version control.
- Tab Completion: In the Terminal, you can start typing a command or file name and press
Tab
to auto-complete it. - Command History: Use the
Up
andDown
arrow keys to cycle through previously used commands. - Copying and Pasting: Use
Cmd + C
to copy andCmd + V
to paste in the Terminal.
By combining these command-line skills with VS Code and Git, you'll have a powerful setup for managing and working on the BMED365 course materials. If you have any specific questions or need further assistance, feel free to ask.
Then you are likely familiar with the command line (shell) and can modify the installation procedures described for MacOS.
On Mac, Windows and Linux we will use the command line (shell)
See e.g. Basic Unix Commands and Computing Skills for Biologists [here] if this is unfamiliar to you. See also the Unix Shell lessons (thanks to Anne Fouilloux, UiO)
... and luckily also the Jupyter notebook
The course code is hosted on the code-sharing platform GitHub (where you now are reading this). If you do not have a GitHub account already you should make one now (https://github.com/join). We recommend that you are using the platform for your own projects during the course. Hence:
- Create an account on GitHub (https://github.com) using the
Sign up for GitHub
form on the right side of the page. - Send your GitHub username to your instructor.
- Once your instructor adds you to the course GitHub organization you will receive an email asking you to accept the invitation. Click on the link to accept.
- Check if this worked
- Go to https://github.com.
- Sign in if necessary.
- In the upper left corner click on the drop down with your name.
- Confirm that the name of the course GitHub organization is present
We recommend installing Python via the Anaconda Distribution. Be sure to use the "Python 3.11.x" version. We will use the Conda Package Management System within the Anaconda Distribution. From the documentation:
Conda is an open source package management system and environment management system that runs on Windows, macOS and Linux. Conda quickly installs, runs and updates packages and their dependencies. Conda easily creates, saves, loads and switches between environments on your local computer.
After the installation run python --version
in a terminal window (e.g. in "Anaconda Prompt" if you are using Windows). If the output show "Python 3.11.x" (and "Anaconda") you are good to go.
Alternatively, some of the Jupyter notebooks in the Labs can be executed on Google Colaboratory. Colaboratory, or “Colab” for short, is a product from Google Research. Colab allows anybody to write and execute arbitrary python code through the browser, and is especially well suited to machine learning, data analysis and education. More technically, Colab is a hosted Jupyter notebook service that requires no setup to use, while providing free access to computing resources including GPUs. Colab is free to use, and resources are not guaranteed and not unlimited, and the usage resource limits sometimes fluctuate.
Colab ode is executed in a virtual machine private to your account, i.e.
you will need to Create a Google Account if you not already have one (you don't need a Gmail account to create a Google Account as you can use your non-Gmail address).
Virtual machines are deleted when idle for a while, and have a maximum lifetime enforced by the Colab service.
Colab notebooks are stored in Google Drive, or can be loaded from GitHub. Colab notebooks can be shared just as you would with Google Docs or Sheets. Simply click the Share button at the top right of any Colab notebook, or follow these Google Drive file sharing instructions.
I prefer to use COLAB in the course. Please provide information about colab and how to proceed. What about using my own notebooks and data?
Using Google Colab (short for Colaboratory) is a great choice for working on the BMED365 course, especially if you prefer an environment that doesn't require local setup for Python and Jupyter Notebooks. Here's an overview and guidance on how to use Google Colab, including working with your own notebooks and data.
- Overview: Google Colab is a free, cloud-based service that provides a Jupyter notebook environment. It allows you to write and execute Python in your browser, with zero configuration required, free access to GPUs, and easy sharing.
- Benefits:
- No setup: Run notebooks with no installation required.
- Free access to GPUs: Useful for machine learning tasks.
- Easy sharing: Just like Google Docs, you can share your Colab notebooks with others.
- Environment: Colab runs notebooks on Google Cloud, meaning you can leverage their hardware (including GPUs and TPUs) and save your work on Google Drive.
-
Access Colab:
- Visit Google Colab.
- You'll need a Google account to use Colab.
-
Create a New Notebook:
- Click on
New Notebook
to create a new notebook. - Alternatively, you can go to
File
>Open notebook
to open existing notebooks from Google Drive, GitHub, or upload from your computer.
- Click on
-
Interface Familiarization:
- The interface is similar to Jupyter Notebook. It has a menu bar, a toolbar, and cells where you can write and execute code or add text.
-
Uploading Notebooks:
- You can upload your Jupyter notebooks (
.ipynb
files) directly to Colab from your local machine or integrate with GitHub to load notebooks.
- You can upload your Jupyter notebooks (
-
Using Data:
- From Local Machine: You can upload files from your local machine using:
from google.colab import files uploaded = files.upload()
- From Google Drive: Mount your Google Drive to access files stored there:
This allows you to access files stored in Google Drive directly from your Colab notebooks.
from google.colab import drive drive.mount('/content/drive')
- From External Sources: You can also download data from external sources directly to Colab using
!wget [URL]
command or Python's requests library.
- From Local Machine: You can upload files from your local machine using:
- Execute Cells: Click the play button on the left of the code cell or use
Shift + Enter
to run the code. - GPU/TPU Access: For tasks requiring heavy computation, you can access GPUs/TPUs by going to
Runtime
>Change runtime type
and selecting your preferred hardware accelerator.
- Auto-save: Colab automatically saves your notebooks to your Google Drive.
- Sharing: Share your notebooks just like a Google Doc by clicking the
Share
button and adding collaborators.
- Version Control: Colab supports version history, allowing you to see previous versions of your notebook.
- Collaboration: Multiple people can work on a notebook simultaneously, just like Google Docs.
- Regular Backups: While Colab auto-saves to Drive, it's a good practice to keep local backups of important notebooks.
- Internet Dependency: Since Colab is cloud-based, you'll need a stable internet connection to use it.
- Session Limits: Be aware that Colab sessions are limited (runtime and GPU usage), and you might lose your session if you're inactive for too long or exceed usage limits.
Google Colab is an efficient and powerful tool for working on machine learning and data science projects, making it suitable for your BMED365 course. It allows you to focus on learning and coding without worrying about your local environment setup. If you have specific needs or run into any issues, feel free to seek further assistance.
After you have successfully installed Anaconda, Git, VS Code, and cloned the ELMED219 corse repository (see above):
Inspect and run the notebook 00-test-installation.ipynb
in the Lab1-mpMRI-glioblastoma/Lab-optional-imaging
directory:
cd Lab1-mpMRI-glioblastoma/Lab-optional-imaging
jupyter notebook (or, jupyter lab, or, code . )
The code and environment will be updated during the course. Run the following commands regularly:
- Update code:
git pull
- Update environment:
conda activate bmed365
conda env update
Learners need to understand what files and directories are and what a working directory is. These concepts are covered in the Unix Shell lessons (thanks to Anne Fouilloux, UiO)
The best way to learn how to program is to do something useful, so this introduction to Python is built around a common scientific task: data analysis ("... we are studying inflammation in patients who have been given a new treatment for arthritis, and need to analyze the first dozen data sets of their daily inflammation ..."); see also here.
It is possible to run R-scripts in Jupyter notebooks (Jupyter = Julia, Python and R).
If you want to continue working with R (not part of this course) you should:
- Be using the latest R version (https://www.r-project.org) and the RStudio Desktop [download] for your Windows, MacOS or Linux system.
- Install the R kernel by opening an R console and then follow the instructions at https://irkernel.github.io/installation
- Necessary (or new) R libraries should be installed via RStudio and the corresponding R environment.
- See also here and here.
- The rpy2 interface to use R form Python is also possible (but can be a bit more messy).