printprev() – print Python sourcecode to console

You already know the problem: Some sections in your Python script run fast, but others (like importing dependencies) take a few seconds. If you’re only half as impacient as me, you don’t want to sit in front of a black console, wondering if your script got stuck.

Usually you would put a print(“…”) before each section:

#=================== import dependencies
print("importing dependencies")
import numpy
import matplotlib
...

But obviously this brings redundancy and is a lot of extra typing.

Instead of all these prints, we define a function printprev():

def printprev(n=1):
    import linecache, inspect
    for l in range(-n, 0):
        print(linecache.getline(__file__,inspect.currentframe().f_back.f_lineno+l).strip())

Calling this function prints your code structure, and even the code itself to the output:

#=================== import dependencies
printprev()
import numpy
import matplotlib
#=================== define variables
m = 100
n = 100000
numbers = numpy.zeros(m)
#=================== running calculations
for i in range(n):
    numbers[numpy.random.randint(0,m)]+=1
printprev(3)
#=================== reporting the results
printprev()
print(numbers)
Comments and code beautifully printed to the console

Comments and code beautifully printed to the console

That’s it. Let me know if you have some interesting ideas what to do with this!
cheers

RCSwitch for Windows 10 IoT

RCSwitch is a library for controlling remote power sockets from Arduino. The original source code by Suat Özgür can be found on GitHub.

In combination with the MX-FS-03V sender MX-05V receiver, I wanted to do the same thing on Windows 10 IoT on my Raspberry Pi. To use RCSwitch in a Windows Universal app, I ported the library into a C++ Windows Runtime Component.

The RCSwitch port to the Windows Universal Platform is now available on NuGet.

To use it in your IoT project, just install the NuGet package and then copy these code samples:

Create an instance of the RCSwitchIO class:

// connect sender to GPIO6 and receiver to GPIO5
RCSwitchIO rcSwitch = new RCSwitchIO(6, 5);

Turning remote power sockets on/off:

// turn device 11011 10000 ON
rcSwitch.Switch("11011", "10000", true);
// ...
// turn device 11011 10000 OFF
rcSwitch.Switch("11011", "10000", false);

In good .NET fashion, you can also subscribe to an event to listen for incoming signals:

//attach the event handler for receiving signals
rcSwitch.OnSignalReceived += RcSwitch_OnSignalReceived;

private void RcSwitch_OnSignalReceived(object sender, Signal signal)
{
   Debug.WriteLine($"received: {signal.Code} via protocol {signal.Protocol} with bitlength {signal.BitLength}");
}

However I found that receiving does not work very reliably. Initially I was able to sometimes receive signals, but a few weeks later (with a different remote) it didn’t work at all. Any help in debugging the receiveProtocol methods that handle the interpretation of interrupt timings is appreciated. You can find all source code including a test and example project on GitHub.

cheers!

Learn and Predict the Gender of German Nouns

The German language is know to be relatively complicated and especially the gender causes lots of confusion. While English has only one article (the), three different articles are used in German:

  • der (male)
  • die (female)
  • das (neuter)

While rules to determine the gender of a noun exist, almost no German native speaker can name them. We can now solve this problem (determine the gender without memorizing the rules) using some simple machine learning with the Accord framework.

Let’s quickly name the steps that will follow:

  1. find and extract a dataset of noun-gender associations
  2. split into training, test and validation dataset
  3. extract features into something the algorithm can use
  4. train a Naive Bayes
  5. test the model with the test dataset

After quite a while of searching, I found this machine readable and CC-BY-SA 4.0 licensed XML file from Daniel Naber.

In our Universal Windows App we can then load all nouns into a List:

Words = await Parser.LoadNounsAsync("morphy-export-20110722.xml", int.MaxValue, MinLength); // MinLength = 4 letters

The next step is to split the dataset into training, test and validation sets. For this purpose I wrote a SplitRandom method that randomly selects elements from an IEnumerable and returns a List<T>[] with a specified size.

// randomly split the dataset into three almost equally sized sets
var splits = Words.SplitRandom(3);
trainingDataset = splits[0];
testDataset = splits[1];
validationDataset = splits[2];

You can look up the definitions of SplitRandom<T>() and LadeSubstantive() in the source code of the sample application at the bottom of this post.

To train the Naive Bayes, we have to select features and represent them as number so the algorithm can use them. Our assumption is that the gender of German nouns can be determined from the suffix, so for simplicity we start with the last four letters. In the sample application, I represent the letters as enums so they can be casted to int or double as required. So each instance of the Wort class can now have a Features property of type int[]:

