Ejemplo n.º 1
0
    def __sub__(self, other):
        if not isinstance(other, CountFingerprint):
            raise E3FPInvalidFingerprintError(
                "variable is not CountFingerprint.")

        if self.bits != other.bits:
            raise E3FPBitsValueError(
                "cannot subtract fingerprints of different sizes")

        if self.level == other.level:
            level = self.level
        else:
            level = -1

        new_counts = self.counts.copy()
        for k, v in other.counts.items():
            new_counts[k] = new_counts.get(k, 0) - v

        new_indices = np.asarray(new_counts.keys(), dtype=np.long)

        if other.__class__ is FloatFingerprint:
            new_class = FloatFingerprint
        else:
            new_class = self.__class__

        return new_class(new_indices,
                         counts=new_counts,
                         bits=self.bits,
                         level=level)
Ejemplo n.º 2
0
    def __ne__(self, other):
        if not isinstance(other, Fingerprint):
            raise E3FPInvalidFingerprintError(
                "variable is %s not CountFingerprint" %
                (other.__class__.__name__))

        return not self.__eq__(other)
Ejemplo n.º 3
0
    def from_fingerprint(cls, fp, **kwargs):
        """Initialize by copying existing fingerprint.

        Parameters
        ----------
        fp : Fingerprint
            Existing fingerprint.
        name : str, optional
            Name of fingerprint.
        props : dict, optional
            Custom properties of fingerprint, consisting of a string keyword
            and some value.

        Returns
        -------
        fingerprint : Fingerprint
        """
        if not isinstance(fp, Fingerprint):
            raise E3FPInvalidFingerprintError(
                "variable is %s not Fingerprint" % (fp.__class__.__name__))

        counts = dict([(i, c) for i, c in fp.counts.items() if c > 0])
        new_fp = cls.from_counts(counts, bits=fp.bits, level=fp.level)
        new_fp.update_props(fp.props)
        new_fp.folded_fingerprint = dict([
            (k, v.__class__.from_fingerprint(v))
            for k, v in fp.folded_fingerprint.items()
        ])
        return new_fp
Ejemplo n.º 4
0
def dtype_from_fptype(fp_type):
    """Get NumPy data type from fingerprint type.

    Parameters
    ----------
    fp_type : class or Fingerprint
        Class of fingerprint

    Returns
    -------
    numpy.dtype
        NumPy data type
    """
    if isinstance(fp_type, Fingerprint):
        fp_type = fp_type.__class__
    if fp_type is Fingerprint:
        return FP_DTYPE
    elif fp_type is CountFingerprint:
        return COUNT_FP_DTYPE
    elif fp_type is FloatFingerprint:
        return FLOAT_FP_DTYPE
    else:
        raise E3FPInvalidFingerprintError(
            "fp_type {} is not a valid fp_type.".format(fp_type)
        )
Ejemplo n.º 5
0
    def from_fingerprint(cls, fp, **kwargs):
        """Initialize by copying existing fingerprint.

        Parameters
        ----------
        fp : Fingerprint
            Existing fingerprint.

        Returns
        -------
        fingerprint : Fingerprint
        """
        if not isinstance(fp, Fingerprint):
            raise E3FPInvalidFingerprintError(
                "variable is %s not Fingerprint" % (fp.__class__.__name__)
            )

        new_fp = cls.from_indices(fp.indices, bits=fp.bits, level=fp.level)
        new_fp.update_props(fp.props)
        new_fp.folded_fingerprint = dict(
            [
                (k, v.__class__.from_fingerprint(v))
                for k, v in fp.folded_fingerprint.items()
            ]
        )
        return new_fp
Ejemplo n.º 6
0
    def __eq__(self, other):
        if not isinstance(other, Fingerprint):
            raise E3FPInvalidFingerprintError(
                "variable is %s not Fingerprint" % (other.__class__.__name__))

        return (self.level == other.level and self.bits == other.bits
                and self.__class__ == other.__class__ and np.all(
                    np.in1d(self.indices, other.indices, assume_unique=True)))
Ejemplo n.º 7
0
    def __eq__(self, other):
        if not isinstance(other, CountFingerprint):
            raise E3FPInvalidFingerprintError(
                "variable is %s not CountFingerprint" %
                (other.__class__.__name__))

        return (self.level == other.level and self.bits == other.bits
                and self.counts == other.counts
                and self.__class__ == other.__class__)
Ejemplo n.º 8
0
    def __or__(self, other):
        if not isinstance(other, Fingerprint):
            raise E3FPInvalidFingerprintError(
                "variable is %s not Fingerprint" % (other.__class__.__name__))

        if self.bits != other.bits:
            raise E3FPBitsValueError(
                "cannot compare fingerprints of different sizes")

        return Fingerprint(np.union1d(self.indices, other.indices),
                           bits=self.bits)
Ejemplo n.º 9
0
    def __sub__(self, other):
        if not isinstance(other, Fingerprint):
            raise E3FPInvalidFingerprintError(
                "variable is %s not Fingerprint" % (other.__class__.__name__))

        if self.bits != other.bits:
            raise E3FPBitsValueError(
                "cannot subtract fingerprints of different sizes")

        return Fingerprint(np.setdiff1d(self.indices,
                                        other.indices,
                                        assume_unique=True),
                           bits=self.bits)