Beispiel #1
0
def test_fromstring_serialized(tmp_path, source, filename, includes_lattice):
    if filename is None:
        context = Context.fromstring(source, frmat='python-literal')
    else:
        target = tmp_path / filename
        kwargs = {'encoding': 'utf-8'}
        target.write_text(source, **kwargs)
        context = Context.fromfile(str(target),
                                   frmat='python-literal',
                                   **kwargs)

    assert context.objects == SERIALIZED['objects']
    assert context.properties == SERIALIZED['properties']
    assert context.bools == [
        (True, False, False, True, False, True, True, False, False, True),
        (True, False, False, True, False, True, False, True, True, False),
        (False, True, True, False, False, True, True, False, False, True),
        (False, True, True, False, False, True, False, True, True, False),
        (False, True, False, True, True, False, True, False, False, True),
        (False, True, False, True, True, False, False, True, True, False)
    ]

    if includes_lattice:
        assert 'lattice' in context.__dict__
    else:
        assert 'lattice' not in context.__dict__
Beispiel #2
0
    def __init__(self,
                 dataframe,
                 leaves,
                 annotate=None,
                 dummy_formatter=None,
                 keep_names=True,
                 comp_prefix=None,
                 col_formatter=None,
                 na_value=None,
                 AOC=False,
                 collections=False,
                 verbose=True):
        """
        Arguments:
            dataframe (:class:`pandas:pandas.DataFrame`): A dataframe
            leaves (dict): Dictionnaire de microclasses
            annotate (dict): Extra annotations to add on lattice.
                Of the form: {<object label>:<annotation>}
            dummy_formatter (func): Function to make dummies from the table. (default to panda's)
            keep_names (bool): whether to keep original column names when dropping duplicate dummy columns.
            comp_prefix (str): If there are two sets of properties, the prefix used to distinguish column names.
            AOC (bool): Whether to limit ourselves to Attribute or Object Concepts.
            col_formatter (func): Function to format columns in the context table.
            na_value : A value tu use as "Na". Defaults to `None`
            collections (bool): Whether the table contains :class:`representations.patterns.PatternCollection` objects.
        """

        self.comp = comp_prefix  # whether there are two sets of properties.
        if na_value is not None:
            dataframe = dataframe.applymap(lambda x: None
                                           if x == na_value else x)
        if collections:
            dummies = to_dummies(dataframe, keep_names=keep_names)
        elif dummy_formatter:
            dummies = dummy_formatter(dataframe)
        else:
            dummies = pd.get_dummies(dataframe, prefix_sep="=")
            dummies = dummies.applymap(lambda x: "X" if x == 1 else "")
        if col_formatter:
            dummies.columns = col_formatter(dummies.columns)
        if verbose:
            print("Reading the context and building the lattice...")
        context_str = dummies.to_csv()
        c1 = Context.fromstring(context_str, frmat='csv')
        self.context = c1
        self.lattice = c1.lattice
        if annotate:
            for label in annotate:
                if label in self.lattice.supremum.extent:
                    self.lattice[[label
                                  ]]._extra_qumin_annotation = annotate[label]

        self.leaves = leaves
        if verbose:
            print("Converting to qumin node...")
        if AOC:
            self.nodes = self._lattice_to_nodeAOC()
        else:
            self.nodes = self._lattice_to_node()
        font = {'family': 'DejaVu Sans', 'weight': 'normal', 'size': 9}
        matplotlib.rc('font', **font)