<commit_before>import numpy as np
import tkinter as tk
import tkinter.ttk as ttk
import plugin_base

#~ TODO padding
class Uniform_Window(tk.Toplevel):
	def __init__(self, parent):
		super().__init__(parent)
		
		self.title('Data from uniform distribution')
		self.resizable(tk.FALSE, tk.FALSE)
		self.protocol('WM_DELETE_WINDOW', self.cancel_action)
		
		config_frame = ttk.Frame(self, borderwidth=10)
		config_frame.grid(column=0, row=0, sticky='N E W S')
		
		ttk.Label(config_frame, text='Min. x1 value').grid(column=0, row=0, sticky='W', padx=5, pady=5)
		self.minX1var = tk.IntVar()
		self.minX1var.set(-100)
		ttk.Entry(config_frame, textvariable=self.minX1var, width=5).grid(column=1, row=0, sticky='E')
		
		ttk.Label(config_frame, text='Max. x1 value').grid(column=0, row=1, sticky='W', padx=5, pady=5)
		self.maxX1var = tk.IntVar()
		self.maxX1var.set(100)
		ttk.Entry(config_frame, textvariable=self.maxX1var, width=5).grid(column=1, row=1, sticky='E')
		
		ttk.Label(config_frame, text='Min. x2 value').grid(column=0, row=2, sticky='W', padx=5, pady=5)
		self.minX2var = tk.IntVar()
		self.minX2var.set(-30)
		ttk.Entry(config_frame, textvariable=self.minX2var, width=5).grid(column=1, row=2, sticky='E')
		
		ttk.Label(config_frame, text='Max. x2 value').grid(column=0, row=3, sticky='W', padx=5, pady=5)
		self.maxX2var = tk.IntVar()
		self.maxX2var.set(30)
		ttk.Entry(config_frame, textvariable=self.maxX2var, width=5).grid(column=1, row=3, sticky='E')
		
		ttk.Label(config_frame, text='Number of points').grid(column=0, row=4, sticky='W', padx=5, pady=5)
		self.point_countvar = tk.IntVar()
		self.point_countvar.set(100)
		self.a = ttk.Entry(config_frame, textvariable=self.point_countvar, width=5)
		self.a.grid(column=1, row=4, sticky='W')
		
		confirmation_frame = ttk.Frame(self)
		confirmation_frame.grid(column=0, row=1, sticky='N E W S')
		ttk.Button(confirmation_frame, text='Cancel', command=self.cancel_action).grid(column=0, row=0, sticky='W', padx=5, pady=5)
		ttk.Button(confirmation_frame, text='Ok', command=self.ok_action).grid(column=1, row=0, sticky='E', padx=5, pady=5)
		
	def ok_action(self):
		self.accept = True
		self.destroy()
	
	def cancel_action(self):
		self.accept = False
		self.destroy()
	
	def get_values(self):
		self.wait_window()
		if self.accept:
			return self.minX1var.get(), self.maxX1var.get(), self.minX2var.get(), self.maxX2var.get(), self.point_countvar.get()
		else:
			raise Exception('cancel')

class Point_Cloud(plugin_base.Plugin_Base):
	def __init__(self):
		super().__init__()
		
		self.program.data_frame.add_action('New uniform', 'pq_new_uniform', None)
		self.program.data_frame.add_action('Clear', 'pq_clear', None)
		self.register_event_handler('pq_new_uniform', self.new_uniform)
		self.register_event_handler('pq_clear', self.clear)
		self.register_event_handler('redraw', self.draw)
	
	def new_uniform(self, event):
		query_window = Uniform_Window(self.program)
		
		try:
			minX1, maxX1, minX2, maxX2, point_count = query_window.get_values()
		except:
			return
		
		pointsX1 = np.random.uniform(minX1, maxX1, (point_count, 1))
		pointsX2 = np.random.uniform(minX2, maxX2, (point_count, 1))
		points = np.column_stack([pointsX1, pointsX2])
		try:
			self.program.get_data()['points'].append( (points, 'black') )
		except KeyError:
			self.program.get_data()['points'] = [ (points, 'black') ]
		
		self.program.redraw()
	
	def clear(self, event):
		self.program.get_data()['points'] = []
		self.program.redraw()
	
	def draw(self, event):
		canvas = self.program.get_canvas()
		for point_set, color in self.program.get_data()['points']:
			for point in point_set:
				canvas.create_oval(point[0], point[1], point[0], point[1], fill=color, outline=color)





<commit_msg>point_cloud: New dialog to set distribution parameters<commit_after>import numpy as np
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.colorchooser as tkcc
import plugin_base

class Parameter_Query_Window(tk.Toplevel):
	def __init__(self, parent):
		super().__init__(parent)
			
		self.title('Data from distribution')
		self.resizable(tk.FALSE, tk.FALSE)
		self.protocol('WM_DELETE_WINDOW', self.cancel_action)
		self.grab_set() # Make window modal
		
		self.nb = ttk.Notebook(self)
		self.nb.grid(column=0, row=0, sticky='N E W S', padx=10, pady=10)
		self.nb.add(self.make_uniform_frame(self.nb), text='Uniform')
		self.nb.add(self.make_normal_frame(self.nb), text='Normal')
		
		general_options_frame = ttk.LabelFrame(self, padding=5, text='General options')
		general_options_frame.grid(column=0, row=1, sticky='N E W S', padx=5, pady=5)
		general_options_frame.columnconfigure(1, weight=1)
		ttk.Label(general_options_frame, text='Number of points').grid(column=0, row=0, sticky='N W S', padx=5, pady=5)
		self.number_of_points = tk.IntVar()
		self.number_of_points.set(100)
		point_count_spinbox = tk.Spinbox(general_options_frame, from_=1, to=1000000, increment=100, textvariable=self.number_of_points, width=7)
		point_count_spinbox.grid(column=1, row=0, sticky='N E S', padx=5, pady=5)
		ttk.Label(general_options_frame, text='Color').grid(column=0, row=1, sticky='N W S', padx=5, pady=5)
		ttk.Button(general_options_frame, text='choose', command=self.color_choose_action).grid(column=1, row=1, sticky='N E S', padx=5, pady=5)
		self.color = '#000000'
		
		confirmation_frame = ttk.Frame(self)
		confirmation_frame.grid(column=0, row=2, sticky='N E W S', padx=10, pady=10)
		confirmation_frame.columnconfigure(1, weight=1)
		ttk.Button(confirmation_frame, text='Cancel', command=self.cancel_action).grid(column=0, row=0, sticky='N W S')
		ttk.Button(confirmation_frame, text='Ok', command=self.ok_action).grid(column=1, row=0, sticky='N E S')
		
	def make_uniform_frame(self, notebook):
		frame = ttk.Frame(notebook, padding=10)
		self.uniform_parameters = {}
		
		x1_frame = ttk.LabelFrame(frame, padding=5, text='X1 dimension')
		x1_frame.grid(column=0, row=0, sticky='N E W S', padx=5, pady=5)
		
		ttk.Label(x1_frame, text='Min').grid(column=0, row=0, padx=5, pady=5)
		self.uniform_parameters['x1_min'] = tk.DoubleVar()
		self.uniform_parameters['x1_min'].set(-100)
		x1_min_spinbox = tk.Spinbox(x1_frame, from_=-1000.0, to=1000.0, increment=10, textvariable=self.uniform_parameters['x1_min'], width=5)
		x1_min_spinbox.grid(column=1, row=0, padx=5, pady=5)
		
		ttk.Label(x1_frame, text='Max').grid(column=0, row=1, padx=5, pady=5)
		self.uniform_parameters['x1_max'] = tk.DoubleVar()
		self.uniform_parameters['x1_max'].set(100)
		x1_min_spinbox = tk.Spinbox(x1_frame, from_=-1000.0, to=1000.0, increment=10, textvariable=self.uniform_parameters['x1_max'], width=5)
		x1_min_spinbox.grid(column=1, row=1, padx=5, pady=5)
		
		x2_frame = ttk.LabelFrame(frame, padding=5, text='X2 dimension')
		x2_frame.grid(column=1, row=0, sticky='N E W S', padx=5, pady=5)
		
		ttk.Label(x2_frame, text='Min').grid(column=0, row=0, padx=5, pady=5)
		self.uniform_parameters['x2_min'] = tk.DoubleVar()
		self.uniform_parameters['x2_min'].set(-30)
		x2_min_spinbox = tk.Spinbox(x2_frame, from_=-1000.0, to=1000.0, increment=10, textvariable=self.uniform_parameters['x2_min'], width=5)
		x2_min_spinbox.grid(column=1, row=0, padx=5, pady=5)
		
		ttk.Label(x2_frame, text='Max').grid(column=0, row=1, padx=5, pady=5)
		self.uniform_parameters['x2_max'] = tk.DoubleVar()
		self.uniform_parameters['x2_max'].set(30)
		x2_min_spinbox = tk.Spinbox(x2_frame, from_=-1000.0, to=1000.0, increment=10, textvariable=self.uniform_parameters['x2_max'], width=5)
		x2_min_spinbox.grid(column=1, row=1, padx=5, pady=5)
		
		return frame
		
	def make_normal_frame(self, notebook): # TODO implement this
		frame = ttk.Frame(notebook)
		ttk.Label(frame, text='bar').grid(column=0, row=0)
		return frame
	
	def color_choose_action(self):
		_, self.color = tkcc.askcolor()
	
	def ok_action(self):
		self.distribution = self.nb.tab(self.nb.select(), 'text')
		self.destroy()
	
	def cancel_action(self):
		self.distribution = None
		self.destroy()
	
	@classmethod
	def get_values(cls, program):
		window = cls(program)
		window.wait_window()
		
		if window.distribution is not None:
			if window.distribution == 'Uniform':
				count = window.number_of_points.get()
				parameters = {}
				parameters['x1_min'] = window.uniform_parameters['x1_min'].get()
				parameters['x1_max'] = window.uniform_parameters['x1_max'].get()
				parameters['x2_min'] = window.uniform_parameters['x2_min'].get()
				parameters['x2_max'] = window.uniform_parameters['x2_max'].get()
				color = window.color
				return 'Uniform', parameters, count, color
			elif window.distribution == 'Normal':
				raise Exception('Not yet implemented') # TODO implement this
		else:
			return None

