Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • bcontreras/intermediate-python
  • grp-bio-it-workshops/intermediate-python
2 results
Show changes
Commits on Source (24)
......@@ -35,4 +35,40 @@ keypoints:
- include an aside about I/O - reading/writing files (pandas (the `.to_*()` methods and highlight some: `csv`, `json`, `feather`, `hdf`), numpy, `open()`, (?) bytes vs strings, (?) encoding)
- Finish with example of `df.plot()` to set the scene for plotting section
> ## Working with Filtered Data
>
> 1. On what date were the most cases reported in Germany so far?
> 2. What was the mean number of cases reported per day in Germany in April 2020?
> 3. Is this higher or lower than the mean for March 2020?
> 4. On how many days in March was the number of cases in Germany higher than the mean for April?
>
> > ## Solution
> > ~~~
> > # 1
> > mask_germany = covid_cases['countryterritoryCode'] == 'DEU'
> > id_max = covid_cases[mask_germany]['cases'].idxmax()
> > print(covid_cases.iloc[id_max]['dateRep'])
> >
> > # 2
> > mask_april = (covid_cases['year'] == 2020) & (covid_cases['month'] == 4)
> > mean_april = covid_cases[mask_germany & mask_april]['cases'].mean()
> > print(mean_april)
> >
> > # 3
> > mask_march = (covid_cases['year'] == 2020) & (covid_cases['month'] == 3)
> > mean_march = covid_cases[mask_germany & mask_march]['cases'].mean()
> > print(mean_march)
> > print("Mean cases per day was {} in April than in March 2020.".
> > format(["lower", "higher"][mean_april > mean_march]))
> >
> > # 4
> > mask_higher_mean_april = (covid_cases['cases'] > mean_april)
> > selection = covid_cases[mask_germany & mask_march & mask_higher_mean_april]
> > nbr_days = len(selection) # Assume clean data
> > print(nbr_days)
> > ~~~
> > {: .language-python }
> {: .solution }
{: .challenge }
{% include links.md %}
......@@ -30,4 +30,59 @@ keypoints:
- [`click`](https://click.palletsprojects.com/en/7.x/) (?)
- [comparison of argparse, docopt and click](https://realpython.com/comparing-python-command-line-parsing-libraries-argparse-docopt-click/)
> ## Options & Arguments
> The code block below shows the start of a program
> that takes three input files and returns either
> the product, the sum, or the mean, of the values
> given somewhere within them.
> (The details of how the files are processed and
> the returned values calculated are not shown.)
>
> Fill in the blanks in the code (indicated with `___`),
> so that the program can take an option,
> `--output=output.csv`, to specify an output file,
> or write to STDOUT otherwise.
> (Note: `sys.stdout` is an object representing the STDOUT stream.)
>
> ~~~
> from sys import argv, stdout
> from numpy import prod # use math.prod here if you're running Python 3.8
> from statistics import mean
>
> input_files = argv[-3:]
>
> modes = {'--sum': sum,
> '--product': prod,
> '--mean': mean}
> output_file = ___
>
> for option in argv[___:-3]:
> if option in modes:
> output_function=modes[option]
> elif ___:
> output_file = option.split___
> ~~~
> {: .language-python}
>
> Once you've done this, try to change the code
> so that the program can take an arbitrary number of input files.
>
> Look at the code again.
> Do you think this is a good way to construct an interface for a program like this?
> What potential problems can you identify with this approach?
> Pair up and discuss with a partner.
{: .challenge}
> ## Carry the Zero
>
> What does `sys.argv[0]` return when you run it in a script?
> What about in a Jupyter Notebook?
> In what circumstances might this be useful?
> Is the full path included in the returned value?
> If not, take a look at [the documentation for `os`][os-file-dir]
> and [`pathlib`][pathlib],
> other modules in the standard library,
> to find another function that could get this for you.
{: .challenge}
{% include links.md %}
......@@ -36,4 +36,81 @@ keypoints:
- be careful with cell order
- clear output before saving
> ## Fashions change. Style is forever.
> Look at the following three code blocks. (Based on [this script][matplotlib-hinton] from the Matplotlib Example Gallery).
>
> **Block A**
> ~~~
> def hinton(matrix, max_weight=None, ax=None):
> """Draw Hinton diagram for visualizing a weight matrix."""
> ax = ax if ax is not None else plt.gca()
>
> if not max_weight:
> max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))
>
> ax.patch.set_facecolor('gray')
> ax.set_aspect('equal', 'box')
> ax.xaxis.set_major_locator(plt.NullLocator())
> ax.yaxis.set_major_locator(plt.NullLocator())
>
> for (x,y), w in np.ndenumerate(matrix):
> color = 'white' if w > 0 else 'black'
> size = np.sqrt(np.abs(w) / max_weight)
> rect = plt.Rectangle([x - size/2, y - size/2], size, size,
> facecolor=color, edgecolor=color)
> ax.add_patch(rect)
> ~~~
> {: .language-python }
>
> **Block B**
> ~~~
> def hinton(matrix, max_weight=None, ax=None):
> """Draw Hinton diagram for visualizing a weight matrix."""
> ax = ax if ax is not None else plt.gca()
>
> if not max_weight:
> max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))
>
> ax.patch.set_facecolor('gray')
> ax.set_aspect('equal', 'box')
> ax.xaxis.set_major_locator(plt.NullLocator())
> ax.yaxis.set_major_locator(plt.NullLocator())
>
> for (x, y), w in np.ndenumerate(matrix):
> color = 'white' if w > 0 else 'black'
> size = np.sqrt(np.abs(w) / max_weight)
> rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
> facecolor=color, edgecolor=color)
> ax.add_patch(rect)
> ~~~
> {: .language-python }
>
> **Block C**
> ~~~
> def hinton (matrix,max_weight=None,ax=None) :
> """Draw Hinton diagram for visualizing a weight matrix."""
> ax=ax if ax is not None else p.gca()
>
> if not max_weight:
> max_weight=2**np.ceil(np.log(np.abs(matrix).max())/np.log(2))
>
> ax.patch.set_facecolor('gray')
> ax.set_aspect('equal','box')
> ax.xaxis.set_major_locator(plt.NullLocator())
> ax.yaxis.set_major_locator(plt.NullLocator())
>
> for (x,y),w in np.ndenumerate(matrix):
> color = 'white' if w>0 else 'black'
> size = np.sqrt(np.abs(w)/max_weight)
> rect = plt.Rectangle([x-size/2,y-size/2],size,size,facecolor=color,edgecolor=color)
> ax.add_patch(rect)
> ~~~
> {: .language-python}
>
> - Which block conforms to the standards described in PEP8?
> - Is it also the block you find easiest to read?
> - In each block, mark the lines/places where you consider the style to be _problematic_ (that is, where the [lack of] style actively makes the code difficult to read/inaccessible in some other way).
> - Pair up and compare your notes with a partner's. Did you both identify the same problems?
{: .challenge }
{% include links.md %}
......@@ -33,11 +33,14 @@
[lesson-reference]: {{ relative_root_path }}{% link reference.md %}
[lesson-setup]: {{ relative_root_path }}{% link setup.md %}
[markdown-cheatsheet]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
[matplotlib-hinton]: https://matplotlib.org/examples/specialty_plots/hinton_demo.html
[mit-license]: https://opensource.org/licenses/mit-license.html
[morea]: https://morea-framework.github.io/
[numfocus]: https://numfocus.org/
[os-file-dir]: https://docs.python.org/3.8/library/os.html#os-file-dir
[osi]: https://opensource.org
[pandoc]: https://pandoc.org/
[pathlib]: https://docs.python.org/3/library/pathlib.html
[paper-now]: https://github.com/PeerJ/paper-now
[python-gapminder]: https://swcarpentry.github.io/python-novice-gapminder/
[pyyaml]: https://pypi.python.org/pypi/PyYAML
......
Countries and territories,Start date,End date
Albania,2020-03-13,2020-06-01
Algeria,2020-03-23,2020-05-14
Argentina,2020-03-19,2020-06-28
Armenia,2020-03-24,2020-05-04
Australia,2020-03-23,
Austria,2020-03-16,2020-04-13
Azerbaijan,2020-03-31,2020-04-20
Bangladesh,2020-03-26,2020-05-16
Barbados,2020-03-28,2020-05-03
Belgium,2020-03-18,2020-05-04
Bermuda,2020-04-04,2020-05-02
Bolivia,2020-03-22,2020-04-15
Botswana,2020-04-02,2020-04-30
Brazil,2020-03-17,2020-05-10
Colombia,2020-03-25,2020-06-30
Republic of the Congo,2020-03-31,2020-04-20
Costa Rica,2020-03-23,
Croatia,2020-03-18,2020-04-19
Czech Republic,2020-03-16,2020-04-12
Ecuador,2020-03-16,2020-03-31
El Salvador,2020-03-12,2020-04-02
Eritrea,2020-04-02,2020-04-23
Fiji,2020-03-20,2020-04-17
Finland,2020-03-27,2020-04-16
France,2020-03-17,2020-05-11
Georgia,2020-03-31,2020-04-21
Germany,2020-03-23,2020-04-20 to 2020-05-10
Ghana,2020-03-30,2020-04-12
Greece,2020-03-23,2020-05-04
Guernsey,2020-03-25,
Honduras,2020-03-20,2020-05-17
Hungary,2020-03-28,2020-04-10
India,2020-03-25,2020-06-30
Iran,2020-03-14,2020-04-20
Iraq,2020-03-22,2020-04-11
Ireland,2020-03-12,2020-05-18
Israel,2020-04-02,
Italy,2020-03-09,2020-05-18
Jamaica,2020-04-15,2020-04-22
Jordan,2020-03-18,2020-04-30
Kosovo,2020-03-14,2020-05-04
Kuwait,2020-03-14,2020-03-29
Lebanon,2020-03-15,2020-03-28
Liberia,2020-03-23,2020-04-11
Libya,2020-03-22,
Lithuania,2020-03-16,2020-06-18
Madagascar,2020-03-23,2020-04-20
Malaysia,2020-03-18,2020-06-09
Mexico,2020-03-23,2020-06-01
Mongolia,2020-03-10,2020-03-16
Montenegro,2020-03-24,
Morocco,2020-03-19,2020-06-10
Namibia,2020-03-27,2020-05-04
Nepal,2020-03-24,2020-06-14
New Zealand,2020-03-26,2020-05-14
Nigeria,2020-03-30,2020-04-12
Northern Cyprus,2020-03-30,
Oman,2020-04-10,
Pakistan,2020-03-24,2020-05-09
Panama,2020-03-25,
Papua New Guinea,2020-03-24,2020-04-07
Paraguay,2020-03-20,2020-05-03
Peru,2020-03-16,2020-06-30
Philippines,2020-03-15,2020-05-31
Poland,2020-03-13,2020-04-11
Portugal,2020-03-19,2020-04-02
Qatar,2020-03-11,
Romania,2020-03-25,2020-05-12
Russia,2020-03-28,2020-05-12
Rwanda,2020-03-21,2020-04-19
Samoa,2020-03-26,2020-04-08
San Marino,2020-03-14,2020-05-05
Saudi Arabia,2020-03-09,
Serbia,2020-03-15,2020-04-21 to 2020-05-04
Singapore,2020-04-07,2020-06-01
South Africa,2020-03-26,2020-04-30
Spain,2020-03-14,2020-05-09
Sri Lanka,2020-03-18,2020-05-26
Thailand,2020-03-25,2020-05-31
Trinidad and Tobago,2020-03-17,2020-03-31
Tunisia,2020-03-22,2020-04-19
Turkey,2020-04-23,2020-04-27
Ukraine,2020-03-17,2020-04-24
United Arab Emirates,2020-03-26,2020-04-17
United Kingdom,2020-03-23,
United States,2020-03-19,
Venezuela,2020-03-17,2020-05-13
Vietnam,2020-04-01,2020-04-22
Zimbabwe,2020-03-30,2020-05-02
data/cilliated_cell.png

2.21 MiB

data/cilliated_cell_nuclei.png

7.09 KiB