Skip to content

Base variable

Base variable

HidroCLVariable

A class to hold information about a hidrocl variable

Examples:

>>> from hidrocl import HidroCLVariable
>>> variable = HidroCLVariable('precipitation', 'precipitation.csv', 'precipitation_pc.csv')
>>> variable
Variable: precipitation. Records: 0

Attributes:

Name Type Description
name str

Name of the variable

database str

Path to the database

pcdatabase str

Path to the database with pixel count

indatabase list

List of IDs in the database

observations pandas.DataFrame

Dataframe with the observations

pcobservations pandas.DataFrame

Dataframe with the pixel count

catchment_names list

List of catchment names

Source code in hidrocl/variables/__init__.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
class HidroCLVariable:
    """A class to hold information about a hidrocl variable

    Examples:
        >>> from hidrocl import HidroCLVariable
        >>> variable = HidroCLVariable('precipitation', 'precipitation.csv', 'precipitation_pc.csv')
        >>> variable
        Variable: precipitation. Records: 0

    Attributes:
        name (str): Name of the variable
        database (str): Path to the database
        pcdatabase (str): Path to the database with pixel count
        indatabase (list): List of IDs in the database
        observations (pandas.DataFrame): Dataframe with the observations
        pcobservations (pandas.DataFrame): Dataframe with the pixel count
        catchment_names (list): List of catchment names
    """

    def __init__(self, name, database, pcdatabase):
        """
        Args:
            name (str): Name of the variable
            database (str): Path to the database
            pcdatabase (str): Path to the database with pixel count
        """
        self.name = name
        self.database = database
        self.pcdatabase = pcdatabase
        self.indatabase = ''
        self.observations = None
        self.pcobservations = None
        self.catchment_names = None
        self.checkdatabase()
        self.checkpcdatabase()

    def __repr__(self):
        """
        Representation of the object

        Returns:
             str: Representation of the object
        """
        return f'Variable: {self.name}. Records: {len(self.indatabase)}'

    def __str__(self):
        """
        String representation of the object

        Returns:
            str: String representation of the object
        """
        return f'''
Variable {self.name}.
Records: {len(self.indatabase)}.
Database path: {self.database}.
Pixel count database path: {self.pcdatabase}.
        '''

    def checkindatabase(self):
        """
        Check IDs in database

        Returns:
            list: List of IDs in the database
        """
        if self.observations is None:
            print('Please, check the database for getting the IDs processed')
            return ''
        else:
            return [str(i) for i in self.observations[self.observations.columns[0]].values.tolist()]

    def checkdatabase(self):
        """
        Check database

        Returns:
            pandas.DataFrame: Dataframe with the observations
        """
        self.observations = methods.checkdatabase(self.database, self.catchment_names)
        self.indatabase = self.checkindatabase()
        try:
            self.catchment_names = self.observations.columns[1:].tolist()
        except AttributeError:
            print('Could not load dataframe, perhaps the database has not been created yet')

    def checkpcdatabase(self):
        """
        Check database with pixel count

        Returns:
            pandas.DataFrame: Dataframe with the pixel count
        """
        self.pcobservations = methods.checkdatabase(self.pcdatabase, self.catchment_names)

    def add_catchment_names(self, catchment_names_list):
        """
        Add catchment names to the variable using cathment_names from database

        Args:
            catchment_names_list (list): list of catchment names

        Returns:
            None
        """
        if self.catchment_names is None:
            if catchment_names_list is not None:
                self.catchment_names = catchment_names_list
                print('Catchment names added. I recommend you to check the database')
            else:
                print("Catchments names can't be None type")
        else:
            print('Catchment names already added!')

    def valid_data(self):
        """
        Return valid data for all catchments

        Returns:
            list: list with valid data with date index
        """
        return self.observations.notnull().sum()[1:]

    def plot_valid_data_all(self):
        """
        Plot valid data for all catchments

        Returns:
            plot: plot with valid data for all catchments with date index
        """
        df = self.observations.drop(self.observations.columns[0], axis=1).notnull().sum().divide(
            len(self.observations.index)).multiply(100)
        ax = df.plot(title='Valid observations by catchment', ylim=(0, 105), color='lightseagreen')
        ax.yaxis.set_major_formatter(mtick.PercentFormatter())
        plt.show()

    def plot_grid_data_all(self):
        """
        Plot valid data for all catchments in grid format

        Returns:
            plot: plot with valid data for all catchments with date index
        """
        methods.plot_variable_all(self.observations, self.catchment_names, self.database, what='obs')

    def plot_grid_pcdata_all(self):
        """
        Plot valid data for all catchments in grid format

        Returns:
            plot: plot with valid data for all catchments with date index
        """
        methods.plot_variable_all(self.pcobservations, self.catchment_names, self.database, what='pc')

    def plot_valid_data_individual(self, catchment):
        """
        Plot valid data for individual catchments

        Args:
            catchment (str): catchment (catchment name) or int (catchment index)

        Returns:
            plot: plot with valid data for individual catchments with date index
        """

        catchment = methods.get_catchment_name(catchment, self.catchment_names)
        methods.plot_variable(catchment, self.observations, what='valid')

    def plot_pixel_count(self, catchment):
        """
        Plot pixel count for individual catchments

        Args:
            catchment (str): catchment (catchment name) or int (catchment index)

        Returns:
            plot: plot with pixel count for individual catchments with date index
        """

        catchment = methods.get_catchment_name(catchment, self.catchment_names)
        methods.plot_variable(catchment, self.pcobservations, what='count')

