Skip to content

Commit 4937edb

Browse files
committed
add homework for week 3
1 parent c1aac26 commit 4937edb

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

Diff for: _episodes/03-multiple_files.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ where anything in the braces is a python variable and it will print the value of
225225
226226
## Project Assignment
227227
228-
This is a project assignment which you can complete to test your skills.
228+
This is a project assignment which you can complete to test your skills. This project should be used when this material is used in a long workshop, or if you are working through this material independently.
229229
230230
> ## File parsing homework
231231
> In the lesson materials, there is a file called `03_Prod.mdout`. This is a file output by the Amber molecular dynamics simulation program.

Diff for: _episodes/07-command_line.md

+105-1
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,110 @@ if __name__ == "__main__":
309309
~~~
310310
{: .language-python}
311311

312-
312+
> ## Project Assignment
313+
>
314+
> For this homework assignment, you will return to your Project from Lesson 3.
315+
>
316+
> Create a command line script using `argparse` which can take in an `mdout` file from Amber, pull out total energy for each time step, and write a new file containing these values. The script should take a file name (`03_Prod.mdout`) OR pattern (`*.mdout`) and output files with the names `filename_Etot.txt` **in the same directory as the files**. Modify your week 1 homework to do this. In contrast to week 1, truncate the last two values from your script (these are an average value and a fluctuation value) from the file you write.
317+
>
318+
> You can download a directory containing more mdout files [here](../data/mdout.zip)
319+
>
320+
> Call your script `analyze_mdout.py`. You should be able to call the script in the following > ways:
321+
>
322+
> ~~~
323+
> $ python analyze_mdout.py 03_Prod.mdout
324+
> ~~~
325+
> {: .language-bash}
326+
>
327+
> or
328+
>
329+
> ~~~
330+
> $ python analyze_mdout.py '*.out'
331+
> ~~~
332+
> {: .language-bash}
333+
>
334+
> When you call help, you should get the following output:
335+
> ~~~
336+
> $ python analyze_mdout.py --help
337+
> ~~~
338+
> {: .language-bash}
339+
>
340+
> ~~~
341+
> usage: This script parses amber mdout files to extract the total energy. You can also use it to create plots.
342+
> [-h] [--make_plots] path
343+
>
344+
> positional arguments:
345+
> path The filepath to the file(s) to be analyzed. To analyze
346+
> multiple files, you can use the `*` pattern.
347+
>
348+
> optional arguments:
349+
> -h, --help show this help message and exit
350+
> ~~~
351+
> {: .output}
352+
> **Extension**
353+
> Add an optional argument to your script to create plots of the data with matplotlib.
354+
>
355+
>> ## Solution
356+
>> ~~~
357+
>> import os
358+
>> import glob
359+
>> import argparse
360+
>> import matplotlib.pyplot as plt
361+
>>
362+
>>
363+
>> if __name__ == "__main__":
364+
>>
365+
>> parser = argparse.ArgumentParser("This script parses amber mdout files to extract the total energy. You can also use it to create plots.")
366+
>> parser.add_argument("path", help="The filepath to the file(s) to be analyzed. To analyze multiple files, you can use the `*` pattern.", type=str)
367+
>> parser.add_argument("--make_plots", help="Create a line plot of the values.", action='store_true')
368+
>>
369+
>> args = parser.parse_args()
370+
>>
371+
>> filenames = args.path
372+
>> dir_name = os.path.dirname(filenames)
373+
>>
374+
>> files = glob.glob(filenames)
375+
>>
376+
>> for file in files:
377+
>> base_name = os.path.basename(file).split('.')[0]
378+
>>
379+
>> # Open the file
380+
>> f = open(file,'r')
381+
>>
382+
>> # Read the data in the file.
383+
>> data = f.readlines()
384+
>>
385+
>> # Close the file.
386+
>> f.close()
387+
>>
388+
>> etot = []
389+
>> # Loop through the lines
390+
>> for line in data:
391+
>> split_line = line.split()
392+
>> if 'Etot' in line:
393+
>> etot.append(float(split_line[2]))
394+
>>
395+
>> # Get rid of values we don't need.
396+
>> values = etot[:-2]
397+
>> # Open a file for writing
398+
>> outfile_location = os.path.join(dir_name, F'{base_name}_Etot.txt')
399+
>> outfile = open(outfile_location, 'w+')
400+
>>
401+
>> for value in values:
402+
>> outfile.write(f'{value}\n')
403+
>>
404+
>> outfile.close()
405+
>>
406+
>> if args.make_plots:
407+
>> plt.figure()
408+
>> plot_name = os.path.join(dir_name, F'{base_name}_Etot.png')
409+
>> plt.plot(values, label=base_name)
410+
>> plt.legend()
411+
>> plt.savefig(plot_name, dpi=300)
412+
>> ~~~
413+
>> {: .language-python}
414+
>>
415+
> {: .solution}
416+
{: .challenge}
313417
314418
{% include links.md %}

Diff for: data/mdout.zip

702 KB
Binary file not shown.

0 commit comments

Comments
 (0)