<commit_before>#!/usr/bin/python
desc = """amber.py
    Functions for primary analysis of AMBER trajectories
    Written by <NAME> on 12-12-01
    Last updated 13-07-21"""
########################################### MODULES, SETTINGS, AND DEFAULTS ############################################
import commands, os, sys
import numpy as np
################################################## ANALYSIS FUNCTIONS ##################################################
def log(segment, time_offset = 0.0, **kwargs):
    """ Parses log for <segment> """
    log         = segment[".out"]
    nstlim      = float(commands.getoutput("grep nstlim " + log).split()[2][:-1])
    ntpr        = float(commands.getoutput("grep ntpr   " + log).split()[2][:-1])
    dt          = float(commands.getoutput("grep dt     " + log).split()[2][:-1])
    length      = nstlim / ntpr
    with open(log, "r") as log:
        raw     = [line.rstrip() for line in log.readlines()]
    seg_attrs   = {}
    i           = 0
    data        = {}
    while i < len(raw):
        line    = raw[i]
        if   line.startswith("| Running AMBER/MPI version on"):
            line                    = line.split()
            seg_attrs["n_cores"]    = int(line[5])
        elif line.startswith("|  Master Total wall time"):
            seconds                 = int(line.split()[5])
            seg_attrs["duration"]   = "{0}:{1:02d}:{2:02d}".format(seconds//60//60, seconds//60%60, seconds%60)
        elif line.startswith("| Run on"):
            line                    = line.split()
            seg_attrs["date"]       = "{0:02d}-{1:02d}-{2:02d}".format(int(line[3][8:]), int(line[3][:2]),
                                                                       int(line[3][3:5]))
            seg_attrs["time"]       = line[5]
        elif line.startswith(" NSTEP"):
            while True:
                line    = raw[i]
                if line.startswith(" Ewald error estimate:") or line.startswith("|E(PBS)"):  break
                line    = line.split("=")
                for j in range(1,len(line)):
                    if j == 1:                  field   = line[j-1].strip()
                    else:
                        field                           = line[j-1].split()
                        if len(field)   == 1:   field   = field[0]
                        else:                   field   = " ".join(field[1:])
                    value   = float(line[j].split()[0])
                    if field in data:   data[field]    += [value]
                    else:               data[field]     = [value]
                i      += 1
        i  += 1
    data["TIME(PS)"]    = np.array(data["TIME(PS)"])[:-2]  / 1000. + time_offset
    dtype_line      = "np.dtype([('time', 'f4'),"
    log_line        = "[tuple(frame) for frame in np.column_stack((data['TIME(PS)'],"
    attrs_line      = "{'time units': 'ns',"
    for field, dest in [("Etot",        "total"),
                        ("EPtot",       "potential"),
                        ("EKtot",       "kinetic"),
                        ("BOND",        "bond"),
                        ("ANGLE",       "angle"),
                        ("DIHED",       "dihedral"),
                        ("EELEC",       "coulomb"),
                        ("1-4 EEL",     "coulomb 1-4"),
                        ("VDWAALS",     "<NAME>"),
                        ("1-4 NB",      "<NAME> 1-4"),
                        ("EHBOND",      "hydrogen bond"),
                        ("RESTRAINT",   "position restraint")]:
        if not (field in data): continue
        data[field]     = np.array(data[field])[1:-1] * 0.239005736
        dtype_line     += "('"     + dest  + "', 'f4'),"
        log_line       += "data['" + field + "'],"
        attrs_line     += "'"      + dest  + " units': 'kcal mol-1',"
    if "TEMP(K)" in data:
        data["TEMP(K)"] = np.array(data["TEMP(K)"])[1:-1]
        dtype_line     += "('temperature', 'f4'),"
        log_line       += "data['TEMP(K)'],"
        attrs_line     += "'temperature units': 'K',"
    if "PRESS" in data:
        data["PRESS"]   = np.array(data["PRESS"])[1:-1]
        dtype_line     += "('pressure', 'f4'),"
        log_line       += "data['PRESS'],"
        attrs_line     += "'pressure units': 'bar',"
    dtype_line  = dtype_line[:-1]   + "])"
    log_line    = log_line[:-1]     + "))]"
    attrs_line  = attrs_line[:-1]   + "}"
    log         = np.array(eval(log_line), eval(dtype_line))
    log_attrs   = eval(attrs_line)
    return [(segment + "/log",    log),
            (segment + "/",       seg_attrs),
            (segment + "/log",    log_attrs)]<commit_msg>primary.amber.log now supports AMOEBA; also has several indexing error corrected<commit_after>#!/usr/bin/python
desc = """amber.py
    Functions for primary analysis of AMBER trajectories
    Written by <NAME> on 12-12-01
    Last updated 13-10-07"""
########################################### MODULES, SETTINGS, AND DEFAULTS ############################################
import commands, os, sys
import numpy as np
################################################## ANALYSIS FUNCTIONS ##################################################
def log(segment, time_offset = 0.0, **kwargs):
    """ Parses log for <segment> """
    log         = segment[".out"]
    nstlim      = float(commands.getoutput("grep nstlim " + log).split()[2][:-1])
    ntpr        = float(commands.getoutput("grep ntpr   " + log).split()[2][:-1])
    dt          = float(commands.getoutput("grep dt     " + log).split()[2][:-1])
    length      = nstlim / ntpr
    with open(log, "r") as log:
        raw     = [line.strip() for line in log.readlines()]
    seg_attrs   = {}
    i           = 0
    data        = {}
    data_loaded = False
    
    while i < len(raw):
        line    = raw[i]
        if   line.startswith("A V E R A G E S"):
            data_loaded = True
        if   line.startswith("| Running AMBER/MPI version on"):
            line                    = line.split()
            seg_attrs["n_cores"]    = int(line[5])
        elif line.startswith("|  Master Total wall time"):
            seconds                 = int(line.split()[5])
            seg_attrs["duration"]   = "{0}:{1:02d}:{2:02d}".format(seconds//60//60, seconds//60%60, seconds%60)
        elif line.startswith("| Run on"):
            line                    = line.split()
            seg_attrs["date"]       = "{0:02d}-{1:02d}-{2:02d}".format(int(line[3][8:]), int(line[3][:2]),
                                                                       int(line[3][3:5]))
            seg_attrs["time"]       = line[5]
        elif line.startswith("NSTEP") and not data_loaded:
            while True:
                line    = raw[i]
                if line.startswith("------------------------------------------------------------------------------"):
                    break
                line    = line.split("=")
                for j in range(1,len(line)):
                    if j == 1:                  field   = line[j-1].strip()
                    else:
                        field                           = line[j-1].split()
                        if len(field)   == 1:   field   = field[0]
                        else:                   field   = " ".join(field[1:])
                    value   = float(line[j].split()[0])
                    if field in data:   data[field]    += [value]
                    else:               data[field]     = [value]
                i      += 1
        i  += 1
    data["TIME(PS)"]    = np.array(data["TIME(PS)"])  / 1000. + time_offset
    dtype_line      = "np.dtype([('time', 'f4'),"
    log_line        = "[tuple(frame) for frame in np.column_stack((data['TIME(PS)'],"
    attrs_line      = "{'time units': 'ns',"
    for field, dest in [("Etot",        "total"),
                        ("EPtot",       "potential"),
                        ("EKtot",       "kinetic"),
                        ("BOND",        "bond"),
                        ("ANGLE",       "angle"),
                        ("DIHED",       "dihedral"),
                        ("EELEC",       "coulomb"),
                        ("1-4 EEL",     "coulomb 1-4"),
                        ("VDWAALS",     "<NAME>"),
                        ("1-4 NB",      "<NAME> 1-4"),
                        ("EHBOND",      "hydrogen bond"),
                        ("RESTRAINT",   "position restraint"),
                        ("EKCMT",       "center of mass motion kinetic"),
                        ("VIRIAL",      "virial"),
                        ("EPOLZ",       "polarization")]:
        if not (field in data): continue
        data[field]     = np.array(data[field])
        dtype_line     += "('"     + dest  + "', 'f4'),"
        log_line       += "data['" + field + "'],"
        attrs_line     += "'"      + dest  + " units': 'kcal mol-1',"
    if "TEMP(K)" in data:
        data["TEMP(K)"] = np.array(data["TEMP(K)"])
        dtype_line     += "('temperature', 'f4'),"
        log_line       += "data['TEMP(K)'],"
        attrs_line     += "'temperature units': 'K',"
    if "PRESS" in data:
        data["PRESS"]   = np.array(data["PRESS"])
        dtype_line     += "('pressure', 'f4'),"
        log_line       += "data['PRESS'],"
        attrs_line     += "'pressure units': 'bar',"
    if "Dipole convergence: rms" in data:
        data["Dipole convergence: rms"] = np.array(data["Dipole convergence: rms"])
        dtype_line     += "('dipole converge rms', 'f4'),"
        log_line       += "data['Dipole convergence: rms'],"
    if "iters" in data:
        data["iters"]   = np.array(data["iters"])
        dtype_line     += "('iters', 'f4'),"
        log_line       += "data['iters'],"
    dtype_line  = dtype_line[:-1]   + "])"
    log_line    = log_line[:-1]     + "))]"
    attrs_line  = attrs_line[:-1]   + "}"
    log         = np.array(eval(log_line), eval(dtype_line))
    log_attrs   = eval(attrs_line)
    return [(segment + "/log",    log),
            (segment + "/",       seg_attrs),
            (segment + "/log",    log_attrs)]
{'fraction_non_alphanumeric': 0.20559080095162569, 'fraction_numerical': 0.019131641554321968, 'mean_word_length': 1.4776522593320236, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3988646', 'n_tokens_mistral': 3134, 'n_tokens_neox': 2985, 'n_words': 629}