public int[] Features
{
    get
    {
        // The Naive Bayes expects the class labels to range from 0 to k
        return new int[]
        {
            (int)GetLetter(-1),
            (int)GetLetter(-2),
            (int)GetLetter(-3),
            (int)GetLetter(-4)
        };
    }
}

Before we continue with building the model, let’s add the required NuGet packages from the Accord-framework. As we are doing this for a Universal Windows App, we can not get the original Accord-packages, but someone already published portable packages. In the NuGet package manager, you can find them by their names:

  • portable.accord.machinelearning
  • portable.accord.statistics

From our training dataset we can now build the feature- and label-arrays using LINQ:

int[][] inputs = trainingDataset.Select(w => w.Features).ToArray<int[]>();
int[] outputs = trainingDataset.Select(w => w.Label).ToArray();

The next step is to build and train a Naive Bayes model:

NaiveBayes bayes = new NaiveBayes(Wort.LabelClasses, inputs[0].Select(i => Extensions.LetterValues.Length).ToArray());
double error = bayes.Estimate(inputs, outputs);

Testing the model can now be done using the training dataset:

// Classify the test dataset using the model
int[][] testFeatures = testDataset.Select(w => w.Features).ToArray();
int[] testLabels = testDataset.Select(w => w.Label).ToArray();
// predict the labels
int[] testPredictions = testFeatures.Apply(bayes.Compute);

By counting the zeores after subtracting the correct labels from the predicted labels, we get the number of correct predictions:

double correctPredictions = testPredictions.Subtract(testLabels).Count(x => (x == 0)); // using Accord.Math for .Subtract() and .Count()
            
System.Diagnostics.Debug.WriteLine($"{correctPredictions / testPredictions.Length * 100} % success rate.");

When you run this, it generates a model with 70-75 % success rate.

The Naive Bayes was already trained and performed with a precision of 73.3 %

The Naive Bayes was already trained and performed with a precision of 73.3 %

In the sample application you can see how a Decision Tree performs compared to the Naive Bayes (hint: better).

You can grab the whole thing from the MSDN code gallery: https://code.msdn.microsoft.com/Predicting-Noun-Genders-ef904a12
Have fun!

iGEM Aachen 2014

You might have wondered why it got a bit more quiet on my blog. Now here’s why: Because of the iGEM competition 2014.

iGEM from Above 2014

The iGEM competition is an international competition in synthetic biology that debuted at MIT in 2004. Since then it has grown to more than 230 participating teams from all over the world. Over the last ten years it has significantly shaped the international synthetic biology community.

In 2013 a few friends and me heard of the competition and founded the first team from RWTH Aachen. Up until March 2014 our team grew to 15 students of Bachelor and Master programs in Biology, Biotechnology, Biomedical Engineering, Computational Engineering Science and Computer science.

After almost a year of hard work, we finished our project “Cellock Holmes – A Case of Identity” and flew to the Giant Jamboree (Finals) in Boston, MS,

We had fulfilled all criteria for the gold medal and won the “Measurement” track (category) that we participated in. Additionally we won the “Best Supporting Software” award in the overgraduate section and were awarded with a very rare “Safety Commendation” for our work on biosafety issues.

iGEM Team Aachen 2014

A complete documentation of our project is available at 2014.igem.org/Team:Aachen

In the coming weeks I hope to find the time and blog about several components of our project that I worked on.

 

Replay Practice

Are you playing an instrument? Yes? Then you probably know what it is like when you’re practicing the same few notes all over again! Sometimes playing it slower can help too.

With Replay Practice you can do exactly that. Choose a song, move the markers and loop a part of the song at a speed of your choice.

Replay Practice EN2

Replay Practice is available for Windows Phone 8.1 and Windows 8 !Windows Store Download Badge

 

Arduino as a MIDI/Bluetooth Relay for Windows 8.1 Apps

In my last post I described how a Bluetooth connection between Arduino and a Windows 8.1 device can be established. The next step for me was to connect the Arduino to my electronic drum kit which has both, a MIDI-IN and a MIDI-OUT jack, but any other electronical instrument will do as well. The wiring diagram for an Arduino Uno R3 with MIDI-IN/OUT and the JY-MCU Bluetooth module is shown in Fig.1. NOTE: Occasionally there are MIDI shields available for Arduino, so you might not have to build it on your own.

Fig.1: You'll need a few resistors, a diod, an optocoupler and preferably one or two DIN-jacks

