text
stringlengths
0
828
""""""
if isinstance(order_or_history, MarketOrderList):
return orders.encode_to_json(order_or_history)
elif isinstance(order_or_history, MarketHistoryList):
return history.encode_to_json(order_or_history)
else:
raise Exception(""Must be one of MarketOrderList or MarketHistoryList."")"
207,"def event_subscriber(event):
""""""
Register a method, which gets called when this event triggers.
:param event: the event to register the decorator method on.
""""""
def wrapper(method):
Registry.register_event(event.name, event, method)
return wrapper"
208,"def dispatch_event(event, subject='id'):
""""""
Dispatch an event when the decorated method is called.
:param event: the event class to instantiate and dispatch.
:param subject_property: the property name to get the subject.
""""""
def wrapper(method):
def inner_wrapper(*args, **kwargs):
resource = method(*args, **kwargs)
if isinstance(resource, dict):
subject_ = resource.get(subject)
data = resource
else:
subject_ = getattr(resource, subject)
data = resource.__dict__
event(subject_, data).dispatch()
return resource
return inner_wrapper
return wrapper"
209,"def add(self, classifier, threshold, begin=None, end=None):
""""""Adds a new strong classifier with the given threshold to the cascade.
**Parameters:**
classifier : :py:class:`bob.learn.boosting.BoostedMachine`
A strong classifier to add
``threshold`` : float
The classification threshold for this cascade step
``begin``, ``end`` : int or ``None``
If specified, only the weak machines with the indices ``range(begin,end)`` will be added.
""""""
boosted_machine = bob.learn.boosting.BoostedMachine()
if begin is None: begin = 0
if end is None: end = len(classifier.weak_machines)
for i in range(begin, end):
boosted_machine.add_weak_machine(classifier.weak_machines[i], classifier.weights[i])
self.cascade.append(boosted_machine)
self.thresholds.append(threshold)
self._indices()"
210,"def create_from_boosted_machine(self, boosted_machine, classifiers_per_round, classification_thresholds=-5.):
""""""Creates this cascade from the given boosted machine, by simply splitting off strong classifiers that have classifiers_per_round weak classifiers.
**Parameters:**
``boosted_machine`` : :py:class:`bob.learn.boosting.BoostedMachine`
The strong classifier to split into a regular cascade.
``classifiers_per_round`` : int
The number of classifiers that each cascade step should contain.
``classification_threshold`` : float
A single threshold that will be applied in all rounds of the cascade.
""""""
indices = list(range(0, len(boosted_machine.weak_machines), classifiers_per_round))
if indices[-1] != len(boosted_machine.weak_machines): indices.append(len(boosted_machine.weak_machines))
self.cascade = []
self.indices = []
for i in range(len(indices)-1):
machine = bob.learn.boosting.BoostedMachine()
for index in range(indices[i], indices[i+1]):
machine.add_weak_machine(boosted_machine.weak_machines[index], boosted_machine.weights[index, 0])
self.cascade.append(machine)
if isinstance(classification_thresholds, (int, float)):
self.thresholds = [classification_thresholds] * len(self.cascade)
else:
self.thresholds = classification_thresholds"
211,"def generate_boosted_machine(self):
""""""generate_boosted_machine() -> strong
Creates a single strong classifier from this cascade by concatenating all strong classifiers.
**Returns:**
``strong`` : :py:class:`bob.learn.boosting.BoostedMachine`
The strong classifier as a combination of all classifiers in this cascade.
""""""
strong = bob.learn.boosting.BoostedMachine()
for machine, index in zip(self.cascade, self.indices):
weak = machine.weak_machines
weights = machine.weights
for i in range(len(weak)):
strong.add_weak_machine(weak[i], weights[i])