Exemplo n.º 1
0
        public Identifier(Boolean isAnnotated, WhatWhyTrainer wwt)
        {
            this.isAnnotated = isAnnotated;
            this.wwt = wwt;

            listWhoCandidates = new List<Candidate>();
            listWhenCandidates = new List<Candidate>();
            listWhereCandidates = new List<Candidate>();
            listWhatCandidates = new List<List<Token>>();
            listWhyCandidates = new List<List<Token>>();
            listSecondaryWhatCandidates = new List<Candidate>();
            listSecondaryWhyCandidates = new List<Candidate>();

            fvPOS = new FastVector(Token.PartOfSpeechTags.Length);
            foreach (String POS in Token.PartOfSpeechTags)
            {
                fvPOS.addElement(POS);
            }

            whoClassifier = (Classifier)SerializationHelper.read(@"..\..\IdentifierModels\who.model");
            whenClassifier = (Classifier)SerializationHelper.read(@"..\..\IdentifierModels\when.model");
            whereClassifier = (Classifier)SerializationHelper.read(@"..\..\IdentifierModels\where.model");
            //whatClassifier = (Classifier)SerializationHelper.read(@"..\..\IdentifierModels\what.model");
            //whyClassifier = (Classifier)SerializationHelper.read(@"..\..\IdentifierModels\why.model");

            initializeAnnotations();
        }
        private void applyClassificationModelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            return;

            Instances ListInstancesTOClassify = GlobalInfo.CurrentScreen.CellBasedClassification.CreateInstancesWithoutClass(dt);

            FastVector attVals = new FastVector();
            for (int i = 0; i  <  GlobalInfo.CurrentScreen.CellBasedClassification.NumClasses; i++)
                attVals.addElement(i.ToString());

            ListInstancesTOClassify.insertAttributeAt(new weka.core.Attribute("Class", attVals), ListInstancesTOClassify.numAttributes());
            ListInstancesTOClassify.setClassIndex(ListInstancesTOClassify.numAttributes() - 1);

            List<int> ListIdx = new List<int>();
            int Max = int.MinValue;
            int Min = int.MaxValue;

            for (int i = 0; i < ListInstancesTOClassify.numInstances(); i++)
            {
                Instance InstToProcess = ListInstancesTOClassify.instance(i);
               int Value =(int)GlobalInfo.CurrentScreen.CellBasedClassification.J48Model.classifyInstance(InstToProcess);
               if (Value > Max) Max = Value;
               if (Value < Min) Min = Value;

               ListIdx.Add(Value);
            }

            byte[][] LUT = GlobalInfo.LUT;

                for (int j = 0; j < this.chartForPoints.Series[0].Points.Count; j++)
                {
                    int ConvertedValue = (int)(((ListIdx[j] - Min) * (LUT[0].Length - 1)) / (Max - Min));
                    this.chartForPoints.Series[0].Points[j].MarkerColor = Color.FromArgb(LUT[0][ConvertedValue], LUT[1][ConvertedValue], LUT[2][ConvertedValue]);
                }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create an instances structure with classes for supervised methods
        /// </summary>
        /// <param name="NumClass"></param>
        /// <returns></returns>
        public Instances CreateInstancesWithClasses(cInfoClass InfoClass, int NeutralClass)
        {
            weka.core.FastVector atts = new FastVector();

            int columnNo = 0;

            for (int i = 0; i < ParentScreening.ListDescriptors.Count; i++)
            {
                if (ParentScreening.ListDescriptors[i].IsActive() == false) continue;
                atts.addElement(new weka.core.Attribute(ParentScreening.ListDescriptors[i].GetName()));
                columnNo++;
            }

            weka.core.FastVector attVals = new FastVector();

            for (int i = 0; i < InfoClass.NumberOfClass; i++)
                attVals.addElement("Class__" + (i).ToString());

            atts.addElement(new weka.core.Attribute("Class__", attVals));

            Instances data1 = new Instances("MyRelation", atts, 0);
            int IdxWell = 0;
            foreach (cWell CurrentWell in this.ListActiveWells)
            {
                if (CurrentWell.GetCurrentClassIdx() == NeutralClass) continue;
                double[] vals = new double[data1.numAttributes()];

                int IdxCol = 0;
                for (int Col = 0; Col < ParentScreening.ListDescriptors.Count; Col++)
                {
                    if (ParentScreening.ListDescriptors[Col].IsActive() == false) continue;
                    vals[IdxCol++] = CurrentWell.ListSignatures[Col].GetValue();
                }
                vals[columnNo] = InfoClass.CorrespondanceTable[CurrentWell.GetCurrentClassIdx()];
                data1.add(new DenseInstance(1.0, vals));
                IdxWell++;
            }
            data1.setClassIndex((data1.numAttributes() - 1));

            return data1;
        }
        public MITesDataCollectionForm(string dataDirectory, string arffFile, bool isHierarchical)
        { 
        

                       //where data is being stored
            this.dataDirectory = dataDirectory;

            //Initialize high resolution unix timer
            UnixTime.InitializeTime();

            //Initialize and start GUI progress thread
            progressMessage = null;
            aProgressThread = new Thread(new ThreadStart(ProgressThread));
            aProgressThread.Start();


            #region Load Configuration files
            //load the activity and sensor configuration files
            progressMessage = "Loading XML protocol and sensors ...";
            AXML.Reader reader = new AXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory);
#if (!PocketPC)
            if (reader.validate() == false)
            {
                throw new Exception("Error Code 0: XML format error - activities.xml does not match activities.xsd!");
            }
            else
            {
#endif
            this.annotation = reader.parse();
            this.annotation.DataDirectory = dataDirectory;
            SXML.Reader sreader = new SXML.Reader(Constants.MASTER_DIRECTORY, dataDirectory);
#if (!PocketPC)

                if (sreader.validate() == false)
                {
                    throw new Exception("Error Code 0: XML format error - sensors.xml does not match sensors.xsd!");
                }
                else
                {
#endif
            this.sensors = sreader.parse(Constants.MAX_CONTROLLERS);
            progressMessage += " Completed\r\n";

            //TODO: remove BT components
            progressMessage += "Loading configuration file ...";
            MITesFeatures.core.conf.ConfigurationReader creader = new MITesFeatures.core.conf.ConfigurationReader(dataDirectory);
            this.configuration = creader.parse();
            progressMessage += " Completed\r\n";
#if (!PocketPC)
                }
            }
#endif
            #endregion Load Configuration files



            #region Initialize External Data Reception Channels

                //Initialize 1 master decoder
                this.masterDecoder = new MITesDecoder();

                //Initialize the software mode
                isExtracting = false;
                isCollectingDetailedData = false;
                isPlotting = true;
                isClassifying = true;


                #region Initialize Feature Extraction
                this.isExtracting = false;
                if (this.sensors.TotalReceivers > 0) // if there is at least 1 MIT
                    //Extractor.Initialize(this.mitesDecoders[0], dataDirectory, this.annotation, this.sensors, this.configuration);
                    Extractor.Initialize(this.masterDecoder, dataDirectory, this.annotation, this.sensors, this.configuration);
                else if (this.sensors.Sensors.Count > 0) // only built in
                    Extractor.Initialize(this.masterDecoder, dataDirectory, this.annotation, this.sensors, this.configuration);
                #endregion Initialize Feature Extraction

                labelIndex = new Hashtable();
                instances = new Instances(new StreamReader(arffFile));
                instances.Class = instances.attribute(Extractor.ArffAttributeLabels.Length);
                classifier = new J48();
                if (!File.Exists("model.xml"))
                {
                    classifier.buildClassifier(instances);
                    TextWriter tc = new StreamWriter("model.xml");
                    classifier.toXML(tc);
                    tc.Flush();
                    tc.Close();
                }
                else
                    classifier.buildClassifier("model.xml", instances);

               
                fvWekaAttributes = new FastVector(Extractor.ArffAttributeLabels.Length + 1);
                for (int i = 0; (i < Extractor.ArffAttributeLabels.Length); i++)
                    fvWekaAttributes.addElement(new weka.core.Attribute(Extractor.ArffAttributeLabels[i]));

                FastVector fvClassVal = new FastVector();
                labelCounters = new int[((AXML.Category)this.annotation.Categories[0]).Labels.Count + 1];
                activityLabels = new string[((AXML.Category)this.annotation.Categories[0]).Labels.Count + 1];
                for (int i = 0; (i < ((AXML.Category)this.annotation.Categories[0]).Labels.Count); i++)
                {
                    labelCounters[i] = 0;
                    string label = "";
                    int j = 0;
                    for (j = 0; (j < this.annotation.Categories.Count - 1); j++)
                        label += ((AXML.Label)((AXML.Category)this.annotation.Categories[j]).Labels[i]).Name.Replace(' ', '_') + "_";
                    label += ((AXML.Label)((AXML.Category)this.annotation.Categories[j]).Labels[i]).Name.Replace(' ', '_');
                    activityLabels[i] = label;
                    labelIndex.Add(label, i);
                    fvClassVal.addElement(label);
                }

                weka.core.Attribute ClassAttribute = new weka.core.Attribute("activity", fvClassVal);

                isClassifying = true;

                this.aMITesActivityCounters = new Hashtable();


                if (!((this.sensors.Sensors.Count == 1) && (this.sensors.HasBuiltinSensors)))
                {
                    //Initialize arrays to store USB and Bluetooth controllers
                    this.mitesControllers = new MITesReceiverController[this.sensors.TotalWiredReceivers];

#if (PocketPC)
                    this.bluetoothControllers = new BluetoothController[this.sensors.TotalBluetoothReceivers];
                    //this.ts = new Thread[this.sensors.TotalBluetoothReceivers];
#endif

                    //Initialize array to store Bluetooth connection status
                    //this.bluetoothConnectionStatus = new bool[this.sensors.TotalBluetoothReceivers];

                    //Initialize a decoder for each sensor
                    this.mitesDecoders = new MITesDecoder[this.sensors.TotalReceivers];


#if (PocketPC)
                    #region Bluetooth reception channels initialization
                    //Initialize and search for wockets connections
                    progressMessage += "Initializing Bluetooth receivers ... searching " + this.sensors.TotalBluetoothReceivers + " BT receivers\r\n";
                    //Try to initialize all Bluetooth receivers 10 times then exit
                    int initializationAttempt = 0;
                    while (initializationAttempt <= 10)
                    {
                        if (InitializeBluetoothReceivers() == false)
                        {
                            initializationAttempt++;

                            if (initializationAttempt == 10)
                            {
                                MessageBox.Show("Exiting: Some Bluetooth receivers in your configuration were not initialized.");
                                Application.Exit();
                                System.Diagnostics.Process.GetCurrentProcess().Kill();

                            }
                            else
                                progressMessage += "Failed to initialize all BT connections. Retrying (" + initializationAttempt + ")...\r\n";

                        }
                        else
                            break;
                        Thread.Sleep(2000);
                    }
                    #endregion Bluetooth reception channels initialization
#endif
                    #region USB reception channels initialization

                    if (InitializeUSBReceivers() == false)
                    {
                        MessageBox.Show("Exiting: Some USB receivers in your configuration were not initialized.");
#if (PocketPC)
                        Application.Exit();
                        System.Diagnostics.Process.GetCurrentProcess().Kill();
#else
                    Environment.Exit(0);
#endif

                    }
                    #endregion USB reception channels initialization

                }
                    //}
            #endregion Initialize External Data Reception Channels

