예제 #1
0
        public static void Run()
        {
            // test config panel
            var bar = new Bar() { C = "sub" };
            var foo = new Foo() { C = "c", D = true, E = 2.0, F = 2, G = bar };
            var config = new ConfigurationPanel(foo);
            var copy = config.GetConfiguredObject() as Foo;
            if (!bar.HasEqualParameters(copy.G))
                throw new Exception("Sub-object comparison failed!");
            foo.G = copy.G = null;
            if (!foo.HasEqualParameters(copy))
                throw new Exception("Main object comparison failed!");
            var fprop = typeof(Foo).GetProperty("F");
            config.SetterFor(fprop)(200);
            if ((int)config.GetterFor(fprop)() != 200)
                throw new Exception("Getter or setter failed!");
            config.PropertyChanged += (args) =>
            {
                if (args.Property != fprop)
                    return;

                if ((int)args.Getter() > 150)
                    args.Setter((int)args.Getter() - 2);
            };
            config.SetterFor(fprop)(190);
            if ((int)config.GetterFor(fprop)() != 150)
                throw new Exception("PropertyChanged failed!");

            // test derived type panel
            var derivedConfig = new DerivedTypeConfigurationPanel(typeof(Interface), bar);
            if (!new Bar() { C = "sub" }.HasEqualParameters((Bar)derivedConfig.GetConfiguredObject()))
                throw new Exception("DerivedTypeConfigurationPanel failed!");
            var diffBar = new Bar() { C = "different" };
            derivedConfig.SetConfiguredObject(diffBar);
            if (!diffBar.HasEqualParameters((Bar)derivedConfig.GetConfiguredObject()))
                throw new Exception("SetConfiguredObject failed!");
        }
        private void BuildView(ClassificationScheme classificationScheme)
        {
            this.SuspendLayout();
            this.Text = classificationScheme.Settings.Name;
            var table = GUIUtils.CreateTable(new double[] { .33, .33, .33 }, Direction.Horizontal);

            // classifier
            var classifierSettings = new DerivedTypeConfigurationPanel(typeof(IClassifier), classificationScheme.Classifier);
            this.getClassifier = () => (IClassifier)classifierSettings.GetConfiguredObject();
            table.Controls.Add(classifierSettings, 0, 0);

            // general settings
            var generalSettings = new ConfigurationPanel(classificationScheme.Settings);
            table.Controls.Add(generalSettings, 1, 0);

            // bin selection
            var panel = new Panel() { Dock = DockStyle.Fill };

            var binList = new CheckedListBox() { Dock = DockStyle.Fill, CheckOnClick = true };
            binList.AddContextMenu();
            this.ToolTip.SetToolTip(binList, "Select which time bins from each trial will be used to train the classifier");
            var timeBins = GeneralClassifierSettings.MAX_BINS
                .CountTo()
                .Select(i => new TimeBin(i) { Checked = classificationScheme.Settings.SelectedBins.Contains(i) })
                .ToIArray();
            binList.ItemCheck += (sender, args) => ((TimeBin)binList.Items[args.Index]).Checked = (args.NewValue == CheckState.Checked);
            Action<int> refreshBinList = (binWidth) =>
            {
                // ensure the right number of items
                int binCount = GeneralClassifierSettings.GetBinCount(binWidth);
                if (binList.Items.Count < binCount)
                    binList.Items.AddRange(timeBins.SubView(binList.Items.Count, binCount - binList.Items.Count).ToArray());
                else
                    for (int i = binList.Items.Count - 1; i >= binCount; i--)
                        binList.Items.RemoveAt(i);

                // ensure correct width and uncheck all
                TimeBin timeBin;
                for (int i = 0; i < binCount; i++)
                {
                    timeBin = (TimeBin)binList.Items[i];
                    timeBin.BinWidth = binWidth;
                    binList.SetItemChecked(i, timeBin.Checked);
                }

                binList.Invalidate();
            };
            refreshBinList(classificationScheme.Settings.BinWidthMillis);
            var binWidthProp = typeof(GeneralClassifierSettings).GetProperty("BinWidthMillis");
            var nameProp = typeof(GeneralClassifierSettings).GetProperty("Name");
            if (binWidthProp == null || nameProp == null)
                throw new Exception("Failed to find properties!");
            generalSettings.PropertyChanged += args =>
            {
                if (args.Property.Equals(binWidthProp))
                    refreshBinList((int)args.Getter());
                else if (args.Property.Equals(nameProp))
                    this.Text = args.Getter().ToString();
            };
            this.getSettings = () =>
            {
                var settings = (GeneralClassifierSettings)generalSettings.GetConfiguredObject();
                settings.SelectedBins = binList.CheckedIndices.Cast<int>().ToIArray();

                return settings;
            };
            panel.Controls.Add(binList);
            panel.Controls.Add("Time Bins".ToLabel());
            var saveButton = GUIUtils.CreateFlatButton("Save", (b) =>
            {
                this.saveDialog.FileName = this.Text;
                if (this.saveDialog.ShowDialog() != DialogResult.OK)
                    return;

                bool saved = this.ClassificationScheme.TrySerializeToFile(this.saveDialog.FileName);
                GUIUtils.Alert((saved ? "Saved" : "Failed to save")
                    + " classifier info to " + this.saveDialog.FileName,
                    (saved ? MessageBoxIcon.Information : MessageBoxIcon.Error));

                string directory = Path.GetDirectoryName(this.saveDialog.FileName);
                if (Directory.Exists(directory))
                    this.saveDialog.InitialDirectory = directory;
            });
            saveButton.Dock = DockStyle.Bottom;
            panel.Controls.Add(saveButton);
            table.Controls.Add(panel, 2, 0);

            this.Controls.Add(table);
            this.ResumeLayout(false);
        }
예제 #3
0
        public static void Run()
        {
            // test config panel
            var bar = new Bar()
            {
                C = "sub"
            };
            var foo = new Foo()
            {
                C = "c", D = true, E = 2.0, F = 2, G = bar
            };
            var config = new ConfigurationPanel(foo);
            var copy   = config.GetConfiguredObject() as Foo;

            if (!bar.HasEqualParameters(copy.G))
            {
                throw new Exception("Sub-object comparison failed!");
            }
            foo.G = copy.G = null;
            if (!foo.HasEqualParameters(copy))
            {
                throw new Exception("Main object comparison failed!");
            }
            var fprop = typeof(Foo).GetProperty("F");

            config.SetterFor(fprop)(200);
            if ((int)config.GetterFor(fprop)() != 200)
            {
                throw new Exception("Getter or setter failed!");
            }
            config.PropertyChanged += (args) =>
            {
                if (args.Property != fprop)
                {
                    return;
                }

                if ((int)args.Getter() > 150)
                {
                    args.Setter((int)args.Getter() - 2);
                }
            };
            config.SetterFor(fprop)(190);
            if ((int)config.GetterFor(fprop)() != 150)
            {
                throw new Exception("PropertyChanged failed!");
            }

            // test derived type panel
            var derivedConfig = new DerivedTypeConfigurationPanel(typeof(Interface), bar);

            if (!new Bar()
            {
                C = "sub"
            }.HasEqualParameters((Bar)derivedConfig.GetConfiguredObject()))
            {
                throw new Exception("DerivedTypeConfigurationPanel failed!");
            }
            var diffBar = new Bar()
            {
                C = "different"
            };

            derivedConfig.SetConfiguredObject(diffBar);
            if (!diffBar.HasEqualParameters((Bar)derivedConfig.GetConfiguredObject()))
            {
                throw new Exception("SetConfiguredObject failed!");
            }
        }