Metadata-Version: 2.4
Name: rankflow
Version: 0.2.0
Summary: Visualize and evaluate rank evolution across retrieval steps - rankflow plots, retrieval metrics, and pipeline comparison for RAG.
Project-URL: Homepage, https://github.com/izikeros/rankflow
Project-URL: Documentation, https://izikeros.github.io/rankflow
Project-URL: Repository, https://github.com/izikeros/rankflow
Project-URL: Bug Tracker, https://github.com/izikeros/rankflow/issues
Project-URL: Changelog, https://github.com/izikeros/rankflow/blob/main/CHANGELOG.md
Author-email: Krystian Safjan <ksafjan@gmail.com>
License: Copyright (c) 2024 Krystian Safjan
        
        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:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        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.
License-File: LICENSE
Keywords: RAG,evaluation,flow,plot,rag-eval,rag-evaluation,rank,rankflow,retriever,visualization
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: matplotlib>=3.9.0
Requires-Dist: numpy>=1.26.4
Provides-Extra: all
Requires-Dist: pandas>=2.0; extra == 'all'
Requires-Dist: plotly>=5.0; extra == 'all'
Provides-Extra: interactive
Requires-Dist: plotly>=5.0; extra == 'interactive'
Provides-Extra: pandas
Requires-Dist: pandas>=2.0; extra == 'pandas'
Provides-Extra: ragas
Requires-Dist: ragas>=0.1; extra == 'ragas'
Provides-Extra: ranx
Requires-Dist: ranx>=0.3; extra == 'ranx'
Description-Content-Type: text/markdown

# RankFlow

![pypi version badge](https://img.shields.io/pypi/v/rankflow.svg)
![python version badge](https://img.shields.io/pypi/pyversions/rankflow.svg)
![license badge](https://img.shields.io/pypi/l/rankflow.svg)
![monthly downloads badge](https://img.shields.io/pypi/dm/rankflow.svg)

Library for plotting multiple ranks evolved over processing steps - drawing a rankflow.

![RankFlow](https://raw.githubusercontent.com/izikeros/rankflow/main/img/rankflow_crop.png)

RankFlow is a Python package that allows you to create rank flow plots (bump charts), helping visualize the changes in ranking of nodes.

Initially it was applied to re-ranking visualization of nodes (parts of documents, document chunks) during the retrieval and re-ranking processes within a Retrieval Augmented Generation (RAG) retriever, but the usage is not limited to RAG.

⭐️ Please star the repository if you find it useful.

## Installation

```bash
pip install rankflow
```

## Usage

### plot from pandas DataFrame

Start with creating [pandas](https://pandas.pydata.org/) DataFrame with ranks for each document at each step.

```python
import pandas as pd
import matplotlib.pyplot as plt
from rankflow import RankFlow

data = {"Doc 1": [2, 1, 3, 2], "Doc 2": [1, 2, 1, 3], "Doc 3": [3, 3, 2, 1]}
df = pd.DataFrame(data, index=["Step_1", "Step_2", "Step_3", "Step_4"])
```
This creates the following DataFrame:

![](https://raw.githubusercontent.com/izikeros/rankflow/main/img/dataframe.png)

**NOTE:** The rows of the DataFrame are the steps and the columns are the documents. The values are the ranks of the documents at each step. Remember to define proper column names and index values since they will be used as labels in the plot.

When the DataFrame is ready, then it is time to create RankFlow object and call `plot()` method.

```python
rf = RankFlow(df=df)
rf.plot()

# save the plot to png
plt.savefig("rankflow.png")

plt.show()
```
Here is the expected output:

![](https://raw.githubusercontent.com/izikeros/rankflow/main/img/rankflow_basic_pandas.png)

### plot from numpy array
You can also create RankFlow object without using pandas DataFrame. You can pass numpy array with ranks for each document at each step and provide labels for steps and documents.
```python
import matplotlib.pyplot as plt
from rankflow import RankFlow
import numpy as np

my_step_labels: list[str] = [
    "Hybrid Search",
    "Cross-encoder",
    "Graph-reranker",
    "Booster",
]
my_chunk_labels: list[str] = [
    "Doc 0",
    "Doc 1",
    "Doc 2",
    "Doc 3",
    "Doc 4",
    "Doc 5",
    "Doc 6",
    "Doc 7",
    "Doc 8",
    "Doc 9",
]
my_ranks = np.array(
    [
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        [3, 0, 2, 4, 1, 6, 7, 9, 5, 8],
        [2, 3, 0, 4, 6, 1, 7, 8, 5, 9],
        [5, 3, 2, 1, 0, 4, 6, 7, 8, 9],
    ]
)

rf = RankFlow(
    ranks=my_ranks,
    step_labels=my_step_labels,
    chunk_labels=my_chunk_labels,
    fig_size=(6, 6),
    title_font_size=24,
)
_ = rf.plot()
plt.show()
```

This should produce the following plot:

![RankFlow](https://raw.githubusercontent.com/izikeros/rankflow/main/img/rankflow.png)

## Further reading

There is and blog article describing usage of this package in RAG retriever: [RankFlow plot for retriever visual evaluation](https://safjan.com/rankflow-plot-for-retriever-visual-evaluation/) that might be helpful if you are wondering how to efficienty track rank changes in your retriever and finally visualize them.

## License

[MIT](LICENSE) © [Krystian Safjan](https://safjan.com/).
