Bokeh plots with Flask and AJAX

During the weekend, I discovered Bokeh, a Python visualization library for the web. The samples looked nice, so I played around a bit, mostly following the accessible Quick Start guide. Eventually, I decided to build a small dashboard with Bokeh for an existing Flask application and wanted the plots to automatically fetch data updates using AJAX requests. This meant fiddling around a bit and reading more documentation. If you plan on doing something similar, the following write-up hopefully saves you some time and pitfalls. ...

February 11, 2018

The basics of Logarithms – with examples

Logarithms are widely used in computer science (e.g. for algorithm analyses, floating point number limitations, scaling data, feature transformations). Not coming from a mathematics background (I don’t!) logarithms can seem confusing at first. Let’s look at the very basics of logarithms to get an understanding of how they can be broken apart, some of the properties that can be utilized, and why they work. What is a Logarithm? $$\log_{b}(x)$$The logarithm is defined as the inverse operation to exponentiation. To get the logarithm of a number, we need to find out, to what exponent another number, called the base, needs to be raised to produce that first number. ...

February 6, 2018

Count elementwise matches for two NumPy arrays

Let’s say we have two integer NumPy arrays and want to count the number of elementwise matches. Here are our two arrays (NumPy imported as np): >>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = np.array([1, 2, 2, 2, 5, 6, 7, 9, 9]) To create a third (boolean) array that contains True for a match, and False otherwise, we can use the equality operator. >>> a == b array([ True, True, False, False, True, True, True, False, True]) Given that, counting the number of matches is as easy as: >>> np.count_nonzero(a==b) 6 (Another option would be to use the (slower) np.sum(a==b))

January 26, 2018

Running Flask on macOS with mod_wsgi/wsgi-express

Flask is a (micro) web development Framework for Python. It is fairly simple to get started. All you need to do is to pip install Flask into your virtualenv, give the FLASK_APP environment variable your file and run flask run (described in detail in installation and quick start). This will launch the development server and you can instantly start hacking around. When you want to use your Apache webserver, however, you need to install and configure a WSGI module. When I first wanted to do this I tried to install mod_wsgi via brew (brew install mod_wsgi from the homebrew/apache tap), but quickly ran into some (apparently common) issues with the XCode toolchain. Then I discovered that there is a much easier way of installing mod_wsgi as a Python package. ...

August 5, 2017

Pandas: Select rows that match a string

Micro tutorial: Select rows of a Pandas DataFrame that match a (partial) string. import pandas as pd #create sample data data = {'model': ['Lisa', 'Lisa 2', 'Macintosh 128K', 'Macintosh 512K'], 'launched': [1983,1984,1984,1984], 'discontinued': [1986, 1985, 1984, 1986]} df = pd.DataFrame(data, columns = ['model', 'launched', 'discontinued']) df model launched discontinued 0 Lisa 1983 1986 1 Lisa 2 1984 1985 2 Macintosh 128K 1984 1984 3 Macintosh 512K 1984 1986 We want to select all rows where the column ‘model’ starts with the string ‘Mac’. df[df['model'].str.match('Mac')] model launched discontinued 2 Macintosh 128K 1984 1984 3 Macintosh 512K 1984 1986 We can also search less strict for all rows where the column ‘model’ contains the string ‘ac’ (note the difference: contains vs. match). ...

June 26, 2017