Python Forum
Mapping a value to an interval
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mapping a value to an interval
#1
Hi,

Let's say I have a variable coordinate=234321, and a variable scale=5000 (a constant, but should also be able to be e.g. 4000). Now, I want to translate this coordinate into an interval [-scale/2, +scale/2] that can contain it, starting on an even 10000nd of the coordinate itself.

So for example, in the case of 234321, the interval would be [232500,237500]. 234321 is inside that interval. Another example would be 239580 => [237500,242500]. One caveat though, this last example would also be satisfied in [235000,240000], however since it's more closer to the mean of that first interval, that should be the preferred one. So the function should kind of decide what interval to return depending on if it's on either side of a "scale/4" value or not, if you get what I mean. How can I write a generic function, that can take a coordinate and a scale as inputs, and produce this interval as an output?
Reply
#2
What have you tried?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Mar-17-2023, 09:04 AM)menator01 Wrote: What have you tried?

I've tried checking the steps/4 intervals on an incremental basis, including overlapping backwards and forwards into the neighbouring intervals, like:

baseCoord = coordinate // 10000*10000
remainder = round(coordinate/100)/10%10

if remainder <= 1.25:
   min = baseCoord - scale/2
elif remainder <= 3.75:
   min = baseCoord
elif remainder <= 6.25:
   min = baseCoord + scale/2
elif remainder <= 8.75:
   min = baseCoord
else:
   min = baseCoord*1.5

return [min, min+scale]
But I thought there must be a pure computational solution without the need for the conditionals, or what do you think? :-)
Reply
#4
The conditions on the interval are unclear. What do you do when the scale is 5387?
Reply
#5
(Mar-17-2023, 01:16 PM)Gribouillis Wrote: The conditions on the interval are unclear. What do you do when the scale is 5387?

Sorry, have clarified. It's a constant, but should work with e.g. both 4000 and 5000.
Reply
#6
Here is my proposal
__version__ = '2023.03.17.1'

def rounding(precision, value):
    y = 0.5 if value >= 0 else -0.5
    return precision * int(y + value / precision)

def interval(coord, scale):
    u = rounding(100, coord)
    m = u + scale // 2
    return (2 * u - m, m)

if __name__ == '__main__':
    from random import randint

    for i in range(10):
        x = randint(0, 1000000)
        print(x, interval(x, 5000))
Output:
883300 (880800, 885800) 879406 (876900, 881900) 797201 (794700, 799700) 499782 (497300, 502300) 834873 (832400, 837400) 670688 (668200, 673200) 726577 (724100, 729100) 657842 (655300, 660300) 230323 (227800, 232800) 738326 (735800, 740800)
Reply
#7
This works for your particular examples.
def ranger(value, range_=5000):
    half_range = range_ / 2
    bottom = value // half_range * half_range
    return (bottom, bottom + range_)


print(ranger(234321), ranger(239580))
Output:
(232500.0, 237500.0) (237500.0, 242500.0)
Reply
#8
(Mar-17-2023, 02:13 PM)Gribouillis Wrote: Here is my proposal
__version__ = '2023.03.17.1'

def rounding(precision, value):
    y = 0.5 if value >= 0 else -0.5
    return precision * int(y + value / precision)

def interval(coord, scale):
    u = rounding(100, coord)
    m = u + scale // 2
    return (2 * u - m, m)

if __name__ == '__main__':
    from random import randint

    for i in range(10):
        x = randint(0, 1000000)
        print(x, interval(x, 5000))
Output:
883300 (880800, 885800) 879406 (876900, 881900) 797201 (794700, 799700) 499782 (497300, 502300) 834873 (832400, 837400) 670688 (668200, 673200) 726577 (724100, 729100) 657842 (655300, 660300) 230323 (227800, 232800) 738326 (735800, 740800)

Thanks, unfortunately if the scale or half the scale should be added to an even 10000nd of the coordinate, these values are not valid. For a scale of 5000 it will either end on 0000, 2500, 5000, or 7500.
Reply
#9
(Mar-17-2023, 02:40 PM)deanhystad Wrote: This works for your particular examples.
def ranger(value, range_=5000):
    half_range = range_ / 2
    bottom = value // half_range * half_range
    return (bottom, bottom + range_)


print(ranger(234321), ranger(239580))
Output:
(232500.0, 237500.0) (237500.0, 242500.0)

Try it with 235000. Here, I'd like it to return (232500, 237500) since the input would be in the middle of that interval. Now it returns (235000, 240000). Any idea how to achieve that?
Reply
#10
(Mar-17-2023, 07:06 PM)JazonKuer Wrote: For a scale of 5000 it will either end on 0000, 2500, 5000, or 7500.
Only replace 100 by 500 in the interval() function. However what are the valid values when the scale is 4000 ?
73874 (71500, 76500)
919227 (916500, 921500)
448634 (446000, 451000)
196040 (193500, 198500)
767177 (764500, 769500)
972937 (970500, 975500)
146383 (144000, 149000)
345814 (343500, 348500)
685618 (683000, 688000)
578875 (576500, 581500)
Edit: if you replace the rounding by 2500 instead of 500 it gives
73874 (71500, 76500)
919227 (916500, 921500)
448634 (446000, 451000)
196040 (193500, 198500)
767177 (764500, 769500)
972937 (970500, 975500)
146383 (144000, 149000)
345814 (343500, 348500)
685618 (683000, 688000)
578875 (576500, 581500)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,786 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Confidence interval after doing penalised cox regression HatemAli 0 1,133 Feb-23-2022, 11:02 PM
Last Post: HatemAli
  Mapping a range ebolisa 5 3,482 Jun-12-2021, 11:17 PM
Last Post: ebolisa
  Complex X Tick Interval JoeDainton123 0 1,470 Oct-05-2020, 07:27 PM
Last Post: JoeDainton123
  mapping joystick axis to specific 'channels' in python DashOrion 1 2,603 Jul-07-2020, 04:26 PM
Last Post: DashOrion
  Updating a matrix in a time interval inside a for loop vp1989 4 2,888 May-17-2020, 07:15 PM
Last Post: vp1989
  Assigning Data from one column to another with different associated timing interval alexafshari 1 1,950 Apr-30-2020, 03:59 PM
Last Post: pyzyx3qwerty
  adddate interval in python Steve42 2 2,202 Apr-16-2020, 08:24 PM
Last Post: Larz60+
  mapping-camera-coordinates-to-a-2d-floor-plan fauveboyxuuki 0 2,531 Dec-10-2019, 10:34 PM
Last Post: fauveboyxuuki
  save data in .txt after certain interval Shaswat 1 2,095 Oct-13-2019, 07:07 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020