Project

General

Profile

ML beam test PID Meetings » History » Version 42

Richard Trotta, 05/31/2024 11:06 AM

1 1 Richard Trotta
{{>toc}}
2
3
h1. ML beam test PID Meetings
4
5 3 Richard Trotta
---
6
7 1 Richard Trotta
h2. Summer 2024
8 4 Richard Trotta
9
---
10
11 25 Richard Trotta
h3. May 28th, 2024
12 1 Richard Trotta
13
* Darren ran with Python3.9
14
* Docker (containerization) Definition
15
** Containerization is a technology that allows developers to package and run applications along with all their dependencies in isolated environments called containers. This ensures that the application runs consistently across different computing environments, from a developer's laptop to testing, staging, and production.
16
** Docker is a popular platform that simplifies containerization. It provides tools to create, deploy, and manage containers. With Docker, developers can write code locally, share their work with colleagues, and deploy to production in a seamless and efficient manner. Docker containers are lightweight, fast, and portable, making them ideal for modern software development and deployment.
17 9 Richard Trotta
18 39 Richard Trotta
* "A Crash Course for Summer Research":https://drive.google.com/file/d/1-TPLo5VGSwCUDjqj_j4g--p4FbBgYHbw/view?usp=sharing
19 17 Richard Trotta
* "GitHub and Python Introduction":https://github.com/trottar/UVA_summer_students
20 18 Richard Trotta
** Navigate to "python_tutorials":https://github.com/trottar/UVA_summer_students/tree/master/python_tutorials and read through the two html files ("Introduction":https://github.com/trottar/UVA_summer_students/blob/master/python_tutorials/Introduction.ipynb and "python_tutorial_2":https://github.com/trottar/UVA_summer_students/blob/master/python_tutorials/python_tutorial_2.ipynb)
21
*** Note, these are html files so you'll need 
22 8 Richard Trotta
23
h4. Near-term Goals
24
25
# Setup Linux Subsystem for Windows
26
# Fork the ML Beam Testing GitHub repository
27
# Containerize ML Beam Testing GitHub repository with Docker
28 5 Richard Trotta
29 14 Richard Trotta
h4. Homework
30
31 16 Richard Trotta
* "Read chapters 0-3 and finish exercises":https://drive.google.com/file/d/1-TPLo5VGSwCUDjqj_j4g--p4FbBgYHbw/view?usp=sharing
32 14 Richard Trotta
33 3 Richard Trotta
---
34 19 Richard Trotta
35 25 Richard Trotta
h3. May 29th, 2024
36 19 Richard Trotta
37
h4. Setting Up Jupyter Notebooks in Docker
38
39
# Create a directory to store, build, and run Docker container
40
	<pre><code class="bash">
41
cd /path/to/directory
42
mkdir beamtest_dir
43 20 Richard Trotta
cd beamtest_dir
44
</code></pre>
45 21 Richard Trotta
# Once in the new directory, create a Dockerfile
46 20 Richard Trotta
	<pre><code class="bash">
47
touch Dockerfile
48
</code></pre>
49 21 Richard Trotta
# Using the text editor (e.g., vim, gedit, or emacs) of your choice, edit the Dockerfile with the following information
50 20 Richard Trotta
	<pre><code class="bash">
51
# Use the official Python 3.9 image as the base image
52
FROM python:3.9
53
54
# Install Jupyter Notebook and other dependencies
55
RUN pip install --no-cache-dir jupyter
56
57
# Create a working directory
58
WORKDIR /workspace
59
60
# Expose the Jupyter Notebook port
61
EXPOSE 8888
62
63
# Set the default command to start Jupyter Notebook
64 1 Richard Trotta
CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]
65
</code></pre>
66 21 Richard Trotta
# Build the Docker image (e.g., helloworld)
67
	<pre><code class="bash">
68
docker build -t helloworld .
69
</code></pre>
70
# Run the Docker container
71
</code></pre>
72
	<pre><code class="bash">
73
docker run -p 8888:8888 helloworld
74
</code></pre>
75
# Jupyter Notebook is now running. Navigate to a browser and type in either
76 23 Richard Trotta
77 21 Richard Trotta
* The custom token authorization screen</code></pre>
78
	<pre><code class="bash">