#if (PocketPC)
            #region Initialize Builtin Data Reception Channels
            if (InitializeBuiltinReceivers() == false)
            {
                MessageBox.Show("Exiting: A built in receiver channel was not found.");
                Application.Exit();
                System.Diagnostics.Process.GetCurrentProcess().Kill();

            }
            #endregion Initialize Builtin Data Reception Channels

#endif            

            #region Initialize GUI Components
            //initialize the interface components
            InitializeComponent();
            //Initialize GUI timers
            progressMessage += "Initializing Timers ...";
            InitializeTimers();
            progressMessage += " Completed\r\n";

            //Initialize different GUI components
            progressMessage += "Initializing GUI ...";
            InitializeInterface();
            progressMessage += " Completed\r\n";

            this.isPlotting = true;
            //count the number of accelerometers
            if (this.sensors.IsHR)
                this.maxPlots = this.sensors.Sensors.Count - 1;
            else
                this.maxPlots = this.sensors.Sensors.Count;
            SetFormPositions();
            if (this.sensors.TotalReceivers > 0)
                aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots, this.masterDecoder, GetGraphSize(false));
            else
                aMITesPlotter = new MITesScalablePlotter(this.panel1, MITesScalablePlotter.DeviceTypes.IPAQ, maxPlots, this.masterDecoder, GetGraphSize(false));

            //Override the resize event
#if (PocketPC)
            this.Resize += new EventHandler(OnResize);
#else
            this.form1.Resize += new EventHandler(OnResizeForm1);
            this.form1.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form2.Resize += new EventHandler(OnResizeForm2);
            this.form2.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form3.Resize += new EventHandler(OnResizeForm3);
            this.form3.FormClosing += new FormClosingEventHandler(form_FormClosing);
            this.form4.Resize += new EventHandler(OnResizeForm4);
            this.form4.FormClosing += new FormClosingEventHandler(form_FormClosing);
#endif

            //Initialize the quality interface
            progressMessage += "Initializing MITes Quality GUI ...";
            InitializeQualityInterface();
            progressMessage += " Completed\r\n";

            //Remove classifier tabs
#if (PocketPC)

            this.tabControl1.TabPages.RemoveAt(4);
            this.tabControl1.SelectedIndex = 0;
#else
            this.ShowForms();
#endif


            #endregion Initialize GUI Components



            #region Initialize Quality Tracking variables
            InitializeQuality();
            #endregion Initialize Quality Tracking variables

            #region Initialize Logging
            InitializeLogging(dataDirectory);
            #endregion Initialize Logging

            #region Initialize CSV Storage (PC Only)
#if (!PocketPC)

            //create some counters for activity counts
            averageX = new int[this.sensors.MaximumSensorID + 1];
            averageY = new int[this.sensors.MaximumSensorID + 1];
            averageZ = new int[this.sensors.MaximumSensorID + 1];

            averageRawX = new int[this.sensors.MaximumSensorID + 1];
            averageRawY = new int[this.sensors.MaximumSensorID + 1];
            averageRawZ = new int[this.sensors.MaximumSensorID + 1];

            prevX = new int[this.sensors.MaximumSensorID + 1];
            prevY = new int[this.sensors.MaximumSensorID + 1];
            prevZ = new int[this.sensors.MaximumSensorID + 1];
            acCounters = new int[this.sensors.MaximumSensorID + 1];
            activityCountWindowSize = 0;

            activityCountCSVs = new StreamWriter[this.sensors.MaximumSensorID + 1];
            samplingCSVs = new StreamWriter[this.sensors.MaximumSensorID + 1];
            averagedRaw = new StreamWriter[this.sensors.MaximumSensorID + 1];
            masterCSV = new StreamWriter(dataDirectory + "\\MITesSummaryData.csv");
            hrCSV = new StreamWriter(dataDirectory + "\\HeartRate_MITes.csv");

            string csv_line1 = "UnixTimeStamp,TimeStamp,X,Y,Z";
            string csv_line2 = "UnixTimeStamp,TimeStamp,Sampling";
            string hr_csv_header = "UnixTimeStamp,TimeStamp,HR";
            string master_csv_header = "UnixTimeStamp,TimeStamp";
            foreach (Category category in this.annotation.Categories)
                master_csv_header += "," + category.Name;


            foreach (Sensor sensor in this.sensors.Sensors)
            {
                int sensor_id = Convert.ToInt32(sensor.ID);
                string location = sensor.Location.Replace(' ', '-');
                if (sensor_id > 0) //exclude HR
                {
                    activityCountCSVs[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_ActivityCount_" + location + ".csv");
                    activityCountCSVs[sensor_id].WriteLine(csv_line1);
                    averagedRaw[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_1s-RawMean_" + location + ".csv");
                    averagedRaw[sensor_id].WriteLine(csv_line1);
                    samplingCSVs[sensor_id] = new StreamWriter(dataDirectory + "\\MITes_" + sensor_id.ToString("00") + "_SampleRate_" + location + ".csv");
                    samplingCSVs[sensor_id].WriteLine(csv_line2);
                    master_csv_header += ",MITes" + sensor_id.ToString("00") + "_SR," + "MITes" + sensor_id.ToString("00") + "_AVRaw_X," +
                        "MITes" + sensor_id.ToString("00") + "_AVRaw_Y," + "MITes" + sensor_id.ToString("00") + "_AVRaw_Z," + "MITes" + sensor_id.ToString("00") + "_AC_X," +
                        "MITes" + sensor_id.ToString("00") + "_AC_Y," + "MITes" + sensor_id.ToString("00") + "_AC_Z";

                }
            }

            master_csv_header += ",HR";
            this.masterCSV.WriteLine(master_csv_header);
            this.hrCSV.WriteLine(hr_csv_header);
#endif

            #endregion Initialize CSV Storage (PC Only)

            #region Start Collecting Data



            //if (this.sensors.TotalReceivers > 0)
            //    isStartedReceiver = true;
            //Start the built in polling thread            
#if (PocketPC)
            if (this.sensors.HasBuiltinSensors)
            {
                this.pollingThread = new Thread(new ThreadStart(this.pollingData));
                this.pollingThread.Priority = ThreadPriority.Lowest;
                this.pollingThread.Start();
            }
#endif

            //Terminate the progress thread
            progressThreadQuit = true;

           
            //Enable all timer functions
            this.readDataTimer.Enabled = true;
            this.qualityTimer.Enabled = true;
            if (this.sensors.IsHR)
                this.HRTimer.Enabled = true;

            #endregion Start Collecting Data

        }
Exemplo n.º 5
0
        /// <summary>
        /// Create a single instance for WEKA
        /// </summary>
        /// <param name="NClasses">Number of classes</param>
        /// <returns>the weka instances</returns>
        public Instances CreateInstanceForNClasses(cInfoClass InfoClass)
        {
            List<double> AverageList = new List<double>();

            for (int i = 0; i < Parent.ListDescriptors.Count; i++)
                if (Parent.ListDescriptors[i].IsActive()) AverageList.Add(GetAverageValuesList()[i]);

            weka.core.FastVector atts = new FastVector();

            List<string> NameList = Parent.ListDescriptors.GetListNameActives();

            for (int i = 0; i < NameList.Count; i++)
                atts.addElement(new weka.core.Attribute(NameList[i]));

            weka.core.FastVector attVals = new FastVector();
            for (int i = 0; i < InfoClass.NumberOfClass; i++)
                attVals.addElement("Class" + i);

            atts.addElement(new weka.core.Attribute("Class__", attVals));

            Instances data1 = new Instances("SingleInstance", atts, 0);

            double[] newTable = new double[AverageList.Count + 1];
            Array.Copy(AverageList.ToArray(), 0, newTable, 0, AverageList.Count);
            //newTable[AverageList.Count] = 1;

            data1.add(new DenseInstance(1.0, newTable));
            data1.setClassIndex((data1.numAttributes() - 1));
            return data1;
        }
Exemplo n.º 6
0
		/// <summary> Set the output format if the class is nominal.</summary>
		private void  setOutputFormat()
		{
			
			FastVector newAtts;
			int newClassIndex;
			System.Text.StringBuilder attributeName;
			Instances outputFormat;
			FastVector vals;
			
			// Compute new attributes
			
			newClassIndex = getInputFormat().classIndex();
			newAtts = new FastVector();
			for (int j = 0; j < getInputFormat().numAttributes(); j++)
			{
				weka.core.Attribute att = getInputFormat().attribute(j);
				if (!att.Nominal || (j == getInputFormat().classIndex()) || !m_Columns.isInRange(j))
				{
					newAtts.addElement(att.copy());
				}
				else
				{
					if (att.numValues() <= 2)
					{
						if (m_Numeric)
						{
							newAtts.addElement(new weka.core.Attribute(att.name()));
						}
						else
						{
							newAtts.addElement(att.copy());
						}
					}
					else
					{
						
						if (newClassIndex >= 0 && j < getInputFormat().classIndex())
						{
							newClassIndex += att.numValues() - 1;
						}
						
						// Compute values for new attributes
						for (int k = 0; k < att.numValues(); k++)
						{
							attributeName = new System.Text.StringBuilder(att.name() + "=");
							attributeName.Append(att.value_Renamed(k));
							if (m_Numeric)
							{
								newAtts.addElement(new weka.core.Attribute(attributeName.ToString()));
							}
							else
							{
								vals = new FastVector(2);
								vals.addElement("f"); vals.addElement("t");
                                newAtts.addElement(new weka.core.Attribute(attributeName.ToString(), vals));
							}
						}
					}
				}
			}
			outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0);
			outputFormat.ClassIndex = newClassIndex;
			setOutputFormat(outputFormat);
		}
Exemplo n.º 7
0
        public void PerformClassification()
        {
            FormForSingleCellClassifOptions FFSC = new FormForSingleCellClassifOptions();
            //cGUI_ListClasses GLC = new cGUI_ListClasses();
            //GLC.ClassType = eClassType.PHENOTYPE;
            //GLC.IsCheckBoxes = true;
            //GLC.IsSelectAll = true;
            //GLC.Run(GlobalInfo);

            PanelForClassSelection PhenotypeSelectionPanel = new PanelForClassSelection( true, eClassType.PHENOTYPE);
            PhenotypeSelectionPanel.Height = FFSC.panelPhenoToBeClassified.Height;
            FFSC.panelPhenoToBeClassified.Controls.Add(PhenotypeSelectionPanel);

            PanelForClassSelection WellClassSelectionPanel = new PanelForClassSelection( true, eClassType.WELL);
            WellClassSelectionPanel.Height = FFSC.panelWellToBeClassified.Height;
            FFSC.panelWellToBeClassified.Controls.Add(WellClassSelectionPanel);

            PanelForPlatesSelection PlatesSelectionPanel = new PanelForPlatesSelection( true, null, true);
            PlatesSelectionPanel.Height = FFSC.panelWellToBeClassified.Height;
            FFSC.tabPagePlates.Controls.Add(PlatesSelectionPanel);

            if (FFSC.ShowDialog() != DialogResult.OK) return;

            // ----------------------- Classification ------------------------------
            int DescrCount = cGlobalInfo.CurrentScreening.ListDescriptors.Count;

            this.UpDateNumberOfCluster();
            if (NumberOfClusters == 0)
            {
                System.Windows.Forms.MessageBox.Show("Number of cluster is null", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            //if (FFSC.checkBoxGenerationRatio.Checked)
            //{
            //    // first we update the descriptor
            //    for (int i = 0; i < this.NumberOfClusters; i++)
            //        GlobalInfo.CurrentScreening.ListDescriptors.AddNew(new cDescriptorType("Ratio_" + GlobalInfo.ListCellularPhenotypes[i].Name, true, 1, GlobalInfo));
            //}

            FormForProgress ProgressWindow = new FormForProgress();
            ProgressWindow.Show();

            int IdxProgress = 0;
            int MaxProgress = 0;

            #region Confusion Matrix init
            cListExtendedTable LT = new cListExtendedTable();
            cExtendedTable ConfusionMatrix = new cExtendedTable(cGlobalInfo.ListCellularPhenotypes.Count, cGlobalInfo.ListCellularPhenotypes.Count, 0);
            ConfusionMatrix.Name = "Confusion Matrix (global)";
            ConfusionMatrix.ListRowNames = new List<string>();
            for (int i = 0; i < cGlobalInfo.ListCellularPhenotypes.Count; i++)
            {
                ConfusionMatrix.ListRowNames.Add(cGlobalInfo.ListCellularPhenotypes[i].Name + "*");
                ConfusionMatrix[i].Name = cGlobalInfo.ListCellularPhenotypes[i].Name;
            }
            LT.Add(ConfusionMatrix);
            for (int i = 0; i < cGlobalInfo.ListWellClasses.Count; i++)
            {
                cExtendedTable ConfusionMatrixTmp = new cExtendedTable(cGlobalInfo.ListCellularPhenotypes.Count, cGlobalInfo.ListCellularPhenotypes.Count, 0);
                ConfusionMatrixTmp.Name = "Confusion Matrix - " + cGlobalInfo.ListWellClasses[i].Name;
                ConfusionMatrixTmp.ListRowNames = new List<string>();
                for (int j = 0; j < cGlobalInfo.ListCellularPhenotypes.Count; j++)
                {
                    ConfusionMatrixTmp.ListRowNames.Add(cGlobalInfo.ListCellularPhenotypes[j].Name + "*");
                    ConfusionMatrixTmp[j].Name =cGlobalInfo.ListCellularPhenotypes[j].Name;
                }
                LT.Add(ConfusionMatrixTmp);
            }
            #endregion

            cListPlates LP = PlatesSelectionPanel.GetListSelectedPlates();

            foreach (cPlate CurrentPlateToProcess in LP /*GlobalInfo.CurrentScreening.ListPlatesAvailable*/)
                MaxProgress += (int)CurrentPlateToProcess.ListActiveWells.Count;
            ProgressWindow.progressBar.Maximum = MaxProgress;

            FastVector attVals = new FastVector();
            for (int i = 0; i < this.NumberOfClusters; i++)
                attVals.addElement(i.ToString());

            cPlate CurrentDispPlate = cGlobalInfo.CurrentScreening.GetCurrentDisplayPlate();

            foreach (cPlate CurrentPlateToProcess in LP/* GlobalInfo.CurrentScreening.ListPlatesAvailable*/)
            {
                foreach (cWell TmpWell in CurrentPlateToProcess.ListActiveWells)
                {
                    ProgressWindow.progressBar.Value = IdxProgress++;

                    if (TmpWell.GetCurrentClassIdx() == -1) continue;
                    if (WellClassSelectionPanel.ListCheckBoxes[TmpWell.GetCurrentClassIdx()].Checked == false) continue;

                    DataTable FinalDataTable = new DataTable();
                    TmpWell.AssociatedPlate.DBConnection = new cDBConnection(TmpWell.AssociatedPlate, TmpWell.SQLTableName);
                    TmpWell.AssociatedPlate.DBConnection.AddWellToDataTable(TmpWell, FinalDataTable, this.GlobalInfo);
                    cListSingleBiologicalObjects LSBO = TmpWell.AssociatedPlate.DBConnection.GetBiologicalPhenotypes(TmpWell);
                    //TmpWell.AssociatedPlate.DBConnection.AddWellToDataTable(TmpWell, FinalDataTable, checkBoxIncludeWellClassAsDesc.Checked, GlobalInfo);
                    Instances ListInstancesTOClassify = this.CreateInstancesWithoutClass(FinalDataTable);

                    ListInstancesTOClassify.insertAttributeAt(new weka.core.Attribute("Class", attVals), ListInstancesTOClassify.numAttributes());
                    ListInstancesTOClassify.setClassIndex(ListInstancesTOClassify.numAttributes() - 1);

                    cExtendedList ListNewClasses = new cExtendedList();

                    int NumInstances = ListInstancesTOClassify.numInstances();
                    for (int i = 0; i < NumInstances; i++)
                    {
                        // ClassId contains the new class
                        Instance CurrentInst = ListInstancesTOClassify.instance(i);

                        double classId = this.CurrentClassifier.classifyInstance(CurrentInst);
                        double[] ClassConfidence = this.CurrentClassifier.distributionForInstance(CurrentInst);
                        LSBO[i].ClassificationConfidence = ClassConfidence[(int)classId];
                        ListNewClasses.Add(classId);

                        if (CurrentPlateToProcess == CurrentDispPlate)
                        {
                            LT[0][LSBO[i].GetAssociatedPhenotype().Idx][(int)classId]++;

                            if (TmpWell.GetCurrentClassIdx() >= 0)
                                LT[TmpWell.GetCurrentClassIdx() + 1][LSBO[i].GetAssociatedPhenotype().Idx][(int)classId]++;
                        }
                    }

                    ProgressWindow.richTextBoxForComment.AppendText(TmpWell.GetShortInfo().Remove(TmpWell.GetShortInfo().Length - 2) + " : " + NumInstances + " objects\n");
                    ProgressWindow.Refresh();
                    // ------------- update class within the database -----------------------------
                    TmpWell.AssociatedPlate.DBConnection.ChangePhenotypeClass(TmpWell, ListNewClasses);

                    //if (FFSC.checkBoxGenerationRatio.Checked)
                    //{
                    //    List<double[]> Histo = ListNewClasses.CreateHistogram(0, ListInstancesTOClassify.numClasses(), ListInstancesTOClassify.numClasses());
                    //    cListSignature LDesc = new cListSignature();

                    //    for (int IdxHisto = 0; IdxHisto < Histo[1].Length - 1; IdxHisto++)
                    //    {
                    //        Histo[1][IdxHisto] = (100.0 * Histo[1][IdxHisto]) / (double)ListInstancesTOClassify.numInstances();

                    //        cSignature NewDesc = new cSignature(Histo[1][IdxHisto], GlobalInfo.CurrentScreening.ListDescriptors[IdxHisto + DescrCount], GlobalInfo.CurrentScreening);
                    //        LDesc.Add(NewDesc);
                    //    }
                    //    TmpWell.AddSignatures(LDesc);
                    //}

                    TmpWell.AssociatedPlate.DBConnection.CloseConnection();
                }
            }

            #region Display Report
            cDesignerSplitter DS = new cDesignerSplitter();
            DS.Orientation = Orientation.Vertical;

            cViewertext VTEXT = new cViewertext();
            VTEXT.SetInputData(ProgressWindow.richTextBoxForComment.Text);
            VTEXT.Run();

            cDesignerTab DT = new cDesignerTab();
            DT.IsMultiline = false;

            foreach (var item in LT)
            {
                cViewerTable VT = new cViewerTable();
                VT.SetInputData(item);
                VT.DigitNumber = 0;
                VT.Run();
                DT.SetInputData(VT.GetOutPut());
            }

            DT.Run();

            cExtendedControl TextEC = DT.GetOutPut();
            TextEC.Width = 0;
            TextEC.Height = 0;
            TextEC.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
                                                                | System.Windows.Forms.AnchorStyles.Left
                                                                | System.Windows.Forms.AnchorStyles.Right);

            DS.SetInputData(VTEXT.GetOutPut());
            DS.SetInputData(TextEC);

            DS.Run();

            ProgressWindow.Close();

            cDisplayToWindow CDT = new cDisplayToWindow();
            CDT.SetInputData(DS.GetOutPut());
            CDT.IsModal = true;
            CDT.Title = "Phenotypic Classificaton Report";
            CDT.Run();
            CDT.Display();

            #endregion

            //if (IsKeepOriginalDesc == System.Windows.Forms.DialogResult.No)
            //{
            //    // int DescNumToRemove = GlobalInfo.CurrentScreen.ListDescriptors.Count -
            //    for (int IdxDesc = 0; IdxDesc < DescrCount; IdxDesc++)
            //        GlobalInfo.CurrentScreening.ListDescriptors.RemoveDesc(GlobalInfo.CurrentScreening.ListDescriptors[0], GlobalInfo.CurrentScreening);
            //}

            cGlobalInfo.CurrentScreening.ListDescriptors.UpDateDisplay();
            cGlobalInfo.CurrentScreening.UpDatePlateListWithFullAvailablePlate();

            for (int idxP = 0; idxP < cGlobalInfo.CurrentScreening.ListPlatesActive.Count; idxP++)
                cGlobalInfo.CurrentScreening.ListPlatesActive[idxP].UpDataMinMax();

            //WindowFormForCellbyCellClassif.Close();
            //WindowClusteringInfo.Close();
        }
Exemplo n.º 8
0
        ///    
        ///     <summary> * Sets the format of output instances. </summary>
        ///     
        private void setOutputFormat()
        {
            // give names to the new attributes
            FastVector newAtts = new FastVector();
            string foName = null;
            for (int i = 0; i < getInputFormat().numAttributes(); i++)
            {
                string attName = getInputFormat().attribute(i).name();
                foName = "'FT " + attName.Replace('\'', ' ').Trim() + " (re)\'";
                Attribute newAttribX = new Attribute(foName);
                newAtts.addElement(newAttribX);

                foName = "'FT " + attName.Replace('\'', ' ').Trim() + " (im)\'";
                Attribute newAttribY = new Attribute(foName);
                newAtts.addElement(newAttribY);
            }

            setOutputFormat(new Instances(getInputFormat().relationName(), newAtts, getNumCoeffs()));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Create an instances structure without classes for unsupervised methods
        /// </summary>
        /// <returns>a weka Instances object</returns>
        public Instances CreateInstancesWithoutClass()
        {
            weka.core.FastVector atts = new FastVector();
            int columnNo = 0;

            // Descriptors loop
            for (int i = 0; i < ParentScreening.ListDescriptors.Count; i++)
            {
                if (ParentScreening.ListDescriptors[i].IsActive() == false) continue;
                atts.addElement(new weka.core.Attribute(ParentScreening.ListDescriptors[i].GetName()));
                columnNo++;
            }
            weka.core.FastVector attVals = new FastVector();
            Instances data1 = new Instances("MyRelation", atts, 0);

            foreach (cWell CurrentWell in this.ListActiveWells)
            {
                double[] vals = new double[data1.numAttributes()];

                int IdxRealCol = 0;

                for (int Col = 0; Col < ParentScreening.ListDescriptors.Count; Col++)
                {
                    if (ParentScreening.ListDescriptors[Col].IsActive() == false) continue;
                    vals[IdxRealCol++] = CurrentWell.ListSignatures[Col].GetValue();
                }
                data1.add(new DenseInstance(1.0, vals));
            }

            return data1;
        }
        private void loadModelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog CurrOpenFileDialog = new OpenFileDialog();

            CurrOpenFileDialog.Filter = "Classification Model files (*.model)|*.model";
            DialogResult Res = CurrOpenFileDialog.ShowDialog();
            if (Res != DialogResult.OK) return;

            if (CurrOpenFileDialog.FileName == "") return;

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(CurrOpenFileDialog.FileName,
                                      FileMode.Open,
                                      FileAccess.Read,
                                      FileShare.Read);

            cMyClassifierModel obj = (cMyClassifierModel)formatter.Deserialize(stream);
            stream.Close();

            if (this.comboBoxAxeX.Items.Count != obj.ListDescNames.Count)
            {
                string ListDescRequired = obj.ListDescNames.Count + " descriptors are required:\n";
                foreach (var item in obj.ListDescNames)
                {
                    ListDescRequired += item + "\n";
                }

                MessageBox.Show(ListDescRequired, "Descriptor list does not match !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int IdxDesc = 0;
            foreach (var item in this.comboBoxAxeX.Items)
            {
                if (obj.ListDescNames[IdxDesc++] != item.ToString())
                {
                    MessageBox.Show("Descriptor list does not match !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            #region Display associated info and graphs

            this.richTextBoxForResults.Clear();
            this.richTextBoxForResults.AppendText(obj.ClassifModel.ToString());

            if (obj.ClassifModel.GetType() == typeof(J48))
            {
                GViewer GraphView = MachineLearning.DisplayTree(GlobalInfo, (J48)obj.ClassifModel, true).gViewerForTreeClassif;
                GraphView.Size = new System.Drawing.Size(this.panelForGraphicalResults.Width, this.panelForGraphicalResults.Height);
                GraphView.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
                this.panelForGraphicalResults.Controls.Clear();
                this.panelForGraphicalResults.Controls.Add(GraphView);

            }

            this.MachineLearning.CurrentClassifier = obj.ClassifModel;

            Instances ListInstancesTOClassify = cGlobalInfo.CurrentScreening.CellBasedClassification.CreateInstancesWithoutClass(/*dt*/this.InputTable);

            SecondListClassesForValidation = null;

            FastVector attVals = new FastVector();
            for (int i = 0; i < MachineLearning.NumberOfClusters; i++)
                attVals.addElement(i.ToString());

            ListInstancesTOClassify.insertAttributeAt(new weka.core.Attribute("Class", attVals), ListInstancesTOClassify.numAttributes());
            ListInstancesTOClassify.setClassIndex(ListInstancesTOClassify.numAttributes() - 1);

            for (int i = 0; i < ListInstancesTOClassify.numInstances(); i++)
            {
                this.MachineLearning.Classes[i] = MachineLearning.CurrentClassifier.classifyInstance(ListInstancesTOClassify.instance(i));
            }

            buttonClassify.Enabled = true;
            ReDraw();

            #endregion
        }
        //private void ToolStripMenuItem_CurrentPhenoVisibility(object sender, EventArgs e)
        //{
        //    int SelectedIdx = (int)(((ToolStripMenuItem)sender).Tag);
        //    if (ListPhenotypeVisible[SelectedIdx]) ListPhenotypeVisible[SelectedIdx] = false;
        //    else ListPhenotypeVisible[SelectedIdx] = true;
        //    this.ReFreshMainGraph();
        //}
        private void ToolStripMenuItem_ApplyCurrentModel(object sender, EventArgs e)
        {
            Instances InstancesList = cGlobalInfo.CurrentScreening.CellBasedClassification.CreateInstancesWithoutClass(/*dt*/this.InputTable);

            FastVector attValsWithoutClasses = new FastVector();

            for (int i = 0; i < cGlobalInfo.ListCellularPhenotypes.Count; i++)
                attValsWithoutClasses.addElement(cGlobalInfo.ListCellularPhenotypes[i].Name);

            InstancesList.insertAttributeAt(new weka.core.Attribute("Class", attValsWithoutClasses), InstancesList.numAttributes());
            //int A = Classes.Count;
            for (int i = 0; i < MachineLearning.Classes.Count; i++)
                InstancesList.get(i).setValue(InstancesList.numAttributes() - 1, MachineLearning.Classes[i]);

            InstancesList.setClassIndex(InstancesList.numAttributes() - 1);

            // Instances ListInstancesTOClassify = ListInstances;
            SecondListClassesForValidation = new double[InstancesList.numInstances()];
            //// ListInstances.setClassIndex(ListInstances.numAttributes() - 1);
            for (int i = 0; i < InstancesList.numInstances(); i++)
            {
                SecondListClassesForValidation[i] = MachineLearning.CurrentClassifier.classifyInstance(InstancesList.instance(i));
            }

            buttonClassify.Enabled = true;
            ReDraw();
        }
Exemplo n.º 12
0
		/// <summary> Constructor for nominal attributes and string attributes, where
		/// metadata is supplied. If a null vector of attribute values is passed
		/// to the method, the attribute is assumed to be a string.
		/// 
		/// </summary>
		/// <param name="attributeName">the name for the attribute
		/// </param>
		/// <param name="attributeValues">a vector of strings denoting the 
		/// attribute values. Null if the attribute is a string attribute.
		/// </param>
		/// <param name="metadata">the attribute's properties
		/// </param>
		//@ requires attributeName != null;
		//@ requires metadata != null;
		/*@ ensures  m_Name == attributeName;
		ensures  m_Index == -1;
		ensures  attributeValues == null && m_Type == STRING
		|| attributeValues != null && m_Type == NOMINAL 
		&& m_Values.size() == attributeValues.size();
		signals (IllegalArgumentException ex) 
		(* if duplicate strings in attributeValues *);
		*/
		public Attribute(System.String attributeName, FastVector attributeValues, ProtectedProperties metadata)
		{
			
			m_Name = attributeName;
			m_Index = - 1;
			if (attributeValues == null)
			{
				m_Values = new FastVector();
				m_Hashtable = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
				m_Type = STRING;
			}
			else
			{
				m_Values = new FastVector(attributeValues.size());
				//m_Hashtable = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable(attributeValues.size()));
                m_Hashtable = new System.Collections.Hashtable(attributeValues.size());
				for (int i = 0; i < attributeValues.size(); i++)
				{
                    // No serialization
					System.Object store = attributeValues.elementAt(i);
					
                    //if (((System.String) store).Length > STRING_COMPRESS_THRESHOLD)
					//{
					//	try
					//	{
					//		store = new SerializedObject(attributeValues.elementAt(i), true);
					//	}
					//	catch (System.Exception ex)
					//	{
					//		System.Console.Error.WriteLine("Couldn't compress nominal attribute value -" + " storing uncompressed.");
					//	}
					//}

					if (m_Hashtable.ContainsKey(store))
					{
						//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
						throw new System.ArgumentException("A nominal attribute (" + attributeName + ") cannot" + " have duplicate labels (" + store + ").");
					}
					m_Values.addElement(store);
                    m_Hashtable.Add(store, (System.Int32) i);

				}
				m_Type = NOMINAL;
			}
			setMetadata(metadata);
		}
Exemplo n.º 13
0
 private FastVector createWhyFastVector()
 {
     FastVector fvWhy = new FastVector(8 + whyWordsBefore * 2 + whyWordsAfter * 2);
     fvWhy.addElement(new weka.core.Attribute("candidate", (FastVector)null));
     fvWhy.addElement(new weka.core.Attribute("wordCount"));
     fvWhy.addElement(new weka.core.Attribute("sentence"));
     fvWhy.addElement(new weka.core.Attribute("candidateScore"));
     fvWhy.addElement(new weka.core.Attribute("numWho"));
     fvWhy.addElement(new weka.core.Attribute("numWhen"));
     fvWhy.addElement(new weka.core.Attribute("numWhere"));
     for (int i = whereWordsBefore; i > 0; i--)
     {
         fvWhy.addElement(new weka.core.Attribute("word-" + i, (FastVector)null));
     }
     for (int i = 1; i <= whereWordsAfter; i++)
     {
         fvWhy.addElement(new weka.core.Attribute("word+" + i, (FastVector)null));
     }
     for (int i = whyWordsBefore; i > 0; i--)
     {
         fvWhy.addElement(new weka.core.Attribute("postag-" + i, fvPOS));
     }
     for (int i = 1; i <= whyWordsAfter; i++)
     {
         fvWhy.addElement(new weka.core.Attribute("postag+" + i, fvPOS));
     }
     FastVector fvClass = new FastVector(2);
     fvClass.addElement("yes");
     fvClass.addElement("no");
     fvWhy.addElement(new weka.core.Attribute("why", fvClass));
     return fvWhy;
 }
Exemplo n.º 14
0
 private FastVector createWhoFastVector()
 {
     FastVector fvWho = new FastVector(7 + whoWordsBefore * 2 + whoWordsAfter * 2);
     fvWho.addElement(new weka.core.Attribute("word", (FastVector)null));
     fvWho.addElement(new weka.core.Attribute("wordCount"));
     fvWho.addElement(new weka.core.Attribute("sentence"));
     fvWho.addElement(new weka.core.Attribute("position"));
     fvWho.addElement(new weka.core.Attribute("sentenceStartProximity"));
     fvWho.addElement(new weka.core.Attribute("wordScore"));
     for (int i = whoWordsBefore; i > 0; i--)
     {
         fvWho.addElement(new weka.core.Attribute("word-" + i, (FastVector)null));
     }
     for (int i = 1; i <= whoWordsAfter; i++)
     {
         fvWho.addElement(new weka.core.Attribute("word+" + i, (FastVector)null));
     }
     for (int i = whoWordsBefore; i > 0; i--)
     {
         fvWho.addElement(new weka.core.Attribute("postag-" + i, fvPOS));
     }
     for (int i = 1; i <= whoWordsAfter; i++)
     {
         fvWho.addElement(new weka.core.Attribute("postag+" + i, fvPOS));
     }
     FastVector fvClass = new FastVector(2);
     fvClass.addElement("yes");
     fvClass.addElement("no");
     fvWho.addElement(new weka.core.Attribute("who", fvClass));
     return fvWho;
 }
Exemplo n.º 15
0
        /// <summary>
        /// Create an instances structure with classes for supervised methods
        /// </summary>
        /// <param name="NumClass"></param>
        /// <returns></returns>
        public Instances CreateInstancesWithClasses(List<bool> ListClassSelected)
        {
            weka.core.FastVector atts = new FastVector();
            int columnNo = 0;
            for (int i = 0; i < ParentScreening.ListDescriptors.Count; i++)
            {
                if (ParentScreening.ListDescriptors[i].IsActive() == false) continue;
                atts.addElement(new weka.core.Attribute(ParentScreening.ListDescriptors[i].GetName()));
                columnNo++;
            }

            weka.core.FastVector attVals = new FastVector();
            foreach (var item in cGlobalInfo.ListWellClasses)
            {
                attVals.addElement(item.Name);
            }

            atts.addElement(new weka.core.Attribute("ClassAttribute", attVals));

            Instances data1 = new Instances("MyRelation", atts, 0);
            int IdxWell = 0;
            foreach (cWell CurrentWell in this.ListActiveWells)
            {
                if (!ListClassSelected[CurrentWell.GetCurrentClassIdx()]) continue;
                double[] vals = new double[data1.numAttributes()];

                int IdxCol = 0;
                for (int Col = 0; Col < ParentScreening.ListDescriptors.Count; Col++)
                {
                    if (ParentScreening.ListDescriptors[Col].IsActive() == false) continue;
                    vals[IdxCol++] = CurrentWell.ListSignatures[Col].GetValue();
                }
                vals[columnNo] = CurrentWell.GetCurrentClassIdx();
                data1.add(new DenseInstance(1.0, vals));
                IdxWell++;
            }
            data1.setClassIndex((data1.numAttributes() - 1));

            return data1;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Create an instances structure with classes for supervised methods
        /// </summary>
        /// <param name="NumClass"></param>
        /// <returns></returns>
        public Instances CreateInstancesWithClassesWithPlateBasedDescriptor(int NumberOfClass)
        {
            weka.core.FastVector atts = new FastVector();

            int columnNo = 0;

            for (int i = 0; i < ParentScreening.ListPlateBaseddescriptorNames.Count; i++)
            {
                atts.addElement(new weka.core.Attribute(ParentScreening.ListPlateBaseddescriptorNames[i]));
                columnNo++;
            }

            weka.core.FastVector attVals = new FastVector();

            for (int i = 0; i < NumberOfClass; i++)
                attVals.addElement("Class" + (i).ToString());

            atts.addElement(new weka.core.Attribute("Class", attVals));

            Instances data1 = new Instances("MyRelation", atts, 0);
            int IdxWell = 0;
            foreach (cWell CurrentWell in this.ListActiveWells)
            {
                if (CurrentWell.GetCurrentClassIdx() == -1) continue;
                double[] vals = new double[data1.numAttributes()];
                int IdxCol = 0;
                for (int Col = 0; Col < ParentScreening.ListPlateBaseddescriptorNames.Count; Col++)
                {
                    vals[IdxCol++] = CurrentWell.ListPlateBasedDescriptors[Col].GetValue();
                }
                vals[columnNo] = CurrentWell.GetCurrentClassIdx();
                data1.add(new DenseInstance(1.0, vals));
                IdxWell++;
            }
            data1.setClassIndex((data1.numAttributes() - 1));

            return data1;
        }
Exemplo n.º 17
0
		/// <summary> Renames the value of a nominal (or string) attribute value. This
		/// change only affects this dataset.
		/// 
		/// </summary>
		/// <param name="att">the attribute's index
		/// </param>
		/// <param name="val">the value's index
		/// </param>
		/// <param name="name">the new name 
		/// </param>
		public virtual void  renameAttributeValue(int att, int val, System.String name)
		{
			
			Attribute newAtt = (Attribute) attribute(att).copy();
			FastVector newVec = new FastVector(numAttributes());
			
			newAtt.setValue(val, name);
			for (int i = 0; i < numAttributes(); i++)
			{
				if (i == att)
				{
					newVec.addElement(newAtt);
				}
				else
				{
					newVec.addElement(attribute(i));
				}
			}
			m_Attributes = newVec;
		}
Exemplo n.º 18
0
        public cInfoForHierarchical CreateInstancesWithUniqueClasse()
        {
            cInfoForHierarchical InfoForHierarchical = new cInfoForHierarchical();
            weka.core.FastVector atts = new FastVector();

            int columnNo = 0;

            for (int i = 0; i < this.ParentScreening.ListDescriptors.Count; i++)
            {
                if (this.ParentScreening.ListDescriptors[i].IsActive() == false) continue;
                atts.addElement(new weka.core.Attribute(this.ParentScreening.ListDescriptors[i].GetName()));
                columnNo++;
            }

            weka.core.FastVector attVals = new FastVector();
            atts.addElement(new weka.core.Attribute("Class_____", attVals));

            InfoForHierarchical.ListInstances = new Instances("MyRelation", atts, 0);
            int IdxWell = 0;
            foreach (cWell CurrentWell in this.ListActiveWells)
            {
                if (CurrentWell.GetCurrentClassIdx() == -1) continue;
                attVals.addElement("Class_____" + (IdxWell).ToString());

                InfoForHierarchical.ListIndexedWells.Add(CurrentWell);

                double[] vals = new double[InfoForHierarchical.ListInstances.numAttributes()];
                int IdxCol = 0;
                for (int Col = 0; Col < this.ParentScreening.ListDescriptors.Count; Col++)
                {
                    if (this.ParentScreening.ListDescriptors[Col].IsActive() == false) continue;
                    vals[IdxCol++] = CurrentWell.ListSignatures[Col].GetValue();
                }
                vals[columnNo] = IdxWell;
                InfoForHierarchical.ListInstances.add(new DenseInstance(1.0, vals));
                IdxWell++;
            }

            InfoForHierarchical.ListInstances.setClassIndex((InfoForHierarchical.ListInstances.numAttributes() - 1));
            return InfoForHierarchical;
        }
Exemplo n.º 19
0
		/// <summary> Reads and stores header of an ARFF file.
		/// 
		/// </summary>
		/// <param name="tokenizer">the stream tokenizer
		/// </param>
		/// <exception cref="IOException">if the information is not read 
		/// successfully
		/// </exception>
		protected internal virtual void  readHeader(StreamTokenizer tokenizer)
		{
			
			System.String attributeName;
			FastVector attributeValues;
			//int i;
            Token token=null;
			// Get name of relation.
			getFirstToken(tokenizer, out token);
			//if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_EOF)
            if ((token != null)   && (token is EofToken))
			{
				errms(tokenizer, "premature end of file");
			}
			if (ARFF_RELATION.ToUpper().Equals(token.StringValue.ToUpper()))
			{
				getNextToken(tokenizer,out token);
				m_RelationName = token.StringValue;
				getLastToken(tokenizer,out token, false);
			}
			else
			{
				errms(tokenizer, "keyword " + ARFF_RELATION + " expected");
			}
			
			// Create vectors to hold information temporarily.
			m_Attributes = new FastVector();
			
			// Get attribute declarations.
			getFirstToken(tokenizer, out token);
			//if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_EOF)
            if ((token != null) && (token is EofToken))
			{
				errms(tokenizer, "premature end of file");
			}
			
			while (Attribute.ARFF_ATTRIBUTE.ToUpper().Equals(token.StringValue.ToUpper()))
			{
				
				// Get attribute name.
				getNextToken(tokenizer,out token);
				attributeName = token.StringValue;
				getNextToken(tokenizer,out token);
				
				// Check if attribute is nominal.
				//if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_WORD)
                if ((token != null) && (token is WordToken))
				{
					
					// Attribute is real, integer, or string.
                    if (token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_REAL.ToUpper()) || token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_INTEGER.ToUpper()) || token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_NUMERIC.ToUpper()))
					{
						m_Attributes.addElement(new Attribute(attributeName, numAttributes()));
						readTillEOL(tokenizer);
					}
                    else if (token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_STRING.ToUpper()))
					{
						m_Attributes.addElement(new Attribute(attributeName, (FastVector) null, numAttributes()));
						readTillEOL(tokenizer);
					}
                    else if (token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_DATE.ToUpper()))
					{
						System.String format = null;
                        tokenizer.NextToken(out token);
						//if (tokenizer.NextToken() != SupportClass.StreamTokenizerSupport.TT_EOL)
                        if ((token != null) && (!(token is EofToken)))
						{
							//if ((tokenizer.ttype != SupportClass.StreamTokenizerSupport.TT_WORD) && (tokenizer.ttype != '\'') && (tokenizer.ttype != '\"'))
                            if ((token != null) && (!(token is WordToken)) && (token.StringValue!="'") && (token.StringValue!="\"") )
							{
								errms(tokenizer, "not a valid date format");
							}
							format = token.StringValue;
							readTillEOL(tokenizer);
						}
						else
						{
							tokenizer.PushBack(token);
						}
						m_Attributes.addElement(new Attribute(attributeName, format, numAttributes()));
					}
					else
					{
						errms(tokenizer, "no valid attribute type or invalid " + "enumeration");
					}
				}
				else
				{
					
					// Attribute is nominal.
					attributeValues = new FastVector();
					tokenizer.PushBack(token);
					
					// Get values for nominal attribute.
                    tokenizer.NextToken(out token);
					if ( token.StringValue != "{")
					{
						errms(tokenizer, "{ expected at beginning of enumeration");
					}
                    tokenizer.NextToken(out token);
					while ( token.StringValue != "}")
					{
						//if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_EOL)
                        if (token is EolToken)
						{
							errms(tokenizer, "} expected at end of enumeration");
						}
						else
						{
							attributeValues.addElement(token.StringValue);
						}

                        tokenizer.NextToken(out token);
					}
					if (attributeValues.size() == 0)
					{
						errms(tokenizer, "no nominal values found");
					}
					m_Attributes.addElement(new Attribute(attributeName, attributeValues, numAttributes()));
				}
				getLastToken(tokenizer,out token, false);
				getFirstToken(tokenizer,out token);
				//if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_EOF)
                if (token is EofToken)
					errms(tokenizer, "premature end of file");
			}
			
			// Check if data part follows. We can't easily check for EOL.
			if (!ARFF_DATA.ToUpper().Equals(token.StringValue.ToUpper()))
			{
				errms(tokenizer, "keyword " + ARFF_DATA + " expected");
			}
			
			// Check if any attributes have been declared.
			if (m_Attributes.size() == 0)
			{
				errms(tokenizer, "no attributes declared");
			}
			
			// Allocate buffers in case sparse instances have to be read
			m_ValueBuffer = new double[numAttributes()];
			m_IndicesBuffer = new int[numAttributes()];
            
            
		}
Exemplo n.º 20
0
        public Instances CreateInstancesWithoutClass(cExtendedTable Input)
        {
            weka.core.FastVector atts = new FastVector();
            int columnNo = 0;

            // Descriptors loop
            for (int i = 0; i < Input.Count; i++)
            {
                //if (ParentScreening.ListDescriptors[i].IsActive() == false) continue;
                atts.addElement(new weka.core.Attribute(Input[i].Name));
                columnNo++;
            }
            // weka.core.FastVector attVals = new FastVector();
            Instances data1 = new Instances("MyRelation", atts, 0);

            for (int IdxRow = 0; IdxRow < Input[0].Count; IdxRow++)
            {
                double[] vals = new double[data1.numAttributes()];
                for (int Col = 0; Col < columnNo; Col++)
                {
                    // if (Glo .ListDescriptors[Col].IsActive() == false) continue;
                    vals[Col] = Input[Col][IdxRow];// double.Parse(dt.Rows[IdxRow][Col].ToString());
                }
                data1.add(new DenseInstance(1.0, vals));
            }

            return data1;
        }
Exemplo n.º 21
0
		/// <summary> Help function needed for stratification of set.
		/// 
		/// </summary>
		/// <param name="numFolds">the number of folds for the stratification
		/// </param>
		protected internal virtual void  stratStep(int numFolds)
		{
			
			FastVector newVec = new FastVector(m_Instances.capacity());
			int start = 0, j;
			
			// create stratified batch
			while (newVec.size() < numInstances())
			{
				j = start;
				while (j < numInstances())
				{
					newVec.addElement(instance(j));
					j = j + numFolds;
				}
				start++;
			}
			m_Instances = newVec;
		}
Exemplo n.º 22
0
		/// <summary> Split up a string containing options into an array of strings,
		/// one for each option.
		/// 
		/// </summary>
		/// <param name="optionString">the string containing the options
		/// </param>
		/// <returns> the array of options
		/// </returns>
		public static System.String[] splitOptions(System.String quotedOptionString)
		{
			
			FastVector optionsVec = new FastVector();
			System.String str = new System.Text.StringBuilder(quotedOptionString).ToString();
			int i;
			
			while (true)
			{
				
				//trimLeft 
				i = 0;
				while ((i < str.Length) && (System.Char.IsWhiteSpace(str[i])))
					i++;
				str = str.Substring(i);
				
				//stop when str is empty
				if (str.Length == 0)
					break;
				
				//if str start with a double quote
				if (str[0] == '"')
				{
					
					//find the first not anti-slached double quote
					i = 1;
					while (i < str.Length)
					{
						if (str[i] == str[0])
							break;
						if (str[i] == '\\')
						{
							i += 1;
							if (i >= str.Length)
								throw new System.Exception("String should not finish with \\");
							if (str[i] != '\\' && str[i] != '"')
								throw new System.Exception("Unknow character \\" + str[i]);
						}
						i += 1;
					}
					if (i >= str.Length)
						throw new System.Exception("Quote parse error.");
					
					//add the founded string to the option vector (without quotes)
					System.String optStr = str.Substring(1, (i) - (1));
					optStr = unbackQuoteChars(optStr);
					optionsVec.addElement(optStr);
					str = str.Substring(i + 1);
				}
				else
				{
					//find first whiteSpace
					i = 0;
					while ((i < str.Length) && (!System.Char.IsWhiteSpace(str[i])))
						i++;
					
					//add the founded string to the option vector
					System.String optStr = str.Substring(0, (i) - (0));
					optionsVec.addElement(optStr);
					str = str.Substring(i);
				}
			}
			
			//convert optionsVec to an array of String
			System.String[] options = new System.String[optionsVec.size()];
			for (i = 0; i < optionsVec.size(); i++)
			{
				options[i] = ((System.String) optionsVec.elementAt(i));
			}
			return options;
		}
Exemplo n.º 23
0
		/// <summary> Merges two sets of Instances together. The resulting set will have
		/// all the attributes of the first set plus all the attributes of the 
		/// second set. The number of instances in both sets must be the same.
		/// 
		/// </summary>
		/// <param name="first">the first set of Instances
		/// </param>
		/// <param name="second">the second set of Instances
		/// </param>
		/// <returns> the merged set of Instances
		/// </returns>
		/// <exception cref="IllegalArgumentException">if the datasets are not the same size
		/// </exception>
		public static Instances mergeInstances(Instances first, Instances second)
		{
			
			if (first.numInstances() != second.numInstances())
			{
				throw new System.ArgumentException("Instance sets must be of the same size");
			}
			
			// Create the vector of merged attributes
			FastVector newAttributes = new FastVector();
			for (int i = 0; i < first.numAttributes(); i++)
			{
				newAttributes.addElement(first.attribute(i));
			}
			for (int i = 0; i < second.numAttributes(); i++)
			{
				newAttributes.addElement(second.attribute(i));
			}
			
			// Create the set of Instances
			Instances merged = new Instances(first.relationName() + '_' + second.relationName(), newAttributes, first.numInstances());
			// Merge each instance
			for (int i = 0; i < first.numInstances(); i++)
			{
				merged.add(first.instance(i).mergeInstance(second.instance(i)));
			}
			return merged;
		}
Exemplo n.º 24
0
        private static Instances CreateInstanceOnFly(double[] a, double[] b)
        {
            FastVector atts;
            Instances data;
            double[] vals;

            // 1. set up attributes
            atts = new FastVector();
            // - numeric
            atts.addElement(new Attribute("att1"));
            atts.addElement(new Attribute("att2"));

            // 2. create Instances object
            data = new Instances("MyRelation", atts, 0);

            for (int i = 0; i < a.Length; ++i)
            {
                // 3. fill with data
                // first instance
                vals = new double[data.numAttributes()];
                // - numeric
                vals[0] = a[i];
                // - nominal
                vals[1] = b[i];
                data.add(new weka.core.DenseInstance(1.0, vals));
            }

            return data;
        }
Exemplo n.º 25
0
		/// <summary> Method for testing this class.
		/// 
		/// </summary>
		/// <param name="argv">should contain one element: the name of an ARFF file
		/// </param>
		//@ requires argv != null;
		//@ requires argv.length == 1;
		//@ requires argv[0] != null;
		public static void  test(System.String[] argv)
		{
			
			Instances instances, secondInstances, train, test, empty;
			//Instance instance;
			//UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.util.Random.Random'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
			System.Random random = new System.Random((System.Int32) 2);
			//UPGRADE_ISSUE: Class hierarchy differences between 'java.io.Reader' and 'System.IO.StreamReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
			System.IO.StreamReader reader;
			int start, num;
			//double newWeight;
			FastVector testAtts, testVals;
			int i, j;
			
			try
			{
				if (argv.Length > 1)
				{
					throw (new System.Exception("Usage: Instances [<filename>]"));
				}
				
				// Creating set of instances from scratch
				testVals = new FastVector(2);
				testVals.addElement("first_value");
				testVals.addElement("second_value");
				testAtts = new FastVector(2);
				testAtts.addElement(new Attribute("nominal_attribute", testVals));
				testAtts.addElement(new Attribute("numeric_attribute"));
				instances = new Instances("test_set", testAtts, 10);
				instances.add(new Instance(instances.numAttributes()));
				instances.add(new Instance(instances.numAttributes()));
				instances.add(new Instance(instances.numAttributes()));
				instances.ClassIndex = 0;
				System.Console.Out.WriteLine("\nSet of instances created from scratch:\n");
				//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
				System.Console.Out.WriteLine(instances);
				
				if (argv.Length == 1)
				{
					System.String filename = argv[0];
					//UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
					reader = new System.IO.StreamReader(filename, System.Text.Encoding.Default);
					
					// Read first five instances and print them
					System.Console.Out.WriteLine("\nFirst five instances from file:\n");
					instances = new Instances(reader, 1);
					instances.ClassIndex = instances.numAttributes() - 1;
					i = 0;
					while ((i < 5) && (instances.readInstance(reader)))
					{
						i++;
					}
					//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
					System.Console.Out.WriteLine(instances);
					
					// Read all the instances in the file
					//UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
					reader = new System.IO.StreamReader(filename, System.Text.Encoding.Default);
					instances = new Instances(reader);
					
					// Make the last attribute be the class 
					instances.ClassIndex = instances.numAttributes() - 1;
					
					// Print header and instances.
					System.Console.Out.WriteLine("\nDataset:\n");
					//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
					System.Console.Out.WriteLine(instances);
					System.Console.Out.WriteLine("\nClass index: " + instances.classIndex());
				}
				
				// Test basic methods based on class index.
				System.Console.Out.WriteLine("\nClass name: " + instances.classAttribute().name());
				System.Console.Out.WriteLine("\nClass index: " + instances.classIndex());
				System.Console.Out.WriteLine("\nClass is nominal: " + instances.classAttribute().Nominal);
				System.Console.Out.WriteLine("\nClass is numeric: " + instances.classAttribute().Numeric);
				System.Console.Out.WriteLine("\nClasses:\n");
				for (i = 0; i < instances.numClasses(); i++)
				{
					System.Console.Out.WriteLine(instances.classAttribute().value_Renamed(i));
				}
				System.Console.Out.WriteLine("\nClass values and labels of instances:\n");
				for (i = 0; i < instances.numInstances(); i++)
				{
					Instance inst = instances.instance(i);
					System.Console.Out.Write(inst.classValue() + "\t");
					System.Console.Out.Write(inst.toString(inst.classIndex()));
					if (instances.instance(i).classIsMissing())
					{
						System.Console.Out.WriteLine("\tis missing");
					}
					else
					{
						System.Console.Out.WriteLine();
					}
				}
				
				// Create random weights.
				System.Console.Out.WriteLine("\nCreating random weights for instances.");
				for (i = 0; i < instances.numInstances(); i++)
				{
					instances.instance(i).Weight = random.NextDouble();
				}
				
				// Print all instances and their weights (and the sum of weights).
				System.Console.Out.WriteLine("\nInstances and their weights:\n");
				System.Console.Out.WriteLine(instances.instancesAndWeights());
				System.Console.Out.Write("\nSum of weights: ");
				System.Console.Out.WriteLine(instances.sumOfWeights());
				
				// Insert an attribute
				secondInstances = new Instances(instances);
				Attribute testAtt = new Attribute("Inserted");
				secondInstances.insertAttributeAt(testAtt, 0);
				System.Console.Out.WriteLine("\nSet with inserted attribute:\n");
				//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
				System.Console.Out.WriteLine(secondInstances);
				System.Console.Out.WriteLine("\nClass name: " + secondInstances.classAttribute().name());
				
				// Delete the attribute
				secondInstances.deleteAttributeAt(0);
				System.Console.Out.WriteLine("\nSet with attribute deleted:\n");
				//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
				System.Console.Out.WriteLine(secondInstances);
				System.Console.Out.WriteLine("\nClass name: " + secondInstances.classAttribute().name());
				
				// Test if headers are equal
				System.Console.Out.WriteLine("\nHeaders equal: " + instances.equalHeaders(secondInstances) + "\n");
				
				// Print data in internal format.
				System.Console.Out.WriteLine("\nData (internal values):\n");
				for (i = 0; i < instances.numInstances(); i++)
				{
					for (j = 0; j < instances.numAttributes(); j++)
					{
						if (instances.instance(i).isMissing(j))
						{
							System.Console.Out.Write("? ");
						}
						else
						{
							System.Console.Out.Write(instances.instance(i).value_Renamed(j) + " ");
						}
					}
					System.Console.Out.WriteLine();
				}
				
				// Just print header
				System.Console.Out.WriteLine("\nEmpty dataset:\n");
				empty = new Instances(instances, 0);
				//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
				System.Console.Out.WriteLine(empty);
				System.Console.Out.WriteLine("\nClass name: " + empty.classAttribute().name());
				
				// Create copy and rename an attribute and a value (if possible)
				if (empty.classAttribute().Nominal)
				{
					Instances copy = new Instances(empty, 0);
					copy.renameAttribute(copy.classAttribute(), "new_name");
					copy.renameAttributeValue(copy.classAttribute(), copy.classAttribute().value_Renamed(0), "new_val_name");
					System.Console.Out.WriteLine("\nDataset with names changed:\n" + copy);
					System.Console.Out.WriteLine("\nOriginal dataset:\n" + empty);
				}
				
				// Create and prints subset of instances.
				start = instances.numInstances() / 4;
				num = instances.numInstances() / 2;
				System.Console.Out.Write("\nSubset of dataset: ");
				System.Console.Out.WriteLine(num + " instances from " + (start + 1) + ". instance");
				secondInstances = new Instances(instances, start, num);
				System.Console.Out.WriteLine("\nClass name: " + secondInstances.classAttribute().name());
				
				// Print all instances and their weights (and the sum of weights).
				System.Console.Out.WriteLine("\nInstances and their weights:\n");
				System.Console.Out.WriteLine(secondInstances.instancesAndWeights());
				System.Console.Out.Write("\nSum of weights: ");
				System.Console.Out.WriteLine(secondInstances.sumOfWeights());
				
				// Create and print training and test sets for 3-fold
				// cross-validation.
				System.Console.Out.WriteLine("\nTrain and test folds for 3-fold CV:");
				if (instances.classAttribute().Nominal)
				{
					instances.stratify(3);
				}
				for (j = 0; j < 3; j++)
				{
					//UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.util.Random.Random'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					train = instances.trainCV(3, j, new System.Random((System.Int32) 1));
					test = instances.testCV(3, j);
					
					// Print all instances and their weights (and the sum of weights).
					System.Console.Out.WriteLine("\nTrain: ");
					System.Console.Out.WriteLine("\nInstances and their weights:\n");
					System.Console.Out.WriteLine(train.instancesAndWeights());
					System.Console.Out.Write("\nSum of weights: ");
					System.Console.Out.WriteLine(train.sumOfWeights());
					System.Console.Out.WriteLine("\nClass name: " + train.classAttribute().name());
					System.Console.Out.WriteLine("\nTest: ");
					System.Console.Out.WriteLine("\nInstances and their weights:\n");
					System.Console.Out.WriteLine(test.instancesAndWeights());
					System.Console.Out.Write("\nSum of weights: ");
					System.Console.Out.WriteLine(test.sumOfWeights());
					System.Console.Out.WriteLine("\nClass name: " + test.classAttribute().name());
				}
				
				// Randomize instances and print them.
				System.Console.Out.WriteLine("\nRandomized dataset:");
				instances.randomize(random);
				
				// Print all instances and their weights (and the sum of weights).
				System.Console.Out.WriteLine("\nInstances and their weights:\n");
				System.Console.Out.WriteLine(instances.instancesAndWeights());
				System.Console.Out.Write("\nSum of weights: ");
				System.Console.Out.WriteLine(instances.sumOfWeights());
				
				// Sort instances according to first attribute and
				// print them.
				System.Console.Out.Write("\nInstances sorted according to first attribute:\n ");
				instances.sort(0);
				
				// Print all instances and their weights (and the sum of weights).
				System.Console.Out.WriteLine("\nInstances and their weights:\n");
				System.Console.Out.WriteLine(instances.instancesAndWeights());
				System.Console.Out.Write("\nSum of weights: ");
				System.Console.Out.WriteLine(instances.sumOfWeights());
			}
			catch (System.Exception)
			{
				//.WriteStackTrace(e, Console.Error);
			}
		}
Exemplo n.º 26
0
		/// <summary> Removes all instances with missing values for a particular
		/// attribute from the dataset.
		/// 
		/// </summary>
		/// <param name="attIndex">the attribute's index
		/// </param>
		//@ requires 0 <= attIndex && attIndex < numAttributes();
		public virtual void  deleteWithMissing(int attIndex)
		{
			
			FastVector newInstances = new FastVector(numInstances());
			
			for (int i = 0; i < numInstances(); i++)
			{
				if (!instance(i).isMissing(attIndex))
				{
					newInstances.addElement(instance(i));
				}
			}
			m_Instances = newInstances;
		}
        private void buttonStartCluster_Click(object sender, EventArgs e)
        {
            FormSingleCellClusteringInfo WindowClusteringInfo = new FormSingleCellClusteringInfo(GlobalInfo);
            if (WindowClusteringInfo.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            Instances ListInstances = GlobalInfo.CurrentScreen.CellBasedClassification.CreateInstancesWithoutClass(dt);
            if ((WindowClusteringInfo.radioButtonAutomated.Checked) && (WindowClusteringInfo.radioButtonEM.Checked))
            {
                ClusterEvaluation eval;
                Classes = new cExtendedList();
                weka.clusterers.EM EMCluster = new EM();
                if(WindowClusteringInfo.checkBoxEMAutomated.Checked)
                    EMCluster.setNumClusters(-1);
                else
                    EMCluster.setNumClusters((int)WindowClusteringInfo.numericUpDownClassNumber.Value);
                EMCluster.buildClusterer(ListInstances);
                EMCluster.getClusterModelsNumericAtts();

                eval = new ClusterEvaluation();
                eval.setClusterer(EMCluster);

                eval.evaluateClusterer(ListInstances);

                Classes.AddRange(eval.getClusterAssignments());
                NumClusters= eval.getNumClusters();
                ReDraw();
                FormForCellByCellClusteringResults WindowFormForCellByCellClusteringResults = new FormForCellByCellClusteringResults();
                WindowFormForCellByCellClusteringResults.richTextBoxResults.Clear();
                WindowFormForCellByCellClusteringResults.richTextBoxResults.AppendText(eval.clusterResultsToString());
                if (WindowFormForCellByCellClusteringResults.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            }
            else if (WindowClusteringInfo.radioButtonDescriptorBased.Checked)
            {
                Classes = new cExtendedList();
                DataTable FinalDataTable = new DataTable();
                int IdxDescForClassSelect = WindowClusteringInfo.comboBoxDescriptorForClass.SelectedIndex;
                for (int IdxWell = 0; IdxWell < GlobalInfo.ListSelectedWell.Count; IdxWell++)
                {
                    cWell TmpWell = GlobalInfo.ListSelectedWell[IdxWell];

                 //   if (IdxWell == 0)
                    if (TmpWell.ListDescriptors[IdxDescForClassSelect].GetAssociatedType().DataType == eDataType.HISTOGRAM)
                    {
                        Classes.AddRange(TmpWell.ListDescriptors[IdxDescForClassSelect].GetOriginalValues());
                    }
                    else
                    {
                        double ClasseValue = TmpWell.ListDescriptors[IdxDescForClassSelect].GetValue();

                        for (int IdxCell = 0; IdxCell < TmpWell.CellNumber; IdxCell++)
                            Classes.Add(ClasseValue);
                        //TmpWell.AddDescriptors
                    }
                }

                List<double> ListClassValues = new List<double>();
                foreach (var item in Classes.Distinct())
                {
                    ListClassValues.Add(item);
                }

                    //(List<double>)Classes.Distinct();
                NumClusters = ListClassValues.Count();
                //Classes = new cExtendedList();
                for (int IdxClust = 0; IdxClust < Classes.Count; IdxClust++)
                {
                    for (int IdxCl = 0; IdxCl < ListClassValues.Count; IdxCl++)
                    {
                        if (ListClassValues[IdxCl] == Classes[IdxClust])
                        {
                            Classes[IdxClust] = IdxCl;
                            break;
                        }
                    }
                    //Classes[IdxClust] = ListClassValues.Find(Classes[IdxClust]);
                }
                //int NumClusters =
                ReDraw();

            }

            //    ReDraw();

            //if (MessageBox.Show("Do you want perform a j48 training process ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes) return;

            weka.core.FastVector attVals = new FastVector();
            for (int i = 0; i < NumClusters; i++)
                attVals.addElement("Class__" + (i).ToString());

            ListInstances.insertAttributeAt(new weka.core.Attribute("Class__", attVals), ListInstances.numAttributes());

            for (int i = 0; i < Classes.Count; i++)
            {
                ListInstances.get(i).setValue(ListInstances.numAttributes() - 1, Classes[i]);
            }
            ListInstances.setClassIndex(ListInstances.numAttributes() - 1);

               GlobalInfo.CurrentScreen.CellBasedClassification.ClassificationModel_CellBased = new weka.classifiers.trees.J48();
               GlobalInfo.CurrentScreen.CellBasedClassification.SetJ48Tree((weka.classifiers.trees.J48)GlobalInfo.CurrentScreen.CellBasedClassification.ClassificationModel_CellBased,Classes.Count);
               GlobalInfo.CurrentScreen.CellBasedClassification.J48Model.setMinNumObj((int)GlobalInfo.OptionsWindow.numericUpDownJ48MinNumObjects.Value);

            weka.core.Instances train = new weka.core.Instances(ListInstances, 0, ListInstances.numInstances());

            GlobalInfo.CurrentScreen.CellBasedClassification.ClassificationModel_CellBased.buildClassifier(train);
            GlobalInfo.ConsoleWriteLine(GlobalInfo.CurrentScreen.CellBasedClassification.ClassificationModel_CellBased.ToString());

               GlobalInfo.CurrentScreen.CellBasedClassification.evaluation = new weka.classifiers.Evaluation(ListInstances);
               GlobalInfo.CurrentScreen.CellBasedClassification.evaluation.crossValidateModel(GlobalInfo.CurrentScreen.CellBasedClassification.ClassificationModel_CellBased, ListInstances, 2, new java.util.Random(1));

            GlobalInfo.CurrentScreen.CellBasedClassification.DisplayTree(GlobalInfo).Show();

            FormForCellbyCellClassif WindowFormForCellbyCellClassif = new FormForCellbyCellClassif();
            if (WindowFormForCellbyCellClassif.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            int DescrCount = GlobalInfo.CurrentScreen.ListDescriptors.Count;

            // first we update the descriptor
            for (int i = 0; i < ListInstances.numClasses(); i++)
                GlobalInfo.CurrentScreen.ListDescriptors.AddNew(new cDescriptorsType("Ratio_Class " + i, true, 1, GlobalInfo));

            FormForProgress ProgressWindow = new FormForProgress();
            ProgressWindow.Show();

            int IdxProgress = 0;
            int MaxProgress = 0;

            foreach (cPlate CurrentPlateToProcess in GlobalInfo.CurrentScreen.ListPlatesAvailable)
                MaxProgress += CurrentPlateToProcess.ParentScreening.Columns * CurrentPlateToProcess.ParentScreening.Rows;
            ProgressWindow.progressBar.Maximum = MaxProgress;

            attVals = new FastVector();
            for (int i = 0; i < NumClusters; i++)
                attVals.addElement(i.ToString());

            //ParallelOptions options = new ParallelOptions();
            //options.MaxDegreeOfParallelism = -1; // -1 is for unlimited. 1 is for sequential.
            //Stopwatch stopwatch = new Stopwatch();
            //stopwatch.Start();
            //////for (int PlateIdx = 0; PlateIdx < NumberOfPlates; PlateIdx++)
            //int NumberOfPlates = CompleteScreening.ListPlatesAvailable.Count;
            //Parallel.For(0, NumberOfPlates, options, (PlateIdx) =>
            //{
            //    cPlate CurrentPlateToProcess = CompleteScreening.ListPlatesActive.GetPlate((string)Parent.GlobalInfo.PlateListWindow.listBoxPlateNameToProcess.Items[PlateIdx]);

            //    for (int row = 0; row < Parent.Rows; row++)
            //        for (int col = 0; col < Parent.Columns; col++)
            //        {
            //            TempWell = CurrentPlateToProcess.GetWell(col, row, false);
            //            if (TempWell == null) continue;
            //            else
            //            {
            //                if (TempWell.GetClass() == this.ClassForClassif)
            //                    Pos.Add(TempWell.ListDescriptors[Parent.ListDescriptors.CurrentSelectedDescriptor].GetValue());
            //            }
            //        }
            //}
            //);

            foreach (cPlate CurrentPlateToProcess in GlobalInfo.CurrentScreen.ListPlatesAvailable)
            //Parallel.ForEach(GlobalInfo.CurrentScreen.ListPlatesActive, options, CurrentPlateToProcess =>
            {
                //Parallel.ForEach(CurrentPlateToProcess.ListActiveWells, options, TmpWell =>
                for(int j=0;j<CurrentPlateToProcess.ParentScreening.Rows;j++)
                    for (int k = 0; k < CurrentPlateToProcess.ParentScreening.Columns; k++)
                {
                    cWell TmpWell = CurrentPlateToProcess.GetWell(k, j, false);
                    if (TmpWell == null) continue;
                    ProgressWindow.progressBar.Value = IdxProgress++;

                    //DataTable FinalDataTable = new DataTable();
                    //TmpWell.AssociatedPlate.DBConnection = new cDBConnection(TmpWell.AssociatedPlate, TmpWell.SQLTableName);
                    //TmpWell.AssociatedPlate.DBConnection.AddWellToDataTable(TmpWell, FinalDataTable, false);
                    DataTable FinalDataTable = TmpWell.GetDescDataTable(true);
                    Instances ListInstancesTOClassify = GlobalInfo.CurrentScreen.CellBasedClassification.CreateInstancesWithoutClass(FinalDataTable);

                    ListInstancesTOClassify.insertAttributeAt(new weka.core.Attribute("Class", attVals), ListInstancesTOClassify.numAttributes());
                    ListInstancesTOClassify.setClassIndex(ListInstancesTOClassify.numAttributes() - 1);

                    cExtendedList ListClasses = new cExtendedList();

                    for (int i = 0; i < ListInstancesTOClassify.numInstances(); i++)
                    {
                        double classId = GlobalInfo.CurrentScreen.CellBasedClassification.ClassificationModel_CellBased.classifyInstance(ListInstancesTOClassify.instance(i));
                        ListClasses.Add(classId);
                    }
                    List<double[]> Histo = ListClasses.CreateHistogram(0, ListInstances.numClasses() - 1, ListInstances.numClasses() - 1);
                    List<cDescriptor> LDesc = new List<cDescriptor>();

                    for (int IdxHisto = 0; IdxHisto < Histo[1].Length; IdxHisto++)
                    {
                        double Value = (100.0 * Histo[1][IdxHisto]) / (double)ListInstancesTOClassify.numInstances();

                        cDescriptor NewDesc = new cDescriptor(Value, GlobalInfo.CurrentScreen.ListDescriptors[IdxHisto + DescrCount], GlobalInfo.CurrentScreen);
                        LDesc.Add(NewDesc);
                    }

                    TmpWell.AddDescriptors(LDesc);
                    //TmpWell.AssociatedPlate.DBConnection.DB_CloseConnection();

                }//);
            }
            ProgressWindow.Close();

            if (WindowFormForCellbyCellClassif.checkBoxKeepOriginalDesc.Checked == false)
            {

               // int DescNumToRemove = GlobalInfo.CurrentScreen.ListDescriptors.Count -
                for (int IdxDesc = 0; IdxDesc < DescrCount; IdxDesc++)
                GlobalInfo.CurrentScreen.ListDescriptors.RemoveDesc(GlobalInfo.CurrentScreen.ListDescriptors[0], GlobalInfo.CurrentScreen);

            }

            GlobalInfo.CurrentScreen.ListDescriptors.UpDateDisplay();
            GlobalInfo.CurrentScreen.UpDatePlateListWithFullAvailablePlate();

            for (int idxP = 0; idxP < GlobalInfo.CurrentScreen.ListPlatesActive.Count; idxP++)
                GlobalInfo.CurrentScreen.ListPlatesActive[idxP].UpDataMinMax();

            if (WindowFormForCellbyCellClassif.checkBoxKeepOriginalDesc.Checked == false)
                GlobalInfo.CurrentScreen.GetCurrentDisplayPlate().DisplayDistribution(0, false);

            //WindowFormForCellbyCellClassif.Close();
            //WindowClusteringInfo.Close();
        }