Skip to content

Commit 0f02f12

Browse files
committed
tutorial: global improvements:
- use string syntax to generate axes - removed the warning box about how to define Session objects before Python 3.6 (Python version previous to 3.6 are too old now) - removed other axis (it is unlikely that users will mix letters and numbers when generating labels with the syntax start..end) - do not use numpy randint() function to generate fake data (to avoid to confuse readers) - avoid to define anonymous axes when calling function to create arrays with predefined values (confusing) - removed example explaining how to create Session objects with Python 3.5- (versions 3.5 or less are too old) - added example using argument after instead of before with the insert() method - added a warning box to explain why we don't use the print() function to display the content of a variable in the tutorial - added warning and examples about how LArray infer type of labels - added explanation about how to create a unique group using slices and individual labels together
1 parent f0b6752 commit 0f02f12

8 files changed

+479
-106
lines changed

doc/source/tutorial/getting_started.ipyml

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@ cells:
2727
__version__
2828

2929

30+
- markdown: |
31+
<div class="alert alert-warning">
32+
33+
**Note:**
34+
The tutorial is generated from Jupyter notebooks which work in the "interactive" mode (like in the LArray Editor console).
35+
In the interactive mode, there is no need to use the print() function to display the content of a variable.
36+
Simply writing its name is enough.
37+
The same remark applies for the returned value of an expression.<br><br>
38+
In a Python script (file with .py extension), you always need to use the print() function to display the content of
39+
a variable or the value returned by a function or an expression.
40+
41+
</div>
42+
43+
44+
- code: |
45+
s = 1 + 2
46+
47+
# In the interactive mode, there is no need to use the print() function
48+
# to display the content of the variable 's'.
49+
# Simply typing 's' is enough
50+
s
51+
52+
53+
- code: |
54+
# In the interactive mode, there is no need to use the print() function
55+
# to display the result of an expression
56+
1 + 2
57+
58+
3059
- markdown: |
3160
## Create an array
3261

@@ -40,9 +69,9 @@ cells:
4069

4170
- code: |
4271
# define some axes to be used later
43-
age = Axis(['0-9', '10-17', '18-66', '67+'], 'age')
44-
gender = Axis(['female', 'male'], 'gender')
45-
time = Axis([2015, 2016, 2017], 'time')
72+
age = Axis('age=0-9,10-17,18-66,67+')
73+
gender = Axis('gender=female,male')
74+
time = Axis('time=2015..2017')
4675

4776

4877
- markdown: |
@@ -429,16 +458,6 @@ cells:
429458
demography_session['foreigners'] = zeros([age, gender, time])
430459

431460

432-
- markdown: |
433-
<div class="alert alert-warning">
434-
If you are using a Python version prior to 3.6, you will have to pass a list of pairs
435-
to the Session constructor otherwise the arrays will be stored in an arbitrary order in
436-
the new session. For example, the session above must be created using the syntax:
437-
438-
`demography_session=Session([('population', population), ('births', births), ('deaths', deaths)])`.
439-
</div>
440-
441-
442461
- markdown: |
443462
One of the main interests of using sessions is to save and load many arrays at once:
444463

@@ -536,7 +555,7 @@ metadata:
536555
name: python
537556
nbconvert_exporter: python
538557
pygments_lexer: ipython3
539-
version: 3.7.3
558+
version: 3.7.7
540559
nbformat: 4
541560
nbformat_minor: 2
542561

