Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a constrained parameter to map_range() #2

Merged
merged 6 commits into from
Apr 14, 2021

Conversation

lesamouraipourpre
Copy link
Contributor

Add a constrained parameter to map_range(), defaulting to True.
This allows for the conversion of unconstrained ranges, eg. Celsius to Fahrenheit.

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than add a control parameter to the original function, how about creating a new function with a different name that is an unconstrained map_range()? It is really a particular kind of linear transformation, though getting into the linear algebra of linear transformations is beyond what most people want to cope with.
map_range() can call this new function to do the mapping, and then do the constraining itself.

I am not sure what to call this new function. Let's think about it.

@lesamouraipourpre
Copy link
Contributor Author

lesamouraipourpre commented Apr 13, 2021

One option would simply be unconstrained_map_range.
Another would be map_calibration. This comes from another example that just dawned on me of having a sensor where we take calibration readings,eg:

sensor_calibration_1 = read_sensor() # at known_real_world_value_1
sensor_calibration_2 = read_sensor() # at known_real_world_value_2
# This obviously assumes linear behaviour between the sensor value and the real world value

current_sensor_value = read_sensor()
real_world_value = map_calibration(
    current_sensor_value,
    sensor_calibration_1,
    sensor_calibration_2,
    known_real_world_value_1,
    known_real_world_value_2
)

@dhalbert
Copy link
Contributor

One option would simply be unconstrained_map_range.
Another would be map_calibration. This comes from another example that just dawned on me of having a sensor where we take calibration readings,eg:

unconstrained_map_range() is straightforward. map_calibration() describes a use case. Your temperature conversion example is really easy to explain, but is not a calibration. So I like the former better.

@lesamouraipourpre
Copy link
Contributor Author

Slight tangent; are you able to run the tests locally?
They don't trigger for me when I run pre-commit run --all-files, I'm not sure if they should.
If I run pytest in the Adafruit_CircuitPython_SimpleMath it fails to run with the following errors:

pi in strawberry in Adafruit_CircuitPython_SimpleMath on  unconstrained-map-range via 🐍 v3.7.3 (testspace)
❯ pytest
================================================= test session starts ==================================================
platform linux -- Python 3.7.3, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: /XXXX/Adafruit_CircuitPython_SimpleMath
plugins: random-order-1.0.4, cov-2.11.1
collected 0 items / 2 errors

======================================================== ERRORS ========================================================
_______________________________________ ERROR collecting tests/constrain_test.py _______________________________________
ImportError while importing test module '/XXXX/Adafruit_CircuitPython_SimpleMath/tests/constrain_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.7/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/constrain_test.py:5: in <module>
    from adafruit_simplemath import constrain
E   ModuleNotFoundError: No module named 'adafruit_simplemath'
_______________________________________ ERROR collecting tests/map_range_test.py _______________________________________
ImportError while importing test module '/XXXX/Adafruit_CircuitPython_SimpleMath/tests/map_range_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.7/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/map_range_test.py:5: in <module>
    from adafruit_simplemath import map_range
E   ModuleNotFoundError: No module named 'adafruit_simplemath'
=============================================== short test summary info ================================================
ERROR tests/constrain_test.py
ERROR tests/map_range_test.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================== 2 errors in 0.38s ===================================================

…uit#3)

* Split map_range into map_range and unconstrained_map_range.
  * map_range will constrain the output value within the provided limits
  * unconstrained_map_range will only use the limits to define the linear equation
* Add tests for constrain with auto-swap
* Add tests for unconstrained_map_range
@lesamouraipourpre
Copy link
Contributor Author

Slight tangent; are you able to run the tests locally?

I figured an answer - use python -m pytest instead of pytest.

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the discussion on this, and making it more useful! I asked a couple of things.

Add tests for out-of-range reversed order map_range.
@lesamouraipourpre
Copy link
Contributor Author

lesamouraipourpre commented Apr 14, 2021

While waiting for my vaccine today, I realised there's a better name for unconstrained_map_range.

map_unconstrained_range

If you want, I can change the name and do another push.

@dhalbert
Copy link
Contributor

I the sound of map_unconstrained_range better too. Idle thinking is often the best. 🙂

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thanks for your work on this.

@dhalbert dhalbert merged commit 5f38265 into adafruit:main Apr 14, 2021
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Apr 15, 2021
Updating https://github.com/adafruit/Adafruit_CircuitPython_BME280 to 2.6.1 from 2.6.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_BME280#46 from jposada202020/improving_docs

Updating https://github.com/adafruit/Adafruit_CircuitPython_BNO055 to 5.3.0 from 5.2.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_BNO055#78 from ViennaMike/axis_remap

Updating https://github.com/adafruit/Adafruit_CircuitPython_IRRemote to 4.0.6 from 4.0.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_IRRemote#46 from kevinjwalters/tx-var-bits
  > "Increase duplicate code check threshold "

Updating https://github.com/adafruit/Adafruit_CircuitPython_SHTC3 to 1.0.6 from 1.0.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_SHTC3#7 from jposada202020/improving_docs
  > "Increase duplicate code check threshold "
  > Merge pull request adafruit/Adafruit_CircuitPython_SHTC3#6 from FoamyGuy/pylintrc_and_versions

Updating https://github.com/adafruit/Adafruit_CircuitPython_MIDI to 1.4.0 from 1.3.5:
  > Added copyright information
  > Merge pull request adafruit/Adafruit_CircuitPython_MIDI#27 from gamblor21/add_control_values

Updating https://github.com/adafruit/Adafruit_CircuitPython_SimpleMath to 2.0.0 from 1.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_SimpleMath#2 from lesamouraipourpre/unconstrained-map-range
  > Added tests check to pre commit config
  > "Increase duplicate code check threshold "

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_Colorsys
@lesamouraipourpre lesamouraipourpre deleted the unconstrained-map-range branch April 15, 2021 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants