def maintain_recs(self, is_series="DataFrame"): # `rec_df` is the dataframe to generate the recommendations on # check to see if globally defined actions have been registered/removed if lux.config.update_actions["flag"] == True: self._recs_fresh = False show_prev = False # flag indicating whether rec_df is showing previous df or current self if self._prev is not None: rec_df = self._prev rec_df._message = Message() rec_df.maintain_metadata( ) # the prev dataframe may not have been printed before last_event = self.history._events[-1].name rec_df._message.add( f"Lux is visualizing the previous version of the dataframe before you applied <code>{last_event}</code>." ) show_prev = True else: rec_df = self rec_df._message = Message() # Add warning message if there exist ID fields if len(rec_df) == 0: rec_df._message.add(f"Lux cannot operate on an empty {is_series}.") elif len(rec_df) < 5 and not rec_df.pre_aggregated: rec_df._message.add( f"The {is_series} is too small to visualize. To generate visualizations in Lux, the {is_series} must contain at least 5 rows." ) elif self.index.nlevels >= 2 or self.columns.nlevels >= 2: rec_df._message.add( f"Lux does not currently support visualizations in a {is_series} " f"with hierarchical indexes.\n" f"Please convert the {is_series} into a flat " f"table via pandas.DataFrame.reset_index.") else: id_fields_str = "" inverted_data_type = lux.config.executor.invert_data_type( rec_df.data_type) if len(inverted_data_type["id"]) > 0: for id_field in inverted_data_type["id"]: id_fields_str += f"<code>{id_field}</code>, " id_fields_str = id_fields_str[:-2] rec_df._message.add( f"{id_fields_str} is not visualized since it resembles an ID field." ) rec_df._prev = None # reset _prev # Check that recs has not yet been computed if not hasattr(rec_df, "_recs_fresh") or not rec_df._recs_fresh: is_sql_tbl = lux.config.executor.name == "SQLExecutor" rec_infolist = [] from lux.action.row_group import row_group from lux.action.column_group import column_group # TODO: Rewrite these as register action inside default actions if rec_df.pre_aggregated: if rec_df.columns.name is not None: rec_df._append_rec(rec_infolist, row_group(rec_df)) rec_df._append_rec(rec_infolist, column_group(rec_df)) elif not (len(rec_df) < 5 and not rec_df.pre_aggregated and not is_sql_tbl) and not (self.index.nlevels >= 2 or self.columns.nlevels >= 2): from lux.action.custom import custom_actions # generate vis from globally registered actions and append to dataframe custom_action_collection = custom_actions(rec_df) for rec in custom_action_collection: rec_df._append_rec(rec_infolist, rec) lux.config.update_actions["flag"] = False # Store _rec_info into a more user-friendly dictionary form rec_df._recommendation = {} for rec_info in rec_infolist: action_type = rec_info["action"] vlist = rec_info["collection"] if len(vlist) > 0: rec_df._recommendation[action_type] = vlist rec_df._rec_info = rec_infolist rec_df.show_all_column_vis() self._widget = rec_df.render_widget() # re-render widget for the current dataframe if previous rec is not recomputed elif show_prev: rec_df.show_all_column_vis() self._widget = rec_df.render_widget() self._recs_fresh = True
def maintain_recs(self): # `rec_df` is the dataframe to generate the recommendations on # check to see if globally defined actions have been registered/removed if lux.update_actions["flag"] == True: self._recs_fresh = False show_prev = False # flag indicating whether rec_df is showing previous df or current self if self._prev is not None: rec_df = self._prev rec_df._message = Message() rec_df.maintain_metadata() # the prev dataframe may not have been printed before last_event = self.history._events[-1].name rec_df._message.add( f"Lux is visualizing the previous version of the dataframe before you applied <code>{last_event}</code>." ) show_prev = True else: rec_df = self rec_df._message = Message() # Add warning message if there exist ID fields id_fields_str = "" if len(rec_df.data_type["id"]) > 0: for id_field in rec_df.data_type["id"]: id_fields_str += f"<code>{id_field}</code>, " id_fields_str = id_fields_str[:-2] rec_df._message.add(f"{id_fields_str} is not visualized since it resembles an ID field.") rec_df._prev = None # reset _prev # Check that recs has not yet been computed if not hasattr(rec_df, "_recs_fresh") or not rec_df._recs_fresh: rec_infolist = [] from lux.action.custom import custom from lux.action.custom import custom_actions from lux.action.correlation import correlation from lux.action.univariate import univariate from lux.action.enhance import enhance from lux.action.filter import filter from lux.action.generalize import generalize from lux.action.row_group import row_group from lux.action.column_group import column_group if rec_df.pre_aggregated: if rec_df.columns.name is not None: rec_df._append_rec(rec_infolist, row_group(rec_df)) rec_df._append_rec(rec_infolist, column_group(rec_df)) else: if rec_df.recommendation == {}: # display conditions for default actions no_vis = lambda ldf: (ldf.current_vis is None) or ( ldf.current_vis is not None and len(ldf.current_vis) == 0 ) one_current_vis = ( lambda ldf: ldf.current_vis is not None and len(ldf.current_vis) == 1 ) multiple_current_vis = ( lambda ldf: ldf.current_vis is not None and len(ldf.current_vis) > 1 ) # globally register default actions lux.register_action("correlation", correlation, no_vis) lux.register_action("distribution", univariate, no_vis, "quantitative") lux.register_action("occurrence", univariate, no_vis, "nominal") lux.register_action("temporal", univariate, no_vis, "temporal") lux.register_action("Enhance", enhance, one_current_vis) lux.register_action("Filter", filter, one_current_vis) lux.register_action("Generalize", generalize, one_current_vis) lux.register_action("Custom", custom, multiple_current_vis) # generate vis from globally registered actions and append to dataframe custom_action_collection = custom_actions(rec_df) for rec in custom_action_collection: rec_df._append_rec(rec_infolist, rec) lux.update_actions["flag"] = False # Store _rec_info into a more user-friendly dictionary form rec_df.recommendation = {} for rec_info in rec_infolist: action_type = rec_info["action"] vlist = rec_info["collection"] if len(vlist) > 0: rec_df.recommendation[action_type] = vlist rec_df._rec_info = rec_infolist self._widget = rec_df.render_widget() # re-render widget for the current dataframe if previous rec is not recomputed elif show_prev: self._widget = rec_df.render_widget() self._recs_fresh = True
def maintain_recs(self): # `rec_df` is the dataframe to generate the recommendations on # check to see if globally defined actions have been registered/removed if lux.config.update_actions["flag"] == True: self._recs_fresh = False show_prev = False # flag indicating whether rec_df is showing previous df or current self if self._prev is not None: rec_df = self._prev rec_df._message = Message() rec_df.maintain_metadata( ) # the prev dataframe may not have been printed before last_event = self.history._events[-1].name rec_df._message.add( f"Lux is visualizing the previous version of the dataframe before you applied <code>{last_event}</code>." ) show_prev = True else: rec_df = self rec_df._message = Message() # Add warning message if there exist ID fields id_fields_str = "" inverted_data_type = lux.config.executor.invert_data_type( rec_df.data_type) if len(inverted_data_type["id"]) > 0: for id_field in inverted_data_type["id"]: id_fields_str += f"<code>{id_field}</code>, " id_fields_str = id_fields_str[:-2] rec_df._message.add( f"{id_fields_str} is not visualized since it resembles an ID field." ) rec_df._prev = None # reset _prev # Check that recs has not yet been computed if not hasattr(rec_df, "_recs_fresh") or not rec_df._recs_fresh: rec_infolist = [] from lux.action.row_group import row_group from lux.action.column_group import column_group # TODO: Rewrite these as register action inside default actions if rec_df.pre_aggregated: if rec_df.columns.name is not None: rec_df._append_rec(rec_infolist, row_group(rec_df)) rec_df._append_rec(rec_infolist, column_group(rec_df)) else: # if rec_df._recommendation == {}: from lux.action.custom import custom_actions # generate vis from globally registered actions and append to dataframe custom_action_collection = custom_actions(rec_df) for rec in custom_action_collection: rec_df._append_rec(rec_infolist, rec) lux.config.update_actions["flag"] = False # Store _rec_info into a more user-friendly dictionary form rec_df._recommendation = {} for rec_info in rec_infolist: action_type = rec_info["action"] vlist = rec_info["collection"] if len(vlist) > 0: rec_df._recommendation[action_type] = vlist rec_df._rec_info = rec_infolist self._widget = rec_df.render_widget() # re-render widget for the current dataframe if previous rec is not recomputed elif show_prev: self._widget = rec_df.render_widget() self._recs_fresh = True