Skip to content

Commit

Permalink
Merge branch 'main' into fix_issue_108_narwhals_pandas_polars_support
Browse files Browse the repository at this point in the history
  • Loading branch information
artiom-matvei committed Oct 23, 2024
2 parents fbbb9a9 + 1d599fb commit 9306fce
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 14 deletions.
84 changes: 84 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Contributing to `marginaleffects` for Python

Thank you for your interest in contributing to the `marginaleffects` Python project!

This document outlines guidelines and instructions for setting up the development environment, running tests, and ensuring your code follows the project standards.

## Development Environment

We manage the development environment using [`uv`](https://docs.astral.sh/uv/guides/projects/#creating-a-new-project) for dependency management, task execution, and testing. All necessary information is in the `.toml` and `.lock` files, and `uv` will handle dependency management automatically.

### Setting Up the Environment

Clone the repository

```bash
git clone [email protected]:vincentarelbundock/pymarginaleffects.git
cd pymarginaleffects
```

Create and activate the virtual environment using `uv`:

```bash
uv venv .venv
source .venv/bin/activate.sh
```

Install the project and its dependencies:

```bash
uv pip install .
```

### Running Tests

We use `pytest` for testing. The testing environment is fully managed by `uv`. To run the tests, simply execute:

```bash
uv run --all-extras pytest
```

`uv` will automatically install any missing dependencies before running the tests.

### Code Formatting and Linting

This project follows strict code formatting and linting rules enforced by `ruff`. Before submitting any code, make sure to run `ruff` to format your files and check for linting errors.

You can run `ruff` by executing:

```bash
uv run --all-extras ruff check marginaleffects
uv run --all-extras ruff format marginaleffects
```

Make sure there are no linting errors before submitting your changes.

## Contribution Guidelines

0. Be nice!
1. Fork the repository and create a new branch for your feature or bugfix.
2. Ensure all your changes are well-documented in the code, documentation, and changelog (if necessary).
3. Write tests for any new functionality or changes you make.
4. Run the tests and ensure everything passes before submitting your pull request.
5. Ensure your code is formatted and linted using `ruff`.
6. Push your changes to your forked repository.
7. Create a pull request (PR) on the main repository.
8. Make sure to include a clear description of your changes and reference any related issues (if applicable).

We appreciate your contributions and look forward to reviewing your pull requests!


## Documentaion Guidelines

* Follow the Numpydoc docstring style for the documentation, see [link](https://numpydoc.readthedocs.io/en/latest/format.html)

## Makefile

The `marginaleffects` repository includes a `Makefile` to facilitate some common tasks.

### Windows

1. Install make for Windows https://gnuwin32.sourceforge.net/packages/make.htm.
2. Add it to your path variable.
3. Then you can use scripts specified in the Makefile. e.g. `make test`

10 changes: 7 additions & 3 deletions marginaleffects/classes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import polars as pl
import narwhals as nw
from .narwhals_utils import is_nw

class MarginaleffectsDataFrame(pl.DataFrame):
Expand Down Expand Up @@ -85,8 +84,13 @@ def __str__(self):
tmp = self.select(valid).rename(self.mapping)
for col in tmp.columns:
if tmp[col].dtype.is_numeric():
tmp = tmp.with_columns(
pl.col(col).map_elements(lambda x: f"{x:.3g}", return_dtype=pl.Utf8)

def fmt(x):
out = pl.Series([f"{i:.3g}" for i in x])
return out

tmp.with_columns(
pl.col(col).map_batches(fmt, return_dtype=pl.Utf8).alias(col)
)
out += tmp.__str__()
out = out + f"\n\nColumns: {', '.join(self.columns)}\n"
Expand Down
4 changes: 2 additions & 2 deletions marginaleffects/comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def comparisons(
# ugly hack, but polars is very strict and `value / 2`` is float
nd, hi, lo, pad = [ to_from_native(upcast, df) for df in [nd, hi, lo, pad] ]
nd, hi, lo = map(
lambda l: nw.from_native(pl.concat([df.to_native() for df in l], how="vertical_relaxed")),
lambda el: nw.from_native(pl.concat([df.to_native() for df in el], how="vertical_relaxed")),
[nd, hi, lo]
)
# hi = pl.concat(hi, how="vertical_relaxed")
Expand All @@ -220,7 +220,7 @@ def comparisons(
pad = nw.concat(pad).unique()
# nd, hi, lo = [ to_from_native(pl.concat, upcast([pad, df]), how="diagonal") for df in [nd, hi, lo] ]
nd, hi, lo = map(
lambda l: nw.from_native(pl.concat(upcast([nw.to_native(pad), nw.to_native(l)]), how="diagonal")),
lambda el: nw.from_native(pl.concat(upcast([nw.to_native(pad), nw.to_native(el)]), how="diagonal")),
[nd, hi, lo])
# hi = pl.concat(upcast([pad, hi]), how="diagonal")
# lo = pl.concat(upcast([pad, lo]), how="diagonal")
Expand Down
8 changes: 4 additions & 4 deletions marginaleffects/equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def get_equivalence(
if np.isinf(df):
x = x.with_columns(
pl.col("statistic_noninf")
.map_elements(lambda x: 1 - norm.cdf(x), return_dtype=pl.Float64)
.map_batches(lambda x: 1 - norm.cdf(x), return_dtype=pl.Float64)
.alias("p_value_noninf"),
pl.col("statistic_nonsup")
.map_elements(lambda x: norm.cdf(x), return_dtype=pl.Float64)
.map_batches(lambda x: norm.cdf(x), return_dtype=pl.Float64)
.alias("p_value_nonsup"),
)
else:
x = x.with_columns(
pl.col("statistic_noninf")
.map_elements(lambda x: 1 - t.cdf(x), return_dtype=pl.Float64)
.map_batches(lambda x: 1 - t.cdf(x), return_dtype=pl.Float64)
.alias("p_value_noninf"),
pl.col("statistic_nonsup")
.map_elements(lambda x: t.cdf(x), return_dtype=pl.Float64)
.map_batches(lambda x: t.cdf(x), return_dtype=pl.Float64)
.alias("p_value_nonsup"),
)

Expand Down
3 changes: 0 additions & 3 deletions marginaleffects/uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ def get_z_p_ci(df, model, conf_level, hypothesis_null=0):
dtype=nw.Float64,
native_namespace=nw.get_native_namespace(df),
)
# nw.col("p_value")
# .map_elements(lambda x: -np.log2(x), return_dtype=nw.Float64)
# .alias("s_value")
)
except Exception as e:
print(f"An exception occurred: {e}")
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[project]
name = "marginaleffects"
version = "0.0.13"
version = "0.0.13.1"
description = "Predictions, counterfactual comparisons, slopes, and hypothesis tests for statistical models."
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"numpy >=2.0.0",
"narwhals",
"patsy >=0.5.6",
"polars >=1.7.0",
"pyarrow >=17.0.0",
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9306fce

Please sign in to comment.