Friday, 20 November 2015

Python importing compiled .pyc files in to the __main__ namespace

I had written a rather long piece of code in Python, which was all in one file, and all using the same namespace. I later needed to split the file and was hoping to find a method that worked by allowing me to "include" the file in a list, just like the code was in one large file.

Using import gave each module its own namespace, and so they weren't able to access the other imported modules without first importing the module again. Similarly, using any combination of from [name] import * had the same problem.

What ended up being the solution was the imp library.

The code that I have used is as follows:

import imp
imp.load_module(“__main__”, filename)

By passing "__main__", this loads the module into the workspace __main__, which is the namespace my code was using. I only used this to load .pyc files. The filename doesn't have to be a full path, and can just be the file name if located in the same folder as the script, assuming you haven't changed directories.

In hindsight, the problems were mostly from poor code structure, and each module should run as a standalone module, but due to time constraints I didn't have time to fix these issues.

Friday, 13 November 2015

Setting default search engine in Chrome to stop geographical redirection

I was somewhat frustrated when using Google Chrome when abroad, as it would always redirect to the local version of Google. The change in interface isn't the main problem, the problem is that localised versions of Google searches give localised results, which is more of a problem when you don't speak the language. There are options to change your Google account display settings, but this don't seem to always stick, and if you aren't logged in, or have multiple accounts then this setting doesn't help. Also there is the tip to use the "No Country Redirect" option, by accessing your prefered version of Google, followed by "/ncr", e.g. http://www.google.com/ncr however this only works if you set it as a bookmark or homepage, and not from the Omnibar.

Finally, I found a way to set the Omnibar to use my local Google. Go to chrome://settings/ and scroll down to the "Search" section. There, the deafault entry for Google will be as follows:

{google:baseURL}search?q=%s&{google:RLZ}... (clipped)

Here, changing the first bit of the string to the local Google isn't quite enough. I originally changed it to the following:

https://google.co.uk/search?q=%s&{google:RLZ}... (clipped)

However when closing the settings, it would revert back to {google:baseURL} which used to revert back to the local Google. By changing it to "https", this seems to have stopped it happening for now. I'm not sure why this is even a "feature", but seems to work for the moment at least. So the full search URL is as follows:

https://google.co.uk/search?q=%s&{google:RLZ}{google:originalQueryForSuggestion}{google:assistedQueryStats}{google:searchFieldtrialParameter}{google:bookmarkBarPinned}{google:searchClient}{google:sourceId}{google:instantExtendedEnabledParameter}{google:omniboxStartMarginParameter}ie={inputEncoding}

The following is a screenshot of how it looks now:



It also works to cut out all of the extra stuff, and so just make the string as follows:

https://google.co.uk/search?q=%s

If this was useful to you, I also have another post on how to correct the language reported by chrome to websites, which you can view here.

Matlab definition of functions, and function definition errors

I was trying to create a new function in Matlab, and had some issues as I am relatively unfamiliar with the format.

When writing a function, it must go in a .m file, and only the function can go in the .m file, no other code.  The typical syntax for a function

function output_variable=function_name(input_variable)

output_variable = 2* input_variable

end
The above function then should go in a file named function_name.m. Furthermore, to use the function, the code does not need to be executed first. The "current folder" must contain the .m script, and then just use the function name with variables if needed. Comments are also allowed.

If the function contains any code outside of the function, the following error will be seen:

This statement is not inside any function.
 (It follows the END that terminates the definition
 of the function "create_const_mat".)

In this case, remove any code before the first line defining the function name, or after the end line.

If the function is saved in a .m file with a different name from the function, the following error will be seen:

Error: Function definitions are not permitted in this
context.

To solve this error, rename the .m file to match the function name, or conversely rename the function name to match the .m file name.

If executing the code in the function, then there are other possible errors, which will depend on the variables in the current workspace, and whether they match the variables needed by the function. If, your function requires varaibles which are not defined, then expect the following error:

Not enough input arguments.

If they are defined incorrectly, then anything could happen.

Thursday, 30 July 2015

Reading a CSV file in Matlab with header rows and mixed text, timestamps, integers and doubles

I wanted to read in a CSV file in Matlab, but was having problems using csvread(), which only supports files with only numeric values. It seems as though the easiest way to read in a mixed CSV file, is to read in the header first, and then read in the data using textscan.

I based my code on an answer from StackExchange. For reading the header, the following code worked well for me:
fid = fopen(file, 'r');
Header = fgetl(fid);
fclose(fid);

