# Buggy code
def __repr__(self):
    if self._source is None:
        return f"<Vis  ({str(self._intent)}) mark: {self._mark}, score: {self.score} >"
    filter_intents = None
    channels, additional_channels = [], []
    for clause in self._inferred_intent:
        if hasattr(clause, "value"):
            if clause.value != "":
                filter_intents = clause
        if hasattr(clause, "attribute"):
            if clause.attribute != "":
                if clause.aggregation != "" and clause.aggregation is not None:
                    attribute = (
                        clause._aggregation_name.upper() + "(" + clause.attribute + ")"
                    )
                elif clause.bin_size > 0:
                    attribute = "BIN(" + clause.attribute + ")"
                else:
                    attribute = clause.attribute
                if clause.channel == "x":
                    channels.insert(0, [clause.channel, attribute])
                elif clause.channel == "y":
                    channels.insert(1, [clause.channel, attribute])
                elif clause.channel != "":
                    additional_channels.append([clause.channel, attribute])

    channels.extend(additional_channels)
    str_channels = ""
    for channel in channels:
        str_channels += channel[0] + ": " + channel[1] + ", "

    if filter_intents:
        return f"<Vis  ({str_channels[:-2]} -- [{filter_intents.attribute}{filter_intents.filter_op}{filter_intents.value}]) mark: {self._mark}, score: {self.score} >"
    else:
        return f"<Vis  ({str_channels[:-2]}) mark: {self._mark}, score: {self.score} >"


# Traceback (KeyError)
C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\formatters.py:345: UserWarning:
Unexpected error in rendering Lux widget and recommendations. Falling back to Pandas display.
Please report the following issue on Github: https://github.com/lux-org/lux/issues

C:\ProgramData\Anaconda3\lib\site-packages\lux\core\frame.py:689: UserWarning:Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2898, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1032, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1039, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\lux\core\frame.py", line 641, in _repr_html_
self.maintain_recs()
File "C:\ProgramData\Anaconda3\lib\site-packages\lux\core\frame.py", line 472, in maintain_recs
custom_action_collection = custom_actions(rec_df)
File "C:\ProgramData\Anaconda3\lib\site-packages\lux\action\custom.py", line 75, in custom_actions
recommendation = lux.actions.__getattr__(action_name).action(ldf)
File "C:\ProgramData\Anaconda3\lib\site-packages\lux\action\enhance.py", line 67, in enhance
vis.score = interestingness(vis, ldf)
File "C:\ProgramData\Anaconda3\lib\site-packages\lux\interestingness\interestingness.py", line 149, in interestingness
chi2_score = chi2_contingency(contingency_table)[0] * 0.9 ** (
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\stats\contingency.py", line 241, in chi2_contingency
observed = np.asarray(observed)
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 882, in __getitem__
return self._get_value(key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 990, in _get_value
loc = self.index.get_loc(label)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2900, in get_loc
raise KeyError(key) from err
KeyError: 0

# Fixed code
def __repr__(self):
    all_clause = all([isinstance(unit, lux.Clause) for unit in self._inferred_intent])
    if all_clause:
        filter_intents = None
        channels, additional_channels = [], []
        for clause in self._inferred_intent:
            if hasattr(clause, "value"):
                if clause.value != "":
                    filter_intents = clause
            if hasattr(clause, "attribute"):
                if clause.attribute != "":
                    if clause.aggregation != "" and clause.aggregation is not None:
                        attribute = (
                            clause._aggregation_name.upper()
                            + "("
                            + clause.attribute
                            + ")"
                        )
                    elif clause.bin_size > 0:
                        attribute = "BIN(" + clause.attribute + ")"
                    else:
                        attribute = clause.attribute
                    if clause.channel == "x":
                        channels.insert(0, [clause.channel, attribute])
                    elif clause.channel == "y":
                        channels.insert(1, [clause.channel, attribute])
                    elif clause.channel != "":
                        additional_channels.append([clause.channel, attribute])

        channels.extend(additional_channels)
        str_channels = ""
        for channel in channels:
            str_channels += channel[0] + ": " + channel[1] + ", "

        if filter_intents:
            return f"<Vis  ({str_channels[:-2]} -- [{filter_intents.attribute}{filter_intents.filter_op}{filter_intents.value}]) mark: {self._mark}, score: {self.score} >"
        else:
            return (
                f"<Vis  ({str_channels[:-2]}) mark: {self._mark}, score: {self.score} >"
            )
    else:
        # When Vis not compiled (e.g., when self._source not populated), print original intent
        return f"<Vis  ({str(self._intent)}) mark: {self._mark}, score: {self.score} >"