79
http://localhost:8888
80
</code></pre>
81 22 Richard Trotta
82 21 Richard Trotta
* To bypass the token screen, copy/paste the URL that splashes in the terminal into your browser
83 24 Richard Trotta
84
h4. Near-term Goals
85
86
# Try and get the "python tutorial":https://github.com/trottar/UVA_summer_students Jupyter notebooks working in a container.
87
88
h4. Homework
89
90
* "Read chapters 0-3 and finish exercises":https://drive.google.com/file/d/1-TPLo5VGSwCUDjqj_j4g--p4FbBgYHbw/view?usp=sharing
91 27 Richard Trotta
92
---
93
94
h3. May 30th, 2024
95
96
h4. Setting Up Bash Script for Running Docker
97
98 28 Richard Trotta
# Create a bash script
99
	<pre><code class="bash">
100
touch run_docker.sh
101
</code></pre>
102 29 Richard Trotta
# Using the text editor (e.g., vim, gedit, or emacs) of your choice, edit run_docker.sh
103
	<pre><code class="bash">
104
#!/bin/bash
105 1 Richard Trotta
106 29 Richard Trotta
# Build docker image ($1 is the first bash script argument)
107 37 Richard Trotta
docker build -t $1 .
108 29 Richard Trotta
109
# Run the docker container (renaming it with the '_container' string)
110
docker run -p 8888:8888 --name "${1}_container" $1
111
</code></pre>
112
# Adjust permissions with "chmod":https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/how-permissions-chmod-with-numbers-command-explained-777-rwx-unix to allow script execution
113
	<pre><code class="bash">
114
chmod 755 run_docker.sh
115
</code></pre>
116 31 Richard Trotta
# Execute run_docker.sh with container name (e.g., helloworld)
117
	<pre><code class="bash">
118
./run_docker.sh helloworld
119
</code></pre>
120 1 Richard Trotta
121 31 Richard Trotta
The docker image should be properly built and the container should be running. Follow instructions from "yesterday":https://redmine.jlab.org/projects/uva-phys-zheng/wiki/ML_beam_test_PID_Meetings#May-29th-2024 to continue.
122
123
h4. Setting Up Bash Script for committing Docker container
124
125 32 Richard Trotta
This should be done while a docker container is running and performed in another terminal.
126
127 31 Richard Trotta
# Create a bash script
128
	<pre><code class="bash">
129
touch commit_docker.sh
130
</code></pre>
131
# Using the text editor (e.g., vim, gedit, or emacs) of your choice, edit commit_docker.sh
132
	<pre><code class="bash">
133
#!/bin/bash
134
135
# Create a formatted string with date to organize committed containers
136
# - `date` is a command that prints or sets the system date and time.
137
# - `+%H` extracts the hour in 24-hour format.
138
# - `+%M` extracts the minute.
139
# - `+%S` extracts the second.
140
# - `+%Y` extracts the year.
141
# - `+%m` extracts the month.
142
# - `%d` extracts the day.
143
f_date=$(date +%Y-%m-%d_h%Hm%Ms%S)
144
145
# While a docker container is running (with root name given as first argument), 
146
# commit to a dated container so that changes in the container are saved
147 38 Richard Trotta
docker commit "${1}_container" "${1}_${f_date}"
148 32 Richard Trotta
</code></pre>
149
# Adjust permissions with "chmod":https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/how-permissions-chmod-with-numbers-command-explained-777-rwx-unix to allow script execution
150
	<pre><code class="bash">
151
chmod 755 commit_docker.sh
152
</code></pre>
153
# Execute run_docker.sh with container name (e.g., helloworld)
154
	<pre><code class="bash">
155
./commit_docker.sh helloworld
156 31 Richard Trotta
</code></pre>
157 28 Richard Trotta
158
h4. Useful Docker Commands
159 27 Richard Trotta
160
* To see all docker images
161
	<pre><code class="bash">
162
docker images
163
</code></pre>
164
* To see all docker containers running
165
	<pre><code class="bash">
166
docker ps
167
</code></pre>
168
* To end specific docker container
169
	<pre><code class="bash">
170 34 Richard Trotta
docker stop <container_id>
171 27 Richard Trotta
</code></pre>
172
* To end all docker container
173
	<pre><code class="bash">
