Tuesday, November 08, 2011
Here you will find a sample java program to generate sparse ARFF file. The following italic text text has been taken from: http://www.cs.waikato.ac.nz/ml/weka/arff.html
Sparse ARFF files are very similar to ARFF files, but data with value 0 are not be explicitly represented.Sparse ARFF files have the same header (i.e @relation and @attribute tags) but the data section is different. Instead of representing each value in order, like this:
@data
0, X, 0, Y, "class A"
0, 0, W, 0, "class B"
the non-zero attributes are explicitly identified by attribute number and their value stated, like this:
@data
{1 X, 3 Y, 4 "class A"}
{2 W, 4 "class B"}
Each instance is surrounded by curly braces, and the format for each entry is: <index> <space> <value> where index is the attribute index (starting from 0).
The following Java code generates a sparse Instances object and outputs it to stdout and a physical file as ARFF file.
1: import java.io.File;
2: import weka.core.Attribute;
3: import weka.core.FastVector;
4: import weka.core.Instance;
5: import weka.core.Instances;
6: import weka.core.converters.ArffSaver;
7: import weka.filters.Filter;
8: import weka.filters.unsupervised.instance.NonSparseToSparse;
9: /**
10: * Generates a sparse ARFF file.
11: *
12: * @author Razan
13: */
14: public class AttTest {
15: public static void main(String[] args) throws Exception
16: {
17: FastVector attributes;
18: Instances dataSet;
19: double[] values;
20: attributes = new FastVector();
21:
22: attributes.addElement(new Attribute("att1"));
23: attributes.addElement(new Attribute("att2"));
24: attributes.addElement(new Attribute("att3"));
25: attributes.addElement(new Attribute("att4"));
26:
27: dataSet = new Instances("ESDN", attributes, 0);
28:
29: values = new double[dataSet.numAttributes()];
30: values[0] = 3;
31: values[1] =7;
32: values[3] = 1;
33: dataSet.add(new Instance(1.0, values));
34:
35: values = new double[dataSet.numAttributes()];
36: values[2] = 2;
37: values[3] = 8;
38: dataSet.add(new Instance(1.0, values));
39:
40: NonSparseToSparse nonSparseToSparseInstance = new NonSparseToSparse();
41: nonSparseToSparseInstance.setInputFormat(dataSet);
42: Instances sparseDataset = Filter.useFilter(dataSet, nonSparseToSparseInstance);
43:
44: System.out.println(sparseDataset);
45:
46: ArffSaver arffSaverInstance = new ArffSaver();
47: arffSaverInstance.setInstances(sparseDataset);
48: arffSaverInstance.setFile(new File("ESDN.arff"));
49: arffSaverInstance.writeBatch();
50: }
51: }
The output of the sample program is in the following:

