From 6e657c15d5be59b0614601649894ac434cc8d88e Mon Sep 17 00:00:00 2001 From: Niklaus Johner <nij2003@med.cornell.edu> Date: Tue, 8 May 2012 11:19:57 -0400 Subject: [PATCH] Added python module trajectory_analysis.py with a function to smooth a vector or list of floats --- modules/mol/alg/pymod/CMakeLists.txt | 1 + modules/mol/alg/pymod/__init__.py | 2 +- modules/mol/alg/pymod/trajectory_analysis.py | 48 ++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 modules/mol/alg/pymod/trajectory_analysis.py diff --git a/modules/mol/alg/pymod/CMakeLists.txt b/modules/mol/alg/pymod/CMakeLists.txt index 004c475ef..0bfc29b0a 100644 --- a/modules/mol/alg/pymod/CMakeLists.txt +++ b/modules/mol/alg/pymod/CMakeLists.txt @@ -10,6 +10,7 @@ set(OST_MOL_ALG_PYMOD_MODULES "__init__.py" views.py superpose.py + trajectory_analysis.py ) if (ENABLE_IMG) diff --git a/modules/mol/alg/pymod/__init__.py b/modules/mol/alg/pymod/__init__.py index 98fe346c5..11c4c0198 100644 --- a/modules/mol/alg/pymod/__init__.py +++ b/modules/mol/alg/pymod/__init__.py @@ -1,3 +1,3 @@ from _ost_mol_alg import * from ost.mol.alg.superpose import * - +import ost.mol.alg.trajectory_analysis diff --git a/modules/mol/alg/pymod/trajectory_analysis.py b/modules/mol/alg/pymod/trajectory_analysis.py new file mode 100644 index 000000000..5e8c452b7 --- /dev/null +++ b/modules/mol/alg/pymod/trajectory_analysis.py @@ -0,0 +1,48 @@ +""" +Some functions for analyzing trajectories + +Author: Niklaus Johner +""" + +from ost import * +import os + +def smooth(vec,n): +#Function to smooth a vector or a list of floats +#for each element it takes the average over itself and the +#n elements on each side, so over (2n+1) elements + try: + vec2=vec.copy() + except: + vec2=vec[:] + for i in range(n): + v=0.0 + count=1.0 + v+=vec[i] + for j in range(n): + count+=1 + v+=vec[i+j+1] + for j in range(i): + count+=1 + v+=vec[i-(j+1)] + vec2[i]=v/float(count) + for i in range(1,n+1): + v=0.0 + count=1.0 + v+=vec[-i] + for j in range(n): + count+=1 + v+=vec[-(i+j+1)] + for j in range(i-1): + count+=1 + v+=vec[-i+j+1] + vec2[-i]=v/float(count) + for i in range(n,len(vec2)-n): + v=vec[i] + for j in range(n): + v+=vec[i+j+1] + v+=vec[i-j-1] + vec2[i]=v/float(2.*n+1.) + return vec2 + + -- GitLab