PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Prod_1_1/SVMPluginViewer/SVMViewerUserControl.cs

#
C# | 407 lines | 242 code | 43 blank | 122 comment | 24 complexity | 79145be1779bb2b73ad4bc54af82dbc7 MD5 | raw file
  1. /** © 2008 Avanade Inc. All Rights Reserved.
  2. *
  3. * Authors: Joris Valkonet joris.valkonet@avanade.com Avanade Netherlands
  4. * Thanh Luc thanh.luc@avanade.com Avanade Netherlands
  5. * Mark Beerens mark.beerens@avanade.com Avanade Netherlands
  6. *
  7. * Content: This is the base class of the viewer and therefore implements the
  8. * IMiningModelViewerControl for the basic functionality and the
  9. * IMiningModelViewerControl2 interface for Excel functionality
  10. *
  11. * */
  12. #region [Using]
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel;
  16. using System.Drawing;
  17. using System.Data;
  18. using System.Text;
  19. using System.Windows.Forms;
  20. using System.Collections;
  21. using System.Data.OleDb;
  22. using Microsoft.DataWarehouse.Interfaces;
  23. //using System.Diagnostics;
  24. using System.Drawing.Drawing2D;
  25. #endregion
  26. namespace Avanade.Datamining.SVMPluginViewer
  27. {
  28. public partial class SVMViewerUserControl : UserControl, IMiningModelViewerControl, IMiningModelViewerControl2
  29. {
  30. #region [Parameters]
  31. //the colors of the predict values
  32. private SortedDictionary<String, Color> ValueColors = new SortedDictionary<String, Color>();
  33. //the color of a misclassification
  34. private Color misclassifiedColor = Color.Red;
  35. //did the user click on load already?
  36. private bool loaded = false;
  37. //do we need to show the misclassifications
  38. private bool showClassifier = true;
  39. //the selected data for visualization: [Trainingdata, TestData];
  40. private String data = "Trainingdata";
  41. //the selected column on the x-axis
  42. public string x_name;
  43. //the selected column on the y-axis
  44. public string y_name;
  45. //do we need to draw the axes
  46. private Boolean drawAxes = true;
  47. #endregion
  48. #region [Loading]
  49. /// <summary>
  50. /// Constructor.. This does nothing special, but initialization
  51. /// </summary>
  52. public SVMViewerUserControl()
  53. {
  54. InitializeComponent();
  55. BackColor = Color.White;
  56. // hide the following text labels
  57. this.label_X_Axis.Hide();
  58. this.label_Y_Axis.Hide();
  59. //set default values for display and grid options
  60. comboBox4.Text = "Yes";
  61. checkBox1.Checked = showClassifier;
  62. comboBox6.Text = "Trainingdata";
  63. //make sure that 2005 won't get into trouble.
  64. if (version != "2008")
  65. {
  66. comboBox6.Enabled = false;
  67. }
  68. }
  69. /// <summary>
  70. /// /...
  71. /// </summary>
  72. /// <param name="isActivated"></param>
  73. public void ViewerActivated(bool isActivated)
  74. {
  75. }
  76. /// <summary>
  77. /// This method is automatically executed by the host
  78. /// it loads the basic information into the model
  79. /// </summary>
  80. /// <param name="obj"></param>
  81. /// <returns></returns>
  82. public bool LoadViewerData(object obj)
  83. {
  84. //get the min and max values
  85. getMinMaxValues();
  86. //get the input attributes
  87. getAttributes();
  88. //set the predict classes
  89. setClasses();
  90. BindingSource source1 = new BindingSource();
  91. source1.DataSource = inputAttributes.Keys;
  92. BindingSource source2 = new BindingSource();
  93. source2.DataSource = inputAttributes.Keys;
  94. comboBox1.DataSource = source1;
  95. comboBox2.DataSource = source2;
  96. if (comboBox2.Items.Count > 1)
  97. {
  98. comboBox2.SelectedIndex = 1;
  99. }
  100. comboBox1.Refresh();
  101. comboBox2.Refresh();
  102. this.predict_name = getPredictAttribute();
  103. textBox1.Text = predict_name;
  104. textBox1.Refresh();
  105. button2.BackColor = misclassifiedColor;
  106. button2.Refresh();
  107. return true;
  108. }
  109. /// <summary>
  110. /// This method loads the data into the viewer. We have added a button for this, because
  111. /// loading the data can take a lot of time...
  112. /// </summary>
  113. /// <param name="sender"></param>
  114. /// <param name="e"></param>
  115. private void button4_Click(object sender, EventArgs e)
  116. {
  117. loaded = true;
  118. button4.Hide();
  119. RedrawScatterplot();
  120. }
  121. /// <summary>
  122. /// THis method assigns the colors to the predict values and loads the
  123. /// predict values in the combobox
  124. /// </summary>
  125. private void setClasses()
  126. {
  127. //get the classes and assign a color to each class
  128. String [] classes = getClasses();
  129. int i = 0;
  130. foreach (string value in classes)
  131. {
  132. switch (i)
  133. {
  134. case 0: ValueColors.Add(value, Color.ForestGreen); break;
  135. case 1: ValueColors.Add(value, Color.Blue); break;
  136. case 2: ValueColors.Add(value, Color.Green); break;
  137. case 3: ValueColors.Add(value, Color.Black); break;
  138. case 4: ValueColors.Add(value, Color.Yellow); break;
  139. case 5: ValueColors.Add(value, Color.Purple); break;
  140. case 6: ValueColors.Add(value, Color.Chocolate); break;
  141. case 7: ValueColors.Add(value, Color.DarkOliveGreen); break;
  142. case 8: ValueColors.Add(value, Color.FloralWhite); break;
  143. default:
  144. {
  145. Random rnd = new Random();
  146. ValueColors.Add(value, Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)));
  147. break;
  148. }
  149. }
  150. i++;
  151. }
  152. //load the classes in the combobox
  153. comboBox3.DataSource = classes;
  154. comboBox3.Refresh();
  155. Color tmpColor;
  156. ValueColors.TryGetValue(comboBox3.Text, out tmpColor);
  157. button1.BackColor = tmpColor;
  158. }
  159. #endregion
  160. #region [Redraw]
  161. /// <summary>
  162. /// This method redraw the scatter plot, after a value has changed somewhere
  163. /// but.. only when the user has clicked on the load button...
  164. /// </summary>
  165. private void RedrawScatterplot()
  166. {
  167. if (loaded)
  168. {
  169. if (this.comboBox1.Text == this.comboBox2.Text)
  170. { // Check whether the X and Y comboboxes contain the same value
  171. MessageBox.Show("The X and Y should not be assigned the same attribute.");
  172. }
  173. else
  174. {
  175. // Turn mouse pointer into hourglass
  176. Cursor.Current = Cursors.WaitCursor;
  177. this.x_name = this.comboBox1.Text;
  178. this.y_name = this.comboBox2.Text;
  179. this.predict_name = this.textBox1.Text;
  180. String x_type;
  181. String y_type;
  182. inputAttributes.TryGetValue(x_name, out x_type);
  183. inputAttributes.TryGetValue(y_name, out y_type);
  184. this.GetTrainingData();
  185. //determine the types of the selected columns and
  186. //the viewer shown depends on the types of the
  187. //attributes
  188. if (x_type == "Continuous" && y_type == "Continuous")
  189. {
  190. //get the min and max values
  191. maxValues.TryGetValue(x_name, out maxX);
  192. minValues.TryGetValue(x_name, out minX);
  193. maxValues.TryGetValue(y_name, out maxY);
  194. minValues.TryGetValue(y_name, out minY);
  195. // Execute Paint
  196. Paint += new PaintEventHandler(OnPaint);
  197. // Refresh user control to redraw itself
  198. this.Refresh();
  199. }
  200. else if (x_type == "Discrete" && y_type == "Discrete")
  201. {
  202. //draw the bars
  203. Paint += new PaintEventHandler(OnDiscretePaint);
  204. this.Refresh();
  205. }
  206. else if (x_type == "Discrete") //and y continuous
  207. {
  208. MessageBox.Show("In this version of the viewer the combination of a discrete and continuous attribute is not supported by a visualization", "Sorry");
  209. }
  210. else
  211. {
  212. //continuous versus discrete
  213. MessageBox.Show("In this version of the viewer the combination of a discrete and continuous attribute is not supported by a visualization", "Sorry");
  214. }
  215. // Set names for Scatterplot Axes
  216. this.label_X_Axis.Text = String.Format("X: {0}", this.comboBox1.Text);
  217. this.label_Y_Axis.Text = String.Format("Y: {0}", this.comboBox2.Text);
  218. this.label_X_Axis.Visible = true;
  219. this.label_Y_Axis.Visible = true;
  220. // Reset mouse pointer to normal
  221. Cursor.Current = Cursors.Default;
  222. }
  223. }
  224. }
  225. #endregion
  226. #region [Control]
  227. /// <summary>
  228. /// Update de button when a different predict value is selected
  229. /// </summary>
  230. /// <param name="sender"></param>
  231. /// <param name="e"></param>
  232. private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
  233. {
  234. Color color = Color.Brown;
  235. if(ValueColors.TryGetValue(comboBox3.Text, out color)){
  236. button1.BackColor = color;
  237. }
  238. else
  239. {
  240. button1.BackColor = color;
  241. }
  242. }
  243. /// <summary>
  244. /// This method enables you to select a color for a predict value
  245. /// </summary>
  246. /// <param name="sender"></param>
  247. /// <param name="e"></param>
  248. private void button1_Click(object sender, EventArgs e)
  249. {
  250. //set the color of the selected class.. if it is predefined and show the color selection dialog
  251. if (ValueColors.ContainsKey(comboBox3.Text))
  252. {
  253. Color tmpColor = Color.Brown;
  254. ValueColors.TryGetValue(comboBox3.Text, out tmpColor);
  255. colorDialog1.Color = tmpColor;
  256. }
  257. colorDialog1.ShowDialog(this);
  258. //obtain the color, update the data structure and repaint the button
  259. button1.BackColor = colorDialog1.Color;
  260. if (ValueColors.ContainsKey(comboBox3.Text))
  261. {
  262. ValueColors.Remove(comboBox3.Text );
  263. }
  264. ValueColors.Add(comboBox3.Text, colorDialog1.Color);
  265. //repaint
  266. RedrawScatterplot();
  267. }
  268. /// <summary>
  269. /// This method can change the color of the misclassifications
  270. /// </summary>
  271. /// <param name="sender"></param>
  272. /// <param name="e"></param>
  273. private void button2_Click(object sender, EventArgs e)
  274. {
  275. //set de misclassification color in the colordialog and show the diaglog
  276. colorDialog1.Color = misclassifiedColor;
  277. colorDialog1.ShowDialog(this);
  278. //ask the misclassification color from the dialog and repaint the button
  279. misclassifiedColor = colorDialog1.Color;
  280. button2.BackColor = misclassifiedColor;
  281. //repaint
  282. RedrawScatterplot();
  283. }
  284. /// <summary>
  285. /// This method redraws the scatter plot when different attributes are chosen
  286. /// </summary>
  287. /// <param name="sender"></param>
  288. /// <param name="e"></param>
  289. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  290. {
  291. RedrawScatterplot();
  292. }
  293. /// <summary>
  294. /// This methode redraws the scatter plot when different attribtues are chosen
  295. /// </summary>
  296. /// <param name="sender"></param>
  297. /// <param name="e"></param>
  298. private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  299. {
  300. RedrawScatterplot();
  301. }
  302. /// <summary>
  303. /// The method redraws the scatter plot when different display is chosen
  304. /// </summary>
  305. /// <param name="sender"></param>
  306. /// <param name="e"></param>
  307. private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
  308. {
  309. RedrawScatterplot();
  310. }
  311. /// <summary>
  312. /// this method redraws the scatter plot when a different grid option is chosen
  313. /// </summary>
  314. /// <param name="sender"></param>
  315. /// <param name="e"></param>
  316. private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
  317. {
  318. RedrawScatterplot();
  319. }
  320. /// <summary>
  321. /// Show classifier is changes
  322. /// </summary>
  323. /// <param name="sender"></param>
  324. /// <param name="e"></param>
  325. private void checkBox1_CheckedChanged(object sender, EventArgs e)
  326. {
  327. showClassifier = checkBox1.Checked;
  328. RedrawScatterplot();
  329. }
  330. /// <summary>
  331. /// The type of data is changed
  332. /// </summary>
  333. /// <param name="sender"></param>
  334. /// <param name="e"></param>
  335. private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
  336. {
  337. if (!comboBox6.Text.Equals(data))
  338. {
  339. if (comboBox6.Text.Equals("Testingdata"))
  340. {
  341. data = "Testingdata";
  342. }
  343. else
  344. {
  345. data = "Trainingdata";
  346. }
  347. RedrawScatterplot();
  348. }
  349. }
  350. /// <summary>
  351. /// Displays the accuracy of the selected data
  352. /// </summary>
  353. /// <param name="sender"></param>
  354. /// <param name="e"></param>
  355. private void button3_Click(object sender, EventArgs e)
  356. {
  357. MessageBox.Show("Trainingset accuracy: " + getTrainingAccuracy() + "\n Testset accuracy: " + getTestAccuracy(), "Dataset details");
  358. }
  359. #endregion
  360. private void SVMViewerUserControl_Load(object sender, EventArgs e)
  361. {
  362. }
  363. }
  364. }