#!/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/>.
#
########################################################################
"""Méta-data - Statistiques OUAHS - Fichier résultat."""
import numpy as np
from pyspc.convention.ouahs import (
FILENAME_RESULT, TABLENAME_RESULT, TABLENAME_DISTS, TABLENAME_SAMPLES)
[docs]
class Table():
"""
Classe permettant la manipulation du fichier résultat de OUAHS.
Attributes
----------
filename : str
Nom du fichier csv OTAMIN v2018 (Calage)
"""
[docs]
def __init__(self, filename=None):
"""
Initialise l'instance de la classe Table (Rdata) de OUAHS.
Parameters
----------
filename : str
Nom du fichier Rdata de OUAHS
"""
if filename is None:
filename = FILENAME_RESULT
self.filename = filename
def __str__(self):
"""Afficher des méta-données de l'instance."""
text = """
*************************************
********* OUAHS - Table *************
*************************************
* NOM FICHIER = {filename}
*************************************
"""
return text.format(**vars(self))
[docs]
def readr(self):
"""
Lire un fichier Rdata de OUAHS.
Returns
-------
samples : pnd.DataFrame
DataFrame des échantillons (fichiers sources, grandeurs)
dists : list
Distributions
data : pnd.DataFrame
DataFrame des données
Notes
-----
- La colonne 'Echantillon' de ``data`` peut être remplie par
``samples``.
- La colonne 'Distribution' de ``data`` peut être remplie par
``dists``. Voir l'avertissement ci-dessous.
Warnings
--------
La correspondance de la colonne 'Distribution' avec la liste ``dists``
n'est pas garantie. Dans la version 2.9, rpy2 donnait le nom de la
distribution. Dans la version 3.6, il s'agirait, sans véritable
certitude, de l'indice dans la liste ``dists``, avec un tri par ordre
alphabétique.
Examples
--------
>>> from pyspc.metadata.ouahs import Table
>>> f = 'data/metadata/ouahs/Backup_Adjustement.RData'
>>> d = Table(filename=f)
>>> print(d)
*************************************
***** OTAMIN 2018 - Table ***********
*************************************
* NOM FICHIER = data/metadata/ouahs/Backup_Adjustement.RData
*************************************
>>> samples, dists, data = d.readr()
>>> samples
Files_path Files Generic_names Varname
1 ./in/K0100020_QJ-X_seasons-ouahs.csv K0100020_QJ-X_seasons-ouahs.csv S_1 QJ-X
>>> dists
['GEV', 'Gumbel']
>>> data
Echantillon Distribution T q IC.low IC.high
1 1 2 1.1 0.00000 0.00000 13.17144
2 1 2 1.2 18.24782 3.63748 31.94433
3 1 2 1.3 32.00772 17.59881 45.41755
4 1 2 1.4 42.81460 28.50686 55.91469
5 1 2 1.5 51.82882 36.91043 64.96064
.. ... ... ... ... ... ...
68 1 1 600.0 885.39099 435.76069 1629.19587
69 1 1 700.0 927.62575 445.55646 1738.30187
70 1 1 800.0 965.60817 455.53757 1842.88087
71 1 1 900.0 1000.22639 463.43530 1943.78240
72 1 1 1000.0 1032.10735 470.55448 2043.41567
[72 rows x 6 columns]
"""
from pyspc.binutils.captured_output import captured_output
with captured_output():
from rpy2.robjects import r
from rpy2.robjects import pandas2ri
r.load(self.filename)
samples = pandas2ri.rpy2py_dataframe(r[TABLENAME_SAMPLES])
samples['Varname'] = samples['Files'].apply(
lambda x: x.split('_')[1])
samples.index = samples.index.astype(int)
dists = sorted(
[str(x) for x in np.array(r[TABLENAME_DISTS])])
data = pandas2ri.rpy2py_dataframe(r[TABLENAME_RESULT])
data.index = data.index.astype(int)
return samples, dists, data
[docs]
def write(self, data=None):
"""
Ecrire un fichier Rdata de OUAHS.
Raises
------
NotImplementedError
"""
raise NotImplementedError