semvec/pypi-packages huggingface dataset:
sourced 0.1.0
Tooling around mass-scale Python source code analysis.
License: MIT
Requires Python: >=3.9

# Sourced

Tooling around mass-scale Python source code analysis.

## Usage

Currently there are two datasets: `pypi-all` and `pypi-popular` although I highly recommend `pypi-popular` if you intend to keep your sample size low (the chance of getting far more relevant results with it higher compared to `pypi-all`).

You can check out any number of datasets with different sample sizes:
```
$ sourced datasets create \
    --source pypi-popular \
    --sample-size 10 \
    playground
```


By default it will download all then source code under `~/.cache/sourced/0.0.1/<name>` but it might be more pleasant to have a separate directory outside of your home:

```
$ sourced datasets create \
    --source pypi-popular \
    --sample-size 5000 \
    --base-data-dir /mnt/my-giant-disk/sourced-datasets \
    top-5000-packages
```

All these datasets are accessible through the CLI as long as those paths
exist:

```
$ sourced datasets list
playground /path/to/.cache/sourced/0.0.1/playground
top-5000-packages /path/to/my-giant-disk/sourced-datasets/top-5000-packages
```

## Running analyses on source code

As soon as you have a dataset checked out, you can run any analyses on
it with the tooling offered in this package. Here is a simple program
that parses every file in the dataset to find out what is the most common
name:

```py
from __future__ import annotations

import ast
import tokenize
from argparse import ArgumentParser
from collections import Counter

from sourced import Sourced


def most_common_name(file: str) -> dict[str, int]:
    usage: dict[str, int] = {}
    try:
        with tokenize.open(file) as stream:
            tree = ast.parse(stream.read())
    except BaseException as exc:
        return usage

    for node in ast.walk(tree):
        if isinstance(node, ast.Name):
            usage.setdefault(node.id, 0)
            usage[node.id] += 1
    return usage


def main():
    parser = ArgumentParser()
    parser.add_argument("dataset")

    options = parser.parse_args()
    sourced = Sourced()

    results = Counter()
    for result in sourced.run_on(options.dataset, most_common_name):
        results.update(result)

    for name, count in results.most_common(n=20):
        print(f"{name}: {count}")


if __name__ == "__main__":
    main()
```

```console
$ python examples/python_specific_source.py playground
Found 10 sources
Collected 959 files from 10 unique projects.
self: 24489
os: 1821
str: 1735
request: 1157
response: 1064
value: 1029
pytest: 984
mock: 966
name: 837
r: 770
isinstance: 715
len: 705
cmd: 701
client: 674
params: 672
path: 668
key: 659
pool: 623
int: 599
config: 553
```
tremetrics 0.2.0
Tremendous Metrics
License: MIT

# Tremetrics

[![PyPi](https://img.shields.io/pypi/v/tremetrics)](https://pypi.org/project/tremetrics/)

Tremendous Metrics.

## Installation

You can install Tremetrics from [PyPi](https://pypi.org/project/tremetrics/) using `pip`.

```
pip install tremetrics
```

## Usage

### ConfusionMatrix

```python
from tremetrics import ConfusionMatrix

y_true, y_pred = ...                            # Generate predictions
cm = ConfusionMatrix.from_pred(y_true, y_pred)  # Create a new confusion matrix object

print(cm)                                       # Print the confusion matrix
array_for_further_use = cm.array                # Get the matrix as a numpy array
print(cm.tp, cm.fn, cm.fp, cm.tn)               # Get the individual quadrant values

print(cm.get_latex_table(multirow=True))        # Get the matrix as code for a Latex table

print(cm.recall_score(average='micro'))         # Call any sklearn.metrics function using the data in the matrix
```

### ROCCurve

```python
from tremetrics import ROCCurve
import matplotlib.pyplot as plt

y_true, y_pred_prob = ...                       # Generate prediction probabilities

curve = ROCCurve.from_pred_prob(y_true, y_pred_prob)  # Create a new roc curve object
print(curve.roc_auc)                            # Print the area under the roc curve

curve.plot()                                    # plot the roc curve
plt.show()                                      # show the curve plot

# generating convex roc curves
curve = ROCCurve.from_pred_prob(y_true, y_pred_prob, convex=True)  # Create a convex roc curve object
print(curve.roc_auc)                            # Print the area under the roc curve

curve.plot()                                    # plot the roc curve
plt.show()                                      # show the curve plot

# static methods meant as sklearn drop-in replacement
fpr, tpr, thresholds = ROCCurve.roc_curve(y_true, y_pred_prob)
fpr, tpr, thresholds = ROCCurve.convex_roc_curve(y_true, y_pred_prob)

# exploiting pyplot state to draw both standard and convex roc curves
curve = ROCCurve.from_pred_prob(y_true, y_pred_prob)
curve.plot()
curve = ROCCurve.from_pred_prob(y_true, y_pred_prob, convex=True)
curve.plot()
plt.show()
```

#### Why should I make my ROC curve convex?

A nice explanation is given in [this paper](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2897827/).

In essence, ROC points below the convex hull are suboptimal classifiers,
which can be outperformed by interpolating nearby classifiers that are on the convex hull.
Practically, this interpolation is a random choice
(i.e. without considering input data or predictions made)
between such better classifiers,
where the probability of picking one over the other
determines the position on the line segment between their ROC points.