Fig.1: You’ll need a few resistors, a diod, an optocoupler and preferably one or two DIN-jacks

The Arduino code to relay MIDI>Bluetooth and Bluetooth>MIDI is actually quite simple.

//======================================================authorship
//by Michael Osthege (2013)
//======================================================includes
#include "SoftwareSerial.h"
//======================================================constants
const int TX_BT = 10;
const int RX_BT = 11;
const int MIDI_TX = 1;
const int MIDI_RX = 0;
//======================================================bluetooth setup
SoftwareSerial btSerial(TX_BT, RX_BT);
//======================================================initialization
void setup()
{
    Serial.begin(31250);
    btSerial.begin(9600);
    Serial.println("Bluetooth initialized");
}
//======================================================do work
void loop()
{
    ReadMIDI();//listen on the MIDI-IN
    ReadBluetooth();//listen for Bluetooth
}
void ReadMIDI()
{
    if (Serial.available() > 0)//there's something in the buffer
    {
        char buffer[3];
        Serial.readBytes(buffer, 3);//receive it
        btSerial.write(buffer[0]);//relay it
        btSerial.write(buffer[1]);
        btSerial.write(buffer[2]);
    }
}
void ReadBluetooth()
{
    if (btSerial.available() > 0)//there's something in the buffer
    {
        char buffer[3];
        btSerial.readBytes(buffer, 3);//receive it
        Serial.write(buffer[0]);//relay it
        Serial.write(buffer[1]);
        Serial.write(buffer[2]);
    }
}

As I understand it, the MIDI protocol communicates with commands of three bytes. Therefore I decided to relay all incoming serial messages in chunks of three bytes. To test the relay, I modified the BluetoothConnectionManager of my previous example to send/receive chunks of 3 bytes as MIDI commands too. Incoming commands are printed into lines of text and simple sounds can be sent as well. You can try it out yourself in this sample application: http://code.msdn.microsoft.com/Arduino-as-a-MIDIBluetooth-1c38384a Have a nice weekend =)

Bluetooth communication between Arduino and Windows 8.1

Introduction

Recently, after being inspired by this video of Arduino Bluetooth communication with Windows Phone 8 by Marcos Pereira, I got myself some new devices to play with:

  • Arduino Uno R3 in a starter kit (link)
  • JY-MCU Bluetooth module (link)
  • and a few extra cables

The thing is: I don’t have a Windows Phone 8 yet and the Windows Phone 7.8 APIs do not support this kind of Bluetooth communication.

But: I have a Surface RT and with Windows 8.1 the RFCOMM API can be used to establish a serial link to the Arduino.

Arduino and Visual Studio

It happens that my developing skills are quite limited to C# and when I had to develop some Kinect software with Processing in early 2012, I almost freaked out. Arduino code is written in C++  and the standard Arduino IDE is derived from the Processing IDE. Fortunately there’s the Visual Studio extension VisualMicro which brings a very intuitive Arduino support for VS2010-VS2013. (not available for Express versions)

You can get VisualMicro for free at http://visualmicro.codeplex.com/. They also offer a pro-version (>$20) with support for Breakpoint-Debugging.

The Arduino IDE should be installed as well and you have to tell the VisualMicro extension where to find it.

Setting up the Arduino board

For the setup I’ll show here, I set up two LEDs, a potentiometer and of course the Bluetooth module (Fig.1).

Arduino UNO R3 with 2 LEDs, a potentiometer and a Bluetooth module

Fig.1: Arduino UNO R3 with 2 LEDs, a potentiometer and a Bluetooth module


Preparing the Code

The Arduino has to be loaded with some code to send and receive Bluetooth messages. I created a Visual Studio solution with a blank Windows 8.1 app and a new Arduino sketch and added a new header file “SoftwareSerial.h” (Fig.2). The code of SoftwareSerial.h can be found here.

A blank VS 2013 solution was created to which a blank Windows Store 8.1 application and an Arduino sketch were added.

Fig.2: A blank VS 2013 solution was created to which a blank Windows Store 8.1 application and an Arduino sketch were added.

The actual Arduino code can be studied in the sample application: http://code.msdn.microsoft.com/Bluetooth-communication-7130c260

For the Windows 8.1 app we build a class, BluetoothConnectionManager to simplify enumerating, connecting, send & receive and state management. The code of the BluetoothConnectionManager and the actual application can also be studied in the sample application.

For the Windows 8.1 app it’s very important to declare the right Bluetooth capabilites. The following snippet should be included in Package.appxmanifest:

