<commit_before># Biofits

[![Build Status](https://travis-ci.org/jimrybarski/biofits.svg?branch=master)](https://travis-ci.org/jimrybarski/biofits)

This library provides functions to fit equilibrium binding data using non-linear regression.
It's effectively just a wrapper around SciPy's `curve_fit` function. You need to know what you're doing though,
biofits won't prevent you from using the wrong equation or misinterpreting the data (for now).

# Examples

### Hyperbolic Fit
```python
from biofits import fit_hyperbola, hyperbola
import numpy as np
import matplotlib.pyplot as plt

# some example real-world data
concentrations = np.array([0, 20, 40, 60, 80, 100, 120, 140, 160])
fluorescence = np.array([17.59393811, 22.8988294, 25.30156157, 26.33397056, 27.44683102, 28.07189301, 28.83145338, 28.89926584, 29.22217763])

# perform the fit
yint, yint_stddev, delta_y, delta_y_stddev, kd, kd_stddev = fit_hyperbola(concentrations, fluorescence)

# plot the results
p = plt.figure(figsize=(5,5 ))
x = np.linspace(min(concentrations), max(concentrations), len(concentrations)*1000)
y = hyperbola(x, yint, delta_y, kd)
p = plt.plot(x,y, label='fit', zorder=0)
p = plt.plot(concentrations, fluorescence, 'o', label='data', color='black', markersize=4)
text = plt.text(40, 20, "$k_{d}=%.1f \pm %.1f\ nM$" % (kd, kd_stddev), fontsize=18)
plt.xlabel("Protein, nM", fontsize=18)
plt.ylabel("Fluorescence (A.U.)", fontsize=18)
```
![hyperbolic fit](hyperbolic-fit.png)

### Quadratic Fit
```python
from biofits import fit_quadratic, quadratic
import numpy as np
import matplotlib.pyplot as plt

concentrations = np.array([0, 20, 40, 60, 80, 100, 120, 140, 160])
fluorescence = np.array([4.04057221945, 8.02206664679, 9.68169248317, 10.4687043773, 10.9989947842, 11.3576978629, 11.6021191924, 11.7486026869, 11.8934660043])
yint, yint_stddev, delta_y, delta_y_stddev, kd, kd_stddev, constant, constant_stddev = fit_quadratic(concentrations, fluorescence)

p = plt.figure(figsize=(5,5 ))
x = np.linspace(min(concentrations), max(concentrations), len(concentrations)*1000)
y = quadratic(x, yint, delta_y, kd, constant)
p = plt.plot(x,y, label='fit', zorder=0)
p = plt.plot(concentrations, fluorescence, 'o', label='data', color='black', markersize=4)
text = plt.text(50, 6, "$k_{d}=%.1f \pm %.1f\ nM$" % (kd, kd_stddev), fontsize=18)
text = plt.text(50, 5.2, "$[L]=%.1f \pm %.1f\ nM$" % (constant, constant_stddev), fontsize=18)
plt.suptitle("Quadratic Fit", fontsize=18)
plt.xlabel("Protein, nM", fontsize=18)
plt.ylabel("Fluorescence (A.U.)", fontsize=18)
```
![quadratic fit](quadratic-fit.png)<commit_msg>better comments and cleaned up examples<commit_after># Biofits

[![Build Status](https://travis-ci.org/jimrybarski/biofits.svg?branch=master)](https://travis-ci.org/jimrybarski/biofits)

This library provides functions to fit equilibrium binding data using non-linear regression.
It's effectively just a wrapper around SciPy's `curve_fit` function. You need to know what you're doing though,
biofits won't prevent you from using the wrong equation or misinterpreting the data (for now).

# Examples

### Hyperbolic Fit
```python
from biofits import fit_hyperbola, hyperbola
import numpy as np
import matplotlib.pyplot as plt

# some example real-world data
concentrations = np.array([0, 20, 40, 60, 80, 100, 120, 140, 160])
fluorescence = np.array([17.59393811, 22.8988294, 25.30156157, 26.33397056, 27.44683102, 28.07189301, 28.83145338, 28.89926584, 29.22217763])

# perform the fit
yint, yint_stddev, delta_y, delta_y_stddev, kd, kd_stddev = fit_hyperbola(concentrations, fluorescence)

# create the fit curve, just for plotting
x = np.linspace(min(concentrations), max(concentrations), len(concentrations)*1000)
y = hyperbola(x, yint, delta_y, kd)

# plot the results
plt.figure(figsize=(5,5))
plt.plot(x,y, label='fit', zorder=0)
plt.plot(concentrations, fluorescence, 'o', label='data', color='black', markersize=4)
text = plt.text(40, 20, "$k_{d}=%.1f \pm %.1f\ nM$" % (kd, kd_stddev), fontsize=18)
plt.xlabel("Protein, nM", fontsize=18)
plt.ylabel("Fluorescence (A.U.)", fontsize=18)
```
![hyperbolic fit](hyperbolic-fit.png)

### Quadratic Fit

Here we titrate a protein at various concentrations with a constant (unknown) concentration of ligand.

```python
from biofits import fit_quadratic, quadratic
import numpy as np
import matplotlib.pyplot as plt

# some example real-world data
concentrations = np.array([0, 20, 40, 60, 80, 100, 120, 140, 160])
fluorescence = np.array([4.04057221945, 8.02206664679, 9.68169248317, 10.4687043773, 10.9989947842, 11.3576978629, 11.6021191924, 11.7486026869, 11.8934660043])

# perform the fit
yint, yint_stddev, delta_y, delta_y_stddev, kd, kd_stddev, constant, constant_stddev = fit_quadratic(concentrations, fluorescence)

# create the fit curve, just for plotting
x = np.linspace(min(concentrations), max(concentrations), len(concentrations)*1000)
y = quadratic(x, yint, delta_y, kd, constant)

# plot the results
plt.figure(figsize=(5,5))
plt.plot(x,y, label='fit', zorder=0)
plt.plot(concentrations, fluorescence, 'o', label='data', color='black', markersize=4)
plt.text(50, 6, "$k_{d}=%.1f \pm %.1f\ nM$" % (kd, kd_stddev), fontsize=18)
plt.text(50, 5.2, "$[L]=%.1f \pm %.1f\ nM$" % (constant, constant_stddev), fontsize=18)
plt.suptitle("Quadratic Fit", fontsize=18)
plt.xlabel("Protein, nM", fontsize=18)
plt.ylabel("Fluorescence (A.U.)", fontsize=18)
```
![quadratic fit](quadratic-fit.png)
{'fraction_non_alphanumeric': 0.1516874541452678, 'fraction_numerical': 0.10564930300807043, 'mean_word_length': 6.128104575163399, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1285548', 'n_tokens_mistral': 2554, 'n_tokens_neox': 2043, 'n_words': 521}