def __call__(self, data): common = _EMSC(self.reference, self.weights, self.order, self.scaling, data.domain) # creates function for transforming data atts = [ a.copy( compute_value=EMSCFeature(i, common) ) # takes care of domain column-wise, by above transformation function for i, a in enumerate(data.domain.attributes) ] model_metas = [] used_names = set( [var.name for var in data.domain.variables + data.domain.metas]) if self.output_model: i = len(data.domain.attributes) for o in range(self.order + 1): n = get_next_name(used_names, "EMSC parameter " + str(o)) model_metas.append( Orange.data.ContinuousVariable(name=n, compute_value=EMSCModel( i, common))) i += 1 n = get_next_name(used_names, "EMSC scaling parameter") model_metas.append( Orange.data.ContinuousVariable(name=n, compute_value=EMSCModel( i, common))) domain = Orange.data.Domain(atts, data.domain.class_vars, data.domain.metas + tuple(model_metas)) return data.from_table(domain, data)
def commit(self): map_data = None if self.data and self.xpoints is not None and self.ypoints is not None \ and self.xpoints * self.ypoints == len(self.data): used_names = [ var.name for var in self.data.domain.variables + self.data.domain.metas ] xmeta = Orange.data.ContinuousVariable.make( get_next_name(used_names, "X")) ymeta = Orange.data.ContinuousVariable.make( get_next_name(used_names, "Y")) # add new variables for X and Y dimension ot the data domain metas = self.data.domain.metas + (xmeta, ymeta) domain = Orange.data.Domain(self.data.domain.attributes, self.data.domain.class_vars, metas) map_data = Orange.data.Table(domain, self.data) map_data[:, xmeta] = np.tile(np.arange(self.xpoints), len(self.data) // self.xpoints).reshape( -1, 1) map_data[:, ymeta] = np.repeat(np.arange(self.ypoints), len(self.data) // self.ypoints).reshape(-1, 1) self.Outputs.map.send(map_data)
def commit(self): map_data = None if self.data and self.xpoints is not None and self.ypoints is not None \ and self.xpoints * self.ypoints == len(self.data): used_names = [var.name for var in self.data.domain.variables + self.data.domain.metas] xmeta = Orange.data.ContinuousVariable.make(get_next_name(used_names, "X")) ymeta = Orange.data.ContinuousVariable.make(get_next_name(used_names, "Y")) # add new variables for X and Y dimension ot the data domain metas = self.data.domain.metas + (xmeta, ymeta) domain = Orange.data.Domain(self.data.domain.attributes, self.data.domain.class_vars, metas) map_data = Orange.data.Table(domain, self.data) map_data[:, xmeta] = np.tile(np.arange(self.xpoints), len(self.data)//self.xpoints).reshape(-1, 1) map_data[:, ymeta] = np.repeat(np.arange(self.ypoints), len(self.data)//self.ypoints).reshape(-1, 1) self.Outputs.map.send(map_data)
def _send_data(self): if self.partition is None or self.data is None: return domain = self.data.domain # Compute the frequency of each cluster index counts = np.bincount(self.partition) indices = np.argsort(counts)[::-1] index_map = {n: o for n, o in zip(indices, range(len(indices)))} new_partition = list(map(index_map.get, self.partition)) cluster_var = DiscreteVariable( get_next_name(domain, 'Cluster'), values=[ 'C%d' % (i + 1) for i, _ in enumerate(np.unique(new_partition)) ]) new_domain = add_columns(domain, metas=[cluster_var]) new_table = self.data.transform(new_domain) new_table.get_column_view(cluster_var)[0][:] = new_partition self.Outputs.annotated_data.send(new_table) if Graph is not None: graph = Graph(self.graph) graph.set_items(new_table) self.Outputs.graph.send(graph)
def __call__(self, data): common = _IntegrateCommon(data.domain) atts = [] if self.limits: methods = self.methods if not isinstance(methods, Iterable): methods = [methods] * len(self.limits) names = self.names if not names: names = [" - ".join("{0}".format(e) for e in l) for l in self.limits] # no names in data should be repeated used_names = [var.name for var in data.domain.variables + data.domain.metas] for i, n in enumerate(names): n = get_next_name(used_names, n) names[i] = n used_names.append(n) for limits, method, name in zip(self.limits, methods, names): atts.append(Orange.data.ContinuousVariable( name=name, compute_value=method(limits, common))) if not self.metas: domain = Orange.data.Domain(atts, data.domain.class_vars, metas=data.domain.metas) else: domain = Orange.data.Domain(data.domain.attributes, data.domain.class_vars, metas=data.domain.metas + tuple(atts)) return data.from_table(domain, data)
def test_var_name_exists(self): self.send_signal("Data", self.collagen[:500]) self.widget.controls.xpoints.setText("5") self.widget.le1_changed() self.widget.commit() m = self.get_output("Map data") self.send_signal("Data", m) self.widget.commit() m = self.get_output("Map data") # after Orange 3.6.0 get_next_name returned different results next_X = get_next_name(["X"], "X") next_Y = get_next_name(["Y"], "Y") np.testing.assert_equal(m[:, "X"].metas[:, 0], m[:, next_X].metas[:, 0]) np.testing.assert_equal(m[:, "Y"].metas[:, 0], m[:, next_Y].metas[:, 0])
def __call__(self, data): common = _IntegrateCommon(data.domain) atts = [] if self.limits: methods = self.methods if not isinstance(methods, Iterable): methods = [methods] * len(self.limits) names = self.names if not names: names = [ " - ".join("{0}".format(e) for e in l) for l in self.limits ] # no names in data should be repeated used_names = [ var.name for var in data.domain.variables + data.domain.metas ] for i, n in enumerate(names): n = get_next_name(used_names, n) names[i] = n used_names.append(n) for limits, method, name in zip(self.limits, methods, names): atts.append( Orange.data.ContinuousVariable(name=name, compute_value=method( limits, common))) if not self.metas: domain = Orange.data.Domain(atts, data.domain.class_vars, metas=data.domain.metas) else: domain = Orange.data.Domain(data.domain.attributes, data.domain.class_vars, metas=data.domain.metas + tuple(atts)) return data.from_table(domain, data)
def test_repeated(self): data = Orange.data.Table([[1, 2, 3, 1, 1, 1]]) i = Integrate(methods=[Integrate.Simple, Integrate.Baseline], limits=[[0, 5], [0, 6]], names=["int", "int"])(data) self.assertEqual(i.domain[0].name, "int") # after Orange 3.6.0 get_next_name returned different results nn = get_next_name(["int"], "int") self.assertEqual(i.domain[1].name, nn)
def __call__(self, data): common = _EMSC(self.reference, self.weights, self.order,self.scaling, data.domain) # creates function for transforming data atts = [a.copy(compute_value=EMSCFeature(i, common)) # takes care of domain column-wise, by above transformation function for i, a in enumerate(data.domain.attributes)] model_metas = [] used_names = set([var.name for var in data.domain.variables + data.domain.metas]) if self.output_model: i = len(data.domain.attributes) for o in range(self.order+1): n = get_next_name(used_names, "EMSC parameter " + str(o)) model_metas.append( Orange.data.ContinuousVariable(name=n, compute_value=EMSCModel(i, common))) i += 1 n = get_next_name(used_names, "EMSC scaling parameter") model_metas.append( Orange.data.ContinuousVariable(name=n, compute_value=EMSCModel(i, common))) domain = Orange.data.Domain(atts, data.domain.class_vars, data.domain.metas + tuple(model_metas)) return data.from_table(domain, data)
def create_groups_table(data, selection): if data is None: return None names = [var.name for var in data.domain.variables + data.domain.metas] name = get_next_name(names, "Selection group") metas = data.domain.metas + (DiscreteVariable( name, ["Unselected"] + ["G{}".format(i + 1) for i in range(np.max(selection))]), ) domain = Domain(data.domain.attributes, data.domain.class_vars, metas) table = data.transform(domain) table.metas[:, len(data.domain.metas):] = \ selection.reshape(len(data), 1) return table
def send_data(self): if self.optimize_k: row = self.selected_row() k = self.k_from + row if row is not None else None else: k = self.k km = self.clusterings.get(k) if not self.data or km is None or isinstance(km, str): self.Outputs.annotated_data.send(None) self.Outputs.centroids.send(None) return domain = self.data.domain existing_vars = [var.name for var in chain(domain.variables, domain.metas)] clust_var = DiscreteVariable( get_next_name(existing_vars, "Cluster"), values=["C%d" % (x + 1) for x in range(km.k)]) clust_ids = km(self.data) silhouette_var = ContinuousVariable( get_next_name(existing_vars, "Silhouette")) if km.silhouette_samples is not None: self.Warning.no_silhouettes.clear() scores = np.arctan(km.silhouette_samples) / np.pi + 0.5 else: self.Warning.no_silhouettes() scores = np.nan new_domain = Domain( domain.attributes, [clust_var, silhouette_var], domain.metas + domain.class_vars) new_table = self.data.transform(new_domain) new_table.get_column_view(clust_var)[0][:] = clust_ids.X.ravel() new_table.get_column_view(silhouette_var)[0][:] = scores centroids = Table(Domain(km.pre_domain.attributes), km.centroids) self.Outputs.annotated_data.send(new_table) self.Outputs.centroids.send(centroids)
def send_data(self): if self.optimize_k: row = self.selected_row() k = self.k_from + row if row is not None else None else: k = self.k km = self.clusterings.get(k) if not self.data or km is None or isinstance(km, str): self.Outputs.annotated_data.send(None) self.Outputs.centroids.send(None) return domain = self.data.domain existing_vars = [ var.name for var in chain(domain.variables, domain.metas) ] clust_var = DiscreteVariable( get_next_name(existing_vars, "Cluster"), values=["C%d" % (x + 1) for x in range(km.k)]) clust_ids = km(self.data) silhouette_var = ContinuousVariable( get_next_name(existing_vars, "Silhouette")) if km.silhouette_samples is not None: self.Warning.no_silhouettes.clear() scores = np.arctan(km.silhouette_samples) / np.pi + 0.5 else: self.Warning.no_silhouettes() scores = np.nan new_domain = Domain(domain.attributes, [clust_var, silhouette_var], domain.metas + domain.class_vars) new_table = self.data.transform(new_domain) new_table.get_column_view(clust_var)[0][:] = clust_ids.X.ravel() new_table.get_column_view(silhouette_var)[0][:] = scores centroids = Table(Domain(km.pre_domain.attributes), km.centroids) self.Outputs.annotated_data.send(new_table) self.Outputs.centroids.send(centroids)
def create_groups_table(data, selection): if data is None: return None names = [var.name for var in data.domain.variables + data.domain.metas] name = get_next_name(names, "Selection group") metas = data.domain.metas + ( DiscreteVariable( name, ["Unselected"] + ["G{}".format(i + 1) for i in range(np.max(selection))]), ) domain = Domain(data.domain.attributes, data.domain.class_vars, metas) table = data.transform(domain) table.metas[:, len(data.domain.metas):] = \ selection.reshape(len(data), 1) return table
def create_groups_table(data, selection): if data is None: return None names = [var.name for var in data.domain.variables + data.domain.metas] name = get_next_name(names, "Selection group") metas = data.domain.metas + (DiscreteVariable( name, ["Unselected"] + ["G{}".format(i + 1) for i in range(np.max(selection))]), ) domain = Domain(data.domain.attributes, data.domain.class_vars, metas) table = Table(domain, data.X, data.Y, metas=np.hstack( (data.metas, selection.reshape(len(data), 1)))) table.attributes = data.attributes table.ids = data.ids return table
def commit(self): self.info.setText(' ') if self.method == 0: alg = cd.label_propagation kwargs = {'iterations': self.iterations} elif self.method == 1: alg = cd.label_propagation_hop_attenuation kwargs = {'iterations': self.iterations, 'delta': self.hop_attenuation} if self.net is None: self.Outputs.items.send(None) self.Outputs.network.send(None) return labels = alg(self.net, **kwargs) domain = self.net.items().domain cd.add_results_to_items(self.net, labels, get_next_name(domain, 'Cluster')) self.info.setText('%d clusters found' % len(set(labels.values()))) self.Outputs.items.send(self.net.items()) self.Outputs.network.send(self.net)
def test_get_var_name(self): self.assertEqual(get_next_name(["a"], "XX"), "XX") self.assertEqual(get_next_name(["a", "XX"], "XX"), "XX (1)") self.assertEqual(get_next_name(["a", "XX (4)"], "XX"), "XX (5)") self.assertEqual(get_next_name(["a", "XX", "XX (4)"], "XX"), "XX (5)")