doc/source/tutorial/getting_started.ipynb

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,48 @@
4646
"__version__"
4747
]
4848
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {},
52+
"source": [
53+
"<div class=\"alert alert-warning\">\n",
54+
"\n",
55+
"**Note:** \n",
56+
"The tutorial is generated from Jupyter notebooks which work in the \"interactive\" mode (like in the LArray Editor console). \n",
57+
"In the interactive mode, there is no need to use the print() function to display the content of a variable.\n",
58+
"Simply writing its name is enough.\n",
59+
"The same remark applies for the returned value of an expression.<br><br>\n",
60+
"In a Python script (file with .py extension), you always need to use the print() function to display the content of \n",
61+
"a variable or the value returned by a function or an expression.\n",
62+
" \n",
63+
"</div>"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"s = 1 + 2\n",
73+
"\n",
74+
"# In the interactive mode, there is no need to use the print() function \n",
75+
"# to display the content of the variable 's'.\n",
76+
"# Simply typing 's' is enough\n",
77+
"s"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"metadata": {},
84+
"outputs": [],
85+
"source": [
86+
"# In the interactive mode, there is no need to use the print() function \n",
87+
"# to display the result of an expression\n",
88+
"1 + 2"
89+
]
90+
},
4991
{
5092
"cell_type": "markdown",
5193
"metadata": {},
@@ -70,9 +112,9 @@
70112
"outputs": [],
71113
"source": [
72114
"# define some axes to be used later\n",
73-
"age = Axis(['0-9', '10-17', '18-66', '67+'], 'age')\n",
74-
"gender = Axis(['female', 'male'], 'gender')\n",
75-
"time = Axis([2015, 2016, 2017], 'time')"
115+
"age = Axis('age=0-9,10-17,18-66,67+')\n",
116+
"gender = Axis('gender=female,male')\n",
117+
"time = Axis('time=2015..2017')"
76118
]
77119
},
78120
{
@@ -739,19 +781,6 @@
739781
"demography_session['foreigners'] = zeros([age, gender, time])"
740782
]
741783
},
742-
{
743-
"cell_type": "markdown",
744-
"metadata": {},
745-
"source": [
746-
"<div class=\"alert alert-warning\">\n",
747-
" If you are using a Python version prior to 3.6, you will have to pass a list of pairs\n",
748-
" to the Session constructor otherwise the arrays will be stored in an arbitrary order in\n",
749-
" the new session. For example, the session above must be created using the syntax:\n",
750-
" \n",
751-
" `demography_session=Session([('population', population), ('births', births), ('deaths', deaths)])`.\n",
752-
"</div>"
753-
]
754-
},
755784
{
756785
"cell_type": "markdown",
757786
"metadata": {},
@@ -885,7 +914,7 @@
885914
"name": "python",
886915
"nbconvert_exporter": "python",
887916
"pygments_lexer": "ipython3",
888-
"version": "3.7.3"
917+
"version": "3.7.7"
889918
}
890919
},
891920
"nbformat": 4,

doc/source/tutorial/tutorial_aggregations.ipyml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ cells:
8686
population.sum(gender, (benelux, fr_de))
8787

8888

89+
- markdown: |
90+
<div class="alert alert-warning">
91+
92+
**Warning:** Mixing slices and individual labels inside the `[ ]` will generate **several groups** (a tuple of groups) instead of a single group.<br>If you want to create a single group using both slices and individual labels, you need to use the `.union()` method (see below).
93+
94+
</div>
95+
96+
97+
- code: |
98+
# mixing slices and individual labels leads to the creation of several groups (a tuple of groups)
99+
except_2016 = time[:2015, 2017]
100+
except_2016
101+
102+
103+
- code: |
104+
# leading to potentially unexpected results
105+
population.sum(except_2016)
106+
107+
108+
- code: |
109+
# the union() method allows to mix slices and individual labels to create a single group
110+
except_2016 = time[:2015].union(time[2017])
111+
except_2016
112+
113+
114+
- code: |
115+
population.sum(except_2016)
116+
117+
89118
# The lines below here may be deleted if you do not need them.
90119
# ---------------------------------------------------------------------------
91120
metadata:
@@ -102,7 +131,7 @@ metadata:
102131
name: python
103132
nbconvert_exporter: python
104133
pygments_lexer: ipython3
105-
version: 3.7.3
134+
version: 3.7.7
106135
nbformat: 4
107136
nbformat_minor: 2
108137