__init__(name, database, pcdatabase)

Parameters:

Name Type Description Default
name str

Name of the variable

required
database str

Path to the database

required
pcdatabase str

Path to the database with pixel count

required
Source code in hidrocl/variables/__init__.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(self, name, database, pcdatabase):
    """
    Args:
        name (str): Name of the variable
        database (str): Path to the database
        pcdatabase (str): Path to the database with pixel count
    """
    self.name = name
    self.database = database
    self.pcdatabase = pcdatabase
    self.indatabase = ''
    self.observations = None
    self.pcobservations = None
    self.catchment_names = None
    self.checkdatabase()
    self.checkpcdatabase()

__repr__()

Representation of the object

Returns:

Name Type Description
str

Representation of the object

Source code in hidrocl/variables/__init__.py
44
45
46
47
48
49
50
51
def __repr__(self):
    """
    Representation of the object

    Returns:
         str: Representation of the object
    """
    return f'Variable: {self.name}. Records: {len(self.indatabase)}'

__str__()

String representation of the object

Returns:

Name Type Description
str

String representation of the object

Source code in hidrocl/variables/__init__.py
53
54
55
56
57
58
59
60
61
62
63
64
65
    def __str__(self):
        """
        String representation of the object

        Returns:
            str: String representation of the object
        """
        return f'''
Variable {self.name}.
Records: {len(self.indatabase)}.
Database path: {self.database}.
Pixel count database path: {self.pcdatabase}.
        '''

add_catchment_names(catchment_names_list)

Add catchment names to the variable using cathment_names from database

Parameters:

Name Type Description Default
catchment_names_list list

list of catchment names

required

Returns:

Type Description

None

Source code in hidrocl/variables/__init__.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def add_catchment_names(self, catchment_names_list):
    """
    Add catchment names to the variable using cathment_names from database

    Args:
        catchment_names_list (list): list of catchment names

    Returns:
        None
    """
    if self.catchment_names is None:
        if catchment_names_list is not None:
            self.catchment_names = catchment_names_list
            print('Catchment names added. I recommend you to check the database')
        else:
            print("Catchments names can't be None type")
    else:
        print('Catchment names already added!')

checkdatabase()

Check database

Returns:

Type Description

pandas.DataFrame: Dataframe with the observations

Source code in hidrocl/variables/__init__.py
80
81
82
83
84
85
86
87
88
89
90
91
92
def checkdatabase(self):
    """
    Check database

    Returns:
        pandas.DataFrame: Dataframe with the observations
    """
    self.observations = methods.checkdatabase(self.database, self.catchment_names)
    self.indatabase = self.checkindatabase()
    try:
        self.catchment_names = self.observations.columns[1:].tolist()
    except AttributeError:
        print('Could not load dataframe, perhaps the database has not been created yet')

checkindatabase()

Check IDs in database

Returns:

Name Type Description
list

List of IDs in the database

