Skip to content

Commit

Permalink
Add Python screenshots and chapter plan
Browse files Browse the repository at this point in the history
  • Loading branch information
RohanAlexander committed Nov 13, 2024
1 parent 959cc35 commit ac3c8a8
Show file tree
Hide file tree
Showing 25 changed files with 475 additions and 322 deletions.
2 changes: 1 addition & 1 deletion 20-r_essentials.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ australian_politicians |>



## Base
## Base R

While the `tidyverse` was established relatively recently to help with data science, `R` existed long before this. There is a host of functionality that is built into `R` especially around the core needs of programming and statisticians.

Expand Down
81 changes: 60 additions & 21 deletions 21-python_essentials.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ engine: knitr
**Software and packages**

- `Python` [@python]
- `datetime>=5.5`
- `uv`
- `polars`

Expand All @@ -26,11 +27,21 @@ Knowing `R` will allow you to pick up `Python` for data science quickly. The mai

## Python, VS Code, and uv

We could use `Python` within RStudio, but it makes sense to use what is used by the community more broadly, which is VS Code. You can download VS Code for free [here](https://code.visualstudio.com) and then install it. If you have difficulties with this, then in the same way we started with Posit Cloud and the shifted to our local machine, you could initially use Google Colab [here](https://colab.google).
We could use `Python` within RStudio, but another option is to use what is used by the community more broadly, which is VS Code. You can download VS Code for free [here](https://code.visualstudio.com) and then install it. If you have difficulties with this, then in the same way we started with Posit Cloud and the shifted to our local machine, you could initially use Google Colab [here](https://colab.google).

Open VS Code, and open a new Terminal to install `uv`, which is a Python package manager, analogous to CRAN: `curl -LsSf https://astral.sh/uv/install.sh | sh`.
Open VS Code (@fig-vscodesetup-a), and open a new Terminal: Terminal -> New Terminal (@fig-vscodesetup-b). We can then install `uv`, which is a Python package manager, by putting `curl -LsSf https://astral.sh/uv/install.sh | sh` into the Terminal and pressing "return/enter" afterwards (@fig-vscodesetup-c). Finally, to install Python we can use `uv` by putting `uv python install` into that Terminal and pressing "return/enter" afterwards (@fig-vscodesetup-d).

Then we can use `uv` in that Terminal to install Python: `uv python install`.
::: {#fig-vscodesetup layout-ncol="2"}
![Opening VS Code](figures/B-VS_Code-1.png){#fig-vscodesetup-a width="50%"}

![Opening a Terminal in VS Code](figures/B-VS_Code-2.png){#fig-vscodesetup-b width="50%"}

![Install uv](figures/B-VS_Code-3.png){#fig-vscodesetup-c width="50%"}

![Install Python](figures/B-VS_Code-4.png){#fig-vscodesetup-d width="50%"}

Opening VS Code and a new terminal and then installing uv and Python
:::

## Getting started

Expand Down Expand Up @@ -101,9 +112,9 @@ After we have modified and saved `hello.py` we can run it with `uv run` in exact

At this point we should close VS Code. We want to re-open it to make sure that our project environment is working as it needs to. In VS Code, a project is a self-contained folder. You can open a folder with "File" -> "Open Folder..." and then select the relevant folder, in this case "shelter_usage". You should then be able to re-run `uv run hello.py` and it should work.

### Simulating data
### Plan

We would like to more thoroughly simulate the dataset that we are interested in. We first used this dataset in @sec-fire-hose, but as a reminder, for each day, for each shelter, there is a number of people that used the shelter. So the dataset that we want to simulate is something like @fig-python_torontohomeless-data and we are wanting to create a table of average daily number of occupied beds each month, along the lines of @fig-python_torontohomeless-table.
We first used this dataset in @sec-fire-hose, but as a reminder, for each day, for each shelter, there is a number of people that used the shelter. So the dataset that we want to simulate is something like @fig-python_torontohomeless-data and we are wanting to create a table of average daily number of occupied beds each month, along the lines of @fig-python_torontohomeless-table.

::: {#fig-python_torontohomeless layout-ncol="2"}
![Quick sketch of a dataset](figures/B-raw_data.jpeg){#fig-python_torontohomeless-data width="50%"}
Expand All @@ -113,7 +124,9 @@ We would like to more thoroughly simulate the dataset that we are interested in.
Sketches of a dataset and table related shelter usage in Toronto
:::

We will use `polars` to provide a dataframe to store our simulated results, so we should add this to our environment with `uv add`.
### Simulate

We would like to more thoroughly simulate the dataset that we are interested in. We will use `polars` to provide a dataframe to store our simulated results, so we should add this to our environment with `uv add`.

```{bash}
#| eval: false
Expand All @@ -131,7 +144,7 @@ Create a new Python file called `00-simulate_data.py`.
#| message: false
#### Preamble ####
# Purpose: Simulates a dataset of the number of people in a bed, in each shelter, on each day.
# Purpose: Simulates a dataset of daily shelter usage
# Author: Rohan Alexander
# Date: 12 November 2024
# Contact: [email protected]
Expand Down Expand Up @@ -182,23 +195,15 @@ print(data)

Write tests

### Acquire

Download data

Apply tests

Manipulate the data

Make a graph

Add it all into Quarto
### Explore

Add GitHub to VS code. Why environments.





## Manipulating data
Manipulate the data

```{python}
#| eval: false
Expand All @@ -210,7 +215,7 @@ import polars as pl
```


## Making graphs
Make a graph

```{python}
#| eval: false
Expand All @@ -222,14 +227,48 @@ import matplotlib.pyplot as plt
```


### Share

Add it all into Quarto

Add GitHub to VS code. Why environments.



## Python

For loops

List comprehensions

## Making graphs

matplotlib

seaborn


## Exploring polars

### Importing data

### Dataset manipulation with joins and pivots

### String manipulation

### Factor variables



## Exercises

### Practice {.unnumbered}

Free Replit "100 Days of Code" Python [course](https://replit.com/learn/100-days-of-python).

### Quiz {.unnumbered}


### Task {.unnumbered}

Free Replit "100 Days of Code" Python [course](https://replit.com/learn/100-days-of-python).

16 changes: 8 additions & 8 deletions docs/02-drinking_from_a_fire_hose.html
Original file line number Diff line number Diff line change
Expand Up @@ -819,15 +819,15 @@ <h3 data-number="2.2.2" class="anchored" data-anchor-id="simulate"><span class="
<pre><code># A tibble: 151 × 2
Division Party
&lt;int&gt; &lt;chr&gt;
1 1 Liberal
2 2 Liberal
3 3 National
4 4 Green
5 5 Labor
6 6 Green
1 1 Green
2 2 Labor
3 3 Other
4 4 Other
5 5 Green
6 6 Liberal
7 7 Other
8 8 Green
9 9 Labor
8 8 National
9 9 Green
10 10 National
# ℹ 141 more rows</code></pre>
</div>
Expand Down
42 changes: 21 additions & 21 deletions docs/03-workflow.html
Original file line number Diff line number Diff line change
Expand Up @@ -982,21 +982,21 @@ <h3 data-number="3.2.7" class="anchored" data-anchor-id="sec-quartocrossreferenc
<!-- preamble start -->

<script>
function styleCell_fdn9gal1tsoeqd7uo1b6(i, j, css_id) {
var table = document.getElementById("tinytable_fdn9gal1tsoeqd7uo1b6");
function styleCell_ipu9gnr95tzj5mbw5frj(i, j, css_id) {
var table = document.getElementById("tinytable_ipu9gnr95tzj5mbw5frj");
table.rows[i].cells[j].classList.add(css_id);
}
function insertSpanRow(i, colspan, content) {
var table = document.getElementById('tinytable_fdn9gal1tsoeqd7uo1b6');
var table = document.getElementById('tinytable_ipu9gnr95tzj5mbw5frj');
var newRow = table.insertRow(i);
var newCell = newRow.insertCell(0);
newCell.setAttribute("colspan", colspan);
// newCell.innerText = content;
// this may be unsafe, but innerText does not interpret <br>
newCell.innerHTML = content;
}
function spanCell_fdn9gal1tsoeqd7uo1b6(i, j, rowspan, colspan) {
var table = document.getElementById("tinytable_fdn9gal1tsoeqd7uo1b6");
function spanCell_ipu9gnr95tzj5mbw5frj(i, j, rowspan, colspan) {
var table = document.getElementById("tinytable_ipu9gnr95tzj5mbw5frj");
const targetRow = table.rows[i];
const targetCell = targetRow.cells[j];
for (let r = 0; r < rowspan; r++) {
Expand All @@ -1022,27 +1022,27 @@ <h3 data-number="3.2.7" class="anchored" data-anchor-id="sec-quartocrossreferenc
targetCell.rowSpan = rowspan;
targetCell.colSpan = colspan;
}
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(0, 0, 'tinytable_css_id7dstg1qw7acvlsdyoxxf') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(0, 1, 'tinytable_css_id8t133091gnfhqigwvve4') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(1, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(2, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(3, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(4, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(5, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(6, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(7, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(8, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(9, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_fdn9gal1tsoeqd7uo1b6(10, 1, 'tinytable_css_idmjpfu6vaj3t9bibt06sd') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(0, 0, 'tinytable_css_iduwjmutvjd5ojbgbcqvw9') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(0, 1, 'tinytable_css_idq7xk5knzebk4knrfxnw9') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(1, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(2, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(3, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(4, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(5, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(6, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(7, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(8, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(9, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
window.addEventListener('load', function () { styleCell_ipu9gnr95tzj5mbw5frj(10, 1, 'tinytable_css_idsuunp3dhh7hr7kb67r3p') })
</script>

<style>
.table td.tinytable_css_id7dstg1qw7acvlsdyoxxf, .table th.tinytable_css_id7dstg1qw7acvlsdyoxxf { border-bottom: solid 0.1em #d3d8dc; }
.table td.tinytable_css_id8t133091gnfhqigwvve4, .table th.tinytable_css_id8t133091gnfhqigwvve4 { text-align: right; border-bottom: solid 0.1em #d3d8dc; }
.table td.tinytable_css_idmjpfu6vaj3t9bibt06sd, .table th.tinytable_css_idmjpfu6vaj3t9bibt06sd { text-align: right; }
.table td.tinytable_css_iduwjmutvjd5ojbgbcqvw9, .table th.tinytable_css_iduwjmutvjd5ojbgbcqvw9 { border-bottom: solid 0.1em #d3d8dc; }
.table td.tinytable_css_idq7xk5knzebk4knrfxnw9, .table th.tinytable_css_idq7xk5knzebk4knrfxnw9 { text-align: right; border-bottom: solid 0.1em #d3d8dc; }
.table td.tinytable_css_idsuunp3dhh7hr7kb67r3p, .table th.tinytable_css_idsuunp3dhh7hr7kb67r3p { text-align: right; }
</style>
<div class="container">
<table class="table table-borderless" id="tinytable_fdn9gal1tsoeqd7uo1b6" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<table class="table table-borderless" id="tinytable_ipu9gnr95tzj5mbw5frj" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>

<tr>
Expand Down
Loading

0 comments on commit ac3c8a8

Please sign in to comment.