Skip to content

Conversation

@MxMartin
Copy link

@MxMartin MxMartin commented Jul 30, 2023

The current code returns erroneous colors.
To make it clear I took the current implementation and put it into a separate function

def new_implementation(value):
    if value <= index[0]:
        return 0
    if value >= index[-1]:
        return len(index) - 1
    return next(i for i, v in enumerate(color_map.index) if v > value) -1

def current_implementation(value):
    if value <= index[0]:
        return 0
    if value >= index[-1]:
        return len(index) - 1
    return len([u for u in index if u < value]) - 1

index = [1, 2, 4]
colors = ["black", 'purple', 'red']

print("value", "current implementation", "new implementation")
for i in range(-2, 5):
    print(i, "\t", current_implementation(i), "\t", new_implementation(i)) 

returns:

value current_implementation new_implementation
-2 	 0 	 0
-1 	 0 	 0
0 	 0 	 0
1 	 0 	 0
2 	 0 	 1
3 	 1 	 1
4 	 2 	 2
5 	 2 	 2

you will notice that for value 2 the current implementation would return color 0, even though value 2 is at index 1 in index.

Or for

index = [2, 4, 5]

this returns

value current_implementation new_implementation
-2 	 0 	 0
-1 	 0 	 0
0 	 0 	 0
1 	 0 	 0
2 	 0 	 0
3 	 0 	 0
4 	 0 	 1
5 	 2 	 2
6 	 2 	 2
7 	 2 	 2

i.e. for index 4 we get color 0 and for index 5 color 2. Color 1 is never returned though it should be returned vor value 4

Last but not least I added a check on init to make sure the index is in order

MxMartin and others added 2 commits July 30, 2023 16:58
current code get the index of the color corresponding to a certain value retuned unexpected value, e.g. for index = [0, 1, 2] and value 1, the color at index 0 was returned, not index 1
@Conengmo
Copy link
Member

Conengmo commented Oct 4, 2023

Superseded by #141

@Conengmo Conengmo closed this Oct 4, 2023
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