A utility library for working with data pipelines on GCP
Project description
mlslib
A lightweight utility library to simplify working with Google Cloud Storage, BigQuery, and DataFrame evaluation on Google Cloud Platform (GCP). This library provides a set of high-level functions to streamline common data engineering, data science, and evaluation workflows.
🚀 Key Features
- Google Cloud Storage Integration: Upload pandas or Spark DataFrames to GCS
- File Management: Upload any local file (CSV, Parquet, Pickle, etc.) to GCS
- Public Access: Make GCS files public and get downloadable links
- BigQuery Integration: Query BigQuery tables directly into Spark DataFrames
- Notebook Display: Beautifully display PySpark DataFrames in Jupyter notebooks
- Data Sampling: Perform session-based sampling on pandas and Spark DataFrames
- Evaluation Utilities: Calculate MRR, save metrics, and display evaluation results
📦 Installation
Install mlslib directly from PyPI:
pip install mlslib
Dependencies
ipython>=7.0.0- For notebook display functionalitypyarrow>=6.0.0- For efficient data serializationpython-dateutil>=2.8.2
Note: Some functions require google-cloud-storage and pyspark to be installed in your environment.
🛠️ Setup
Before using mlslib, ensure you have:
- Google Cloud SDK installed and configured
- Authentication set up (service account key or gcloud auth)
- Required packages installed:
pip install google-cloud-storage pyspark
📖 Usage
Importing Key Functions
from mlslib import (
display_df, download_csv, load_bigquery_table_spark,
sample_by_session, upload_df_to_gcs, upload_df_to_gcs_csv,
calculate_mrr, save_metrics_to_json, display_mrr_comparison
)
Google Cloud Storage Utilities
from mlslib.gcs_utils import upload_file_to_gcs, upload_df_to_gcs
# ... see full usage in the API Reference below ...
BigQuery Utilities
from mlslib.bigquery_utils import load_bigquery_table_spark
Evaluation Utilities
Calculate Mean Reciprocal Rank (MRR)
from mlslib import calculate_mrr
results = calculate_mrr(
df=my_dataframe,
position_col="rank",
label_col="is_relevant",
group_by_cols=["store_id"]
)
print(results)
Save Metrics to JSON
from mlslib import save_metrics_to_json
save_metrics_to_json(results, "metrics.json")
Display MRR Comparison
from mlslib import display_mrr_comparison
# results_list = [results1, results2, ...]
display_mrr_comparison(results_list)
🗂️ Project Structure
mlslib/
├── __init__.py # Package initialization and exports
├── gcs_utils.py # Google Cloud Storage utilities
├── bigquery_utils.py # BigQuery integration utilities
├── display_utils.py # Notebook display utilities
├── sampling_utils.py # Data sampling utilities
├── date_utils.py # Date range utilities
├── evaluate_utils.py # Evaluation utilities (MRR, metrics)
📚 API Reference
gcs_utils
upload_file_to_gcs(file_path, bucket_name, gcs_path)upload_df_to_gcs(df, bucket_name, gcs_path, format='parquet')download_csv(bucket_name, file_path)
bigquery_utils
load_bigquery_table_spark(spark, sql_query, table_name, project_id, dataset_id)
display_utils
display_df(df, limit_rows=50, title=None)
sampling_utils
sample_by_session(df, session_column, fraction, seed=None)
date_utils
generate_periodic_date_ranges(start_date_str, num_periods, period_days)get_relative_day_range(days, offset_days=0)
evaluate_utils
calculate_mrr(df, position_col, label_col, group_by_cols=None)save_metrics_to_json(metrics, output_path)display_mrr_comparison(results_list, model_col='Model', test_set_col='Test Set')
🤝 Contributing
Contributions are welcome! Please open issues or submit pull requests for bug fixes, improvements, or new features.
📄 License
This project is licensed under the MIT License. See the LICENSE file for details.
📢 Contact
Author: Raj Jha
Email: rjha4@wayfair.com
🚀 Publishing to PyPI
- Ensure your version is updated in
setup.py - Build the package:
python -m pip install --upgrade build python -m build
- Upload to PyPI:
python -m pip install --upgrade twine twine upload dist/*
- (Optional) Test on TestPyPI first:
twine upload --repository testpypi dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mlslib-0.1.12.tar.gz.
File metadata
- Download URL: mlslib-0.1.12.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3033de65b2d5943e8f72589bd7f75bd4cdd64e8649d13c72c6b5651b8d85c5c4
|
|
| MD5 |
5d7bfcfc855fa8cdfa3486e1d0fb893f
|
|
| BLAKE2b-256 |
8e1df744ac982ad5f8789902765b690459863e9bc6da39ee631b33110aa0dbf0
|
File details
Details for the file mlslib-0.1.12-py3-none-any.whl.
File metadata
- Download URL: mlslib-0.1.12-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcdd192e240382618c3a25e1f61a8116da8acc37df498f1a7dd24d0e59910a30
|
|
| MD5 |
b4da1f5a03f0e9d42634ad5a35b4dd22
|
|
| BLAKE2b-256 |
067f8fb862f06bb3fd63b8ec1d9e52b6448e07990aeca3e29774134df46a5c56
|