• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CCPBioSim / CodeEntropy / 14059657073

25 Mar 2025 12:31PM UTC coverage: 38.633% (+8.9%) from 29.725%
14059657073

push

github

web-flow
Merge pull request #73 from CCPBioSim/34-implement-python-logging

Implement Python Logging

137 of 211 new or added lines in 9 files covered. (64.93%)

5 existing lines in 1 file now uncovered.

243 of 629 relevant lines covered (38.63%)

1.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.29
/CodeEntropy/calculations/ConformationFunctions.py
1
import logging
3✔
2

3
import numpy as np
3✔
4

5
logger = logging.getLogger(__name__)
3✔
6

7
# from MDAnalysis.analysis.dihedrals import Dihedral
8

9

10
def assign_conformation(
3✔
11
    data_container, dihedral, number_frames, bin_width, start, end, step
12
):
13
    """
14
    Create a state vector, showing the state in which the input dihedral is
15
    as a function of time. The function creates a histogram from the timeseries of the
16
    dihedral angle values and identifies points of dominant occupancy
17
    (called CONVEX TURNING POINTS).
18
    Based on the identified TPs, states are assigned to each configuration of the
19
    dihedral.
20

21
    Input
22
    -----
23
    dihedral_atom_group : the group of 4 atoms defining the dihedral
24
    number_frames : number of frames in the trajectory
25
    bin_width : the width of the histogram bit, default 30 degrees
26
    start : int, starting frame, will default to 0
27
    end : int, ending frame, will default to -1 (last frame in trajectory)
28
    step : int, spacing between frames, will default to 1
29

30
    Return
31
    ------
32
    A timeseries with integer labels describing the state at each point in time.
33

34
    """
35
    conformations = np.zeros(number_frames)
×
36
    phi = np.zeros(number_frames)
×
37

38
    # get the values of the angle for the dihedral
39
    # dihedral angle values have a range from -180 to 180
40
    for timestep in data_container.trajectory[start:end:step]:
×
41
        value = dihedral.value()
×
42
        # we want postive values in range 0 to 360 to make the peak assignment work
43
        # using the fact that dihedrals have circular symetry
44
        # (i.e. -15 degrees = +345 degrees)
45
        if value < 0:
×
46
            value += 360
×
47
        phi[timestep.frame] = value
×
48

49
    # create a histogram using numpy
50
    number_bins = int(360 / bin_width)
×
51
    popul, bin_edges = np.histogram(a=phi, bins=number_bins, range=(0, 360))
×
52
    bin_value = [0.5 * (bin_edges[i] + bin_edges[i + 1]) for i in range(0, len(popul))]
×
53

54
    # identify "convex turning-points" and populate a list of peaks
55
    # peak : a bin whose neighboring bins have smaller population
56
    # NOTE might have problems if the peak is wide with a flat or sawtooth top
57
    peak_values = []
×
58

59
    for bin_index in range(number_bins):
×
60
        # if there is no dihedrals in a bin then it cannot be a peak
61
        if popul[bin_index] == 0:
×
62
            pass
×
63
        # being careful of the last bin
64
        # (dihedrals have circular symmetry, the histogram does not)
65
        elif (
×
66
            bin_index == number_bins - 1
67
        ):  # the -1 is because the index starts with 0 not 1
68
            if (
×
69
                popul[bin_index] >= popul[bin_index - 1]
70
                and popul[bin_index] >= popul[0]
71
            ):
72
                peak_values.append(bin_value[bin_index])
×
73
        else:
74
            if (
×
75
                popul[bin_index] >= popul[bin_index - 1]
76
                and popul[bin_index] >= popul[bin_index + 1]
77
            ):
78
                peak_values.append(bin_value[bin_index])
×
79

80
    # go through each frame again and assign conformation state
81
    for frame in range(number_frames):
×
82
        # find the TP that the snapshot is least distant from
83
        distances = [abs(phi[frame] - peak) for peak in peak_values]
×
84
        conformations[frame] = np.argmin(distances)
×
85

NEW
86
    logger.debug(f"Final conformations: {conformations}")
×
87

88
    return conformations
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc