{"id":15284535,"url":"https://github.com/aminnj/tidfit","last_synced_at":"2025-04-12T23:36:07.936Z","repository":{"id":57475421,"uuid":"369966800","full_name":"aminnj/tidfit","owner":"aminnj","description":"small 1D fitter","archived":false,"fork":false,"pushed_at":"2022-01-15T00:48:32.000Z","size":296,"stargazers_count":11,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-05T08:37:14.675Z","etag":null,"topics":["curve-fit","error-bands","plotting","python","scipy"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aminnj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-23T05:15:48.000Z","updated_at":"2022-01-15T00:29:14.000Z","dependencies_parsed_at":"2022-09-07T14:10:10.276Z","dependency_job_id":null,"html_url":"https://github.com/aminnj/tidfit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminnj%2Ftidfit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminnj%2Ftidfit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminnj%2Ftidfit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aminnj%2Ftidfit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aminnj","download_url":"https://codeload.github.com/aminnj/tidfit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239581750,"owners_count":19662960,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["curve-fit","error-bands","plotting","python","scipy"],"created_at":"2024-09-30T14:58:05.453Z","updated_at":"2025-02-22T12:30:48.131Z","avatar_url":"https://github.com/aminnj.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## tidfit\n\n![Tests](https://github.com/aminnj/tidfit/actions/workflows/python-app.yml/badge.svg)\n[![PyPI](https://badge.fury.io/py/tidfit.svg)](https://pypi.python.org/pypi/tidfit/)\n\n\n```bash\npip install tidfit\n```\n\n### Overview\n\nThis package provides a tiny routine to fit a curve to pairs of points and draw it\nwith some error bands. Only depends on `numpy`, `scipy`, and `matplotlib`. It's essentially\na wrapper around `scipy.optimize.curve_fit`.\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nx = np.array([0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95])\ny = np.array([184., 193., 199., 208., 200., 225., 216., 190., 212., 173.])\n\nfig, ax = plt.subplots()\nax.errorbar(x, y, yerr=y**0.5, fmt=\"o\", label=\"data\")\n```\n\nTo specify the function to fit, one can use an `eval`-able string expression,\nwhich needs one `x` to serve as the independent variable. \nThe remaining variables are considered as fittable function parameters.\nOf course, `fit` takes a regular callable function (`lambda x,a,b: a*x+b`) as well, but who has the time to type out 10 more characters?\n\n```python\nfrom tidfit import fit\nfit(\"a*x+b\", x, y)\n```\n\u003cimg src=\"images/image1.png\" width=\"450px\" /\u003e\n\nA boolean `mask` specifies which points to consider in each fit.\n\n```python\nbins = np.linspace(-5, 5, 41)\ny  = np.histogram(np.random.normal(-2, 1, 500), bins=bins)[0]\ny += np.histogram(np.random.normal(+2, 1, 500), bins=bins)[0]\nx = bins[:-1] + 0.25\n\nfig, ax = plt.subplots()\nax.errorbar(x, y, yerr=y**0.5, fmt=\"o\", ms=5)\n\ngaussian = \"const + peak * np.exp(-((x - mu) ** 2) / (2 * sigma ** 2))\"\n\nfit(gaussian, x, y, sigma=y**0.5, mask=(x \u003c -1), color=\"C1\")\nfit(gaussian, x, y, sigma=y**0.5, mask=(x \u003e +1), color=\"C2\")\n```\n\u003cimg src=\"images/image2.png\" width=\"450px\" /\u003e\n\nAn array of initial parameter values, `p0`, is also accepted as a keyword argument to `fit` and passed through to `curve_fit`,\nbut keeping track of an array while modifying the fitting function is cumbersome.\nIf an explicit function is specified, any default arguments are extracted and used as the initial `p0` to `curve_fit`.\n```python\ndef f(x, const=None, peak=None, mu1=+2, sigma1=1, mu2=-2, sigma2=1):\n    return (\n        const\n        + peak * np.exp(-((x - mu1) ** 2) / (2 * sigma1 ** 2))\n        + peak * np.exp(-((x - mu2) ** 2) / (2 * sigma2 ** 2))\n    )\n\nfit(f, x, y, sigma=y**0.5)\n```\n\u003cimg src=\"images/image3.png\" width=\"450px\" /\u003e\n\nThe object returned by `fit` has a nice representation in notebooks\n```python\nout = fit(\"a+b*x\", [0,1,2], [1,3,3], draw=False)\nout\n```\nparameter | value\n-- | --\na | 1.333 ± 0.7454\nb | 1 ± 0.5774\n\nbut `out` is just a `dict`, and provides two ways of getting the parameter names, values, and errors:\n``` python\nprint(out)\n```\n\n```python\n{'func': \u003cfunction fit.\u003clocals\u003e.\u003clambda\u003e at 0x12e049f28\u003e,\n 'params': {'a': {'error': 0.75, 'value': 1.3333},\n            'b': {'error': 0.5774, 'value': 1.0}},\n 'parerrors': array([0.745, 0.577]),\n 'parnames': ('a', 'b'),\n 'parvalues': array([1.333, 1.   ])}\n```\n\nAnd for convenience, `out` contains the fitted function ready to be called with an array of x-values\n```python\nfunc = out[\"func\"]\n\nresiduals = ydata - func(xdata)\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faminnj%2Ftidfit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faminnj%2Ftidfit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faminnj%2Ftidfit/lists"}