Header = regexp(Header, '([^,]*)', 'tokens');
Header = cat(2, Header{:});
The values are then in cells, and we need to use the function cell2mat() to convert the data in to something we can work with. This works for both strings and numerical values.

Then to read the main body of the text file, I used textscan(). This requires a string defining the format. As my format string is quite long, due to having a lot of columns, I've defined the format string separately. I have used the following code to define the format:
format_a='%u%u%s%u%u%f%f%u%u';
format_b=repmat('%f',1,109); %repeat the string '%f' 109 times
format_c='%u%u%u';

format=strcat(format_a, format_b, format_c);
This generates a long string specifying the format of the file that will be read in. The input string '%u' is an unsigned integer, and the input string is for '%s' strings. I also has a time and date column in the CSV, which I read with the datenum() function later, so for now I have treated it as a string.

The reading in of the file is done with the textscan() function in the following few lines:
fid = fopen(file, 'r');D = textscan(fid, format, 'Delimiter', ',', 'HeaderLines', 1);fclose(fid);

By setting the Delimiter, this allows other options than only the comma. Also the number of header columns can be set by the number following 'HeaderLines'.

This reads all of the data in to the variable which is saved in cells. So to read in the numbers from the cells, I have used the function cell2mat(). Here is an example of how I read in one column:
index=cell2mat(D(1));
Similarly, to read in the data from multiple columns, I've used the following code:
 data=cell2mat(D(10:118));
 This works for integers and doubles. To handle the date, I have used the following line:
timestamps=datenum(D{3}, 'dd/mm/yyyy HH:MM');
This interprets the information in the strings, and creates a "datetime" object, which can be used much more easily when plotting data.

Thursday, 16 July 2015

Matlab PLSR and PCA Tutorial - Undefined Function pca

When following the Matlab tutorial for "Partial Least Squares Regression and Principal Components Regression", there is an error with the following line:
[PCALoadings,PCAScores,PCAVar] = pca(X,'Economy',false);
The error is as follows:
 Undefined function 'pca' for input arguments of type 'double'.
The pca() function is actually the same function as the princomp() function (source), however this function doesn't take the same arguments as pca().  Hence removing all other arguments accept for "X" and replacing the function with princomp() appears to give the same results.

The new line that should replace it is as follows:
[PCALoadings,PCAScores,PCAVar] = princomp(X);
Continuing with the tutorial produces identical plots for the remainder of the tutorial.



Thursday, 28 May 2015

Setting the time on a Raspberry Pi over HTTP/without NTP

One of my Raspberry Pis is installed in a location with a restrictive firewall, which means it is not possible to get the time and date using the NTP service, and I am not aware of a local NTP server.

I found an alternative which was to update the time over HTTP by using www.timeapi.org to request the time and date, in the correct format, and then use this to set the time. To set the time, I copied the code posted to the Raspberry Pi forums by ytsentas however with a modification.

The problem was that the URL originally specified the "I" flag for the format, which requests the number of hours in a 12 hour format. So changing this to "H" gives a 24 hour number. More information about the formating string can be found here (original website is no longer available).

The following batch script can be used to update the time and date.
#!/bin/bash
# Visit www.timeapi.org to find the correct url for your timezone. Then replace the url in the first line

time=$(wget http://www.timeapi.org/utc/in+one+hours?format=%25d%20%25b%20%25Y%20%25H:%25M:%25S -q -O -)
echo "Time set to:"
sudo date -s "`echo $time`"

The script needs to be saved as filename.sh, and then run the command
chmod 755 filename.sh
Which will allow the script to be executed. To execute the script from the same directory, enter:
./filename.sh

Wednesday, 22 October 2014

Python Find Serial Ports - Windows and Raspberry Pi

I started with code taken from stack overflow at the following page: http://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python with the code posted by user "Thomas" (code has since been modified). I tested the code on Windows 7 and a Raspberry Pi. The original 'unix' code did not work for me, so I found a method that did.
import sys
import glob
import serial
def ScanPorts():
    """
    Returns a generator for all available serial ports
    """
    if os.name == 'nt':
        # windows
        for i in range(256):
            try:
                s = serial.Serial(i)
                s.close()
                yield 'COM' + str(i + 1)
            except serial.SerialException:
                pass
    else:
        # unix
        ports = glob.glob('/dev/tty[A-Za-z]*')
       
        for port in ports:
            yield port
To run this on the Raspberry Pi, first the python serial package must be installed by running:
sudo apt-get install python-serial
Also the software package "Device Monitoring Studio" found online here: http://freeserialanalyzer.com/ was useful for peeking into what messages were being sent and received from another program. The free version has a 22 minute time limit.