Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __table_args__(cls):
return (UniqueConstraint('name', 'group'),)
def __repr__(self):
return str(self.__class__.__name__) + " (" + str(self.name) + ")"
class GoldLabelKey(AnnotationKeyMixin, SnorkelBase):
pass
class LabelKey(AnnotationKeyMixin, SnorkelBase):
pass
class FeatureKey(AnnotationKeyMixin, SnorkelBase):
pass
class PredictionKey(AnnotationKeyMixin, SnorkelBase):
pass
class AnnotationMixin(object):
"""
Mixin class for defining annotation tables. An annotation is a value associated with a Candidate.
Examples include labels, features, and predictions.
New types of annotations can be defined by creating an annotation class and corresponding annotation,
for example:
.. code-block:: python
from .meta import SnorkelBase
from sqlalchemy import Column, String, Integer, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship, backref
class Feature(SnorkelBase):
"""A feature that Candidate."""
__tablename__ = 'feature'
id = Column(Integer, primary_key=True)
candidate_id = Column(Integer, ForeignKey('candidate.id'), nullable=False)
candidate = relationship('Candidate', backref=backref('features', cascade='all, delete-orphan', cascade_backrefs=False))
name = Column(String, nullable=False)
__table_args__ = (
UniqueConstraint(candidate_id, name),
)
def __repr__(self):
return "Feature (" + str(self.name) + " on " + str(self.candidate) + ")"
def __eq__(self, other):
try:
from __future__ import unicode_literals
from builtins import *
from sqlalchemy import (
Column, String, Integer, Float, Boolean, ForeignKey, UniqueConstraint,
MetaData
)
from sqlalchemy.orm import relationship, backref
from functools import partial
from snorkel.models.meta import SnorkelBase
from snorkel.models import snorkel_engine
from snorkel.utils import camel_to_under
class Candidate(SnorkelBase):
"""
An abstract candidate relation.
New relation types should be defined by calling candidate_subclass(),
**not** subclassing this class directly.
"""
__tablename__ = 'candidate'
id = Column(Integer, primary_key=True)
type = Column(String, nullable=False)
split = Column(Integer, nullable=False, default=0, index=True)
__mapper_args__ = {
'polymorphic_identity': 'candidate',
'polymorphic_on': type
}
__mapper_args__ = {
'polymorphic_identity': 'raw_text',
}
def get_parent(self):
return None
def get_children(self):
return None
def __repr__(self):
return "Raw Text " + str(self.text)
# Adds the corresponding table to the underlying database's schema
SnorkelBase.metadata.create_all(snorkel_engine)
@declared_attr
def candidate(cls):
return relationship('Candidate', backref=backref(camel_to_under(cls.__name__) + 's', cascade='all, delete-orphan', cascade_backrefs=False),
cascade_backrefs=False)
def __repr__(self):
return self.__class__.__name__ + " (" + str(self.key.name) + " = " + str(self.value) + ")"
class GoldLabel(AnnotationMixin, SnorkelBase):
"""A separate class for labels from human annotators or other gold standards."""
value = Column(Integer, nullable=False)
class Label(AnnotationMixin, SnorkelBase):
"""
A discrete label associated with a Candidate, indicating a target prediction value.
Labels are used to represent the output of labeling functions.
A Label's annotation key identifies the labeling function that provided the Label.
"""
value = Column(Integer, nullable=False)
class Feature(AnnotationMixin, SnorkelBase):
"""
An element of a representation of a Candidate in a feature space.
A Feature's annotation key identifies the definition of the Feature, e.g., a function that implements it
or the library name and feature name in an automatic featurization library.
from .meta import SnorkelBase, snorkel_postgres
from sqlalchemy import Column, String, Integer, Text, ForeignKey, UniqueConstraint
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import relationship, backref
from sqlalchemy.types import PickleType
from sqlalchemy.sql import select, text
class Context(SnorkelBase):
"""
A piece of content from which Candidates are composed.
"""
__tablename__ = 'context'
id = Column(Integer, primary_key=True)
type = Column(String, nullable=False)
stable_id = Column(String, unique=True, nullable=False)
__mapper_args__ = {
'polymorphic_identity': 'context',
'polymorphic_on': type
}
def get_sentence_generator(self):
raise NotImplementedError()
# Every annotation is with respect to a candidate
@declared_attr
def candidate_id(cls):
return Column('candidate_id', Integer, ForeignKey('candidate.id', ondelete='CASCADE'), primary_key=True)
@declared_attr
def candidate(cls):
return relationship('Candidate', backref=backref(camel_to_under(cls.__name__) + 's', cascade='all, delete-orphan', cascade_backrefs=False),
cascade_backrefs=False)
def __repr__(self):
return self.__class__.__name__ + " (" + str(self.key.name) + " = " + str(self.value) + ")"
class GoldLabel(AnnotationMixin, SnorkelBase):
"""A separate class for labels from human annotators or other gold standards."""
value = Column(Integer, nullable=False)
class Label(AnnotationMixin, SnorkelBase):
"""
A discrete label associated with a Candidate, indicating a target prediction value.
Labels are used to represent the output of labeling functions.
A Label's annotation key identifies the labeling function that provided the Label.
"""
value = Column(Integer, nullable=False)
class Feature(AnnotationMixin, SnorkelBase):
"""A separate class for labels from human annotators or other gold standards."""
value = Column(Integer, nullable=False)
class Label(AnnotationMixin, SnorkelBase):
"""
A discrete label associated with a Candidate, indicating a target prediction value.
Labels are used to represent the output of labeling functions.
A Label's annotation key identifies the labeling function that provided the Label.
"""
value = Column(Integer, nullable=False)
class Feature(AnnotationMixin, SnorkelBase):
"""
An element of a representation of a Candidate in a feature space.
A Feature's annotation key identifies the definition of the Feature, e.g., a function that implements it
or the library name and feature name in an automatic featurization library.
"""
value = Column(Float, nullable=False)
class Prediction(AnnotationMixin, SnorkelBase):
"""
A probability associated with a Candidate, indicating the degree of belief that the Candidate is true.
A Prediction's annotation key indicates which process or method produced the Prediction, e.g., which
model with which ParameterSet.
"""
or the library name and feature name in an automatic featurization library.
"""
value = Column(Float, nullable=False)
class Prediction(AnnotationMixin, SnorkelBase):
"""
A probability associated with a Candidate, indicating the degree of belief that the Candidate is true.
A Prediction's annotation key indicates which process or method produced the Prediction, e.g., which
model with which ParameterSet.
"""
value = Column(Float, nullable=False)
class StableLabel(SnorkelBase):
"""
A special secondary table for preserving labels created by *human annotators* (e.g. in the Viewer)
in a stable format that does not cascade, and is independent of the Candidate ids.
"""
__tablename__ = 'stable_label'
context_stable_ids = Column(String, primary_key=True) # ~~ delimited list of the context stable ids
annotator_name = Column(String, primary_key=True)
split = Column(Integer, default=0)
value = Column(Integer, nullable=False)
def __repr__(self):
return "%s (%s : %s)" % (self.__class__.__name__, self.annotator_name, self.value)
def name(cls):
return Column(String, nullable=False)
@declared_attr
def group(cls):
return Column(Integer, nullable=False, default=0)
@declared_attr
def __table_args__(cls):
return (UniqueConstraint('name', 'group'),)
def __repr__(self):
return str(self.__class__.__name__) + " (" + str(self.name) + ")"
class GoldLabelKey(AnnotationKeyMixin, SnorkelBase):
pass
class LabelKey(AnnotationKeyMixin, SnorkelBase):
pass
class FeatureKey(AnnotationKeyMixin, SnorkelBase):
pass
class PredictionKey(AnnotationKeyMixin, SnorkelBase):
pass
class AnnotationMixin(object):