'Correction of time series data with conditional statements?

I need to correct a time series data which has jumps in between. Please look at the attached trajectory plot. The trajectory is around 550 (y-axis) and then suddenly it drops to very small value, let's 1. Now I want to put a conditional statement if the difference between the previous number and the current number is lager than 200, the current number is subtracted by 614. That will be 614-1 = 613 and the trajectory will look continuous. Does anyone has any idea how it can be done?

enter image description here

Here is complete code(mve) listing. I want to put the condition on this line h5md.append_dataset(position['value'], coms). The data can be obtained from here. Basically this code read the .xtc file and store the time series data into a .h5 format.

import argparse
from h5md import h5md

# define and parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('input', metavar='INPUT', help='XTC input file')
parser.add_argument('--output', metavar='FILENAME', help='H5MD output file')
parser.add_argument('--last-frame', type=int, default=-1, help='limit processing up to this frame')
parser.add_argument('-v', '--verbose', action='store_true', help='be verbose')
args = parser.parse_args()

import MDAnalysis as mda
import numpy as np

XTC = args.input
TPR = os.path.splitext(XTC)[0] + '.tpr'
if not os.path.isfile(TPR):
    print("Error: TPR file {0} does not exists".format(TPR))
    sys.exit(1)

# build reduced universe to match XTC
u0 = mda.Universe(TPR, tpr_resid_from_one=True)
u = mda.Merge(u0.select_atoms("not resname W and not resname WF and not resname ION"))
u.load_new(XTC)

# creating empty .h5 file
H5MD = args.output or os.path.splitext(XTC)[0] + '.h5'
h5file = h5md(H5MD)

# analyze trajectory
#
# loop over all the different protein types
for seg in u.segments:
    name = seg.segid[4:]
    print(name)
    group, position, box = h5file.create_particle_group(name, len(seg.atoms.fragments), boundary='periodic')

    # loop over all frames
    for ts in u.trajectory:
        # unwrap to get the true COM under PDB (double check!!)
        coms = seg.atoms.center_of_mass(unwrap=False, compound='fragments')[np.newaxis]

        h5md.append_dataset(position['step'], np.array([ts.frame,]))
        h5md.append_dataset(position['time'], np.array([ts.time,]))
        h5md.append_dataset(position['value'], coms)
        h5md.append_dataset(box['edges/value'], ts.triclinic_dimensions[np.newaxis])

        if ts.frame == args.last_frame:
            break



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source