{"id":15912045,"url":"https://github.com/drgfreeman/pytools","last_synced_at":"2025-04-03T02:26:22.602Z","repository":{"id":130571642,"uuid":"80292887","full_name":"DrGFreeman/PyTools","owner":"DrGFreeman","description":"A library of Python tools for general use","archived":false,"fork":false,"pushed_at":"2019-08-16T18:49:00.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-08T16:38:08.700Z","etag":null,"topics":["filter","median-filter","pid","pid-controller","timer"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DrGFreeman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-28T15:46:51.000Z","updated_at":"2020-04-20T04:45:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"d88f1dad-6f11-4922-b829-c779e984ae59","html_url":"https://github.com/DrGFreeman/PyTools","commit_stats":{"total_commits":19,"total_committers":1,"mean_commits":19.0,"dds":0.0,"last_synced_commit":"795e06b5a07f49a990df3c545d2d103b16dd8b4d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrGFreeman%2FPyTools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrGFreeman%2FPyTools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrGFreeman%2FPyTools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrGFreeman%2FPyTools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DrGFreeman","download_url":"https://codeload.github.com/DrGFreeman/PyTools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246923927,"owners_count":20855641,"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":["filter","median-filter","pid","pid-controller","timer"],"created_at":"2024-10-06T16:01:32.693Z","updated_at":"2025-04-03T02:26:22.570Z","avatar_url":"https://github.com/DrGFreeman.png","language":"Python","readme":"# PyTools\n\nThis repository contains various general use python tools split into different modules.\n\n## Modules\n\n* [filters](#filters) (filters.py)\n* [pytools](#pytools-1) (pytools.py)\n* [timedpid](#timedpid) (timedpid.py)\n* [timer](#timer) (timer.py)\n\n### filters\nThis module defines filters for data processing. This module requires [Numpy](http://www.numpy.org/).\n\n#### Class `Filter1D`\nThis class defines a one dimensional (1D) filter useful for real-time smoothing of time series data such as sensor signals. The filter stores a certain number of data points defined by the `maxSize` argument passed to the `Filter1D()` contructor. As new data is added with the `.addDataPoint()` method, older data is eliminated so that the number of data points remains constant. Mean and median values of the last _n_ data points in the filter can be obtained with the `.getMean(windowSize)` and `.getMedian(windoSize)` methods where _n_ is defined by the `windoSize` argument.\n\n* `Filter1D(maxSize=3)`  \nClass constructor. `maxSize` argument defines the size (number of data points) of the signal to be kept. `maxSize` must be an odd integer \u003e= 3. If `maxSize` is not defined, it is by default set to 3.\n\n* `.addDataPoint(dataPoint)`  \nAdds new data point(s) to the data array. If the data array size exceeds the `maxSize` attribute, the older data points will be trimmed from the array (left trim). `dataPoint` can be a single point, a list or a Numpy one dimensional ndarray.\n\n* `.getData()`  \nReturns the complete data array as a Numpy one dimensional ndarray.\n\n* `.getLast()`  \nReturns the last (most recent) data point from the data array.\n\n* `.getMean(windowSize=0)`  \nReturns the mean of the last _n_ points from the data array where _n_ is defined by the `windowSize` argument. If `windowSize` is not specified, is set to 0 or is greater than `maxSize`, `windowSize` will be automatically set to `maxSize` and the mean of the entire data array will be returned.\n\n* `.getMedian(windowSize=0)`  \nReturns the median of the last _n_ points from the data array where _n_ equals `windowSize`. `windowSize` must be an odd integer. If `windowSize` is not specified or is set to 0, `windowSize` will be automatically set to `maxSize` and the median of the entire data array will be returned.\n\n### pytools\n\nThis module defines various useful functions.\n\n* `.constrain(value, min, max)`  \nReturns the `value` argument constrained within the range defined by the `min` and `max` arguments. If `value` is within `min` and `max`, it will be returned without modification. If `value` is smaller than `min` or greater than `max` the returned value will equal `min` or `max` respectively.\n\n### timedpid\n\nThis module defines a simple [Proportional - Integral - Derivative (PID) controller](https://en.wikipedia.org/wiki/PID_controller) with different time step calculation methods. This is a python implementation of my Arduino TimedPID library which can be found at https://github.com/DrGFreeman/TimedPID or thru the Arduino Library Manager.\n\nThe controller features three options for time step calculation (the time step is used for integral and derivative error terms calculation):\n\n1. Non-specified (unit) time step (`.getCmd()` method)\n1. Auto time step calculation (uses time between calls to `.getCmdAutoStep()` method)\n1. Defined time step (passed as argument to `.getCmdStep()` method)\n\n#### Class `TimedPID`\n\n* `TimedPID(kp=1.0, ki=0.0, kd=0.0)`  \nConstructor: `kp`, `ki` and `kd` are the proportional, integral and derivative gains respectively.\n\n* `.getCmd(setPoint, procVar)`  \nReturns the system command. `setPoint` is the desired \"target\" value for the process variable being controlled. `procVar` is the current value of the process variable being controlled. This method uses unit time step for integral and derivative error terms calculation.\n\n* `.getCmdAutoStep(setPoint, procVar)`  \nSimilar to `.getCmd()` method except this method automatically calculates the time step based on the time between two calls to this method. The calculated time step is in seconds units.\n\n* `.getCmdStep(setPoint, procVar, timeStep)`  \nSimilar to `.getCmdAutoStep()` method except the time step is passed to the method via the `timeStep` argument. The time step can be in any units.\n\n* `.reset()`  \nResets the PID error terms. The method also resets to the current time the time variable used by the `getCmdAutoStep` to calculate the time step.\n\n* `.setCmdRange(cmdMin, cmdMax)`  \nSets the min and max command values that can be returned by the `.getCmd()`, `.getCmdAutoStep()` and `.getCmdStep()` methods to the range defined by the arguments `cmdMin` and `cmdMax`. Unless this method is called, the command range will not be limited.\n\n* `.setGains(kp=1.0, ki=0.0, kd=0.0)`  \nSets the PID controller gains. `kp`, `ki` and `kd` are the proportional, integral and derivative gains respectively.\n\n### timer  \nThis module defines a multi-purpose timer class. It can be used to measure elapsed time, manage time steps in loops, control execution times, etc. This module uses the Python _time_ module and all time values are in seconds units.\n\n#### Class `Timer`\n\n* `Timer()`  \nConstructor, starts the timer at instantiation.\n\n* `.getElapsed()`  \nReturns the time elapsed since instantiation or last reset, minus the sum of paused time.\n\n* `.isWithin(delay)`  \nReturns `True` if elapsed time is within (less than) `delay` argument, `False` otherwise. This method is useful to control execution of `while` loops for a fixed time duration as shown in the example below:\n```python\n    t = Timer()\n    while t.isWithin(5):\n      # Code here will execute until 5 seconds have passed since\n      # instantiation of t.\n```\n\n* `.pause()`  \nPauses the timer.\n\n* `.reset()`  \nResets the timer initial time to the current time.\n\n* `.resume()`  \nResumes the timer following call to `.pause()` method.\n\n* `sleepToElapsed(delay, reset=True)`  \nSleeps until elapsed time reaches the time specified by the `delay` argument. If `reset` argument is set to `True` (default), the timer will also be reset. This method is useful to control fixed time steps in loops as shown in the example below:\n```python\n    t = Timer()\n    while True:\n        # Code to be executed\n        # ...\n        # Wait until a time step of 0.1 second is reached. This ensures the\n        # loop will execute at fixed time steps, regardless of the code\n        # execution time, provided it does not exceed the specified delay value.\n        t.sleepToElapsed(0.1)\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrgfreeman%2Fpytools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrgfreeman%2Fpytools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrgfreeman%2Fpytools/lists"}