Skip to content

Commit

Permalink
Give PiecewiseFunction a more sensible default keyfunc.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwiggins committed Oct 9, 2014
1 parent dcfa7bf commit a66d68f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ensemble/ctf/editor.py
Expand Up @@ -14,7 +14,7 @@


def create_function(values):
fn = PiecewiseFunction(key=lambda x: x[0])
fn = PiecewiseFunction()
for v in values:
fn.insert(v)
return fn
Expand Down
3 changes: 2 additions & 1 deletion ensemble/ctf/piecewise.py
@@ -1,12 +1,13 @@
from bisect import bisect
from operator import itemgetter
from types import FloatType


class PiecewiseFunction(object):
""" A piecewise linear function.
"""
def __init__(self, key=None):
self.keyfunc = key or (lambda x: id(x))
self.keyfunc = key or itemgetter(0)
self.clear()

def clear(self):
Expand Down
8 changes: 4 additions & 4 deletions ensemble/ctf/tests/test_piecewise.py
Expand Up @@ -2,7 +2,7 @@


def test_piecewise_insert():
pf = PiecewiseFunction(key=lambda x: x[0])
pf = PiecewiseFunction()

values = [(0.0, 0.0), (0.5, 0.5), (1.0, 1.0)]
for val in values:
Expand All @@ -14,7 +14,7 @@ def test_piecewise_insert():


def test_piecewise_neighbors():
pf = PiecewiseFunction(key=lambda x: x[0])
pf = PiecewiseFunction()

values = [(0.0, 0.0), (0.5, 0.5), (0.9, 1.0)]
for val in values:
Expand All @@ -25,7 +25,7 @@ def test_piecewise_neighbors():


def test_piecewise_remove():
pf = PiecewiseFunction(key=lambda x: x[0])
pf = PiecewiseFunction()

values = [(0.0, 0.0), (0.5, 0.5), (0.9, 1.0)]
for val in values:
Expand All @@ -36,7 +36,7 @@ def test_piecewise_remove():


def test_piecewise_update():
pf = PiecewiseFunction(key=lambda x: x[0])
pf = PiecewiseFunction()

values = [(0.0, 0.0), (0.5, 0.5), (0.9, 1.0)]
for val in values:
Expand Down
2 changes: 1 addition & 1 deletion ensemble/ctf/tests/test_utils.py
Expand Up @@ -6,7 +6,7 @@


def build_piecewise_func(data):
pf = PiecewiseFunction(key=lambda x: x[0])
pf = PiecewiseFunction()

for value in data:
pf.insert(value)
Expand Down
6 changes: 2 additions & 4 deletions ensemble/ctf/utils.py
@@ -1,8 +1,6 @@
from abc import abstractmethod
import json
from math import sqrt
from operator import itemgetter


import numpy as np

Expand Down Expand Up @@ -61,8 +59,8 @@ def load_ctf(filename):
msg = "{0} does not have valid transfer function data."
raise IOError(msg.format(filename))

alpha_func = PiecewiseFunction(key=itemgetter(0))
color_func = PiecewiseFunction(key=itemgetter(0))
alpha_func = PiecewiseFunction()
color_func = PiecewiseFunction()
parts = (('alpha', alpha_func), ('color', color_func))
for name, func in parts:
for value in loaded_data[name]:
Expand Down

0 comments on commit a66d68f

Please sign in to comment.