"""
# Deep Learning in Biomedical Imaging
## BMENE4460-2022-1
"""
"""
## Kaggle environment and Python programming tutorial
#### Sections:
1. Notebook environment
2. Bash commands
3. Python language
4. Python libraries
5. Read and display an image in python
6. Simple image processing (Denoising with low-pass filters)

"""
"""
## 1. Notebook environment
"""
"""
According to the pre-lecture slides, you should have your Kaggle account, and you should be able to create a notebook like this one.
"""
"""
#### 1.1 Starting a session
After creating a notebook, you may open it, but the session will not start automatically. Instead, you should press the power button in order to start your session.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/power.png?raw=true" /></div>

After starting the session, you'll see a panel like the below picture.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/started.png?raw=true" /></div>

You may also see how many resources Kaggle servers are assigned to your current session.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/hardwares.png?raw=true" /></div>

Remember, after starting a session, you'll have 9 hours to write your scripts, and after that, it will be restarted. Moreover, if you've been inactive for more than 40min, you'll receive a message, and your notebook will be restarted.

#### 1.2 Adding data
Adding data has been explained in the pre-lecture slides.

#### 1.3 Markdown, LaTex, and code blocks
In Kaggle notebooks, you may add a Markdown block, which is just formatted text, and code blocks to write your codes.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/code_markdown.png?raw=true" /></div>

Markdown block

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/markdown_block.png?raw=true" /></div>

Code block

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/code_block.png?raw=true" /></div>

In markdown blocks, you can use LaTex by using `$` for the inline formula or `$$` for the new-line formula.
Example: 
This is an inline formula, $y=\frac{1}{2}x+\pi$

This is a newline formula:
$$
y=\sin{x}+\mu
$$

Double click on Markdown blocks to edit it.

#### 1.4 Save a notebook
To save your notebook, you should click on the "Save Version" button on the top-right side of the window.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/save_button.png?raw=true" /></div>

This will open a panel like the following picture.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/save_panel.png?raw=true" /></div>

If you save as "Save & Run All (Commit)", you'll make a version of your notebook and may always come back and edit it. But, be careful if you have a long-running script as it may take a long time to save the version.
The other option is to save it as it is by choosing the "Quick Save" option. The next time you open your notebook, you'll see the latest version of it.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/quick_save_panel.png?raw=true" /></div>


#### 1.5 Share notebook and make it public
You may also share your notebook with your collaborators by clicking on the "Share" button:

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/share_button.png?raw=true" /></div>

Then find whom you want to share the notebook with by clicking on "Add collaborators."

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/share_panel.png?raw=true" /></div>

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/share_panel_people.png?raw=true" /></div>

But if you wish to make your notebook publicly available, on the same panel, choose the "Public" option.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/share_panel_public.png?raw=true" /></div>

Remember, you'll need to make your assignments' notebook public and then submit the "Public url" in the CourseWorks.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/share_panel_public_url.png?raw=true" /></div>


#### 1.6 Enable GPU and Internet
The default setting of each newly created notebook is set to access the internet through the notebook. This means that either the notebook will have access to online resources via code or markdowns or not. For the purpose of this course, make sure you have internet access.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/internet.png?raw=true" /></div>

The notebooks are not allowed to use GPU resources unless you activate them through the "Accelerator" setting. Remember, each Kaggle account will have limited hours of GPU quota, which may vary between 32 hours/week up to 50 hours/week. Therefore, enable the GPU whenever you need it, such as running the training script.

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/gpu.png?raw=true" /></div>

<div align="center"><img src="https://github.com/soroush361/AoE_BME/blob/main/gpu_quota.png?raw=true" /></div>


Remember changing the accelerator causes the notebook to be restarted.
"""
"""
## 2. Bash commands
"""
"""
Kaggle notebooks are running on a Linux OS. Thus, you can use bash commands in code blocks. All you need is to add an exclamation mark before the command, like `!bash`.

Some of the bash commands that we may use are listed below:

A. Print current working directory: `pwd` \
B. Make a directory: `mkdir` \
C. Change current directory: `cd` (will not work on Kaggle for security reasons) \
D. Print the list of files and folders in the current directory: `ls` \
E. Copy a file: `cp` \
F. Move/rename a file: `mv` \
G. Remove a file: `rm` \
H. Download a file from the internet: `wget`

Check the scripts below:
"""
!pwd
!mkdir image
# This should not work!
!cd image/
!pwd
!ls
!wget -O mandrill.png https://upload.wikimedia.org/wikipedia/commons/c/c1/Wikipedia-sipi-image-db-mandrill-4.2.03.png
!cp mandrill.png baboon.png
!rm mandrill.png
!rm -rf image
!ls
"""
For a complete bash commands cheat sheet, go to [this link](https://cheatography.com/davechild/cheat-sheets/linux-command-line/).
"""
"""
## 3. Python language
"""
"""
Time and experience are necessary for writing seamless Python scripts. Nonetheless, we need you to have a beginner-level understanding of this popular programming language in our course.
We will not be able to cover all the details about Python due to time constraints. However, as a result of completing the [Python tutorial course on Kaggle](https://www.kaggle.com/learn/python), you can now understand most of the coding components of the course.
"""
"""
## 4. Python libraries
Most of the contents in this section were borrowed from [This webpage](https://www.analyticsvidhya.com/blog/2020/11/top-13-python-libraries-every-data-science-aspirant-must-know-and-their-resources/).
"""
"""

#### 4.1 Numpy
<div align="center"><img src="https://cdn.analyticsvidhya.com/wp-content/uploads/2017/02/17084443/numpy_project_page.jpg" width="400"></div>

NumPy is one of the most essential Python Libraries for scientific computing and it is used heavily for the applications of Machine Learning and Deep Learning. NumPy stands for NUMerical PYthon. Machine learning algorithms are computationally complex and require multidimensional array operations. NumPy provides support for large multidimensional array objects and various tools to work with them.

Various other libraries which we are going to discuss further like Pandas, Matplotlib and Scikit-learn are built on top of this amazing library! We have just the right resource for you to get started with NumPy

[The Ultimate NumPy Tutorial for Data Science Beginners](https://www.analyticsvidhya.com/blog/2020/04/the-ultimate-numpy-tutorial-for-data-science-beginners/?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt)

#### 4.2 MatplotLib
<div align="center"><img src="https://cdn.analyticsvidhya.com/wp-content/uploads/2020/03/matplotlib.png" width="400"></div>

Matplotlib is the most popular library for exploration and data visualization in the Python ecosystem. Every other library is built upon this library.

Matplotlib offers endless charts and customizations from histograms to scatterplots, matplotlib lays down an array of colors, themes, palettes, and other options to customize and personalize our plots. matplotlib is useful whether you’re performing data exploration for a machine learning project or building a report for stakeholders, it is surely the handiest library!

If you are just starting out, we have a few resources that will help you get started

[A Beginner’s Guide to matplotlib for Data Visualization and Exploration in Python](https://www.analyticsvidhya.com/blog/2020/02/beginner-guide-matplotlib-data-visualization-exploration-python/?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt) \
[10 matplotlib Tricks to Master Data Visualization in Python](https://www.analyticsvidhya.com/blog/2020/05/10-matplotlib-tricks-data-visualization-python/?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt)

#### 4.3 OpenCv
<div align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/32/OpenCV_Logo_with_text_svg_version.svg/1200px-OpenCV_Logo_with_text_svg_version.svg.png" width="200"></div>

OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.

[Official website](https://opencv.org/)

#### 4.4 Scipy
<div align="center"><img src="https://cdn.analyticsvidhya.com/wp-content/uploads/2020/11/scipy-logo.png" width="200"></div>

SciPy (Scientific Python) is the go-to library when it comes to scientific computing used heavily in the fields of mathematics, science, and engineering. It is equivalent to using Matlab which is a paid tool.

SciPy as the Documentation says is – “provides many user-friendly and efficient numerical routines such as routines for numerical integration and optimization.” It is built upon the NumPy library.

[Official website](https://www.scipy.org/)

#### 4.5 Sklearn
<div align="center"><img src="https://cdn.analyticsvidhya.com/wp-content/uploads/2017/02/17085331/scikit-learn-logo.png" width="400"></div>

Sklearn is the Swiss Army Knife of data science libraries. It is an indispensable tool in your data science armory that will carve a path through seemingly unassailable hurdles. In simple words, it is used for making machine learning models.

Scikit-learn is probably the most useful library for machine learning in Python. The sklearn library contains a lot of efficient tools for machine learning and statistical modeling including classification, regression, clustering, and dimensionality reduction.

Sklearn is a compulsory Python library you need to master. Analytics Vidhya offers a free course on it. You can check out the resources here

[Getting Started with scikit-learn (sklearn) for Machine Learning](https://courses.analyticsvidhya.com/bundles/certified-ai-ml-blackbelt-plus?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt) \
[7 Impressive Scikit-learn Hacks, Tips and Tricks for Data Science](https://www.analyticsvidhya.com/blog/2020/05/7-scikit-learn-hacks-tips-tricks/?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt)

#### 4.6 Pandas
<div align="center"><img src="https://cdn.analyticsvidhya.com/wp-content/uploads/2017/02/17084214/pandas_logo.png" width="400"></div>

From Data Exploration to visualization to analysis – Pandas is the almighty library you must master!

Pandas is an open-source package. It helps you to perform data analysis and data manipulation in Python language. Additionally, it provides us with fast and flexible data structures that make it easy to work with Relational and structured data.

If you are new to Pandas, you should definitely check out this free course

[Pandas for Data Analysis](https://courses.analyticsvidhya.com/courses/pandas-for-data-analysis-in-python?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt)

#### 4.7 Seaborn
<div align="center"><img src="https://files.ai-pool.com/a/21155149cb560f48f085a21264277c3c.png" width="200"></div>

Seaborn is a free and open-source data visualization library based on Matplotlib. Many data scientists prefer seaborn over matplotlib due to its high-level interface for drawing attractive and informative statistical graphics.

Seaborn provides easy functions that help you focus on the plot and now how to draw it. Seaborn is an essential library you must master. Here’s a great resource to checkout

[Become a Data Visualization Whiz with this Comprehensive Guide to Seaborn in Python](https://www.analyticsvidhya.com/blog/2019/09/comprehensive-data-visualization-guide-seaborn-python/?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt)

#### 4.8 NiBabel
<div align="center"><img src="https://nipy.org/nibabel/_static/nibabel-logo.svg" width="200"></div>

This package provides read and write access to some common medical and neuroimaging file formats, including: ANALYZE (plain, SPM99, SPM2 and later), GIFTI, NIfTI1, NIfTI2, CIFTI-2, MINC1, MINC2, AFNI BRIK/HEAD, MGH and ECAT as well as Philips PAR/REC. We can read and write FreeSurfer geometry, annotation and morphometry files. There is some very limited support for DICOM. NiBabel is the successor of PyNIfTI.

The various image format classes give full or selective access to header (meta) information and access to the image data is made available via NumPy arrays.

[Official website](https://nipy.org/nibabel/)

#### 4.9 Pytorch
<div align="center"><img src="https://miro.medium.com/max/1400/1*myMaRWYYzlFYnLfRyqAC3w.png" width="400"></div>

Many data science enthusiasts hail Pytorch as the best deep learning framework (that’s a debate for later on). It has helped accelerate the research that goes into deep learning models by making them computationally faster and less expensive.

PyTorch is a Python-based library that provides maximum flexibility and speed. Some of the features of Pytorch are as follows

Production Ready \
Distributed Training \
Robust Ecosystem \
Cloud support 

Excited? You can learn more about PyTorch here

[Introduction to PyTorch for Deep Learning [FREE COURSE]](https://courses.analyticsvidhya.com/courses/introduction-to-pytorch-for-deeplearning?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt) \
[A Beginner-Friendly Guide to PyTorch and How it Works from Scratch](https://www.analyticsvidhya.com/blog/2019/09/introduction-to-pytorch-from-scratch/?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt)

#### 4.10 TensorFlow
<div align="center"><img src="https://cdn.analyticsvidhya.com/wp-content/uploads/2019/02/Tensorflow.png" width="400"></div>

Over the years, TensorFlow, developed by the Google Brain team has gained traction and become the cutting edge library when it comes to machine learning and deep learning. TensorFlow had its first public release back in 2015. At the time, the evolving deep learning landscape for developers & researchers was occupied by Caffe and Theano. In a short time, TensorFlow emerged as the most popular library for deep learning.

TensorFlow is an end-to-end machine learning library that includes tools, libraries, and resources for the research community to push the state of the art in deep learning and developers in the industry to build ML & DL powered applications.

To be a future-ready data scientist here are a few resources to learn TensorFlow

[Deep Learning Guide: Introduction to Implementing Neural Networks using TensorFlow in Python](https://www.analyticsvidhya.com/blog/2016/10/an-introduction-to-implementing-neural-networks-using-tensorflow/?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt) \
[TensorFlow 2.0 Tutorial for Deep Learning](https://www.analyticsvidhya.com/blog/2020/03/tensorflow-2-tutorial-deep-learning/?utm_source=top-python-libraries-ram&utm_medium=blog&utm_campaign=blackbelt)
"""