PymateIO-mapping: Difference between revisions

From pymate.io wiki
Jump to navigation Jump to search
(Created page with "# Abstract The {{fn|map()}} # Bibliothèques maps La bibliothèque [maps.py](lib/maps.py) propose des functions permettant d'implémenter facilement des traitement d'interpolation, de catégorisation et autres. # fonction map() La fonction `map()` est utilisée pour effectuer une interpolation linéaire entre deux valeurs. `maps()` retourne un float qui peut éventuellement être transformé en entier en réalisant un casting avec `int()`. ``` python def map(value, i...")
 
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
# Abstract
== Abstract ==


The {{fn|map()}}
The {{fname|map()}} function is very popular and useful function in Arduino world.


# Bibliothèques maps
Pymate includes the '''{{fname|maps.py}}''' library regrouping interpolation, categorization and other functions [https://github.com/mchobby/esp8266-upy/blob/master/LIBRARIAN/maps_readme.md sourced from MC Hobby libraries].


La bibliothèque [maps.py](lib/maps.py) propose des functions permettant d'implémenter facilement des traitement d'interpolation, de catégorisation et autres.
== map() function ==


# fonction map()
The {{fname|map()}} function is used to perform linear interpolation along the line identified by two points (start and stop).  
La fonction `map()` est utilisée pour effectuer une interpolation linéaire entre deux valeurs. `maps()` retourne un float qui peut éventuellement être transformé en entier en réalisant un casting avec `int()`.
``` python
def map(value, istart, istop, ostart, ostop):
```


avec:
[[File:linear-interpolation.png|320px]]


* __value__ : valeur d'entrée (entier ou float) située entre un minimum `istart` et un maximum `istop`.
The {{fname|maps()}} results is floating point value. It can be casted to integer with {{fname|int(maps( ... ))}}.
* __istart__ : valeur minimale de la plage d'entrée (entier ou float).
* __istop__ : valeur maximale de la plage d'entrée (entier ou float).
* __ostart__ : borne inférieure de la valeur de sortie (entier ou float).
* __ostop__ : borne supérieur de la valeur de sortie (entier ou float).


retourne:
<nowiki>def map(value, istart, istop, ostart, ostop):</nowiki>


La fonction retourne la valeur de sortie (float) correspondant à la valeur d'entrée `value`
{{underline|Function parameters:}}


# fonction ranking()
* '''value''' : The input value (integer or float) within the range istart..istop.
La fonction `ranking()` permet de catégoriser facilement une valeur d'entrée parmi une liste de valeurs précisée dans `ranges`. La fonction permet également de transposer la valeur catégorisée avec une des valeurs de sortie mentionnées dans `transposed` (si celui-ci est défini).
* '''istart''' : minimal value of input range (integer or float).
* '''istop''' : maximal value of the input range (integer or float).
* '''ostart''' : minimal value of output range (integer or float).
* '''ostop''' : maximal value of the output range (integer or float).


``` python
{{underline|Function results:}}
def ranking( value, ranges, transposed=None ):
```


avec:
The function will returns the output value (a float) in the range ostart..ostop. It is the resulting value corresponding to the input '''value''' .


* __value__ : valeur comparée aux différents intervalles. Lorsqu'une valeur tombe dans un intervalle, la limite inférieure de celui-ci est retourné (ou valeur transposée correspondante). `None` est retourné si `value` est en dessous du premier intervalle.
== ranking() function ==
* __ranges__ : liste des valeurs croissantes définissant les n intervalles.
* __transposed__ : (optionnel) liste des valeurs transposées correspondant à aux intervalles. Si définit c'est la valeur transposée qui est retournée n lieu et place de la borne inférieure de l'intervalle.


retourne:
The {{fname|ranking()}} function is used to categorize an input value within a limited set of {{fname|ranges}}.


La borne inférieure de l'intervalle dans lequel la valeur `value` tombe ou `None`. Si une liste de valeurs transposée existe alors le résultat (borne inférieure) est extrait de la liste `transposed` avant d'être retourné
The function can also transpose the categorized value with a list of output values (when defined in the call).
 
<nowiki>def ranking( value, ranges, transposed=None ):</nowiki>
 
{{underline|Function parameters:}}
 
* '''value''' : the input value compared to the various ranges. When falling into a range, the resulting output value is the lower value of the range (or corresponding ''transposed'' value when defined). '''None''' is return when the input value is below the very first range.
* '''ranges''' : list of increasing values defining the n ranges. Ranges can be read as follows range[0]..range[1], range[1]..range[2], range[2]..range[3], ... range[n-1]..range[n]
* '''transposed''' : (optional) list of transposed value for each entry of the ranges list. When defined, the resulting output (lower value of the range) is replaced with the corresponding entry from the '''transposed''' list.
 
{{underline|Function results:}}
 
The lower value of the range within the input value falls otherwise `None` when the input value do not fit any range. The resulting value can be replace with its `transposed` equivalent (when `transposed` is defined).
 
=== Example 1 ===
 
<syntaxhighlight lang="python" line>
from maps import ranking


Exemple:
```
result = ranking( val, [10,20,30,40] )
result = ranking( val, [10,20,30,40] )
```
</syntaxhighlight>
 
* will returns None for val < 10
* will returns 10 for val>= 10 and val < 20
* will returns 20 for val>= 20 and val < 30
* will returns 30 for val>= 30 and val < 40
* will returns 40 for val>= 40


* retourne None si val < 10
=== Example 2 ===
* retourne 10 si val>= 10 et val < 20
<syntaxhighlight lang="python" line>
* retourne 20 si val>= 20 et val < 30
from maps import ranking
* retourne 30 si val>= 30 et val < 40
* retourne 40 si val>= 40


```
result = ranking( val, [10,20,30,40], ["A", "B", "C", "D"] )
result = ranking( val, [10,20,30,40], ["A", "B", "C", "D"] )
```
</syntaxhighlight>
 
* will returns None for val < 10
* will returns "A" for val>= 10 and val < 20
* will returns "B" for val>= 20 and val < 30
* will returns "C" for val>= 30 and val < 40
* will returns "D" for val>= 40


* retourne None si val < 10
== slice_by() function ==
* retourne "A" si val>= 10 et val < 20
* retourne "B" si val>= 20 et val < 30
* retourne "C" si val>= 30 et val < 40
* retourne "D" si val>= 40


remarks: l'appel pourrait également être écrit `result = ranking( val, [10,20,30,40], "ABCD" )`
It exists situation where you need to split a long list into smaller sub-lists of a given length.


# fonction slice_by()
It is the purpose of the {{fname|slice_by()}} function creating a list of sub-list each having N items.
La fonction `slice_by()` permet diviser un liste en sous-listes de N éléments.


``` python
<nowiki>def slice_by( lst, by_len ):</nowiki>
def slice_by( lst, by_len ):
```


avec:
{{underline|Function parameters:}}


* __lst__ : la liste source.
* '''lst''' : the source list.
* __by_len__ : le nombre d'élément dans chaque sous-liste.
* '''by_len''' : the split length of the sub-lists.




Exemple:
=== Example 1 ===
``` python
<syntaxhighlight lang="python" line>
from maps import slice_by
from maps import slice_by


l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
print( "slice by 5", slice_by(l,5) )
print( "slice by 5", slice_by(l,5) )
```
</syntaxhighlight>


ce qui affiche le resultat suivant:
which prints the following


```
<nowiki>slice by 5 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],  
slice by 5 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26]]
[11, 12, 13, 14, 15], [16, 17, 18, 19, 20],  
```
[21, 22, 23, 24, 25], [26]]</nowiki>

