Skip to content

Instantly share code, notes, and snippets.

@fabian-paul
fabian-paul / pubsub.py
Last active May 19, 2025 21:35
publish-subscribe with Starlette
import typing
import asyncio
import uvicorn
import random
import weakref
from pydantic import BaseModel
#from starlette.applications import Starlette
#from starlette.routing import Route
from fastapi import FastAPI, Query
from sse_starlette.sse import EventSourceResponse
@fabian-paul
fabian-paul / switch_network_renderer.py
Last active August 29, 2021 21:17
switch_network_renderer
class Renderer:
def __init__(self, root):
self.root = root
def svg(self):
leftmost = self.root.leftmost(0)
s = min(300 / self.root.w, 300 / self.root.h)
svg = f"""
<!DOCTYPE html>
<html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import typing
import numpy as np
import scipy.optimize
def _pcca_plus_score(partial_coarse_graining_matrix_flat: np.ndarray, eigenvectors: np.ndarray, n: int) -> float:
"""Implementation of the PCCA+ score.
Parameters
----------
@fabian-paul
fabian-paul / npy_append.py
Created September 27, 2020 20:48
create npy in append mode
dtype = np.dtype([("row", np.int32, ), ("col", np.int32, ), ("cov", np.float32, )])
a = np.zeros((3,), dtype=dtype)
for i in range(3):
a[i]["row"] = np.random.randint(2*15)
a[i]["col"] = np.random.randint(2*15)
a[i]["cov"] = np.random.randn()
with NpyStream("test.npy") as s:
s.append(a[0])
s.append(a[1])
s.append(a[2])
@fabian-paul
fabian-paul / short_widest_path.c
Created July 30, 2020 07:53
An implementation of Dijkstra's algorithm for the shortest path, the widest path, and an "interpolation" between shortest and widest path.
#include <math.h>
#include <stddef.h>
#include <signal.h>
#include <assert.h>
static volatile sig_atomic_t interrupted;
static void (*old_handler)(int);
static void signal_handler(int signo) {
interrupted = 1;
@fabian-paul
fabian-paul / induced_fit_parameter_inference.ipynb
Created March 27, 2020 18:04
parameter inference for kinetic model using HMC
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@fabian-paul
fabian-paul / reorder-atoms.py
Created January 30, 2020 17:30
Reorder small molecule atoms in PDB file to match the order in template PDB file. Uses RDKit.
import numpy as np
import rdkit.Chem
def reorder_atoms(mol_pdb_fname, template_pdb_fname, output_pdb_fname):
from rdkit.Chem import rdmolfiles
mol_to_transform = rdkit.Chem.rdmolfiles.MolFromPDBFile(mol_pdb_fname, removeHs=False)
transform_order = list(rdmolfiles.CanonicalRankAtoms(mol_to_transform))
mol_template = rdkit.Chem.rdmolfiles.MolFromPDBFile(template_pdb_fname, removeHs=False)
@fabian-paul
fabian-paul / mutate.py
Last active April 5, 2020 08:18
Generate trajectories for computational alanine scan (mass generation of mutant structures)
import numpy as np
import mdtraj
import os
import os.path
import argparse
def prepare_maps(mut_pdb='mutant.pdb', wt_pdb=None):
if wt_pdb is None:
wt_pdb = os.path.expanduser('~/system/crystal.pdb')
@fabian-paul
fabian-paul / colvars_def_reader.py
Last active October 16, 2019 16:13
Reads some NAMD colvar defintions in Python
import pyparsing as pp
from pyparsing import pyparsing_common as ppc
__all__ = ['load_colvars', 'handle_colvars']
pp.ParserElement.setDefaultWhitespaceChars(' \t')
vector = pp.Group(pp.Literal('(').suppress() +
pp.delimitedList(ppc.number, delim=',') +
pp.Literal(')').suppress())