Skip to content

Base functions

Class Get

The Get class contains functions that fetch immediate return values without requiring computational processing. It serves to retrieve parameters and other immediate data, leveraging the Parameters class for default settings and bounds in specific contexts.

Source code in wgrp/base_functions.py
 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
class Get:
    """
    Class `Get`

    The `Get` class contains functions that fetch immediate return values without requiring 
    computational processing.
    It serves to retrieve parameters and other immediate data, leveraging the 
    `Parameters` class for default settings and bounds in specific contexts.
    """

    def __init__(self):
        self.parameters = Parameters()

    def get_parameters(
        self,
        nSamples: int = 0,
        nInterventions: int = None,
        a: float = None,
        b: float = None,
        q: float = 0.5,
        propagations: list = None,
        reliabilities: list = None,
        previousVirtualAge: int = 0,
        interventionsTypes: list = None,
        formalism: str = 'RP',
        cumulativeFailureCount: int = None,
        timesPredictFailures: list = None,
        nIntervetionsReal: int = None,
    ) -> dict:
        """
        Function `get_parameters`

        The `get_parameters` method encapsulates methods for retrieving parameters related to 
        the work group model (`wgrp`).
        It utilizes an instance of the `Parameters` class to access default settings and 
        bounds for function minimization
        in the search for optimal parameters for the `wgrp`.

        Args:
            nSamples (int): Number of samples to be simulated in the inference process.
            nInterventions (int): Number of interventions.praf: parei
            a (float): Parameter a.
            b (float): Parameter b.
            q (float): Parameter q.
            propagations (list): List of propagations.
            reliabilities (list): List of reliabilities.
            previousVirtualAge (int): Previous virtual age.
            interventionsTypes (list): List of intervention types.
            formalism (str): Formalism type.
            cumulativeFailureCount (int): Cumulative failure count.
            timesPredictFailures (list): Times to predict failures.
            nIntervetionsReal (int): Number of real interventions.

        Returns:
            dict: A dictionary containing the parameters and their values.

        Examples:
            >>> params = Get().get_parameters(nSamples=10, a=0.1, b=0.2, q=0.6, formalism='RP')
            >>> params['nSamples']
            10
            >>> params['a']
            0.1
            >>> params['b']
            0.2
            >>> params['q']
            0.6
            >>> params['formalism']
            'RP'
            >>> params['bBounds']
            {'min': 1e-100, 'max': 5}
            >>> params['qBounds']
            {'min': 0, 'max': 1}
        """
        parameters = {
            'nSamples': nSamples,
            'nInterventions': nInterventions,
            'a': a,
            'b': b,
            'q': q,
            'propagations': propagations,
            'reliabilities': reliabilities,
            'previousVirtualAge': previousVirtualAge,
            'interventionsTypes': interventionsTypes,
            'formalism': formalism,
            'cumulativeFailureCount': cumulativeFailureCount,
            'timesPredictFailures': timesPredictFailures,
            'nIntervetionsReal': nIntervetionsReal,
            'bBounds': self.parameters.bBounds,  # Accesses the bounds for 'b' from Parameters
            'qBounds': self.parameters.qBounds,  # Accesses the bounds for 'q' from Parameters
        }

        return parameters

    @staticmethod
    def get_optimum(mle_objs, df) -> dict:
        """
        Return the best model found based on the minimum BIC score.

        Args:
            mle_objs (list or dict): List or dictionary of maximum likelihood estimation objects.
            df (pandas.DataFrame): DataFrame containing the BIC scores and formalism information.

        Returns:
            object: The maximum likelihood estimation object corresponding to the model with the lowest BIC score.

        Raises:
            ValueError: If `df` is empty or if the indices do not align properly.

        Examples:
            >>> mle_objs = [('model1'), ('model2'), ('model3')]
            >>> data = {'BIC': [100.5, 95.3, 110.2], 'Formalism': ['formalism1', 'formalism2', 'formalism3']}
            >>> df = pd.DataFrame(data)
            >>> best_model = Get().get_optimum(mle_objs, df)
            >>> best_model
            'model2'
        """

        # Identify the formalism of the model with the minimum BIC score
        idx_min_bic = df['BIC'].idxmin()
        best_formalism = df.loc[idx_min_bic, 'Formalism']

        # Generate a string identifier for the optimum model
        str_optimum = f'optimum_{best_formalism}'

        return mle_objs[idx_min_bic]

get_optimum(mle_objs, df) staticmethod

Return the best model found based on the minimum BIC score.

Parameters:

Name Type Description Default
mle_objs list or dict

List or dictionary of maximum likelihood estimation objects.

required
df DataFrame

DataFrame containing the BIC scores and formalism information.

required

Returns:

Name Type Description
object dict

The maximum likelihood estimation object corresponding to the model with the lowest BIC score.

Raises:

Type Description
ValueError

If df is empty or if the indices do not align properly.

Examples:

