#!/usr/bin/python3
# -*- coding: utf-8 -*-
########################################################################
#
# This file is part of python module <pyspc>.
# Copyright (C) 2013-2021 R. Marty
# (renaud.marty@developpement-durable.gouv.fr)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see COPYING.txt).
# If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
"""Bibliothèque pyspc du projet pyspc - IO - Ouahs - read."""
import itertools
import pandas as pnd
import warnings
import pyspc.core.exception as _exception
from pyspc.core.statistics import Stat, Stats
from pyspc.convention.ouahs import DATATYPES, VARNAMES
from pyspc.metadata.ouahs import Table
warnings.simplefilter(action='ignore', category=pnd.errors.PerformanceWarning)
[docs]
def read_ouahs(filenames=None, datatype=None):
"""
Créer une instance Series à partir d'un fichier Ouahs.
Parameters
----------
filename : list
Nom des fichiers Ouahs.
datatype : str
Type du fichier Ouahs.
Returns
-------
stats : pyspc.core.statistics.Stats
Résultats statistiques.
Examples
--------
>>> import os.path
>>> from pyspc.io.ouahs import read_ouahs
>>> dirname = os.path.join('data', 'metadata', 'ouahs')
CAS AVEC UN FICHIER DE RESULTAT STATISTIQUE ISSU DE OUAHS
>>> f = os.path.join(dirname, 'Backup_Adjustement.RData')
>>> stats = read_ouahs(filenames=[f], datatype='ouahs_stat')
>>> stats
*************************************
************ STATS ******************
*************************************
* NOM DE LA COLLECTION = Ouahs
* TYPE DE COLLECTION = ouahs_stat
* NOMBRE DE SERIES = 1
* ----------------------------------
* STAT #1 : S_1
*************************************
>>> stats['S_1']
*************************************
************* STAT ******************
*************************************
* NOM ECHANTILLON = S_1
* NOM VARIABLE SPC = QJ
* INTITULE VARIABLE = Débit moyen journalier
* IDENTIFIANT = K0100020
* FOURNISSEUR = Provider(name='Ouahs')
* NOM VARIABLE = QJ
* TAILLE ECHANTILLON = None
* PERIODE ECHANTILLON = None
* GRADEX PLUVIOMETRIQUE= None
* COUVERTURE INCERT. = None
* METHODE AJUSTEMENT = Gumbel
* AJUSTEMENT
return_period value value_low value_high
0 1.1 0.00000 0.00000 13.17144
1 1.2 18.24782 3.63748 31.94433
2 1.3 32.00772 17.59881 45.41755
3 1.4 42.81460 28.50686 55.91469
4 1.5 51.82882 36.91043 64.96064
5 1.6 59.61424 44.43157 73.22193
6 1.7 66.49394 50.83861 80.32410
7 1.8 72.67292 56.56472 87.20899
8 1.9 78.29068 61.85467 93.08357
9 2.0 83.44709 66.16420 98.26254
10 3.0 120.25865 98.61123 138.67322
11 4.0 143.81846 118.44689 165.93560
12 5.0 161.25879 132.93820 185.95424
13 6.0 175.12941 145.17961 201.86777
14 7.0 186.65200 154.88838 215.33289
15 8.0 196.50997 163.64381 226.45120
16 9.0 205.12524 170.98585 236.58701
17 10.0 212.77690 177.53681 245.65868
18 20.0 262.19431 219.19383 302.27849
19 30.0 290.62293 242.25619 335.52086
20 40.0 310.66598 258.42250 358.60561
21 50.0 326.16008 270.88499 376.22585
22 60.0 338.79296 281.57236 390.78737
23 70.0 349.45846 290.78498 403.24847
24 80.0 358.68761 298.34703 413.79374
25 90.0 366.82176 304.80427 423.19963
26 100.0 374.09342 310.67489 431.57380
27 200.0 421.85186 350.43546 488.02742
28 300.0 449.74514 373.44946 520.58106
29 400.0 469.52368 390.12992 543.71074
30 500.0 484.86006 402.37899 561.68505
31 600.0 497.38820 412.89128 576.38754
32 700.0 507.97909 421.50845 588.41549
33 800.0 517.15238 428.99293 599.11256
34 900.0 525.24316 435.59418 608.54730
35 1000.0 532.48016 441.49884 616.98643
*************************************
"""
# -------------------------------------------------------------------------
# 0- Contrôles
# -------------------------------------------------------------------------
_exception.check_listlike(filenames)
_exception.raise_valueerror(
datatype not in DATATYPES,
f"Type de données Ouahs '{datatype}' incorrect")
provider = 'Ouahs'
# -------------------------------------------------------------------------
# 1.2- Conversion - DONNEES - STATISTIQUES
# -------------------------------------------------------------------------
if datatype == 'ouahs_stat':
return _ouahs_stat(filenames, provider)
# -------------------------------------------------------------------------
# X- Conversion - CAS inconnu
# -------------------------------------------------------------------------
raise NotImplementedError
def _ouahs_stat(filenames, provider):
"""Conversion - META-DONNEES - ECHANTILLON."""
# Initialisation
stats = Stats(datatype='ouahs_stat', name=provider)
for filename in filenames:
reader = Table(filename=filename)
samples, dists, data = reader.readr()
sample_ids = list(data['Echantillon'].unique())
dist_ids = list(data['Distribution'].unique())
data = data.set_index(['Echantillon', 'Distribution'])
for si, di in itertools.product(sample_ids, dist_ids):
try:
varname = VARNAMES[samples.loc[si, 'Varname']]
except KeyError:
varname = 'QI'
name = samples.loc[si, 'Generic_names']
code = samples.loc[si, 'Files'].split('_')[0]
df = data.xs((si, di))
stat = Stat.from_records(
values=list(df['q']),
return_periods=list(df['T']),
values_low=list(df['IC.low']),
values_high=list(df['IC.high']),
method=dists[di-1],
name=name, code=code, varname=varname, provider=provider
)
stats.add(stat=stat)
return stats