Skip to content

Commit

Permalink
mostly whitespace changes, precommit update
Browse files Browse the repository at this point in the history
  • Loading branch information
jkitchin committed Jul 10, 2024
1 parent 1668225 commit 7176cbe
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 24.4.2
hooks:
- id: black
entry: black --line-length 100 pycse

- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
rev: 7.1.0
hooks:
- id: flake8
entry: flake8 --max-line-length 100 pycse
Expand Down
1 change: 1 addition & 0 deletions pycse/PYCSE.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Copyright 2020, John Kitchin
(see accompanying license files for details).
"""

# pylint: disable=invalid-name

import warnings
Expand Down
1 change: 1 addition & 0 deletions pycse/beginner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
of these is to delay introducing indexing syntax.
"""

import collections.abc
from scipy.optimize import fsolve as _fsolve
from scipy.integrate import quad
Expand Down
1 change: 1 addition & 0 deletions pycse/colab.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ def gdownload(*FILES, **kwargs):
# if not kwargs.get('keep', False):
# os.unlink(zip)


##################################################################
# Get to a shell
##################################################################
Expand Down
1 change: 1 addition & 0 deletions pycse/orgmode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provides classes to convert python data into org markup."""

import IPython
import tabulate

Expand Down
1 change: 1 addition & 0 deletions pycse/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
version.
"""

import os

from hashlib import md5
Expand Down
1 change: 1 addition & 0 deletions pycse/sklearn/lr_uq.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
A Linear regressor with uncertainty quantification.
"""

import numpy as np
from pycse import regress
from pycse import predict as _predict
Expand Down
163 changes: 103 additions & 60 deletions pycse/sklearn/surface_response.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,75 @@
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import PolynomialFeatures
from pycse.sklearn.lr_uq import LinearRegressionUQ
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt

from pyDOE3 import bbdesign
from pyDOE3 import fullfact, ff2n, pbdesign, gsd, bbdesign, ccdesign, lhs
import numpy as np
import pandas as pd
import tabulate


class SurfaceResponse(Pipeline):
"""A class for a Surface Response design of experiment.
TODO: can we make it more flexible on the design, e.g. to select all the
options from pyDOE3? I need to check the signatures of these to see how
compatible they are.
"""

def __init__(self, inputs=None, outputs=None, bounds=None, order=2, **kwargs):
"""A class for a Surface Response design of experiment."""

def __init__(
self, inputs=None, outputs=None, bounds=None, design="bbdesign", model=None, **kwargs
):
"""inputs : list of strings, name of each factor
outputs : list of strings, name of each response
bounds : 2D array, Each row is [xmin, xmax] for a component.
This assumes that [-1, 1] map to the bounds.
order: int, polynomial model order
design: string, one of the designs in pyDOE3
fullfact, ff2n, pbdesign, gsd, bbdesign, ccdesign, lhs
except fracfact. That one is confusing and I don't know how you use it.
kwargs are passed to pyDOE3.bbenken
model : an sklearn model, defaults to scaled, order-2 polynomial
features with linear regression.
kwargs are passed to the pyDOE3 model
Builds a linear regression model. The polynomial features are
automatically generated.
automatically generated up to the specified order.
"""
self.inputs = inputs
self.outputs = outputs
self.bounds = np.array(bounds)
self._design = bbdesign(len(inputs), **kwargs)
self.order = order
super().__init__(
steps=[
("poly", PolynomialFeatures(order)),
("surface response", LinearRegressionUQ()),
]
)
if design == "fullfact":
self._design = fullfact(**kwargs)
elif design == "ff2n":
self._design = ff2n(len(inputs))
elif design == "pbdesign":
self._design = pbdesign(len(inputs))
elif design == "gsd":
self._design = gsd(**kwargs)
elif design == "bbdesign":
self._design = bbdesign(len(inputs), **kwargs)
elif design == "ccdesign":
self._design == ccdesign(len(inputs), **kwargs)
elif design == "lhs":
self._design = lhs(len(inputs), **kwargs)
else:
raise Exception(f"Unsupported design option: {design}")

self.model = model

if model is None:
self.default = True
super().__init__(
steps=[
("minmax", MinMaxScaler(feature_range=(-1, 1))),
("poly", PolynomialFeatures(2)),
("surface response", LinearRegressionUQ()),
]
)
else:
self.default = False
super().__init__(steps=[("usermodel", model)])

def design(self, shuffle=True):
"""Creates a design dataframe.
Expand Down Expand Up @@ -84,6 +109,12 @@ def design(self, shuffle=True):
return df

