<jupyter_start><jupyter_text>Table of contents0. [Introduction](0-Introduction.ipynb)1. [Variables](1-Variables.ipynb)2. [Data structures](2-Data-Structures.ipynb)3. [Conditional statements and loops](3-Conditional-Statements-Loops.ipynb)4. [Some exercises](4-Some-Exercises.ipynb)5. [Introduction to functions](5-0-Introduction-function.ipynb)    1. [File manipulation](5-1-File-manipulation.ipynb) $\leftarrow$6. [From 0D to 1D](6-1-From-0D-to-1D.ipynb)    1. [Adding lateral diffusion](6-2-Adding-lateral-diffusion.ipynb)7. [From 1D to 2D](7-From-1D-to-2D.ipynb)8. [Playing with the model](8-Playing-with-the-model.ipynb)  Exercise 7**(With an intermezzo about file manipulation)**You can play with the different parameters to see how the concentration dynamics change according to these parameters.Here we would like you to systematically try different parameters and save the produced plots as png files with names containing the parameter values (for example `'test_a0.4_i0.15_dt0.001_k-0.005_tau0.1.png'`).To save the produced plots, you can use the argument `save_path` of the function `plot_concentration_1cell`.If you set its value to the file name and path you want to create, it will save it there under its name.Before being able to do so, you might need some information about how to manipulate strings.In the previous exercises you might have seen that it is possible to insert values from variables within a string using the curved brackets `{` and `}`.Simply put, the way it works is by putting the character `f` before your string and then everything within curved brackets will be transformed into string if possible.For example:```pythonf'test_a{A[0]}_i{I[0]}_dt{dt}'```will produce the following string:```python'test_a0.4_i0.15_dt0.001'```Note that if the `f` is not in front of the string, the curved brackets will be interpreted as normal characters.**For more on string manipulation, you can read [there](https://docs.python.org/3/tutorial/inputoutput.htmlinput-and-output)**<jupyter_code>from Resources.UsefulFunctions import *
from Resources.Answers import answer, hint

# Carry over here the previously declared variables and the code from the previous exercises
mu_a = 2.8e-4
mu_i = 5e-3
tau = .1
k = -.005
size = 100
dx = dy = 2. / size
T = 9.0
dt = .001
n = int(T / dt)

def compute_AI(): # Don't forget to add arguments
    # A, I = [a], [i] 
    A, I = [0], [0]
    # Uncoment above and
    # Do something here
    return A, I

A, I = compute_AI()
s1 = f'test_a{A[0]}_i{I[0]}_dt{dt}'
s2 = 'test_a{A[0]}_i{I[0]}_dt{dt}'
print(s1)
print(s2)<jupyter_output><empty_output><jupyter_text>Avoiding cramping up you current folderIf you want to be a little bit cleaner, you can create a folder in which you will save your images.You can create such a folder directly in python using `Path` from the `pathlib` library and the command:```pythonPath.mkdir('')```For example, to create a folder named `question_7` one could run the command```pythonPath.mkdir('question_7')```Though, if the folder already exists, the command line will not work and stop the notebook from running.To avoid such a problem, it is possible to check whether the folder already exists using the method `exists` of `Path` as shown below.Let's create the folder `question_7`:<jupyter_code>from pathlib import Path
folder = Path('question_7')
if not folder.exists():
    Path.mkdir(folder)<jupyter_output><empty_output><jupyter_text>Path manipulationSome of you might already be aware that playing with paths can be a pain.The problem comes from the fact that Windows has a different way to represent a path to a folder than Linux and MacOs.> **_Side Note: what's a path?!_**>> In a computer the folders and files are organised hierarchically.> What it means is that each file or folder except for one, the root, is in a folder.> For example, the folder you created earlier (`question_7`) is itself in a folder.>> To access a file or folder, it is sometimes necessary to know the sequence of folders it is in so there is no ambiguity for the computer.> The sequence of folders a folder or a file belongs to is the **path** and it can be represented as a string.> For example, you can call the function `Path.cwd` (for the current working directory).> To query the list of directories your notebook is running in:<jupyter_code>print('Our current path:')
print(Path.cwd())<jupyter_output><empty_output><jupyter_text>> You can maybe see that the folders are separated by a `/` (or a `\` for Windows).> This difference between Linux or MacOs and Windows has been quite a source of trouble, some of you might have experienced it.Now, to save an image in the folder `'question_7'`, as we would like to do, we just need to concatenate the image name to the folder name:```pythonfolder / 'test_a0.4_i0.15_dt0.001.png'```Note that the `/` in this case is a concatenation operator specific to the objects of the `path` library. The operator concatenates two `Path` or a `Path` and a `str` putting the operating specific folder separator (`/` or `\`).**More info about the `pathlib` can be found [there](https://docs.python.org/3/library/pathlib.html)**<jupyter_code>## folder is the path previously created
# Concatenation of two Paths
print(folder / Path('test_a0.4_i0.15_dt0.001.png'))
# Concatenation of a Path and a str (same result)
print(folder / 'test_a0.4_i0.15_dt0.001.png')<jupyter_output><empty_output><jupyter_text>Now we can *cleanly* answer question 6.Let assumes that we want the following values:- `tau` changes from `0.05` to `3` and that we want `5` values within that interval- `k` changes from `-1` to `1` and that we also want `5` values within that interval- and a fixed `dt=0.01`**Write some lines of code to compute and save the requested plots**Note: you can use the function `np.linspace` to generate the desired values<jupyter_code>import numpy as np
folder = Path('question_7')
for test_tau in np.linspace(.05, 1, 5):
    for test_k in np.linspace(-1, 1, 5):
#         A, I = answer_results(4, A=0.4, I=0.15, dt=dt, k=test_k, tau=test_tau, n=n)
        plot_concentration_1cell(A, I,
                                 save_path=folder / f'k{test_k}_tau{test_tau}.png')<jupyter_output><empty_output><jupyter_text>An interesting configuration where we can see some oscillations:- `dt=0.01`- `k=0.05`- `tau=2`You can manually change the parameters to try to find other *weird* configurations<jupyter_code>A, I = answer_results(4, A=0.4, I=0.15, dt=.01, k=.05, tau=2, n=n)
plot_concentration_1cell(A, I)<jupyter_output><empty_output><jupyter_text>Exercise 8**This exercise is difficult, it might take a bit longer to solve. If you are stuck, don't hesitate to look at the following cells for some help**For this exercise, we will discuss about file manipulation, in other words how to move files automatically.**Attention here! Proceed with caution for this exercise but also in general. Files removed using Python (or the shell for example) do not end up in the trash but are directly removed!**In this exercise we would like to sort the files created in the previous exercise. We would like to group the plots generated previously in folders by values of `k` and so that the folders are named according to that `k` value.For example if you had the following files in your `exercise_7` folder:```exercise_7: | k0_tau0.png | k0_tau1.png | k0_tau2.png | k1_tau0.png | k1_tau1.png | k1_tau2.png | k2_tau0.png | k2_tau1.png | k2_tau2.png```We would like you to create the following hierarchy:```exercise_7: | k0:   | k0_tau0.png   | k0_tau1.png   | k0_tau2.png | k1:   | k1_tau0.png   | k1_tau1.png   | k1_tau2.png | k2:   | k2_tau0.png   | k2_tau1.png   | k2_tau2.png```To do so you can use the following functions (assuming `p` is a `Path`):- `Path.iterdir` allows to loop through all the files of a directory- `p.name` retrieves the name of the file in `p` as a `str`- `str.split` splits a string- `Path.exists` see above- `p.rename` allows to rename (and therefore move) `p`Do not hesitate to look at the help of each of these functions (you should do it!).<jupyter_code>p = Path('question_7')
for file in p.iterdir():
    ## Do things here
    print(file)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09995118379301929, 'fraction_numerical': 0.02623871125213571, 'mean_word_length': 4.69492703266157, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '144040', 'license': 'MIT', 'n_tokens_mistral': 2662, 'n_tokens_neox': 2518, 'n_words': 1058, 'path': '1-Introduction-to-Python/5-1-File-manipulation.ipynb', 'repo_name': 'GuignardLab/CenTuri-Course-2022', 'size': '8194'}