framex 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
framex/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from framex.datasets import available, load
2
+
3
+ __all__ = ["available", "load"]
@@ -0,0 +1,14 @@
1
+ from framex._dicts._all import _DATASETS, _REMOTE_DATASETS
2
+ from framex._dicts._constants import _EXTENSION, _LOCAL_DIR, _REMOTE_DIR_GITHUB
3
+ from framex._dicts._github import _GITHUB_DATASETS
4
+ from framex._dicts._local import _LOCAL_CACHES
5
+
6
+ __all__ = [
7
+ "_EXTENSION",
8
+ "_LOCAL_DIR",
9
+ "_REMOTE_DIR_GITHUB",
10
+ "_GITHUB_DATASETS",
11
+ "_LOCAL_CACHES",
12
+ "_DATASETS",
13
+ "_REMOTE_DATASETS",
14
+ ]
framex/_dicts/_all.py ADDED
@@ -0,0 +1,8 @@
1
+ from framex._dicts._github import _GITHUB_DATASETS
2
+ from framex._dicts._local import _LOCAL_CACHES
3
+
4
+ # more will be added later
5
+
6
+ # merge dictionaries
7
+ _REMOTE_DATASETS = {**_GITHUB_DATASETS}
8
+ _DATASETS = {**_LOCAL_CACHES, **_REMOTE_DATASETS}
@@ -0,0 +1,5 @@
1
+ from pathlib import Path
2
+
3
+ _LOCAL_DIR = Path(__file__).parent.parent / "data"
4
+ _REMOTE_DIR_GITHUB = "https://github.com/Zaf4/datasets/raw/main/feather/"
5
+ _EXTENSION = ".feather"
@@ -0,0 +1,38 @@
1
+ from __future__ import annotations
2
+
3
+ import requests
4
+
5
+ from framex._dicts._constants import _EXTENSION
6
+
7
+ # GitHub API URL for the directory contents
8
+ _API_URL = "https://api.github.com/repos/Zaf4/datasets/contents/feather"
9
+
10
+
11
+ def _get_names(api_url: str) -> dict[str, str]:
12
+ """
13
+ Get the names of the datasets from the GitHub API.
14
+
15
+ Parameters
16
+ ----------
17
+ api_url : str
18
+ The URL of the GitHub API.
19
+
20
+ Returns
21
+ -------
22
+ list of str
23
+ The names of the datasets.
24
+ """
25
+ response = requests.get(api_url)
26
+ files = response.json()
27
+
28
+ # Extract .feather files
29
+ datasets = {
30
+ file["name"].rstrip(_EXTENSION): file["download_url"]
31
+ for file in files
32
+ if file["name"].endswith(_EXTENSION)
33
+ }
34
+
35
+ return datasets
36
+
37
+
38
+ _GITHUB_DATASETS = _get_names(_API_URL)
@@ -0,0 +1,6 @@
1
+ from pathlib import Path
2
+
3
+ from framex._dicts._constants import _EXTENSION, _LOCAL_DIR
4
+
5
+ # find locally cached _EXTENSION files
6
+ _LOCAL_CACHES = {path.stem: path for path in Path(_LOCAL_DIR).glob(f"*{_EXTENSION}")}
@@ -0,0 +1,3 @@
1
+ from framex.datasets._core import available, load
2
+
3
+ __all__ = ["available", "load"]
@@ -0,0 +1,99 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import TYPE_CHECKING
5
+
6
+ from polars import read_ipc, scan_ipc
7
+
8
+ from framex._dicts import _DATASETS, _LOCAL_CACHES, _REMOTE_DATASETS
9
+ from framex._dicts._constants import _LOCAL_DIR
10
+
11
+ if TYPE_CHECKING:
12
+ from polars import DataFrame, LazyFrame
13
+
14
+
15
+ def load(
16
+ name: str, *, lazy: bool = False, check_local: bool = True, cache: bool = True
17
+ ) -> DataFrame | LazyFrame:
18
+ """
19
+ Loads dataset by with the given name if available.
20
+
21
+ Parameters
22
+ ----------
23
+ name : str
24
+ Name of the dataset to load.
25
+ lazy : bool, optional
26
+ Lazy loading,
27
+ Default is False
28
+
29
+ check_local : bool, optional
30
+ Whether to check if the dataset is available locally
31
+ Default is True
32
+
33
+ cache : bool, optional
34
+ Whether to cache the dataset locally.
35
+ Default is True
36
+
37
+ Returns
38
+ -------
39
+ polars.DataFrame or polars.LazyFrame
40
+ """
41
+ # select the function to load the dataset
42
+ loader = scan_ipc if lazy else read_ipc
43
+ # check if the dataset is available
44
+ if name not in _DATASETS:
45
+ msg = f"Dataset {name} not found."
46
+ raise ValueError(msg)
47
+
48
+ local_path = _LOCAL_CACHES.get(name)
49
+ # load the dataset
50
+ if check_local:
51
+ if Path(local_path).exists():
52
+ frame = loader(local_path)
53
+ else:
54
+ frame = loader(_DATASETS.get(name))
55
+ else:
56
+ frame = loader(_DATASETS.get(name))
57
+
58
+ # cache the dataset locally
59
+ if cache and not Path(local_path).exists():
60
+ if lazy:
61
+ frame.sink_ipc(local_path, compression="zstd")
62
+ else:
63
+ frame.write_ipc(local_path, compression="zstd")
64
+
65
+ return frame
66
+
67
+
68
+ def available(option: str | None = None) -> list[str]:
69
+ """
70
+ List available datasets.
71
+
72
+ Parameters
73
+ ----------
74
+ option : str, optional {"remote", "local"}
75
+ The option to list available datasets.
76
+ Default is None
77
+
78
+ Returns
79
+ -------
80
+ list of available datasets
81
+
82
+ Raises
83
+ ------
84
+ ValueError
85
+ If the option is not 'remote' or 'local'.
86
+
87
+ """
88
+ if option is None:
89
+ return {
90
+ "remote": list(_REMOTE_DATASETS.keys()),
91
+ "local": list(_LOCAL_CACHES.keys()),
92
+ }
93
+ elif option == "remote":
94
+ return {option: list(_REMOTE_DATASETS.keys())}
95
+ elif option == "local":
96
+ return {option: list(_LOCAL_CACHES.keys())}
97
+ else:
98
+ msg = "Invalid option. Please use either 'remote' or 'local'."
99
+ raise ValueError(msg)
@@ -0,0 +1,7 @@
1
+ Copyright 2024 Zafer Kosar
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ Metadata-Version: 2.1
2
+ Name: framex
3
+ Version: 0.1.0
4
+ Summary: dataframe operations
5
+ License: MIT
6
+ Author: Zaf4
7
+ Author-email: zafermolbio@gmail.com
8
+ Requires-Python: >=3.11,<4.0
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Requires-Dist: polars (>=1.5.0,<2.0.0)
14
+ Requires-Dist: requests (>=2.32.3,<3.0.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ # FRAMES
18
+
19
+ Getting __testing datasets__ made easy.
20
+ Built on top of polars.
21
+
22
+ ## Installation
23
+
24
+ to get started, install the library with:
25
+
26
+ ```shell
27
+ pip install framex
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ```python
33
+ import framex as fx
34
+ ```
35
+
36
+ ### Loading datasets
37
+
38
+ ```python
39
+ iris = fx.load("iris")
40
+ ```
41
+
42
+ which returns a polars __DataFrame__
43
+
44
+ ```python
45
+ iris = fx.load("iris", lazy=True)
46
+ ```
47
+
48
+ which returns a polars __LazyFrame__
49
+
50
+ Both these operations create local copies of the datasets
51
+ as by default ```cahce=True```.
52
+
53
+ ### Available datasets
54
+
55
+ To see the list of available datasets, run:
56
+
57
+ ```python
58
+ fx.available()
59
+ ```
60
+
61
+ ```python
62
+ {'remote': ['iris', 'mpg', 'netflix', 'starbucks', 'titanic'], 'local': ['titanic']}
63
+ ```
64
+
65
+ which returns a dictionary of both __locally__ and __remotely__ available datasets.
66
+
67
+ To see only __local__ or __remote__ datasets, run:
68
+
69
+ ```python
70
+ fx.available("local")
71
+ fx.available("remote")
72
+ ```
73
+
74
+ ```python
75
+ {'local': ['titanic']}
76
+ {'remote': ['iris', 'mpg', 'netflix', 'starbucks', 'titanic']}
77
+ ```
78
+
@@ -0,0 +1,12 @@
1
+ framex/__init__.py,sha256=rO73T5fJxhs0t6Qzc_WMgKQx3fj0Fd9cubHPEgqH6h0,77
2
+ framex/_dicts/__init__.py,sha256=3-mDSMDeUVURj1BDtY67Er7jaDo05NSnrQHMOSWq5KQ,400
3
+ framex/_dicts/_all.py,sha256=LjU72VHGfNvi7S1qNp402Vmf5thgU5OH_RnwjsHswm4,238
4
+ framex/_dicts/_constants.py,sha256=tdvnL2qOD41QbfELs18hYtDk9CUuO5YL7Wh7im7lhqE,175
5
+ framex/_dicts/_github.py,sha256=42Gn5ugBgFT2V87nTOBTtUfHjpgj2rcrGK_oDTeWlaI,814
6
+ framex/_dicts/_local.py,sha256=maqfttO_kE2fz78MS02_p1t7dNhorslPS7Xp2x-Fr34,211
7
+ framex/datasets/__init__.py,sha256=oTlZpDyeAnANn5mIZzrPN2vf9PNCOt_f6li-qQT-vuU,83
8
+ framex/datasets/_core.py,sha256=5N9vRrwl_7Po3nDXLW-FA3TczbRrIhDcKAgwsvIJ2D4,2531
9
+ framex-0.1.0.dist-info/LICENSE,sha256=_lLk_jL2x0SoWTOqFXAHyjiTsMi0qd2QWukZcpqC56w,1058
10
+ framex-0.1.0.dist-info/METADATA,sha256=Y4SkqzcCXuEf0s1yUaiREdrl8c74bONGSpQy0GaRsFc,1480
11
+ framex-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
12
+ framex-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.9.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any