def set_output(self, data):
"""Set output to data.
data : list or array of results. Each row should correspond to the same
row in the input.
"""
index = self.input.index
df = pd.DataFrame(data, index=index, columns=self.outputs)
self.output = df
Expand All @@ -97,8 +128,6 @@ def score(self, X=None, y=None):
X, y = self.input, self.output
return super().score(X, y)

# No need to define fit/predict here, we get them from Pipeline

def parity(self):
"""Creates parity plot between true values and predicted values."""
X, y = self.input, self.output
Expand All @@ -123,46 +152,60 @@ def summary(self):
yp = self.predict(X)
errs = y - yp

features = self["poly"].get_feature_names_out()

pars = self["surface response"].coefs_
pars_cint = self["surface response"].pars_cint
pars_se = self["surface response"].pars_se

nrows, ncols = pars.shape

mae = [self._sigfig(x) for x in (np.abs(errs).mean())]
rmse = [self._sigfig(x) for x in (errs**2).mean()]

s += [f" score: {self.score(X, y)}"]
s += [
f" mae = {(mae)}",
"",
f" rmse = {rmse}",
"",
]

for i in range(ncols):
data = []
s += [f"Output_{i} = {y.columns[i]}"]
for j, name in enumerate(features):
data += [
[
f"{name}_{i}",
pars[j][i],
pars_cint[0][j][i],
pars_cint[1][j][i],
pars_se[j][i],
np.sign(pars_cint[0][j][i] * pars_cint[1][j][i]) > 0,
if self.default:
features = self["poly"].get_feature_names_out()

pars = self["surface response"].coefs_
pars_cint = self["surface response"].pars_cint
pars_se = self["surface response"].pars_se

nrows, ncols = pars.shape

mae = [self._sigfig(x) for x in (np.abs(errs).mean())]
rmse = [self._sigfig(x) for x in (errs**2).mean()]

s += [f" score: {self.score(X, y)}"]
s += [
f" mae = {(mae)}",
"",
f" rmse = {rmse}",
"",
]

for i in range(ncols):
data = []
s += [f"Output_{i} = {y.columns[i]}"]
for j, name in enumerate(features):
data += [
[
f"{name}_{i}",
pars[j][i],
pars_cint[0][j][i],
pars_cint[1][j][i],
pars_se[j][i],
np.sign(pars_cint[0][j][i] * pars_cint[1][j][i]) > 0,
]
]
s += [
tabulate.tabulate(
data,
headers=["var", "value", "ci_lower", "ci_upper", "se", "significant"],
tablefmt="orgtbl",
)
]
s += [""]
else:
s += ["User defined model:", repr(self["usermodel"])]

mae = [self._sigfig(x) for x in (np.abs(errs).mean())]
rmse = [self._sigfig(x) for x in (errs**2).mean()]

s += [f" score: {self.score(X, y)}"]
s += [
tabulate.tabulate(
data,
headers=["var", "value", "ci_lower", "ci_upper", "se", "significant"],
tablefmt="orgtbl",
)
f" mae = {(mae)}",
"",
f" rmse = {rmse}",
"",
]
s += [""]

return "\n".join(s)
1 change: 1 addition & 0 deletions pycse/tests/test_lisp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for lisp module."""

from pycse.lisp import (
Symbol,
Quote,
Expand Down
1 change: 1 addition & 0 deletions pycse/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for utils module."""

from pycse.utils import feq, fgt, flt, fle, fge, ignore_exception


Expand Down
1 change: 1 addition & 0 deletions pycse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
2. An ignore exception decorator
3. A handy function to read a google sheet.
"""

# Copyright 2015, John Kitchin
# (see accompanying license files for details).
import re
Expand Down

0 comments on commit 7176cbe

Please sign in to comment.