def test_match(): v1 = [1, 1, 2, 2, 3, 3] v2 = [1, 2, 3] v3 = [0, 1, 2] c = [1] assert match(v1, v2) == [0, 0, 1, 1, 2, 2] assert match(v1, v2, incomparables=c) == [-1, -1, 1, 1, 2, 2] assert match(v1, v3) == [1, 1, 2, 2, -1, -1]
def train_position_scales(self, layout, layers): """ Compute ranges for the x and y scales """ _layout = layout.layout panel_scales_x = layout.panel_scales_x panel_scales_y = layout.panel_scales_y # loop over each layer, training x and y scales in turn for layer in layers: data = layer.data match_id = match(data['PANEL'], _layout['PANEL']) if panel_scales_x: x_vars = list(set(panel_scales_x[0].aesthetics) & set(data.columns)) # the scale index for each data point SCALE_X = _layout['SCALE_X'].iloc[match_id].tolist() panel_scales_x.train(data, x_vars, SCALE_X) if panel_scales_y: y_vars = list(set(panel_scales_y[0].aesthetics) & set(data.columns)) # the scale index for each data point SCALE_Y = _layout['SCALE_Y'].iloc[match_id].tolist() panel_scales_y.train(data, y_vars, SCALE_Y) return self
def map(self, data, layout): if not len(data): data['PANEL'] = pd.Categorical( [], categories=layout['PANEL'].cat.categories, ordered=True) return data vars = [x for x in self.rows + self.cols] margin_vars = [ list(data.columns & self.rows), list(data.columns & self.cols) ] data = add_margins(data, margin_vars, self.margins) facet_vals = eval_facet_vars(data, vars, self.plot.environment) data, facet_vals = add_missing_facets(data, layout, vars, facet_vals) # assign each point to a panel if len(facet_vals) == 0: # Special case of no facetting data['PANEL'] = 1 else: keys = join_keys(facet_vals, layout, vars) data['PANEL'] = match(keys['x'], keys['y'], start=1) data = data.sort_values('PANEL', kind='mergesort') # matching dtype and # the categories(panel numbers) for the data should be in the # same order as the panels. i.e the panels are the reference, # they "know" the right order data['PANEL'] = pd.Categorical( data['PANEL'], categories=layout['PANEL'].cat.categories, ordered=True) data.reset_index(drop=True, inplace=True) return data