Commit 64fc4155 authored by paugier's avatar paugier

First commit training UGA 2017

parents
The main contributors are:
- Pierre Augier (LEGI, CNRS)
- Eric Maldonado (Irstea)
- Cyrille Bonamy (LEGI, CNRS)
- Franck Thollard (ISTERRE)
- Oliver Henriot (GRICAD)
- Christophe Picard (LJK)
This diff is collapsed.
IPYNBDIR := ipynb
IPYNBFILES := $(shell find $(IPYNBDIR) -name '*.ipynb')
IPYNBPRES = $(basename $(IPYNBFILES))
define STR_HELP
This makefile can be used for
help: print this help.
notebook: run a jupyter-notebook.
ipynb/pres*: make the slides and display them (use Control-C to stop this server).
for example:
make ipynb/pres00_intro_first_steps
endef
export STR_HELP
.PHONY: help notebook $(IPYNBPRES)
help:
@echo "$$STR_HELP"
notebook:
python $(shell which jupyter-notebook)
# --ServePostProcessor.ip='127.0.0.2'
$(IPYNBPRES): ipynb%: ipynb%.ipynb
jupyter-nbconvert $< --to slides --post serve
Python training UGA 2017
========================
**A training to acquire strong basis in Python to use it efficiently**
The code of this training has been written for Python 3.
To display and/or modify the presentations, use the Makefile. See::
make help
Repositories
------------
The old repository is https://sourcesup.renater.fr/projects/py-training-uga/
We now use a repository hosted in the Gitlab of UGA:
https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga
- Clone the repository with Git::
git clone https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga/py-training-2017.git
- Clone the repository with Mercurial (and the extension hg-git, as explained
`here
<http://fluiddyn.readthedocs.io/en/latest/mercurial_bitbucket.html>`_)::
hg clone git+ssh://git@gricad-gitlab.univ-grenoble-alpes.fr/python-uga/py-training-2017.git
Authors
-------
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck
Thollard (ISTERRE), Christophe Picard (LJK)
path = 'data/file1.txt'
nb_numbers = 0
mysum = 0.
with open(path) as f:
for line in f:
number = float(line)
nb_numbers += 1
mysum += number
average = mysum / nb_numbers
print(('mysum = {}\n'
'nb_numbers = {}\n'
'average = {}').format(mysum, nb_numbers, average))
def compute_stat(path):
nb_numbers = 0
mysum = 0.
with open(path) as f:
for line in f:
if line.strip().startswith('#'):
continue
if '#' in line:
line = line.split()[0]
number = float(line)
nb_numbers += 1
mysum += number
average = mysum / nb_numbers
return mysum, nb_numbers, average
if __name__ == '__main__':
fmt = 'data/file{}.txt'
files = [fmt.format(s) for s in ['1', '1.1', '1.2', '2', '2.1']]
results = []
sum_tot = 0.
nb_values_tot = 0
for path in files:
mysum, nb_numbers, average = compute_stat(path)
print(('file {}\n'.format(path) +
'mysum = {}\n'
'nb_numbers = {}\n'
'average = {}\n' + 16 * '-').format(
mysum, nb_numbers, average))
results.append((mysum, nb_numbers, average))
sum_tot += mysum
nb_values_tot += nb_numbers
average = sum_tot / nb_values_tot
print(('sum_tot = {}\n'
'nb_values_tot = {}\n'
'average = {}').format(sum_tot, nb_values_tot, average))
path = 'data/file_mut_cols.txt'
path = 'data/file_mut_cols_with_error.txt'
startings = ['p{}='.format(i) for i in range(1, 4)]
with open(path) as f:
for line in f:
words = line.split()
if len(words) != 3:
print(line)
if any([not word.startswith(start) for word, start in
zip(words, startings)]):
print(line)
#!/usr/bin/env python3
"""Computes basic statistics on file that contains a set of lines, each line
containing one float.
"""
file_name = '../data/file0.1.txt'
sum = 0.0
number = 0
with open(file_name) as handle:
for line in handle:
elem = float(line)
sum = sum + elem
number += 1
handle.close()
# not formatted output
# print('nb={}, sum={}, avg={}'.format(number, sum, sum/float(number)))
# formatted output
print('file = "{}"\nnb = {}; sum = {:.2f}; avg = {:.2f}'.format(
file_name, number, sum, sum/float(number)))
#!/usr/bin/python3
""" computes basic statistics on file
that contains a set of lines, each line containing
one float
"""
def compute_stats(file_name):
"""
computes the statistics of data in file_name
:param file_name: the name of the file to process
:type file_name: str
:return: the statistics
:rtype: a tuple (number, sum, average)
"""
total_sum = 0.0
number = 0
with open(file_name) as handle:
for line in handle:
elem = float(line)
total_sum += elem
number += 1
return (number, total_sum, total_sum/float(number))
file_names = ['../data/file0.{}.txt'.format(x) for x in range(1, 4)]
# equivalent to
# file_names = ["../data/file0.1.txt", "../data/file0.2.txt",
# "../data/file0.3.txt"]
numbers = []
sums = []
for file_name in file_names:
(local_number, local_sum, local_avg) = compute_stats(file_name)
numbers.append(local_number)
sums.append(local_sum)
for (file_name, local_number, local_sum) in zip(file_names, numbers, sums):
print('file = "{}"\nnb = {:<5}; sum = {:<7,.2f}; avg = {:<5,.2f}'.format(
file_name, local_number, local_sum,
local_number/local_sum))
all_sum = sum(sums)
all_numbers = sum(numbers)
print('# total over all files:\n'
'nb = {: <5}; sum = {:<7,.2f}; avg = {:.2f}'.format(
all_numbers, all_sum, all_sum/all_numbers))
#!/usr/bin/env python3
"""Computes basic statistics on file that contains a set of lines, each line
containing one float.
"""
file_name = '../data/file_with_comment_col0.txt'
total = 0.0
number = 0
with open(file_name) as handle:
for line in handle:
if line.startswith('#'):
continue
elem = float(line)
total = total + elem
number += 1
print('file = ' + file_name +
'\nnb = {}; total = {:.2f}; avg = {:.2f}'.format(
number, total, total/number))
#!/usr/bin/env python3
"""Computes basic statistics on file that contains a set of lines, each line
containing one float and possibly some comments in the middle of the line.
"""
def compute_stats(file_name, comment_sign):
"""
computes the statistics of data in file_name
:param file_name: the name of the file to process
:type file_name: str
:param comment_sign: the character that starts a comment
:type comment_sign: str
:return: the statistics
:rtype: a tuple (number, sum, average)
"""
total_sum = 0.0
number = 0
with open(file_name) as handle:
for line in handle:
if line.startswith(comment_sign):
continue
if comment_sign in line:
idx = line.index(comment_sign)
line = line[:idx]
elem = float(line)
total_sum += elem
number += 1
return (number, total_sum, total_sum/float(number))
file_names = ['../data/file_with_comment_col0.txt',
'../data/file_with_comment_anywhere.txt']
numbers = []
sums = []
comment_sign = '#'
for file_name in file_names:
(local_number, local_sum, local_avg) = compute_stats(
file_name, comment_sign)
numbers.append(local_number)
sums.append(local_sum)
for (file_name, local_number, local_sum) in zip(file_names, numbers, sums):
print('file = "{}"\nnb = {:<5}; sum = {:<7,.2f}; avg = {:<5,.2f}'.format(
file_name, local_number, local_sum,
local_sum/local_number))
all_sum = sum(sums)
all_numbers = sum(numbers)
print('# total over all files:\n'
'nb = {: <5}; sum = {:<7,.2f}; avg = {:.2f}'.format(
all_numbers, all_sum, all_sum/all_numbers))
#!/usr/bin/env python3
"""Checking file content (supposely 3 fields, p1, p2, p3, each field of the
form key=val where key is p1, p2 or p3, and val is a float.
"""
def compute_stats(file_name):
"""
computes the statistics of data in file_name
:param file_name: the name of the file to process
:type file_name: str
:return: the statistics
:rtype: a tuple (number, sum, average)
"""
total_sum = 0.0
number = 0
with open(file_name) as handle:
for line in handle:
if line.startswith('#'):
continue
elem = float(line)
total_sum += elem
number += 1
return (number, total_sum, total_sum/float(number))
def check_format(file_name, cols_names_set):
"""
check the file has the right format.
:param file_name: the input file name
:type file_name: str
:param cols_names_set: the columns names we want to have
:type cols_names_set: set of str
"""
print('checking ' + file_name)
nb_fields = len(cols_names_set)
with open(file_name) as handle:
for line_num, line in enumerate(handle):
cols = line.split()
if len(cols) != nb_fields:
print('line {} contains only {} fields, expecting {}'.format(
line_num, len(cols), nb_fields))
continue
# extracting field name
field_names = set()
for col in cols:
if '=' not in col:
print('line {}: malformed column ({})'.format(
file_name, line_num, col))
continue
field_name = col.split('=')[0]
field_names.add(field_name)
if cols_names_set != field_names:
print(('line {}: keys do not match the required keys: '
'problem with keys {}').format(
line_num, cols_names_set ^ field_names))
cols_names_set = set(['p1', 'p2', 'p3'])
file_names = ['../data/file_mut_cols.txt',
'../data/file_mut_cols_with_error.txt']
numbers = []
sums = []
for file_name in file_names:
check_format(file_name, cols_names_set)
0.749739390056
0.347626552025
0.740166747679
0.105528742289
0.514017265932
0.777196140874
0.244154100432
0.70686469809
0.9278927349
0.795964624713
0.787921978071
0.696655160744
0.0262847941964
0.426135084021
0.814540244536
0.156173660862
0.261795938353
0.121138386197
0.882756951304
0.7369203022
0.442108647458
0.889828438468
0.290251703398
0.0672396793052
0.213607026802
0.559487725377
0.950851913897
0.772352852024
0.460635012144
0.448260379747
0.245446104808
0.586631854122
0.968550457219
0.149144882368
0.93282162864
0.287646504848
0.25250020643
0.359194670006
0.715283625284
0.949063069467
0.845198763675
0.195332718659
0.814923807023
0.809591360844
0.231370303037
0.570731329761
0.45224522962
0.365671693715
0.689844391511
0.501323632264
0.108390040052
0.319040099028
0.978652223832
0.86684762829
0.318909185436
0.964246329084
0.608449691551
0.464748860159
0.360523117984
0.762683628131
0.973876480185
0.726279144019
0.524912147672
0.00962600227806
0.625007634023
0.81008081404
0.765935399282
0.593366031443
0.703968524441
0.134378044552
0.310412621083
0.539254545906
0.982463903378
0.61965397145
0.541839006003
0.751269471613
0.134170268304
0.12313967727
0.749739390056
0.347626552025
0.740166747679
0.105528742289
0.514017265932
0.777196140874
0.244154100432
0.70686469809
0.9278927349
0.795964624713
0.787921978071
0.696655160744
0.0262847941964
0.426135084021
0.814540244536
0.156173660862
0.261795938353
0.121138386197
0.882756951304
0.7369203022
0.442108647458
0.889828438468
0.290251703398
0.0672396793052
0.213607026802
0.559487725377
0.950851913897
0.772352852024
0.460635012144
0.448260379747
0.245446104808
0.586631854122
0.968550457219
0.149144882368
0.93282162864
0.287646504848
0.25250020643
0.359194670006
0.715283625284
0.949063069467
0.845198763675
0.195332718659
0.814923807023
0.809591360844
0.231370303037
0.570731329761
0.45224522962
0.365671693715
0.689844391511
0.501323632264
0.108390040052
0.319040099028
0.978652223832
0.86684762829
0.318909185436
0.964246329084
0.608449691551
0.464748860159
0.360523117984
0.762683628131
0.973876480185
0.726279144019
0.524912147672
0.00962600227806
0.625007634023
0.81008081404
0.765935399282
0.593366031443
0.703968524441
0.134378044552
0.310412621083
0.539254545906
0.982463903378
0.61965397145
0.541839006003
0.751269471613
0.134170268304
0.12313967727
0.0271840122713
0.813374254764
0.976360237712
0.565029214423
0.226489213205
0.0338239864498
0.0367619633357
0.135955620712
0.541077758584
0.436784622177
0.339261177382
0.925092462111
0.155029954633
0.905870092632
0.89368486098
0.109243138868
0.503533967861
0.159188258887
0.646291152608
0.963486695047
0.978481152266
0.457206846344
\ No newline at end of file
0.749739390056
0.347626552025
0.740166747679
0.105528742289
0.514017265932
0.777196140874
0.244154100432
0.70686469809
0.9278927349
0.795964624713
0.787921978071
0.696655160744
0.0262847941964
0.426135084021
0.814540244536
0.156173660862
0.261795938353
0.121138386197
0.882756951304
0.7369203022
0.442108647458
0.889828438468
0.290251703398
0.0672396793052
0.213607026802
p1=0.777196140874 p2=0.749739390056 p3=0.777196140874
p1=0.740166747679 p2=0.347626552025 p3=0.442108647458
p1=0.426135084021 p2=0.740166747679 p3=0.882756951304
p1=0.244154100432 p2=0.105528742289 p3=0.290251703398
p1=0.795964624713 p2=0.514017265932 p3=0.261795938353
p1=0.213607026802 p2=0.777196140874 p3=0.787921978071
p1=0.290251703398 p2=0.244154100432 p3=0.795964624713
p1=0.121138386197 p2=0.70686469809 p3=0.9278927349
p1=0.889828438468 p2=0.9278927349 p3=0.814540244536
p1=0.0262847941964 p2=0.795964624713 p3=0.7369203022
p1=0.9278927349 p2=0.787921978071 p3=0.426135084021
p1=0.156173660862 p2=0.696655160744 p3=0.749739390056
p1=0.0672396793052 p2=0.0262847941964 p3=0.0262847941964
p1=0.70686469809 p2=0.426135084021 p3=0.0672396793052
p1=0.749739390056 p2=0.814540244536 p3=0.156173660862
p1=0.347626552025 p2=0.156173660862 p3=0.889828438468
p1=0.7369203022 p2=0.261795938353 p3=0.696655160744
p1=0.105528742289 p2=0.121138386197 p3=0.70686469809
p1=0.514017265932 p2=0.882756951304 p3=0.347626552025
p1=0.442108647458 p2=0.7369203022 p3=0.121138386197
p1=0.814540244536 p2=0.442108647458 p3=0.740166747679
p1=0.696655160744 p2=0.889828438468 p3=0.514017265932
p1=0.882756951304 p2=0.290251703398 p3=0.105528742289
p1=0.261795938353 p2=0.0672396793052 p3=0.213607026802
p1=0.787921978071 p2=0.213607026802 p3=0.244154100432
p1=0.777196140874 p2=0.749739390056 p3=0.777196140874
p1=0.740166747679 p2=0.347626552025 p3=0.442108647458
p1=0.426135084021 p2=0.740166747679 p3=0.882756951304
p1=0.244154100432 p2=0.105528742289 p3=0.290251703398
p1=0.795964624713 p2=0.514017265932 p3=0.261795938353
p1=0.213607026802 p2=0.777196140874 p3=0.787921978071
p1=0.290251703398 p2=0.244154100432 p3=0.795964624713
p1=0.121138386197 p2=0.70686469809 p3=0.9278927349
p1=0.889828438468
p1=0.0262847941964 p2=0.795964624713 p3=0.7369203022
p1=0.9278927349 p2=0.787921978071 p3=0.426135084021
p1=0.156173660862 p2=0.696655160744 p3=0.749739390056
p1=0.0672396793052 p2=0.0262847941964 p3=0.0262847941964
p1=0.70686469809 p2=0.426135084021 p3=0.0672396793052
p1=0.749739390056 p2=0.814540244536 p3=0.156173660862
p2=0.156173660862 p3=0.889828438468
p1=0.7369203022 p2=0.261795938353 p3=0.696655160744
p1=0.105528742289 p2=0.121138386197 p3=0.70686469809
p1=0.514017265932 p2=0.882756951304 p3=0.347626552025
p1=0.442108647458 p2=0.7369203022 p3=0.121138386197
p1=0.814540244536 p1=0.442108647458 p3=0.740166747679
p1=0.696655160744 p2=0.889828438468 p3=0.514017265932
p1=0.882756951304 p2=0.290251703398 p3=0.105528742289
p1=0.261795938353 p2=0.0672396793052 p7=0.213607026802
p1=0.787921978071 p2=0.213607026802 p3=0.244154100432
# probability
0.749739390056
0.347626552025# weird event
0.740166747679
# 0.105528742289
# 0.514017265932
# 0.777196140874
# 0.244154100432# even more strange
0.70686469809
0.9278927349
0.795964624713
0.787921978071
0.696655160744
0.0262847941964
0.426135084021
0.814540244536
0.156173# gasp I'am puzzled097578
0.261795938353
0.121138386197
0.882756951304
0.7369203022
0.442108647458
0.889828438468
0.290251703398
0.0672396793052
0.213607026802
0.559487725377
0.950851913897
0.772352852024
0.460635012144
0.448260379747
0.245446104808
0.586631854122
0.968550457219
0.149144882368
0.93282162864
0.287646504848
0.25250020643
0.359194670006
0.715283625284
0.949063069467
0.845198763675
0.195332718659
0.814923807023
0.809591360844
0.231370303037
0.570731329761
0.45224522962
0.365671693715
0.689844391511
0.501323632264
0.108390040052
0.319040099028
0.978652223832
0.86684762829
0.318909185436
0.964246329084
0.608449691551
0.464748860159
0.360523117984
0.762683628131