Blog

Find here all the articles written at Python for the Lab.

Get all the information directly to your inbox

Get relevant information, unsubscribe at any time.
Pynta Screenshot Dec. 14, 2018 PyNTA: Nanoparticle Tracking Analysis PyNTA is a program that aims at bridging the gap between data acquisition and analysis for experiments of nanoparticle tracking. PyNTA is my first public release of a package on PyPI, the Python repository. It is a desktop application that can be used to record images from a camera, track nanoparticles and build histograms of the distribution of sizes. It is still in beta, but the basic functionality is there. Read Article
Step by Step Guide to Building a GUI Aug. 27, 2018 Step by Step Guide to Building a GUI In this tutorial, we are going to build a Graphical User Interface (GUI) to acquire images from your webcam. We are going to use OpenCV to quickly acquire an image from your camera and PyQt5 to build the user interface. You may find a lot of tutorials online on how to use Python for different tasks, but it is very hard to find a complete guide on how to build a desktop application using Python. Read Article
What are hashable objects Aug. 27, 2018 What are Hashable Objects To understand hashable objects in Python, it is important to review what a hash table is. Following the article on Wikipedia, a hash table is a data structure that can map keys to values and that implements a hash function to compute the index to an array of buckets or slots. Heavy words, I know. Read Article
Mutable and immutable tuples Aug. 24, 2018 Mutable or Immutable Tuples Broadly speaking, Python variables belong to one of two types: mutable and immutable. We have discussed this yesterday, in the Introduction To Mutable and Immutable Data Types. The first one refers to those elements that can be changed without the need of creating a new one, while the latter refers to those that cannot be changed after instantiation. A paradigmatic example of immutable objects is tuples. However, as we are going to see in this article, tuples may seem to change. Read Article
Mutable and immutable attributes of classes Aug. 24, 2018 Mutable and Immutable Attributes of Classes We have seen how to leverage the differences between mutable and immutable objects and what happens when you use mutable types as default function arguments. However, we haven't discussed what happens when you use mutable types as default attributes of classes. Read Article
Mutable and Immutable Objects Aug. 23, 2018 Mutable and Immutable Objects People who start programming in Python quickly stumble upon the existence of lists and tuples. They are defined in a similar way, they look the same. Sometimes they are even used interchangeably. The obvious question is, therefore, why do you have two different types of elements for the same goal? The answer lays in understanding the differences between mutable and immutable data types in Python. Read Article
Storing data with SQLite Aug. 12, 2018 Storing Data with SQLite Using databases for storing data may sound much more complicated than what it actually is. In this article, we are going to cover how to use databases to store different types of data. We will quickly review how you can search for specific parameters and how to get exactly what you want. Read Article
Storing binary data and serializing Aug. 11, 2018 Storing Binary Data and Serializing Last week we have seen how to store data into plain text files that can be read by any editor or by other programs. We have also seen that if you separate your data with commas your file will follow a standard and it will be automatically compatible with other applications, such as spreadsheets. One of the main limitations of this strategy is that if the data itself contains a comma, your file will not be readable anymore. Read Article
Introduction to Storing Data in Files Aug. 10, 2018 Introduction to Storing Data in Files Storing data to reuse it later is a central part in most Python applications. Whether you are doing a measurement in the lab or developing a web application, you will need to save information in a persistent way. For example, you would like to analyze your results after you have performed an experiment. Or you would like to keep a list of e-mails of people who registered on your website. Read Article
Learning (not) to handle exceptions June 4, 2018 Learning (not) to Handle Exceptions When you develop code, it is almost impossible not to run into an error. Some problems are going to arise as soon as you start your program, for example, if you forgot to close a parenthesis, or forgot the : after an if-statement. However, errors at runtime are also very frequent and harder to deal with. In this article, you are going to learn how to handle exceptions, i.e. how to avoid program crashes when you can anticipate that an error may appear. Read Article
Documenting with Sphinx and Readthedocs May 31, 2018 Documenting with Sphinx and Readthedocs If you have ever followed a guide on how to start programming, most likely you have encounter reflections about the importance of adding comments to your code. Comments allow you to understand what the developer was thinking when programming, maybe left some traces of what could be improved. If you are developing software in places where people change often, it is crucial to leave extra information behind, to speed up the catching up of the new developers. Read Article
Implementing threads for measurements May 29, 2018 Implementing Threads for Measurements Probably you have run into the problem of wanting to update a plot while acquiring a signal, but finding that Python is busy during the acquisition. This happens, for example, when functions or methods take long to execute and you can't regain control until it is done. Python has at least two different ways of solving this issue, one is the threading module and the other is the multiprocessing module. They look the same but are fundamentally different, and therefore you need to understand their differences in order to decide when to use one or the other. Read Article
A primer on classes in Python May 22, 2018 A Primer on Classes in Python Python is an object-oriented programming (OOP) language. Object-oriented programming is a programming design that allows developers not only to define the type of data of a variable but also the operations that can act on that data. For example, a variable can be of type integer, float, string, etc. We know that we can multiply an integer to another, or divide a float by another, but that we cannot add an integer to a string. Objects allow programmers to define operations both between different objects as with themselves. For example, we can define an object person, add a birthday and have a function that returns the person's age. Read Article
How to use decorators part 2 May 18, 2018 How to use decorators Part 2 Decorators are a very useful programming pattern that allows changing the behavior of functions with little refactoring. Decorators allow developers to abstract common options from functions, but mastering their use in Python can be challenging. In this article, we are going to go in depth regarding different options when implementing decorators. The topics covered are: Read Article
How to use HDF5 files March 19, 2018 How to use HDF5 files in Python When dealing with large amounts of data, either experimental or simulated, saving it to several text files is not very efficient. Sometimes you need to access a very specific subset of data and you want to do it fast. In these situations, the HDF5 format solves both issues thanks to a very optimized underlying library. HDF5 is broadly used in scientific environments and has a great implementation in Python, designed to work with numpy out of the box. Read Article
How to use decorators to validate input March 12, 2018 How to Use Decorators to Validate Input Python is rich in resources that can shorten the time it takes to develop new programs and simplify repetitive tasks. Decorators are one of such elements but more often than not they are not considered by less experienced developers. Adding decorators to the syntactic toolbox can be of great use in different contexts, and in this article, we are going to discuss how can they help you when communicating with a device. The example code can be found in our Github repository. Read Article
Virtual environment is a must-have tool March 9, 2018 Virtual Environment is a Must-Have Tool When you start developing software, it is of utmost importance to have an isolated programming environment in which you can control precisely the packages installed. This will allow you, for example, to use experimental libraries without overwriting software that other programs use on your computer. Isolated environments allow you, for example, to update a package only within that specific environment, without altering the dependencies in every other development you are doing. Read Article
How to control a device through the network Feb. 28, 2018 How to Control a Device Through the Network In the lab, it is common to find different computers connected to specific devices. For example,when you keep older PCs which are able to communicate with very specific hardware. You may also have different computers when there are mobile instruments that you share among different users. In these situations it becomes very useful to be able to exchange information between your main computer and a secondary one. Read Article
How to Write a Driver with Lantz Feb. 23, 2018 How to Write a Driver with Lantz Lantz is a package written by several researchers who wanted to have a framework to build instrumentation on Python. It is open source and hosted on Github. Their description is very clear: Read Article
Controlling a National Instruments card with Python Feb. 21, 2018 Controlling a National Instruments Card with Python One of the most common devices in a lab is a National Instruments acquisition card, also called a DAQ. As you probably know by now, the default programming environment for such cards is Lab View, but what you may not be aware is that there are libraries for interfacing with other languages. NI provides a common driver to all of their devices called NI-DAQmx and how to use their hardware through the C programming language is well documented. Read Article

Get all the information directly to your inbox

Get relevant information, unsubscribe at any time.