Hope this will save some of your time.
Technorati Tags:
WEKA,
RapidMiner,
JAVA
Friday, September 18, 2009
If we use proxy class to construct communication channel between server and client , then when we will change data contract, Service contract, callback contract we need to regenerate the proxy for client. If we use channel factory to construct the channel we do not need to change channel factory code when there is a change in data contract, service contract and callback contract. To use Channel Factory to construct channel between server and client , all the contracts must be kept in a separate DLL that must be shared by server and client. But if you do not know, who are your service clients or any third party can consume your service, then by default we have to depend on proxy generation.
IPv4 address has two basic parts: the network part and the host part. As we know, if network potions of two IPs are same, they are in the same network. By performing and operation between subnet mask and IP address, we can get the network portion of an IP. By this way, we have found the network portions of two IPs. Then just check whether the network portions are equal or not. For this the following code is written:
1: private static bool CheckWhetherInSameNetwork(string firstIP, string subNet, string secondIP )
2: {
3: uint subnetmaskInInt = ConvertIPToUint(subNet);
4: uint firstIPInInt = ConvertIPToUint(firstIP);
5: uint secondIPInInt = ConvertIPToUint(secondIP);
6: uint networkPortionofFirstIP = firstIPInInt & subnetmaskInInt;
7: uint networkPortionofSecondIP = secondIPInInt & subnetmaskInInt;
8: if (networkPortionofFirstIP == networkPortionofSecondIP)
9: return true;
10: else
11: return false;
12: }
13:
14: static public uint ConvertIPToUint(string ipAddress)
15: {
16: System.Net.IPAddress iPAddress = System.Net.IPAddress.Parse(ipAddress);
17: byte[] byteIP = iPAddress.GetAddressBytes();
18: uint ipInUint = (uint)byteIP[3] << 24;
19: ipInUint += (uint)byteIP[2] << 16;
20: ipInUint += (uint)byteIP[1] << 8;
21: ipInUint += (uint)byteIP[0];
22: return ipInUint;
23: }
Hope this will save some of your time.
We can make a deep copy of a WPF object using XamlWriter and XamlReader. Here the XamlWriter.Save is used to serialize the contents of a WPF object into xaml string. XamlReader.Load is used to parse XAML string into a WPF object. To make deep copy of an wpf UIelement , you can use the following method.
1: public UIElement DeepCopy(UIElement element)
2: {
3: string shapestring = XamlWriter.Save(element);
4: StringReader stringReader = new StringReader(shapestring);
5: XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
6: UIElement DeepCopyobject = (UIElement)XamlReader.Load(xmlTextReader);
7: return DeepCopyobject;
8: }
If you would like to make deep copy of WPF objects without XamlWriter, you have to use reflection .For that you have to navigate recursively inside the type to get all Dependency Properties and Dependency Objects as well as plain .net properties.
According to Ben Constable, Remora Pattern allows you to attach a chunk of logic to any existing element that you have. This pattern can be implemented using an Attached Dependency Property in WPF. Here an Attached Dependency Property is attached with an object. When the object is initiated, it goes to set the value of the Attached Dependency Property, which results in calling an Attached Dependency Property Change event. In the event handler, you can add your intended functionality, which is the additional functionality of the object.
You can get more info about this pattern in Ben Constable's blog. You can take a look at it here. I have written an article using Remora Pattern to implement a Touch Screen Keyboard. You can take a look at it here (no advertisement intended).
This scheduler is not a time-based scheduler. It schedules the user tasks according to scheduling policy; scheduling policy is First Come First Serve. It performs the following tasks:
Here a specific number of threads is used to process the clients request. When Scheduler starts, A pool of threads is created to process pending requests of client, which may be created by software to meet certain requirements , sent by some network client , polled from database . In thread pooling, after a thread is created, it is placed in the pool and it is used over again so that a new thread does not have to be created. If all the threads are being used, the requests remain in pending. As soon as one of the threads becomes free, it handles the next client request in FIFO manner.
1: class Scheduler
2: {
3:
4: private ManualResetEvent manualResetEvent = new ManualResetEvent(false);
5:
6: private Queue _ClientRequests;
7: public Queue ClientRequests
8: {
9: get { return _ClientRequests; }
10: set { _ClientRequests = value; }
11: }
12:
13: private int _NumberofThreads = 3;
14: public int NumberofThreads
15: {
16: get { return _NumberofThreads; }
17: set { _NumberofThreads = value; }
18: }
19:
20: private bool IsSchedulerStarted = false;
21: private Thread[] StaticThreadPool = null;
22:
23: public Scheduler()
24: {
25: _ClientRequests = Queue.Synchronized(new Queue());
26: StaticThreadPool = new Thread[_NumberofThreads];
27:
28: for (int i = 0; i < StaticThreadPool.Length; i++)
29: {
30: StaticThreadPool[i] = new Thread(new ThreadStart(this.Work));
31: StaticThreadPool[i].IsBackground = true;
32: }
33: }
34:
35: private void Work()
36: {
37: while (true)
38: {
39: ICommand request = null;
40: lock (_ClientRequests.SyncRoot)
41: {
42: if (_ClientRequests.Count > 0)
43: {
44: request = (ICommand)_ClientRequests.Dequeue();
45: }
46: }
47: if (request != null)
48: request.Execute();
49: else
50: manualResetEvent.WaitOne();
51: if (!IsSchedulerStarted) break;
52: }
53: }
54:
55: public void Enqueue(ICommand request)
56: {
57: lock (_ClientRequests.SyncRoot)
58: {
59: _ClientRequests.Enqueue(request);
60: }
61: manualResetEvent.Set();
62: }
63:
64: public void StartScheduler()
65: {
66: foreach (Thread item in StaticThreadPool)
67: {
68: item.Start();
69: }
70: IsSchedulerStarted = true;
71: }
72:
73: public void StopScheduler()
74: {
75: IsSchedulerStarted = false;
76: foreach (Thread item in StaticThreadPool)
77: {
78: if (item != null)
79: {
80: if (item.IsAlive)
81: {
82: item.Join();
83: }
84: }
85: }
86: }
87: }
According to MSDN, Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads. To guarantee the thread safety of the queue, all operations is done through the synchronized (thread safe) wrapper of queue.
To improve performance, when we do not have any messages to process in the queue, we simply block the thread until a new message appears. According to MSDN, WaitOne( ) method of ManualResetEvent blocks the current thread until the current instance receives a signal. So in each working thread, we call manualResetEvent.WaitOne to block each working thread when there is no request to process.
1: interface ICommand
2: {
3: void Execute();
4: }
5:
6: class ClientRequestType1 : ICommand
7: {
8: // data structure for requst type 1
9:
10: #region ICommand Members
11:
12: public void Execute()
13: {
14: //write code to process a requet type 1
15: }
16:
17: #endregion
18: }
19:
20:
21:
22: class ClientRequestType2 : ICommand
23: {
24: // data structure for requst type 1
25:
26: #region ICommand Members
27:
28: public void Execute()
29: {
30: //write code to process a requet type 1
31: }
32:
33: #endregion
34: }
To process each type of user request, there is a method with some parameters. Here we represent these methods using ICommand type to encapsulate each method with its parameters into an object. To process each type of user request, we ask the corresponding Icommand object to execute and it knows what to do.