Source code in hidrocl/variables/__init__.py
67
68
69
70
71
72
73
74
75
76
77
78
def checkindatabase(self):
    """
    Check IDs in database

    Returns:
        list: List of IDs in the database
    """
    if self.observations is None:
        print('Please, check the database for getting the IDs processed')
        return ''
    else:
        return [str(i) for i in self.observations[self.observations.columns[0]].values.tolist()]

checkpcdatabase()

Check database with pixel count

Returns:

Type Description

pandas.DataFrame: Dataframe with the pixel count

Source code in hidrocl/variables/__init__.py
 94
 95
 96
 97
 98
 99
100
101
def checkpcdatabase(self):
    """
    Check database with pixel count

    Returns:
        pandas.DataFrame: Dataframe with the pixel count
    """
    self.pcobservations = methods.checkdatabase(self.pcdatabase, self.catchment_names)

plot_grid_data_all()

Plot valid data for all catchments in grid format

Returns:

Name Type Description
plot

plot with valid data for all catchments with date index

Source code in hidrocl/variables/__init__.py
144
145
146
147
148
149
150
151
def plot_grid_data_all(self):
    """
    Plot valid data for all catchments in grid format

    Returns:
        plot: plot with valid data for all catchments with date index
    """
    methods.plot_variable_all(self.observations, self.catchment_names, self.database, what='obs')

plot_grid_pcdata_all()

Plot valid data for all catchments in grid format

Returns:

Name Type Description
plot

plot with valid data for all catchments with date index

Source code in hidrocl/variables/__init__.py
153
154
155
156
157
158
159
160
def plot_grid_pcdata_all(self):
    """
    Plot valid data for all catchments in grid format

    Returns:
        plot: plot with valid data for all catchments with date index
    """
    methods.plot_variable_all(self.pcobservations, self.catchment_names, self.database, what='pc')

plot_pixel_count(catchment)

Plot pixel count for individual catchments

Parameters:

Name Type Description Default
catchment str

catchment (catchment name) or int (catchment index)

required

Returns:

Name Type Description
plot

plot with pixel count for individual catchments with date index

Source code in hidrocl/variables/__init__.py
176
177
178
179
180
181
182
183
184
185
186
187
188
def plot_pixel_count(self, catchment):
    """
    Plot pixel count for individual catchments

    Args:
        catchment (str): catchment (catchment name) or int (catchment index)

    Returns:
        plot: plot with pixel count for individual catchments with date index
    """

    catchment = methods.get_catchment_name(catchment, self.catchment_names)
    methods.plot_variable(catchment, self.pcobservations, what='count')

plot_valid_data_all()

Plot valid data for all catchments

Returns:

Name Type Description
plot

plot with valid data for all catchments with date index

Source code in hidrocl/variables/__init__.py
131
132
133
134
135
136
137
138
139
140
141
142
def plot_valid_data_all(self):
    """
    Plot valid data for all catchments

    Returns:
        plot: plot with valid data for all catchments with date index
    """
    df = self.observations.drop(self.observations.columns[0], axis=1).notnull().sum().divide(
        len(self.observations.index)).multiply(100)
    ax = df.plot(title='Valid observations by catchment', ylim=(0, 105), color='lightseagreen')
    ax.yaxis.set_major_formatter(mtick.PercentFormatter())
    plt.show()

plot_valid_data_individual(catchment)

Plot valid data for individual catchments

Parameters:

Name Type Description Default
catchment str

catchment (catchment name) or int (catchment index)

required

Returns:

Name Type Description
plot

plot with valid data for individual catchments with date index

Source code in hidrocl/variables/__init__.py
162
163
164
165
166
167
168
169
170
171
172
173
174
def plot_valid_data_individual(self, catchment):
    """
    Plot valid data for individual catchments

    Args:
        catchment (str): catchment (catchment name) or int (catchment index)

    Returns:
        plot: plot with valid data for individual catchments with date index
    """

    catchment = methods.get_catchment_name(catchment, self.catchment_names)
    methods.plot_variable(catchment, self.observations, what='valid')

valid_data()

Return valid data for all catchments

Returns:

Name Type Description
list

list with valid data with date index

Source code in hidrocl/variables/__init__.py
122
123
124
125
126
127
128
129
def valid_data(self):
    """
    Return valid data for all catchments

    Returns:
        list: list with valid data with date index
    """
    return self.observations.notnull().sum()[1:]