def induced_substream(self, tns): if isinstance(tns, ABC.TemporalNodeSet): if bool(self) and bool(tns): assert tns.discrete == self.discrete if isinstance(tns, TemporalNodeSetB): tdf = self.df_[self.df_['v'].isin(tns.nodeset_) & self.df_['u'].isin(tns.nodeset_)] if self.weighted: tdf = tdf.intersection(ts_to_df(tns.timeset_), on_columns=['u', 'v'], by_key=False) else: tdf = tdf.intersection(ts_to_df(tns.timeset_), on_columns=['u', 'v'], by_key=False, intersection='unweighted') if not tdf.empty: return TemporalLinkSetDF(tdf, discrete=self.discrete, weighted=self.weighted) else: base_df = tns_to_df(tns) out = (self.df_.cartesian_intersection( base_df, cartesian_intersection_function='unweighted') if self.weighted else self.df_.cartesian_intersection(base_df)) return TemporalLinkSetDF(out, discrete=self.discrete, weighted=self.weighted) else: raise UnrecognizedTemporalNodeSet('ns') return TemporalLinkSetDF(discrete=self.discrete, weighted=self.weighted)
def induced_substream(self, ns): if isinstance(ns, ABC.TemporalNodeSet): if bool(self) and bool(ns): assert self.discrete == ns.discrete idf_ = class_interval_df(discrete=self.discrete, weighted=self.weighted) if isinstance(ns, TemporalNodeSetB): # if all nodes appear at all times # take the nodes that are in the nodeset tdf, ts = self.df_[self.df_['v'].isin(ns.nodeset_) & self.df_['u'].isin(ns.nodeset_)], ns.timeset_ if isinstance(ts, ABC.ITimeSet): ts = its_to_idf(ts) else: df, ts = idf_(self.df), ts_to_df(ts) # and intersect thorugh time with the time-set tdf = tdf.intersection(ts, on_columns=['u', 'v'], by_key=False) else: if isinstance(ns, ABC.ITemporalNodeSet): df, base_df = self.df, ins_to_idf(ns) else: df, base_df = idf_(self.df), tns_to_df(ns) if self.weighted: tdf = df.cartesian_intersection(base_df, cartesian_intersection_function='unweighted') else: tdf = df.cartesian_intersection(base_df) if not tdf.empty: if isinstance(tdf, idf_): tdf = tdf.drop(columns=['tf']) return self.__class__(tdf, discrete=self.discrete, weighted=self.weighted) else: raise UnrecognizedTemporalNodeSet('ns') return self.__class__(discrete=self.discrete, weighted=self.weighted)
def __and__(self, tns): if isinstance(tns, ABC.TemporalNodeSet): assert self.discrete == tns.discrete if tns and bool(self): if isinstance(tns, sg.TemporalNodeSetB): return TemporalNodeSetDF(self.df[self.df.u.isin( tns.nodeset)].intersection(ts_to_df(tns.timeset_), by_key=False)) else: if not isinstance(tns, TemporalNodeSetDF): try: return tns & self except NotImplementedError: pass df = tns_to_df(tns).intersection(self.df) if not df.empty: if isinstance(tns, ABC.ITemporalNodeSet): from .itemporal_node_set_df import ITemporalNodeSetDF return ITemporalNodeSetDF( df.drop(columns=['tf'] + ([] if self.discrete else ['s', 'f'])), discrete=self.discrete) else: return TemporalNodeSetDF(df, discrete=self.discrete) else: raise UnrecognizedTemporalNodeSet('second operand') return TemporalNodeSetDF(discrete=self.discrete)
def __sub__(self, ns): if isinstance(ns, ABC.TemporalNodeSet): if bool(self): if ns: assert ns.discrete == self.discrete if isinstance(ns, ABC.ITemporalNodeSet): # If we have an instantaneous object - take the difference, by extracting the df return self.__class__(self.df.difference( utils.ins_to_idf(ns)), discrete=self.discrete) else: # If we don't have, convert our object to a non-instantaneous object, # subtract and then by converting back to our to our original type, we will have our valid result. df = self.df df['tf'] = df['ts'] from .temporal_node_set_df import TemporalNodeSetDF return self.__class__( (TemporalNodeSetDF(df, discrete=self.discrete) - ns).df.drop(columns=['tf']), discrete=self.discrete) else: return self.copy() else: raise UnrecognizedTemporalNodeSet('second operand') return self.__class__()
def issuperset(self, ts): if isinstance(ts, ABC.TimeSet): assert ts.discrete == self.discrete if bool(ts): if not isinstance(ts, self.__class__): ts = self.__class__(ts) return not bool(ts) or self.df.issuper(ts.df) else: raise UnrecognizedTemporalNodeSet('ns') return False
def issuperset(self, tns): if isinstance(tns, ABC.TemporalNodeSet): if bool(self) or bool(tns): if isinstance(tns, TemporalNodeSetB): return (self.timeset_.issuperset(tns.timeset_) and self.nodeset_.issuperset(tns.nodeset_)) else: return TemporalNodeSetDF(self).issuperset(tns) else: return True else: raise UnrecognizedTemporalNodeSet('ns') return False
def __and__(self, ns): if isinstance(ns, ABC.TemporalNodeSet): assert ns.discrete == self.discrete if isinstance(ns, TemporalNodeSetB): if ns and bool(self): return TemporalNodeSetB( timeset=self.timeset_ & ns.timeset_, nodeset=self.nodeset_ & ns.nodeset_) else: return ns & self else: raise UnrecognizedTemporalNodeSet('right operand') return TemporalNodeSetB()
def temporal_neighborhood(self, ns, direction='out'): # if df join on u / combine (intersect) and the union intervals (for union) # if range derror = False if not isinstance(ns, ABC.TemporalNodeSet): raise UnrecognizedTemporalNodeSet('ns') assert self.discrete == ns.discrete cidf_ = class_interval_df(discrete=self.discrete, weighted=self.weighted) if isinstance(ns, TemporalNodeSetB): # if all nodes appear at all times # take the neighbors if direction == 'out': df = self.df.rename(columns={'v': 'u', 'u': 'v'}) df = df[df.v.isin(ns.nodeset_)].drop('v', axis=1) elif direction == 'in': df = self.df[self.df.v.isin(ns.nodeset_)].drop('v', axis=1) elif direction == 'both': df = self.df.rename(columns={'v': 'u', 'u': 'v'}) df = df[df.v.isin(ns.nodeset_)].drop('v', axis=1) df = df.append(self.df[self.df.v.isin(ns.nodeset_)].drop('v', axis=1)) else: derror = True if not derror: ts = ns.timeset_ if isinstance(ts, ABC.ITimeSet): ts = its_to_idf(ts) else: df, ts = cidf_(self.df), ts_to_df(ts) # and apply intersection for all keys with the same time-set df = df.intersection(df, ts, on_columns=['u', 'v'], by_key=False) else: if isinstance(ns, ABC.ITemporalNodeSet): df, base_df = self.df, ins_to_idf(ns) else: df, base_df = cidf_(self.df), tns_to_df(ns) if direction == 'out': df = df.map_intersection(base_df) elif direction == 'in': df = df.rename(columns={'u': 'v', 'v': 'u'}).map_intersection(base_df) elif direction == 'both': dfo, df = df, df.map_intersection(base_df) df = df.append(dfo.rename(columns={'u': 'v', 'v': 'u'}).map_intersection(base_df), ignore_index=True, merge=True) else: derror = True if derror: raise UnrecognizedDirection() if isinstance(df, cidf_): df = df.drop(columns=['tf']) return ITemporalNodeSetDF(df, no_duplicates=False, discrete=self.discrete)
def issuperset(self, tns): if isinstance(tns, ABC.TemporalNodeSet): assert self.discrete == tns.discrete if not bool(self): return False elif bool(tns): if isinstance(tns, sg.TemporalNodeSetB): ns = tns.nodeset if ns.issuperset(self.nodeset): return self.df[self.df.u.isin(ns)].issuper( ts_to_df(tns.timeset_), by_key=False) else: return not tns or self.df.issuper(tns_to_df(tns)) else: raise UnrecognizedTemporalNodeSet('ns') return False
def __or__(self, ns): if isinstance(ns, ABC.TemporalNodeSet): if not bool(self): return ns.copy() assert ns.discrete == self.discrete if isinstance(ns, TemporalNodeSetB): if bool(ns): return TemporalNodeSetB( timeset=self.timeset_ | ns.timeset_, nodeset=self.nodeset_ | ns.nodeset_) else: return self.copy() else: return ns | self else: raise UnrecognizedTemporalNodeSet('right operand') return TemporalNodeSetB()
def temporal_neighborhood(self, tns, direction='out'): # if df join on u / combine (intersect) and the union intervals (for union) # if range derror = False if not isinstance(tns, ABC.TemporalNodeSet): raise UnrecognizedTemporalNodeSet('ns') if isinstance(tns, TemporalNodeSetB): if direction == 'out': df = self.df.rename(columns={'v': 'u', 'u': 'v'}) df = df[df.v.isin(tns.nodeset_)].drop('v', axis=1) elif direction == 'in': df = self.df[self.df.v.isin(tns.nodeset_)].drop('v', axis=1) elif direction == 'both': df = self.df.rename(columns={'v': 'u', 'u': 'v'}) df = df[df.v.isin(tns.nodeset_)].drop('v', axis=1, merge=False) df = df.append(self.df[self.df.v.isin(tns.nodeset_)].drop( 'v', axis=1, merge=False), merge=True) else: derror = True if not derror: df = df.intersection(ts_to_df(tns.timeset_), on_columns=['u', 'v'], by_key=False) else: base_df = tns_to_df(tns) if direction == 'out': df = self.df elif direction == 'in': df = self.df.rename(columns={'u': 'v', 'v': 'u'}) elif direction == 'both': df = self.df.append(self.df.rename(columns={ 'u': 'v', 'v': 'u' }), ignore_index=True, merge=True) else: derror = True df = df.map_intersection(base_df) if derror: raise UnrecognizedDirection() return TemporalNodeSetDF(df, disjoint_intervals=False, discrete=self.discrete)
def __sub__(self, tns): if isinstance(tns, ABC.TemporalNodeSet): assert tns.discrete == self.discrete if bool(self): if bool(tns): if isinstance(tns, sg.TemporalNodeSetB): dfp = self.df[self.df.u.isin(tns.nodeset)].difference( ts_to_df(tns.timeset_), by_key=False) df = self.df[~self.df.u.isin(tns.nodeset)].append( dfp, ignore_index=True, sort=False) else: df = self.df.difference(tns_to_df(tns)) return TemporalNodeSetDF(df, discrete=self.discrete) else: return self.copy() else: raise UnrecognizedTemporalNodeSet('second operand') return TemporalNodeSetDF(discrete=self.discrete)
def __sub__(self, tns): if isinstance(tns, ABC.TemporalNodeSet): if bool(self): if bool(tns): assert tns.discrete == self.discrete if isinstance(tns, TemporalNodeSetB): # If all time-stamps appear at all times also for the second temporal-node-set. ns_in = self.nodeset_ & tns.nodeset_ # For the subset of nodes that are common if bool(ns_in): # Take the difference in time. ts_diff = self.timeset_ - tns.timeset_ # Calculate their difference in time if bool(ts_diff): # First take the difference in nodes with all the times. tnsa_ = TemporalNodeSetB( nodeset=self.nodeset_ - tns.nodeset_, timeset=self.timeset_) # And append it to the common nodes with their difference in time. tnsb_ = TemporalNodeSetB(nodeset=ns_in, timeset=ts_diff) iter_ = chain(iter(tnsa_), iter(tnsb_)) nsa = TemporalNodeSetDF( iter_, disjoint_intervals=True, discrete=self.discrete) return nsa ns_diff = self.nodeset_ - tns.nodeset_ if bool(ns_diff): return TemporalNodeSetB(nodeset=ns_diff, timeset=self.timeset_) else: try: return tns.__rsub__(self) except (AttributeError, NotImplementedError): return TemporalNodeSetDF( self, discrete=self.discrete) - TemporalNodeSetDF( tns, discrete=self.discrete) else: return self.copy() else: raise UnrecognizedTemporalNodeSet('right operand') return TemporalNodeSetB()
def issuperset(self, ns): if isinstance(ns, ABC.TemporalNodeSet): if not bool(self): return False elif bool(ns): assert ns.discrete == self.discrete if isinstance(ns, ABC.ITemporalNodeSet): return self.df.issuper(utils.ins_to_idf(ns)) else: df = self.df df['tf'] = df['ts'] from .temporal_link_set_df import TemporalNodeSetDF return TemporalNodeSetDF( df, discrete=self.discrete).issuper(ns) else: return True else: raise UnrecognizedTemporalNodeSet('ns') return False
def __and__(self, ns): if isinstance(ns, ABC.TemporalNodeSet): if isinstance(ns, ABC.ITemporalNodeSet): if ns and bool(self): assert ns.discrete == self.discrete if not isinstance(ns, self.__class__): try: # If not an instance of this class, # see if the other class has a method. return ns & self except NotImplementedError: pass # Or else convert from its iterator form. df = utils.ins_to_idf(ns).intersection(self.df) if not df.empty: return self.__class__(df, discrete=self.discrete) else: return ns & self else: raise UnrecognizedTemporalNodeSet('second operand') return self.__class__()
def __or__(self, ns): if isinstance(ns, ABC.TemporalNodeSet): if not bool(self): return ns.copy() if bool(ns): if isinstance(ns, ABC.ITemporalNodeSet): if not isinstance(ns, self.__class__): try: # If not an instance of this class, # see if the other class has a method. return ns | self except NotImplementedError: pass # Or else convert from its iterator form. return self.__class__(self.df.union(utils.ins_to_idf(ns)), discrete=self.discrete) else: return ns | self else: return self.copy() else: raise UnrecognizedTemporalNodeSet('second operand') return self.__class__()
def __or__(self, tns): if isinstance(tns, ABC.TemporalNodeSet): assert tns.discrete == self.discrete if not bool(self): return tns.copy() if tns: if isinstance(tns, sg.TemporalNodeSetB): ns, tdf = tns.nodeset, ts_to_df(tns.timeset_) df = self.df[~self.df.u.isin(ns)].append( self.df[self.df.u.isin(ns)].union(tdf, by_key=False), ignore_index=True, merge=False) nstd = ns - self.nodeset if bool(nstd): plist = [(n, ) + key for n in nstd for key in itertuples_raw(tdf, tns.discrete)] dfp = init_interval_df(data=plist, discrete=tns.discrete, keys=['u'], disjoint_intervals=True) df = df.append(dfp, merge=False, ignore_index=True, sort=False) return TemporalNodeSetDF(df.merge(inplace=False), discrete=self.discrete) elif not isinstance(tns, TemporalNodeSetDF): try: return tns | self except NotImplementedError: pass return TemporalNodeSetDF(self.df.union(tns_to_df(tns))) else: return self.copy() else: raise UnrecognizedTemporalNodeSet('second operand') return TemporalNodeSetDF(discrete=self.discrete)