def substream(self, nsu=None, ts=None):
        if nsu is not None:
            if not isinstance(nsu, ABC.NodeSet):
                try:
                    nsu = NodeSetS(nsu)
                except Exception as ex:
                    raise UnrecognizedNodeSet('nsu: ' + ex)
        if ts is not None:
            if not isinstance(ts, ABC.TimeSet):
                try:
                    ts = TimeSetDF(ts, discrete=self.discrete)
                except Exception as ex:
                    raise UnrecognizedTimeSet('ts: ' + ex)
        if all(o is None for o in [nsu, ts]):
            return self.copy()
        if bool(self) and all((o is None or bool(o)) for o in [nsu, ts]):
            if nsu is not None:
                df = self.df[self.df.u.isin(nsu)]
            else:
                df = self.df

            if ts is not None:
                df = df.intersection(ts_to_df(ts),
                                     by_key=False,
                                     on_column=['u'])
            return self.__class__(df, discrete=self.discrete)
        else:
            return self.__class__()
Beispiel #2
0
 def __or__(self, ts):
     if isinstance(ts, ABC.ITimeSet):
         # If instantaneous
         if not bool(self):
             return ts.copy()
         if bool(ts):
             assert self.discrete == ts.discrete
             if not isinstance(ts, self.__class__):
                 # If not of the same class
                 try:
                     # check if the other class has a method for it
                     return ts | self
                 except NotImplementedError:
                     # Else cast to class (through iterator)
                     ts = self.__class__(ts, discrete=self.discrete)
             return self.__class__(self.times_ | ts.times_,
                                   discrete=self.discrete)
         else:
             return self.copy()
     elif isinstance(ts, ABC.TimeSet):
         assert self.discrete == ts.discrete
         # If our input is not instantaneous, the result will probably be not instantaneous
         uni = self.timeset_df | ts
         if any(a != b for a, b in uni):
             # If so return output as is
             return uni
         else:
             # Else cast to the instantaneous class of `self`
             return self.__class__((a for a, _ in uni),
                                   discrete=self.discrete)
     else:
         raise UnrecognizedTimeSet('right operand')
Beispiel #3
0
    def substream(self, nsu=None, nsv=None, ts=None):
        if nsu is not None:
            # check if we have a valid nodeset or cast
            if not isinstance(nsu, ABC.NodeSet):
                try:
                    nsu = NodeSetS(nsu)
                except Exception as ex:
                    raise UnrecognizedNodeSet('nsu: ' + str(ex))
        if nsv is not None:
            # check if we have a valid nodeset or cast
            if not isinstance(nsv, ABC.NodeSet):
                try:
                    nsv = NodeSetS(nsv)
                except Exception as ex:
                    raise UnrecognizedNodeSet('nsv: ' + str(ex))
        if ts is not None:
            # check if we have a valid nodeset or try to cast
            if not isinstance(ts, ABC.TimeSet):
                try:
                    ts = list(ts)
                    if any(isinstance(t, Iterable) for t in ts):
                        ts = TimeSetDF(ts, discrete=self.discrete)
                    else:
                        ts = ITimeSetS(ts, discrete=self.discrete)
                except Exception as ex:
                    raise UnrecognizedTimeSet('ts: ' + str(ex))

        if all(o is None for o in [nsu, nsv, ts]):
            return self.copy()
        if bool(self) and all((o is None or bool(o)) for o in [nsu, nsv, ts]):
            # Extract the valid nodes
            if nsu is not None and nsv is not None:
                df = self.df[self.df.u.isin(nsu) & self.df.v.isin(nsv)]
            elif nsu is not None:
                df = self.df[self.df.u.isin(nsu)]
            elif nsv is not None:
                df = self.df[self.df.v.isin(nsv)]
            else:
                df = self.df

            if ts is not None:
                if ts.instantaneous:
                    # if instantaneous, intersect each key with the same time-stamps.
                    if self.weighted:
                        df = df.intersection(set(ts), by_key=False, on_column=['u', 'v'], intersection_function='unweighted')
                    else:
                        df = df.intersection(set(ts), by_key=False, on_column=['u', 'v'])
                else:
                    ts = list(ts_to_df(ts).sort_values(by='ts').itertuples(index=False, name=None))
                    # keep valid instant by doing a binary search on intervals.
                    df = [key for key in weighted_iter(df) if t_in(ts, key[2], 0, len(ts) - 1)]
            return self._init_base_class(df)
        else:
            return self._init_base_class()
