Posts

Condition Stable Diffusion images with ControlNet

21 September 2023

This is another aspect of the Stable Diffusion AI art library, covered previously. With ControlNet, users can ‘condition’ the generation of an image with a spatial context such as a segmentation map or a scribble. That is, you can weight the model to produce images that are constrained to the form of another. We can turn a cartoon drawing into a realistic photo for example, or place another face in a portrait. We can still provide a prompt to guide...

Read more...

AI art with Stable Diffusion in Python

19 September 2023

Stable Diffusion is a text-to-image model trained on 512x512 images from a subset of the LAION-5B dataset. You can implement this model on your own computer using the Python Diffusers library, which is a library for state-of-the-art pre-trained diffusion models for generating images, audio, and 3D structures. It is hosted by huggingface. The workings of the library are beyond the scope of this post, there are guides here if interested. To make this work fast enough you should have a...

Read more...

SNP clustering and type naming of pathogens from WGS

15 September 2023

Whole genome sequencing (WGS) has emerged as a powerful tool in the field of public health, specifically in the context of pathogen strain typing. In order to categorize strains they are traditionally ‘typed’ using molecular methods like spoligotyping, MIRU-VNTR, RFLP or MLST. With WGS this can be accomplished at a superior resolution. WGS provides a detailed blueprint of virtually the entire genome. This allows every SNP difference between samples to be counted, within the error of instrument measurement. This information...

Read more...

A phylogenetic tree viewer with PyQt and ToyTree

18 June 2023

Toyplot is a Python toolkit for plotting using HTML, SVG, and Javascript to create embeddable graphs. Toytree is based on toyplot and provides the ability to plot tree structures. It is a fairly minimalist package but can create useful tree plots that are better than some alternatives in Python. There are relatively few packages in Python that support rendering of phylogenetic trees that are much use. I have tried to use ete3 but found it frustrating and overly complex. Biopython...

Read more...

Simulate land parcels and fragmentation with geopandas

22 March 2023

Land fragmentation is the division of land holdings into discrete parcels that may be dispersed over a wide area. It is common in an agricultural context where a single farmer can own multiple parcels of contiguous (separated by roads, hedgerows or fencing) or non-contiguous land. For cattle farming this means animals could spend their time in different portions of land at different times of the year. This presents a challenge for something like contact networks where determining connections between farms...

Read more...

An individual based model of farm pathogen spread with Python/Mesa

19 March 2023

Computer-based disease spread models are frequently used in veterinary science to simulate disease spread. So called Agent based (ABM) or individual based models simulate each ‘agent’ in a network of contacts. Each agent could be an animal or other entity like a herd with certain behaviours. For example the agents can randomly contact each other and transmit disease. Running such a simulation with many heterogeneous entities over many steps produces emergent complex behaviour that is meant to approximate real life...

Read more...

Make a contact network from a geopandas geodataframe

15 March 2023

Similar to the previous post the example here shows how to get a contact network from a random geodataframe of points. Unlike the delaunay graph this uses a distance threshold to determing if nodes should be connected. The function returns the position of nodes taken from the original geodataframe. The attrs parameter is if we want to assign any of the columns in the dataframe to the node attributes. Code

Read more...

Make networkx Delaunay graphs from geopandas dataframes

23 January 2023

Geopandas Geodataframes store spatial data such as points and polygons. This post adapts code a from some examples online to show how to convert spatial points into a Delaunay graph. This is a undirected graph with edges between adjacent points only. This may be useful for building a spatial contact network of neighbouring points and doing further processing. Here we convert into networkx graphs. First we make a GeoDataFrame from some random points: def random_points(n): import random # Create an...

Read more...

Using Molecular Nodes in Blender to visualise proteins

14 January 2023

Blender is a free program capable of advanced 3D modelling, animation and rendering. It is used to create photo realistic images for many applications such as animated films or architecture. I used to be interested in using Blender to create images of proteins and other molecules. This was a rather convoluted process. It seems things have moved on since I last looked at this area. There is now a very useful addon called Molecular Nodes that can import and render...

Read more...

Scrape dynamic tables in Python with Playwright

29 December 2022

Sometimes it’s necessary to scrape a website or some pages that contain elements generated dynamically, often via javascript. This means you can’t always get all the html content in the page directly from the url. You may have to simulate interaction in the browser like clicking a button on a form and then waiting to get the content. This can be done with the Playwright library which was created to support automated testing of websites. Note that some websites won’t...

Read more...

All posts