<Capabilities>
    <m2:DeviceCapability Name="bluetooth.rfcomm">
      <m2:Device Id="any">
        <m2:Function Type="name:serialPort" />
      </m2:Device>
    </m2:DeviceCapability>
  </Capabilities>

As soon as you have everything set up, the Arduino code can be deployed via DebugStart new instance and the Windows 8.1 app can be launched.

In the sample application I implemented broadcast of the potentiometer value and switching the LEDs on/off. Additionally the red LED automatically turns on/off depending on the analog value.

http://www.youtube.com/watch?v=lhj0aShdcz8

One problem that this implementation brings, which I did not yet solve, is that simultaneous sending of a message by either party causes the other one to crash or get an exception. But due to my lack of experience, I do not know how to lock the streams to prevent this from happening..

Anyways, have fun =)

A Custom (Async) MessageBox for WPF and Windows Phone

The System.Windows.MessageBox provided by .NET is limited to a very small set of MessageBoxButtons (Yes/No/OK/Cancel) which do not allow for custom Button texts. The System.Windows.Forms.MessageBox also blocks the UI thread, which can be an annoying disadvantage as well.

The MessageBox on Windows Phone has the same disadvantages.

Custom buttons in a MessageBoxe simplify the decision for the user, because they can describe the results of the action:

Use your own buttons and await the result!

Use your own buttons and await the result!

This MessageBox does not block the UI thread!

This MessageBox does not block the UI thread!

However you need to do this with a custom control, so for WPF as well as Windows Phone I designed a simple method for asynchronously showing a MessageBox with custom buttons. (The WPF implementation based on this control by Evan Wondrasek)

So let’s bring some sample code.In WPF you can use multiple custom buttons:

asynchronously show a CustomMessageBox - int result is the number of the selected button, or the default

asynchronously show a CustomMessageBox – int result is the number of the selected button, or the default

And on Windows Phone you’re limited to 2 buttons:

asynchronously show a CustomMessageBox - int result is the number of the selected button, or the default

asynchronously show a CustomMessageBox – int result is the number of the selected button, or the default

To use CustomMessageBox, you need to get TCD.Controls from NuGet:

Install the TCD.Controls package from NuGet

Install the TCD.Controls package from NuGet

And on Windows Phone 7.x you also need async support, which is included in the Microsoft.Bcl.Async package:

Microsoft.Bcl.Async is required for async/await on WP7

Microsoft.Bcl.Async is required for async/await on WP7

Just like the other methods from TCD.Controls, CustomMessageBox also features IntelliSense descriptions (or I’ll add them very soon).

Please feel free to provide feedback and/or report bugs 😉

In-App Donations for Windows 8 Apps

Recently I wanted to offer my customers the opportunity to donate some money. No extra features, just a clean donation option.

In-app purchases seemed to be the best option. The idea is to have a list of products which have a price and contain “donate” in their product ID (Fig. 1).

Fig. 1: in-app products contain “donate” in their product name.

Fig. 1: in-app products contain “donate” in their product name.

In the app the list of products can be retrieved and shown when a donate button is tapped. If donations have been given can be checked by enumerating the existing licenses and looking for the “donate” term (Fig. 2).

Fig. 2: The buttons first indicates its activity and then shows the available in-app donation products.

Fig. 2: The buttons first indicates its activity and then shows the available in-app donation products.

Reasonably simple right? Well the principle is simple, but in-app purchases are a bit tricky to develop, debug and get through certification. (It took me 7 submissions to get it right..)

Those are the things that can result in certification failure:

  • no internet connection -> exception
  • store does not respond properly -> exception
  • error message looks to much like a error message -> fails requirement 3.2

The CurrentApp static class offers the methods to retrieve LicenseInformation and to execute purchases, but it won’t function until the app is actually released. Therefore you have to use the CurrentAppSimulator – conditionally depending on the build mode. For example:

#if DEBUG
return CurrentAppSimulator.LicenseInformation;
#else
return CurrentApp.LicenseInformation;
#endif

Even in the certification environment, the store does not know your app, which results in an exception!

As you usually need to handle this, its common practice to show a MessageDialog informing the user what has gone wrong. The dialog should not look like a error message, but rather like a information, otherwise the tester might decide to let you fail said requirement 3.2!

As I want to offer in-app donations in multiple of my apps, I created a UserControl for it – the AppBar button you saw above.

I set up a code sample at MSDN, so you can take a look at the code there =)