Skip to content

examples

fetch_dataset(example, dataset_name=None)

Fetchs the dataset from our examples/data folder. Called by our examples notebooks.

Parameters

example : str The name of the example. Must be one of the following: "california_housing", "climate_change_survey", "wages", "titanic" dataset_name : str (optional) The name of the dataset if needed. For example: "X_train", "X_test", "y_train", "y_test"

Returns

pd.DataFrame A DataFrame corresponding to the dataset requested of the specified example.

Source code in src/antakia/utils/examples.py
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
def fetch_dataset(example: str, dataset_name: str = None) -> pd.DataFrame | dict[str:pd.DataFrame]:
    """
    Fetchs the dataset from our examples/data folder. Called by our examples notebooks.

    Parameters
    ----------
    example : str
        The name of the example. Must be one of the following: "california_housing", "climate_change_survey", "wages", "titanic"
    dataset_name : str (optional)
        The name of the dataset if needed. For example: "X_train", "X_test", "y_train", "y_test"

    Returns 
    -------
    pd.DataFrame
        A DataFrame corresponding to the dataset requested of the specified example.
    """
    if dataset_name:
        file_name = f'{example}-{dataset_name}.csv'
    elif AVAILABLE_EXAMPLES[example]:
        datasets = {}
        for name in AVAILABLE_EXAMPLES[example]:
            datasets[name] = fetch_dataset(example, name)
        return datasets
    else:
        file_name = f'{example}.csv'
    example_folder = files("antakia").joinpath("assets/examples/")
    location = str(example_folder.joinpath(f"{file_name}"))
    if os.path.exists(location):
        dataset = pd.read_csv(location)
        if 'Unnamed: 0' == dataset.columns[0]:
            dataset = dataset.set_index(dataset.columns[0])
            dataset.index.name=None
        return dataset
    else:
        if not os.path.exists(example_folder):
            os.mkdir(example_folder)
        dataset = _get_remote_dataset(example, dataset_name)
        dataset.to_csv(location, index=False)
        if 'Unnamed: 0' == dataset.columns[0]:
            dataset = dataset.set_index(dataset.columns[0])
            dataset.index.name = None
        return dataset