Commit 42679de7 authored by Mathias Diez's avatar Mathias Diez Committed by Anton Akhmerov
Browse files

Added to Oracle a dictionary _xintervals which replaces the loop over all...

Added to Oracle a dictionary _xintervals which replaces the loop over all intervals in request_points and add_data.
parent 732d79ce
......@@ -278,8 +278,10 @@ class Oracle(object):
"""
def __init__(self, a):
# internal variables:
self._xintervals = {}
self._intervals = self._split_interval(a.interval, [None, None],
a.init_points)
a.init_points)
self._add_xintervals(self._intervals)
self._requested_x = []
self._a = a
self._n_done = 0
......@@ -319,6 +321,17 @@ class Oracle(object):
return dict(((points[i], points[i + 1]), [values[i], values[i + 1]])
for i in xrange(len(points) - 1))
def _add_xintervals(self, intervals):
for k in intervals.iterkeys():
if k[0] in self._xintervals:
self._xintervals[k[0]].add(k)
else:
self._xintervals[k[0]] = {k}
if k[1] in self._xintervals:
self._xintervals[k[1]].add(k)
else:
self._xintervals[k[1]] = {k}
# functions visible from the outside:
def add_data(self, data):
"""Adds data to the internal intervals.
......@@ -336,53 +349,66 @@ class Oracle(object):
# determine new intervals
new_intervals = {}
finished_intervals = []
if self._n_done < max(a.init_points *.75, 7):
# We're in the beginning -> split all finished
for k, v in self._intervals.iteritems():
for x, y in data:
# find and set x, y in internal intervals
index = None
for x, y in data:
finished_intervals = []
for k in self._xintervals[x]:
# find x and set y in internal intervals
#print k
v = self._intervals[k]
if k[0] == x:
index = 0
elif k[1] == x:
else:
index = 1
if index is not None:
v[index] = y
self._n_done +=1
if v[(index+1)%2] is not None:
# interval is finished -> split it
new_intervals.update(
self._split_interval(k, v, a.n_split))
finished_intervals.append(k)
for k in finished_intervals:
del self._intervals[k]
v[index] = y
self._n_done +=1
if v[(index+1)%2] is not None:
# interval is finished -> split it
new_intervals.update(
self._split_interval(k, v, a.n_split))
finished_intervals.append(k)
for k in finished_intervals:
del self._intervals[k]
if k[0] in self._xintervals:
self._xintervals[k[0]].discard(k)
if k[1] in self._xintervals:
self._xintervals[k[1]].discard(k)
del self._xintervals[x]
self._intervals.update(new_intervals)
self._add_xintervals(new_intervals)
return
# Main loop, split only if not meeting precision goal
for k, v in self._intervals.iteritems():
for x, y in data:
# find and set x, y in internal intervals
index = None
for x, y in data:
finished_intervals = []
for k in self._xintervals[x]:
# find x and set y in internal intervals
v = self._intervals[k]
if k[0] == x:
index = 0
elif k[1] == x:
else:
index = 1
if index is not None:
v[index] = y
sq_scales = self._get_sq_scales()
if v[(index+1)%2] is not None:
# interval is finished -> split if precision is not met
dx = (k[0] - k[1]) ** 2 / a.sq_x_scale
dist = dx + self._sq_norm(v[0], v[1], sq_scales)
if dist > a.precision_goal_sq \
and dx > a.min_sq_x_scale:
new_intervals.update(
self._split_interval(k, v, a.n_split))
finished_intervals.append(k)
for k in finished_intervals:
del self._intervals[k]
self._intervals[k][index] = y
v[index] = y
sq_scales = self._get_sq_scales()
if v[(index+1)%2] is not None:
# interval is finished -> split if precision is not met
dx = (k[0] - k[1]) ** 2 / a.sq_x_scale
dist = dx + self._sq_norm(v[0], v[1], sq_scales)
if dist > a.precision_goal_sq \
and dx > a.min_sq_x_scale:
new_intervals.update(
self._split_interval(k, v, a.n_split))
finished_intervals.append(k)
for k in finished_intervals:
del self._intervals[k]
if k[0] in self._xintervals:
self._xintervals[k[0]].discard(k)
if k[1] in self._xintervals:
self._xintervals[k[1]].discard(k)
del self._xintervals[x]
self._add_xintervals(new_intervals)
self._intervals.update(new_intervals)
def request_points(self):
......@@ -395,13 +421,10 @@ class Oracle(object):
additional data is added).
"""
points = []
for k, v in self._intervals.iteritems():
if (v[0] is None) and (not k[0] in self._requested_x):
points.append(k[0])
self._requested_x.append(k[0])
if (v[1] is None) and (not k[1] in self._requested_x):
points.append(k[1])
self._requested_x.append(k[1])
for k in self._xintervals.iterkeys():
if not k in self._requested_x:
points.append(k)
self._requested_x.append(k)
return set(points)
def exec_job_list_ipython(ipycluster, module, jobs):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment