Ejemplo n.º 1
0
 /// <summary>
 /// Reads an arff file and gets all the headers (<see cref="ArffHeader"/>) and instances (<see cref="object[][]"/>).
 /// </summary>
 /// <param name="arff">Arff file name</param>
 private void ReadArff(string arff)
 {
     Header    = null;
     Instances = null;
     try
     {
         if (File.Exists(arff))
         {
             using (ArffReader arffReader = new ArffReader(arff))
             {
                 Header    = arffReader.ReadHeader();
                 Instances = arffReader.ReadAllInstances();
             }
         }
         else
         {
             throw new FileNotFoundException(string.Format("File \"{0}\" does not exist.", arff), arff);
         }
     }
     catch (Exception e)
     {
         if (Verbose)
         {
             throw new Exception(string.Format("Arff file could not be read: {0}\n{1}", e.Message, e.StackTrace), e);
         }
         else
         {
             throw new Exception(string.Format("Arff file could not be read: {0}", e.Message), e);
         }
     }
 }
Ejemplo n.º 2
0
        private void AssertReader(string arff, string expectedRelationName = null, ICollection expectedAttributes = null, object[][] expectedInstances = null)
        {
            ArffReader arffReader = CreateArffReader(arff);

            ArffHeader arffHeader = arffReader.ReadHeader();

            if (expectedRelationName != null)
            {
                Assert.AreEqual(expectedRelationName, arffHeader.RelationName, "Unexpected relation name.");
            }

            if (expectedAttributes != null)
            {
                CollectionAssert.AreEqual(expectedAttributes, arffHeader.Attributes, "Unexpected attributes.");
            }

            object[][] instances = arffReader.ReadAllInstances();

            if (expectedInstances != null)
            {
                CollectionAssert.AreEqual(expectedInstances, instances, new InstanceComparer(), "Unexpected instances.");
            }
        }
Ejemplo n.º 3
0
        private void WriteArffFile(string[] allfiles)
        {
            using (ArffWriter arffWriter = new ArffWriter(textBox3.Text + ".arff"))
            {
                bool       relation = true;
                object[]   inst     = null;
                object[][] insts    = new object[allfiles.Length][];
                int        n        = 0;

                foreach (string file in allfiles)
                {
                    FileInfo fi = new FileInfo(file);

                    ArffReader datafile = new ArffReader(file);

                    ArffHeader header = datafile.ReadHeader();

                    var instances = datafile.ReadAllInstances();

                    //var instance = datafile.ReadInstance();


                    object[] instance = null;

                    foreach (var ins in instances)
                    {
                        instance = ins;
                    }

                    if (header != null)
                    {
                        var attributes = header.Attributes;

                        string line;

                        StreamReader attrfile = new StreamReader("attributes.txt");

                        int i = 0;
                        int j = 0;
                        while ((line = attrfile.ReadLine()) != null)
                        {
                            foreach (var attribute in attributes)
                            {
                                if (attribute.Name == line)
                                {
                                    if (relation == true)
                                    {
                                        arffWriter.WriteRelationName(header.RelationName);
                                        relation = false;
                                    }

                                    arffWriter.WriteAttribute(new ArffAttribute(attribute.Name, attribute.Type));

                                    j++;
                                }
                                i++;
                            }
                        }

                        inst = new object[j];
                        j    = 0;
                        i    = 0;

                        while ((line = attrfile.ReadLine()) != null)
                        {
                            foreach (var attribute in attributes)
                            {
                                if (attribute.Name == line)
                                {
                                    inst.SetValue(instance.GetValue(i), j);
                                    j++;
                                }
                                i++;
                            }
                        }

                        insts.SetValue(inst, n);

                        attrfile.Close();
                    }
                    datafile.Dispose();
                    n++;
                    progress++;
                }
                foreach (var ins in insts)
                {
                    arffWriter.WriteInstance(new object[] { ins });
                }
            }
        }