Source code for cytocalc.csmframe

#! /usr/bin/env python3

import numpy as np
import pandas as pd

[docs] class CSMFrame: """ Class representing a single frame of a simulation. The data extracted from a frame file is stored in frame.data as a pandas object. """ def __init__(self, pandas_obj, **kwargs): self.data = pandas_obj self._npobj = self._get_pos_nparray() self.time = None # time after simulation started
[docs] def _get_pos_nparray(self): """ obtains 3D position frome parsed data. If simulation is 2D or 1D other dimensions are set to 0 """ obj = self.data if 'posX' not in obj.columns: return None if 'posY' not in obj.columns: obj['posY'] = 0 if 'posZ' not in obj.columns: obj['posZ'] = 0 return obj[['posX','posY','posZ']].to_numpy(dtype=float)
[docs] def compute_com(self): """ returns centre of mass of all filaments together (uniform mass) """ if self._npobj is None or len(self._npobj) == 0: raise ValueError("Cannot compute center of mass: no position data available") return np.mean(self._npobj, axis=0)
[docs] def compute_radius(self): """ returns a measure of "network radius" of filaments (see appendix of Belmonte et al. Mol Syst Biol. 2017 Sep 27;13(9):941. doi: 10.15252/msb.20177796 in https://pmc.ncbi.nlm.nih.gov/articles/instance/5615920/bin/MSB-13-941-s001.pdf)""" if self._npobj is None or len(self._npobj) == 0: raise ValueError("Cannot compute radius: no position data available") # get position vectors of all points radial_vectors = self._npobj - self.compute_com() radial_dist = np.linalg.norm(radial_vectors, axis=1) radius = np.sqrt(2*np.mean(radial_dist**2)) return radius