174 36 Richard Trotta
docker stop $(docker ps -q)
175 27 Richard Trotta
</code></pre>
176 33 Richard Trotta
177
h4. Near-term Goals
178
179
# Try and get the "python tutorial":https://github.com/trottar/UVA_summer_students Jupyter notebooks working in a container.
180
# Take a look at the "GitHub ML Beam Test Repository":https://github.com/JeffersonLab/solid_beamtest_hallc_2022/tree/main/aiml
181
* In particular, look at the script "solid_beamtest_hallc_2022/aiml/Pencil_Beam/Pencil_Cher_ML.ipynb":https://github.com/JeffersonLab/solid_beamtest_hallc_2022/blob/main/aiml/Pencil_Beam/Pencil_Cher_ML.ipynb
182
** Try to get this notebook working in a container by creating a requirements.txt file (use this "site as a general guide":https://www.docker.com/blog/containerized-python-development-part-1/)
183
** We will cover this in detail tomorrow
184
185
h4. Homework
186
187
* "Read chapters 0-3 and finish exercises":https://drive.google.com/file/d/1-TPLo5VGSwCUDjqj_j4g--p4FbBgYHbw/view?usp=sharing
188 40 Richard Trotta
189
---
190
191
h3. May 31st, 2024
192
193
h4. Clone a git repository into the Docker container by adding a line to the Dockerfile
194
195 41 Richard Trotta
* Using the text editor (e.g., vim, gedit, or emacs) of your choice, edit the Dockerfile with the following information (the python tutorial GitHub repo is used as an example)
196 40 Richard Trotta
	<pre><code class="bash">
197
# Use the official Python 3.9 image as the base image
198
FROM python:3.9
199
200
# Install Jupyter Notebook and other dependencies
201
RUN pip install --no-cache-dir jupyter
202
203
# Create a working directory
204
WORKDIR /workspace
205
206
# Clone GitHub repo
207 1 Richard Trotta
RUN git clone https://github.com/trottar/UVA_summer_students.git
208 41 Richard Trotta
209
# Expose the Jupyter Notebook port
210
EXPOSE 8888
211
212
# Set the default command to start Jupyter Notebook
213
CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]
214
</code></pre>
215
216
h4. Quick requirements.txt introduction
217
218
* requirements.txt is a way to easily install many python packages by recursively reading through a text file
219
* A Docker container can install all these packages upon building its image by adding a few lines to the Dockerfile
220
* Using the text editor (e.g., vim, gedit, or emacs) of your choice, edit the Dockerfile with the following information
221 42 Richard Trotta
	<pre><code class="bash">
222 41 Richard Trotta
# Use the official Python 3.9 image as the base image
223
FROM python:3.9
224
225
# Install Jupyter Notebook and other dependencies
226
RUN pip install --no-cache-dir jupyter
227
228
# Create a working directory
229
WORKDIR /workspace
230
231
# Clone GitHub repo
232
RUN git clone https://github.com/trottar/UVA_summer_students.git
233
234
# copy the dependencies file to the working directory
235
COPY requirements.txt .
236
237
# install dependencies
238
RUN pip install -r requirements.txt
239 40 Richard Trotta
240
# Expose the Jupyter Notebook port
241
EXPOSE 8888
242
243
# Set the default command to start Jupyter Notebook
244
CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]
245
</code></pre>
246
247
h4. Useful Docker Commands
248
249
* Remove all stopped containers (use with caution)
250
	<pre><code class="bash">
251
docker system prune -f
252
</code></pre>
253
254
h4. Near-term Goals
255
256
# Take a look at the "GitHub ML Beam Test Repository":https://github.com/JeffersonLab/solid_beamtest_hallc_2022/tree/main/aiml
257
* In particular, look at the script "solid_beamtest_hallc_2022/aiml/Pencil_Beam/Pencil_Cher_ML.ipynb":https://github.com/JeffersonLab/solid_beamtest_hallc_2022/blob/main/aiml/Pencil_Beam/Pencil_Cher_ML.ipynb
258
** Try to get this notebook working in a container by creating a requirements.txt file (use this "site as a general guide":https://www.docker.com/blog/containerized-python-development-part-1/)
259
260
h4. Homework
261
262
* "Read chapters 0-3 and finish exercises":https://drive.google.com/file/d/1-TPLo5VGSwCUDjqj_j4g--p4FbBgYHbw/view?usp=sharing