class Point_Cloud(plugin_base.Plugin_Base):
	def __init__(self):
		super().__init__()
		
		self.program.data_frame.add_action('New uniform', 'pq_new_uniform', None)
		self.program.data_frame.add_action('Clear', 'pq_clear', None)
		self.register_event_handler('pq_new_uniform', self.new_uniform)
		self.register_event_handler('pq_clear', self.clear)
		self.register_event_handler('redraw', self.draw)
	
	def new_uniform(self, event):
		result = Parameter_Query_Window.get_values(self.program)
		
		if result is not None:
			distribution, parameters, count, color = result
			
			if distribution == 'Uniform':
				points_x1 = np.random.uniform(parameters['x1_min'], parameters['x1_max'], (count, 1))
				points_x2 = np.random.uniform(parameters['x2_min'], parameters['x2_max'], (count, 1))
				points = np.column_stack([points_x1, points_x2])
			elif distribution == 'Normal':
				pass # TODO implement this
			else:
				raise Exception('Unknown distribution')
			
			try:
				self.program.get_data()['points'].append( (points, color) )
			except KeyError:
				self.program.get_data()['points'] = [ (points, color) ]
			self.program.redraw()
	
	def clear(self, event):
		self.program.get_data()['points'] = []
		self.program.redraw()
	
	def draw(self, event):
		canvas = self.program.get_canvas()
		for point_set, color in self.program.get_data()['points']:
			for point in point_set:
				canvas.create_oval(point[0], point[1], point[0], point[1], fill=color, outline=color)






{'fraction_non_alphanumeric': 0.15725370788724094, 'fraction_numerical': 0.032020430213142125, 'mean_word_length': 5.41183879093199, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1035348', 'n_tokens_mistral': 4263, 'n_tokens_neox': 3803, 'n_words': 669}