Beispiel #4
0
 def issuperset(self, ts):
     if isinstance(ts, ABC.ITimeSet):
         assert self.discrete == ts.discrete
         if not isinstance(ts, ITimeSetS):
             ts = self.__class__(ts, discrete=self.discrete)
         return self.times_.issuperset(ts.times_)
     elif isinstance(ts, ABC.TimeSet):
         assert self.discrete == ts.discrete
         return self.timeset_df.issuperset(ts)
     else:
         raise UnrecognizedTimeSet('right operand')
Beispiel #5
0
    def substream(self, nsu=None, nsv=None, ts=None):
        if nsu is not None:
            if not isinstance(nsu, ABC.NodeSet):
                try:
                    nsu = NodeSetS(nsu)
                except Exception as ex:
                    raise UnrecognizedNodeSet('nsu: ' + str(ex))
        if nsv is not None:
            if not isinstance(nsv, ABC.NodeSet):
                try:
                    nsv = NodeSetS(nsv)
                except Exception as ex:
                    raise UnrecognizedNodeSet('nsv: ' + str(ex))
        if ts is not None:
            if not isinstance(ts, ABC.TimeSet):
                try:
                    ts = list(ts)
                    if any(isinstance(t, Iterable) for t in ts):
                        from stream_graph import TimeSetDF
                        ts = TimeSetDF(ts, discrete=self.timeset_.discrete)
                    else:
                        from stream_graph import ITimeSetS
                        ts = ITimeSetS(ts, discrete=self.timeset_.discrete)
                except Exception as ex:
                    raise UnrecognizedTimeSet('ts: ' + ex)
        if all(o is None for o in [nsu, nsv, ts]):
            return self.copy()

        if nsu is not None and nsv is not None:
            ns = nsu | nsv
        elif nsu is not None:
            ns = nsu
        elif nsv is not None:
            ns = nsv
        else:
            ns = None

        # Build the new nodeset
        nodeset = (self.nodeset if ns is None else self.nodeset_ & ns)

        # Build the new timeset
        timeset = (self.timeset if ts is None else self.timeset_ & ts)

        # Build the new temporal-nodeset
        temporal_nodeset = self.temporal_nodeset_.substream(nsu=ns, ts=ts)

        # Build the new temporal-nodeset
        temporal_linkset = self.temporal_linkset_.substream(nsu=nsu,
                                                            nsv=nsv,
                                                            ts=ts)

        return self.__class__(nodeset, timeset, temporal_nodeset,
                              temporal_linkset)
Beispiel #6
0
 def __sub__(self, ts):
     if isinstance(ts, ABC.TimeSet):
         assert self.discrete == ts.discrete
         if bool(self) and bool(ts):
             if not isinstance(ts, TimeSetDF):
                 try:
                     return ts.__rsub__(self)
                 except (AttributeError, NotImplementedError):
                     ts = self.__class__(ts)
             return self.__class__(self.df.difference(ts.df))
     else:
         raise UnrecognizedTimeSet('right operand')
     return self.copy()
Beispiel #7
0
 def __and__(self, ts):
     if isinstance(ts, ABC.TimeSet):
         assert self.discrete == ts.discrete
         if bool(self) and bool(ts):
             if not isinstance(ts, TimeSetDF):
                 try:
                     return ts & self
                 except NotImplementedError:
                     ts = self.__class__(ts)
             return self.__class__(self.df.intersection(ts.df))
     else:
         raise UnrecognizedTimeSet('right operand')
     return self.__class__()
Beispiel #8
0
 def __or__(self, ts):
     if isinstance(ts, ABC.TimeSet):
         assert self.discrete == ts.discrete
         if not bool(self):
             return ts.copy()
         if bool(ts):
             if not isinstance(ts, TimeSetDF):
                 try:
                     return ts | self
                 except NotImplementedError:
                     ts = self.__class__(ts)
             return self.__class__(self.df.union(ts.df))
         else:
             return self.copy()
     else:
         raise UnrecognizedTimeSet('right operand')