Latest revision as of 00:40, 12 September 2026

Abstract

The map() function is very popular and useful function in Arduino world.

Pymate includes the maps.py library regrouping interpolation, categorization and other functions sourced from MC Hobby libraries.

map() function

The map() function is used to perform linear interpolation along the line identified by two points (start and stop).

Linear-interpolation.png

The maps() results is floating point value. It can be casted to integer with int(maps( ... )).

def map(value, istart, istop, ostart, ostop):

Function parameters:

  • value : The input value (integer or float) within the range istart..istop.
  • istart : minimal value of input range (integer or float).
  • istop : maximal value of the input range (integer or float).
  • ostart : minimal value of output range (integer or float).
  • ostop : maximal value of the output range (integer or float).

Function results:

The function will returns the output value (a float) in the range ostart..ostop. It is the resulting value corresponding to the input value .

ranking() function

The ranking() function is used to categorize an input value within a limited set of ranges.

The function can also transpose the categorized value with a list of output values (when defined in the call).

def ranking( value, ranges, transposed=None ):

Function parameters:

  • value : the input value compared to the various ranges. When falling into a range, the resulting output value is the lower value of the range (or corresponding transposed value when defined). None is return when the input value is below the very first range.
  • ranges : list of increasing values defining the n ranges. Ranges can be read as follows range[0]..range[1], range[1]..range[2], range[2]..range[3], ... range[n-1]..range[n]
  • transposed : (optional) list of transposed value for each entry of the ranges list. When defined, the resulting output (lower value of the range) is replaced with the corresponding entry from the transposed list.

Function results:

The lower value of the range within the input value falls otherwise `None` when the input value do not fit any range. The resulting value can be replace with its `transposed` equivalent (when `transposed` is defined).

Example 1

from maps import ranking

result = ranking( val, [10,20,30,40] )
  • will returns None for val < 10
  • will returns 10 for val>= 10 and val < 20
  • will returns 20 for val>= 20 and val < 30
  • will returns 30 for val>= 30 and val < 40
  • will returns 40 for val>= 40

Example 2

from maps import ranking

result = ranking( val, [10,20,30,40], ["A", "B", "C", "D"] )
  • will returns None for val < 10
  • will returns "A" for val>= 10 and val < 20
  • will returns "B" for val>= 20 and val < 30
  • will returns "C" for val>= 30 and val < 40
  • will returns "D" for val>= 40

slice_by() function

It exists situation where you need to split a long list into smaller sub-lists of a given length.

It is the purpose of the slice_by() function creating a list of sub-list each having N items.

def slice_by( lst, by_len ):

Function parameters:

  • lst : the source list.
  • by_len : the split length of the sub-lists.


Example 1

from maps import slice_by

l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
print( "slice by 5", slice_by(l,5) )

which prints the following

slice by 5 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], 
[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], 
[21, 22, 23, 24, 25], [26]]