generated from mwc/lab_scatter
I was getting errors when running test_transform.py not because of transform.py (I think) but instead because of how the arguments were being passed into the functions within. I removed the list-in-a-list from the clamp(), ratio(), and scale() tests and things seem to be working as expected. A lot of the extra thinking for checkpoint 2 was related to my attempt to avoid overthinking the problem. I found myself more than once starting to rewrite functions that I had already made, so upon taking another look at my plan, I realized the easier route would be to use things like the clamp() or minimum() function, for example, in order to save myself time and possible hard-to-diagnose bugs later on. Keeping with the theme of top-down design, breaking the problem up into smaller, simpler pieces also makes things easier to test as progress is made. The biggest strategy I employed was writing down, in English/pseudocode, what I wanted to do, then as I started to write out my code, I would explain to myself what was supposed to happen, which is where I started to go back to my previous functions to continue using them in the next function to be made.
76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
# transform.py
|
|
# ------------
|
|
# By MWC Contributors
|
|
# The functions in this module transform data.
|
|
# None of them are finished; this is your job!
|
|
|
|
def maximum(data):
|
|
"Returns the largest number in data"
|
|
highest = None
|
|
for number in data:
|
|
if highest is None:
|
|
highest = number
|
|
if number > highest:
|
|
highest = number
|
|
return highest
|
|
|
|
def minimum(data):
|
|
"Returns the smallest number in data"
|
|
lowest = None
|
|
for number in data:
|
|
if lowest is None:
|
|
lowest = number
|
|
if number < lowest:
|
|
lowest = number
|
|
return lowest
|
|
|
|
def bounds(data):
|
|
"Returns a list of the smallest and largest numbers in data"
|
|
min = minimum(data)
|
|
max = maximum(data)
|
|
return [min,max]
|
|
|
|
def clamp(value, low, high):
|
|
"""Clamps a value to a range from low to high.
|
|
Returns value if it is between low and high.
|
|
If value is lower than low, returns low. If value is higher than high, returns high.
|
|
"""
|
|
if value > high:
|
|
return high
|
|
elif value < low:
|
|
return low
|
|
else:
|
|
return value
|
|
|
|
def ratio(value, start, end):
|
|
"""Returns a number from 0.0 to 1.0, representing how far along value is from start to end.
|
|
The return value is clamped to [0, 1], so even if value is lower than start, the return
|
|
value will not be lower than 0.0.
|
|
"""
|
|
low = minimum([start,end])
|
|
high = maximum([start,end])
|
|
value = clamp(value,low,high)
|
|
|
|
return (value - start) / (end - start)
|
|
|
|
def scale(value, domain_min, domain_max, range_min, range_max):
|
|
"Given a value within a domain, returns the scaled equivalent within range."
|
|
r = ratio(value, domain_min, domain_max)
|
|
|
|
return range_min + (r * (range_max - range_min))
|
|
|
|
def get_x_values(points):
|
|
"Returns the first value for each point in points."
|
|
pointX = []
|
|
for x, y in points:
|
|
pointX.append(x)
|
|
|
|
return pointX
|
|
|
|
def get_y_values(points):
|
|
"Returns the second value for each point in points."
|
|
pointY = []
|
|
for x, y in points:
|
|
pointY.append(y)
|
|
|
|
return pointY |