Beispiel #9
0
    def substream(self, nsu=None, ts=None):
        if nsu is not None:
            if not isinstance(nsu, ABC.NodeSet):
                try:
                    nsu = NodeSetS(nsu)
                except Exception as ex:
                    raise UnrecognizedNodeSet('nsu: ' + ex)
        if ts is not None:
            # check if we have a valid nodeset or try to cast
            if not isinstance(ts, ABC.TimeSet):
                try:
                    ts = list(ts)
                    if any(isinstance(t, Iterable) for t in ts):
                        from stream_graph import TimeSetDF
                        ts = TimeSetDF(ts, discrete=self.discrete)
                    else:
                        ts = ITimeSetS(ts, discrete=self.discrete)
                except Exception as ex:
                    raise UnrecognizedTimeSet('ts: ' + ex)
        if all(o is None for o in [nsu, ts]):
            return self.copy()
        if bool(self) and all((o is None or bool(o)) for o in [nsu, ts]):
            if nsu is not None:
                df = self.df[self.df.u.isin(nsu)]
            else:
                df = self.df

            if ts is not None:
                if ts.instantaneous:
                    # if instantaneous, intersect each key with the same time-stamps.
                    df = df.intersection(set(ts),
                                         by_key=False,
                                         on_column=['u', 'v'])
                else:
                    ts = list(
                        ts_to_df(ts).sort_values(by='ts').itertuples(
                            index=False, name=None))
                    # keep valid instant by doing a binary search on intervals.
                    df = [
                        key for key in df.itertuples()
                        if t_in(ts, key[1], 0,
                                len(ts) - 1)
                    ]
            return self.__class__(df, discrete=self.discrete)
        else:
            return self.__class__()
Beispiel #10
0
 def substream(self, nsu=None, nsv=None, ts=None):
     if nsu is not None:
         if not isinstance(nsu, ABC.NodeSet):
             try:
                 nsu = NodeSetS(nsu)
             except Exception as ex:
                 raise UnrecognizedNodeSet('nsu: ' + ex)
     if ts is not None:
         if not isinstance(ts, ABC.TimeSet):
             try:
                 ts = ts.__class__(ts, discrete=self.discrete)
             except Exception as ex:
                 raise UnrecognizedTimeSet('ts: ' + ex)
     if all(o is None for o in [nsu, nsv, ts]):
         return self.copy()
     if bool(self) and all((o is None or bool(o)) for o in [nsu, ts]):
         nodeset = (self.nodeset if nsu is None else self.nodeset_ & nsu)
         timeset = (self.timeset if ts is None else self.timeset_ & ts)
         return self.__class__(nodeset, timeset)
     else:
         return self.__class__()
Beispiel #11
0
 def __sub__(self, ts):
     if isinstance(ts, ABC.ITimeSet):
         if bool(self) and bool(ts):
             assert self.discrete == ts.discrete
             if not isinstance(ts, ITimeSetS):
                 try:
                     # check if the other class has a method for it
                     # [notice that we search for `__rsub__`]
                     return ts.__rsub__(self)
                 except (AttributeError, NotImplementedError):
                     # Else cast to class (through iterator)
                     ts = self.__class__(ts, discrete=self.discrete)
             return self.__class__(self.times_ - ts.times_,
                                   discrete=self.discrete)
     elif isinstance(ts, ABC.TimeSet):
         assert self.discrete == ts.discrete
         # Cast to TimeSetDF subtract and then keep the instantaneous part, to get a valid output
         return self.__class__((a for a, _ in (self.timeset_df - ts)),
                               discrete=self.discrete)
     else:
         raise UnrecognizedTimeSet('right operand')
     return self.copy()
Beispiel #12
0
 def __and__(self, ts):
     if isinstance(ts, ABC.ITimeSet):
         # If instantaneous
         if bool(self) and bool(ts):
             assert self.discrete == ts.discrete
             if not isinstance(ts, self.__class__):
                 # If not of the same class
                 try:
                     # check if the other class has a method for it
                     return ts & self
                 except NotImplementedError:
                     # Else cast to class (through iterator)
                     ts = self.__class__(ts, discrete=self.discrete)
             return self.__class__(self.times_ & ts.times_,
                                   discrete=self.discrete)
     elif isinstance(ts, ABC.TimeSet):
         assert self.discrete == ts.discrete
         # Cast to TimeSetDF, apply the operation and keep only the instants.
         return self.__class__((a for a, _ in (self.timeset_df & ts)),
                               discrete=self.discrete)
     else:
         raise UnrecognizedTimeSet('right operand')
     return self.__class__(discrete=self.discrete)