doc/source/tutorial/tutorial_aggregations.ipynb

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,58 @@
147147
"source": [
148148
"population.sum(gender, (benelux, fr_de))"
149149
]
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"metadata": {},
154+
"source": [
155+
"<div class=\"alert alert-warning\">\n",
156+
"\n",
157+
"**Warning:** Mixing slices and individual labels inside the `[ ]` will generate **several groups** (a tuple of groups) instead of a single group.<br>If you want to create a single group using both slices and individual labels, you need to use the `.union()` method (see below).\n",
158+
" \n",
159+
"</div>"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": null,
165+
"metadata": {},
166+
"outputs": [],
167+
"source": [
168+
"# mixing slices and individual labels leads to the creation of several groups (a tuple of groups)\n",
169+
"except_2016 = time[:2015, 2017]\n",
170+
"except_2016"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": null,
176+
"metadata": {},
177+
"outputs": [],
178+
"source": [
179+
"# leading to potentially unexpected results\n",
180+
"population.sum(except_2016)"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": null,
186+
"metadata": {},
187+
"outputs": [],
188+
"source": [
189+
"# the union() method allows to mix slices and individual labels to create a single group\n",
190+
"except_2016 = time[:2015].union(time[2017])\n",
191+
"except_2016"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": null,
197+
"metadata": {},
198+
"outputs": [],
199+
"source": [
200+
"population.sum(except_2016)"
201+
]
150202
}
151203
],
152204
"metadata": {
@@ -165,7 +217,7 @@
165217
"name": "python",
166218
"nbconvert_exporter": "python",
167219
"pygments_lexer": "ipython3",
168-
"version": "3.7.3"
220+
"version": "3.7.7"
169221
}
170222
},
171223
"nbformat": 4,

doc/source/tutorial/tutorial_combine_arrays.ipyml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,28 @@ cells:
1515
- code: |
1616
# load the 'demography_eurostat' dataset
1717
demography_eurostat = load_example_data('demography_eurostat')
18+
19+
# load 'gender' and 'time' axes
20+
gender = demography_eurostat.gender
21+
time = demography_eurostat.time
1822

1923

2024
- code: |
2125
# load the 'population' array from the 'demography_eurostat' dataset
2226
population = demography_eurostat.population
27+
28+
# show 'population' array
2329
population
2430

2531

32+
- code: |
33+
# load the 'population_benelux' array from the 'demography_eurostat' dataset
34+
population_benelux = demography_eurostat.population_benelux
35+
36+
# show 'population_benelux' array
37+
population_benelux
38+
39+
2640
- markdown: |
2741
The LArray library offers several methods and functions to combine arrays:
2842

@@ -38,13 +52,19 @@ cells:
3852

3953

4054
- code: |
41-
other_countries = zeros((Axis('country=Luxembourg,Netherlands'), population.gender, population.time), dtype=int)
55+
other_countries = zeros((Axis('country=Luxembourg,Netherlands'), gender, time), dtype=int)
4256

4357
# insert new countries before 'France'
4458
population_new_countries = population.insert(other_countries, before='France')
4559
population_new_countries
4660

4761

62+
- code: |
63+
# insert new countries after 'France'
64+
population_new_countries = population.insert(other_countries, after='France')
65+
population_new_countries
66+
67+
4868
- markdown: |
4969
See [insert](../_generated/larray.Array.insert.rst#larray.Array.insert) for more details and examples.
5070

@@ -66,7 +86,7 @@ cells:
6686

6787

6888
- code: |
69-
population_lux = Array([-1, 1], population.gender)
89+
population_lux = Array([-1, 1], gender)
7090
population_lux
7191

7292

@@ -97,7 +117,7 @@ cells:
97117

98118
- markdown: |
99119
## Extend
100-
120+
101121
Extend an array along an axis with another array *with* that axis (but other labels)
102122

103123

@@ -123,6 +143,13 @@ cells:
123143
population_fr = population['France']
124144
population_de = population['Germany']
125145

146+
print(population_be)
147+
print(population_fr)
148+
print(population_de)
149+
150+
151+
- code: |
152+
# create a new array with an extra axis 'country' by stacking the three arrays population_be/fr/de
126153
population_stacked = stack({'Belgium': population_be, 'France': population_fr, 'Germany': population_de}, 'country')
127154
population_stacked
128155

@@ -147,7 +174,7 @@ metadata:
147174
name: python
148175
nbconvert_exporter: python
149176
pygments_lexer: ipython3
150-
version: 3.7.3
177+
version: 3.7.7
151178
nbformat: 4
152179
nbformat_minor: 2
153180

0 commit comments

Comments
 (0)