>>> mle_objs = [('model1'), ('model2'), ('model3')]
>>> data = {'BIC': [100.5, 95.3, 110.2], 'Formalism': ['formalism1', 'formalism2', 'formalism3']}
>>> df = pd.DataFrame(data)
>>> best_model = Get().get_optimum(mle_objs, df)
>>> best_model
'model2'
Source code in wgrp/base_functions.py
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
@staticmethod
def get_optimum(mle_objs, df) -> dict:
    """
    Return the best model found based on the minimum BIC score.

    Args:
        mle_objs (list or dict): List or dictionary of maximum likelihood estimation objects.
        df (pandas.DataFrame): DataFrame containing the BIC scores and formalism information.

    Returns:
        object: The maximum likelihood estimation object corresponding to the model with the lowest BIC score.

    Raises:
        ValueError: If `df` is empty or if the indices do not align properly.

    Examples:
        >>> mle_objs = [('model1'), ('model2'), ('model3')]
        >>> data = {'BIC': [100.5, 95.3, 110.2], 'Formalism': ['formalism1', 'formalism2', 'formalism3']}
        >>> df = pd.DataFrame(data)
        >>> best_model = Get().get_optimum(mle_objs, df)
        >>> best_model
        'model2'
    """

    # Identify the formalism of the model with the minimum BIC score
    idx_min_bic = df['BIC'].idxmin()
    best_formalism = df.loc[idx_min_bic, 'Formalism']

    # Generate a string identifier for the optimum model
    str_optimum = f'optimum_{best_formalism}'

    return mle_objs[idx_min_bic]

get_parameters(nSamples=0, nInterventions=None, a=None, b=None, q=0.5, propagations=None, reliabilities=None, previousVirtualAge=0, interventionsTypes=None, formalism='RP', cumulativeFailureCount=None, timesPredictFailures=None, nIntervetionsReal=None)

Function get_parameters

The get_parameters method encapsulates methods for retrieving parameters related to the work group model (wgrp). It utilizes an instance of the Parameters class to access default settings and bounds for function minimization in the search for optimal parameters for the wgrp.

Parameters:

Name Type Description Default
nSamples int

Number of samples to be simulated in the inference process.

0
nInterventions int

Number of interventions.praf: parei

None
a float

Parameter a.

None
b float

Parameter b.

None
q float

Parameter q.

0.5
propagations list

List of propagations.

None
reliabilities list

List of reliabilities.

None
previousVirtualAge int

Previous virtual age.

0
interventionsTypes list

List of intervention types.

None
formalism str

Formalism type.

'RP'
cumulativeFailureCount int

Cumulative failure count.

None
timesPredictFailures list

Times to predict failures.

None
nIntervetionsReal int

Number of real interventions.

None

Returns:

Name Type Description
dict dict

A dictionary containing the parameters and their values.

Examples:

>>> params = Get().get_parameters(nSamples=10, a=0.1, b=0.2, q=0.6, formalism='RP')
>>> params['nSamples']
10
>>> params['a']
0.1
>>> params['b']
0.2
>>> params['q']
0.6
>>> params['formalism']
'RP'
>>> params['bBounds']
{'min': 1e-100, 'max': 5}
>>> params['qBounds']
{'min': 0, 'max': 1}
Source code in wgrp/base_functions.py
 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
def get_parameters(
    self,
    nSamples: int = 0,
    nInterventions: int = None,
    a: float = None,
    b: float = None,
    q: float = 0.5,
    propagations: list = None,
    reliabilities: list = None,
    previousVirtualAge: int = 0,
    interventionsTypes: list = None,
    formalism: str = 'RP',
    cumulativeFailureCount: int = None,
    timesPredictFailures: list = None,
    nIntervetionsReal: int = None,
) -> dict:
    """
    Function `get_parameters`

    The `get_parameters` method encapsulates methods for retrieving parameters related to 
    the work group model (`wgrp`).
    It utilizes an instance of the `Parameters` class to access default settings and 
    bounds for function minimization
    in the search for optimal parameters for the `wgrp`.

    Args:
        nSamples (int): Number of samples to be simulated in the inference process.
        nInterventions (int): Number of interventions.praf: parei
        a (float): Parameter a.
        b (float): Parameter b.
        q (float): Parameter q.
        propagations (list): List of propagations.
        reliabilities (list): List of reliabilities.
        previousVirtualAge (int): Previous virtual age.
        interventionsTypes (list): List of intervention types.
        formalism (str): Formalism type.
        cumulativeFailureCount (int): Cumulative failure count.
        timesPredictFailures (list): Times to predict failures.
        nIntervetionsReal (int): Number of real interventions.

    Returns:
        dict: A dictionary containing the parameters and their values.

    Examples:
        >>> params = Get().get_parameters(nSamples=10, a=0.1, b=0.2, q=0.6, formalism='RP')
        >>> params['nSamples']
        10
        >>> params['a']
        0.1
        >>> params['b']
        0.2
        >>> params['q']
        0.6
        >>> params['formalism']
        'RP'
        >>> params['bBounds']
        {'min': 1e-100, 'max': 5}
        >>> params['qBounds']
        {'min': 0, 'max': 1}
    """
    parameters = {
        'nSamples': nSamples,
        'nInterventions': nInterventions,
        'a': a,
        'b': b,
        'q': q,
        'propagations': propagations,
        'reliabilities': reliabilities,
        'previousVirtualAge': previousVirtualAge,
        'interventionsTypes': interventionsTypes,
        'formalism': formalism,
        'cumulativeFailureCount': cumulativeFailureCount,
        'timesPredictFailures': timesPredictFailures,
        'nIntervetionsReal': nIntervetionsReal,
        'bBounds': self.parameters.bBounds,  # Accesses the bounds for 'b' from Parameters
        'qBounds': self.parameters.qBounds,  # Accesses the bounds for 'q' from Parameters
    }

    return parameters