class InCloseII(InClose): rnew = 1 initial_state = [core.Concept(), core.Concept()] def __init__(self, context): super().__init__(context) def _in_close(self, r, y): jchildren = list() rchildren = list() for j in range(y, self.context.matrix.shape[1]): if not j in self.concepts[r].intent: self.concepts[self.rnew].extent = set() for i in self.concepts[r].extent: if self.context[i,j]: self.concepts[self.rnew].extent = self.concepts[self.rnew].extent.union({i}) if self.concepts[self.rnew].extent == self.concepts[r].extent: self.concepts[r].intent = self.concepts[r].intent.union({j}) else: if self._is_cannonical(r, j): jchildren.append(j) rchildren.append(int(self.rnew)) self.concepts[self.rnew].intent = self.concepts[r].intent.union({j}) self.rnew += 1 self.concepts.append(core.Concept()) for k in range(len(jchildren)): self._in_close(rchildren[k], jchildren[k] + 1)
def _fast_generate_from(self, concept, y, N): # Sort whether a concept is certain or plausible based on minimal context if concept not in self.certain_concepts: if (not concept.intent) or (not concept.extent): self.certain_concepts.append(concept) elif self.min_context.matrix[list(concept.extent), list(concept.intent)].all(): self.certain_concepts.append(concept) else: self.plausible_concepts.append(concept) # if not certain, find certain subconcept extent = set( np.array(list( concept.extent))[self.min_context.matrix[np.ix_( list(concept.extent), list(concept.intent))].all(axis=1)]) intent = set( np.array(list( concept.intent))[self.min_context.matrix[np.ix_( list(concept.extent), list(concept.intent))].all(axis=0)]) if extent: if self._is_cannonical_extent(extent, concept.intent): self.certain_concepts.append( core.Concept(extent=extent, intent=concept.intent)) if intent: if self._is_cannonical_intent(concept.extent, intent): self.certain_concepts.append( core.Concept(extent=concept.extent, intent=intent)) q = queue.Queue() if concept.intent == set( self.max_context.attrs) or y > max(self.max_context.attrs): return for j in range(y, len(self.max_context.attrs)): Yj = set(a for a in self.max_context.attrs if a < j) if not j in concept.intent and (N[j].intersection(Yj)).issubset( concept.intent.intersection(Yj)): C = concept.extent.intersection( self.max_context.attr_closure(j)) D = self.max_context.obj_closure(C) new_concept = core.Concept(extent=C, intent=D) if concept.intent.intersection( Yj) == new_concept.intent.intersection(Yj): q.put((new_concept, j)) else: N[j] = D while not q.empty(): next_input = q.get() self._fast_generate_from(next_input[0], next_input[1] + 1, N)
def __init__(self, context, mincontext): self.max_context = context self.min_context = mincontext self.certain_concepts = [] self.plausible_concepts = [] self._fast_generate_from( core.Concept(extent=set(self.max_context.objs)), 0, {i: set() for i in self.max_context.attrs})
def _in_close(self, r, y): self.rnew += 1 self.concepts.append(core.Concept()) for j in range(y, self.context.matrix.shape[1]): self.concepts[self.rnew].extent = set() for i in self.concepts[r].extent: if self.context[i,j]: self.concepts[self.rnew].extent.add(i) if self.concepts[self.rnew].extent == self.concepts[r].extent: self.concepts[r].intent.add(j) else: if self._is_cannonical(r, j): self.concepts[self.rnew].intent = self.concepts[r].intent.union({j}) self._in_close(self.rnew, j + 1)
def _generate_from(self, concept, y): if concept not in self.concepts: self.concepts.append(concept) q = queue.Queue() if concept.intent == set( self.context.attrs) or y > max(self.context.attrs): return for j in range(y, len(self.context.attrs)): Yj = set(a for a in self.context.attrs if a < j) C = concept.extent.intersection(self.context.attr_closure(j)) D = self.context.obj_closure(C) new_concept = core.Concept(extent=C, intent=D) if concept.intent.intersection( Yj) == new_concept.intent.intersection(Yj): q.put((new_concept, j)) while not q.empty(): next_input = q.get() self._generate_from(next_input[0], next_input[1] + 1)
class InClose(core.BaseAlgorithm): rnew = 0 initial_state = [core.Concept()] def __init__(self, context): self.context = context self.concepts = self.initial_state.copy() self.concepts[0].extent = self.context.objs.keys() self._in_close(0, 0) if not self._is_closed(self.concepts[-1]): del self.concepts[-1] def _is_closed(self, concept): for con2 in self.concepts: if not concept.intent.difference(con2.intent): if not concept.extent.difference(con2.extent): return False return True def _is_cannonical(self, r, y): for col in reversed(range(y)): if col in self.concepts[r].intent: continue else: if all(self.context.matrix[list(self.concepts[self.rnew].extent), col]): return False return True def _in_close(self, r, y): self.rnew += 1 self.concepts.append(core.Concept()) for j in range(y, self.context.matrix.shape[1]): self.concepts[self.rnew].extent = set() for i in self.concepts[r].extent: if self.context[i,j]: self.concepts[self.rnew].extent.add(i) if self.concepts[self.rnew].extent == self.concepts[r].extent: self.concepts[r].intent.add(j) else: if self._is_cannonical(r, j): self.concepts[self.rnew].intent = self.concepts[r].intent.union({j}) self._in_close(self.rnew, j + 1)
def _in_close(self, r, y): jchildren = list() rchildren = list() for j in range(y, self.context.matrix.shape[1]): if not j in self.concepts[r].intent: self.concepts[self.rnew].extent = set() for i in self.concepts[r].extent: if self.context[i,j]: self.concepts[self.rnew].extent = self.concepts[self.rnew].extent.union({i}) if self.concepts[self.rnew].extent == self.concepts[r].extent: self.concepts[r].intent = self.concepts[r].intent.union({j}) else: if self._is_cannonical(r, j): jchildren.append(j) rchildren.append(int(self.rnew)) self.concepts[self.rnew].intent = self.concepts[r].intent.union({j}) self.rnew += 1 self.concepts.append(core.Concept()) for k in range(len(jchildren)): self._in_close(rchildren[k], jchildren[k] + 1)
def __init__(self, context): self.context = context self.concepts = [] self._generate_from(core.Concept(extent=set(self.context.objs)), 0)
def __init__(self, context): self.context = context self.concepts = [] self._fast_generate_from(core.Concept(extent=set(self.context.objs)), 0, {i: set() for i in self.context.attrs})
if concept.intent.intersection( Yj) == new_concept.intent.intersection(Yj): q.put((new_concept, j)) else: N[j] = D while not q.empty(): next_input = q.get() self._fast_generate_from(next_input[0], next_input[1] + 1, N) if __name__ == '__main__': tuples = [{0, 1, 3}, {1, 2}, {0, 2, 3, 4}, {1, 3}] expected = [ core.Concept(extent={0, 1, 2, 3}, intent=set()), core.Concept(extent={0, 2, 3}, intent={3}), core.Concept(extent={1, 2}, intent={2}), core.Concept(extent={0, 1, 3}, intent={1}), core.Concept(extent={0, 3}, intent={1, 3}), core.Concept(extent={1}, intent={1, 2}), core.Concept(extent={0, 2}, intent={0, 3}), core.Concept(extent={2}, intent={0, 2, 3, 4}), core.Concept(extent={0}, intent={0, 1, 3}), core.Concept(extent=set(), intent={0, 1, 2, 3, 4}) ] context = core.Context.from_attribute_sets(tuples) concepts = CbO(context) assert not [c for c in expected if c not in concepts