def get_classification(self): """ Get classification result map, returning a label-to-confidence dict. We do no place any guarantees on label value types as they may be represented in various forms (integers, strings, etc.). Confidence values are in the [0,1] range. :raises NoClassificationError: No classification labels/confidences yet set. :return: Label-to-confidence dictionary. :rtype: dict[collections.Hashable, float] """ q_select = self.SELECT_TMPL.format(**dict( table_name=self.table_name, type_col=self.type_col, uuid_col=self.uuid_col, classification_col=self.classification_col, )) q_select_values = { "type_val": self.type_name, "uuid_val": str(self.uuid) } conn = self._get_psql_connection() cur = conn.cursor() try: self._ensure_table(cur) cur.execute(q_select, q_select_values) r = cur.fetchone() # For server cleaning (e.g. pgbouncer) conn.commit() if not r: raise NoClassificationError( "No PSQL backed classification for " "label='%s' uuid='%s'" % (self.type_name, str(self.uuid))) else: b = r[0] c = cPickle.loads(str(b)) return c except: conn.rollback() raise finally: cur.close() conn.close()
def max_label(self): """ Get the label with the highest confidence. :raises NoClassificationError: No classification set. :return: The label with the highest confidence. :rtype: collections.Hashable """ m = (None, 0.) for i in six.iteritems(self.get_classification()): if i[1] > m[1]: m = i if m[0] is None: raise NoClassificationError("No classifications set to pick the " "max of.") return m[0]
def get_classification(self): """ Get classification result map, returning a label-to-confidence dict. We do no place any guarantees on label value types as they may be represented in various forms (integers, strings, etc.). Confidence values are in the [0,1] range. :raises NoClassificationError: No classification labels/confidences yet set. :return: Label-to-confidence dictionary. :rtype: dict[collections.Hashable, float] """ if not self.has_classifications(): raise NoClassificationError("No classification values.") with open(self.filepath) as f: return cPickle.load(f)
def max_label(self): """ Get the label with the highest confidence. :raises NoClassificationError: No classification set. :return: The label with the highest confidence. :rtype: collections.abc.Hashable """ # Temp (label, confidence) tuple #: :type: (collections.abc.Hashable, float) m = (None, NEG_INF) for i in six.iteritems(self.get_classification()): if i[1] > m[1]: m = i if m[0] is None: raise NoClassificationError("No classifications set to pick the " "max of.") return m[0]
def get_classification(self): """ Get classification result map, returning a label-to-confidence dict. We do no place any guarantees on label value types as they may be represented in various forms (integers, strings, etc.). Confidence values are in the [0,1] range. :raises NoClassificationError: No classification labels/confidences yet set. :return: Label-to-confidence dictionary. :rtype: dict[collections.Hashable, float] """ with self._c_lock: if self._c: return self._c else: raise NoClassificationError("No classification labels/values")