Skip to content

Commit 17f970f

Browse files
authored
code clean up and finalize for 1.1.2 (#50)
* use direct assignment instead of setattr * moved util.run_residue to gibbs.run_residue * Update CHANGELOG for version 1.1.3 release - Updated version number and release date for 1.1.3. - added notes on code cleanup changes
1 parent 1072f38 commit 17f970f

File tree

4 files changed

+54
-33
lines changed

4 files changed

+54
-33
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The rules for this file:
1515
* YYYY-MM-DD date format (following ISO 8601)
1616
* accompany each entry with github issue/PR number (Issue #xyz)
1717
-->
18-
## [1.1.3] - UNRELEASES
18+
## [1.1.3] - 2025-09-11
1919

2020
### Authors
2121
* @orbeckst
@@ -28,11 +28,15 @@ The rules for this file:
2828
(Issue #48)
2929

3030
### Changed
31+
The following changes do not functionally change the code but users relying
32+
on default values need to be aware of the changes to gibbs.Gibbs.
33+
3134
* Default kwargs for the skipping in the Gibbs sampler are now
3235
gibbs.Gibbs(g=100, gskip=1) (used to be g=50, gskip=2) but for most users
3336
gskip for processing data is not important and it makes more sense to focus
3437
on g as the stride at which we sample AND process data (#48, PR #49)
35-
38+
* internal code refactor and clean-up: moved util.run_residue() to
39+
gibbs.run_residue() and use direct assignment instead of setattr() (PR #50)
3640

3741
## [1.1.2] - 2025-07-22
3842

basicrta/cluster.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ def reprocess(self, nproc=1):
119119

120120
taus = np.array(taus)
121121
bars = get_bars(taus)
122-
setattr(self, 'taus', taus[:, 1])
123-
setattr(self, 'bars', bars)
124-
setattr(self, 'residues', np.array(residues))
125-
setattr(self, 'files', np.array(results))
122+
self.taus = taus[:, 1]
123+
self.bars = bars
124+
self.residues = np.array(residues)
125+
self.files = np.array(results)
126126

127127
def get_taus(self, nproc=1):
128128
r"""Get :math:`\tau` and 95\% confidence interval bounds for the slowest
@@ -158,10 +158,10 @@ def get_taus(self, nproc=1):
158158

159159
taus = np.array(taus)
160160
bars = get_bars(taus)
161-
setattr(self, 'taus', taus[:, 1])
162-
setattr(self, 'bars', bars)
163-
setattr(self, 'residues', np.array(residues))
164-
setattr(self, 'files', np.array(results))
161+
self.taus = taus[:, 1]
162+
self.bars = bars
163+
self.residues = np.array(residues)
164+
self.files = np.array(results)
165165
return taus[:, 1], bars
166166

167167
def write_data(self, fname='tausout'):

basicrta/gibbs.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from tqdm import tqdm
99
from MDAnalysis.analysis.base import Results
1010
from basicrta.util import confidence_interval
11+
import multiprocessing
1112
from multiprocessing import Pool, Lock
1213
import MDAnalysis as mda
1314
from basicrta import istarmap
@@ -47,8 +48,6 @@ def run(self, run_resids=None, g=100):
4748
:param run_resids: Resid(s) for which to run a Gibbs sampler.
4849
:type run_resids: int or list, optional
4950
"""
50-
from basicrta.util import run_residue
51-
5251
with open(self.contacts, 'r+b') as f:
5352
contacts = pickle.load(f)
5453

@@ -96,6 +95,38 @@ def run(self, run_resids=None, g=100):
9695
pass
9796

9897

98+
def run_residue(residue, time, proc, ncomp, niter, cutoff, g, from_combined=False):
99+
"""Run Gibbs sampler for a single residue.
100+
101+
:param residue: Residue name
102+
:type residue: str
103+
:param time: Residence times data
104+
:type time: array-like
105+
:param proc: Process number for progress bar positioning
106+
:type proc: int
107+
:param ncomp: Number of mixture components
108+
:type ncomp: int
109+
:param niter: Number of iterations
110+
:type niter: int
111+
:param cutoff: Cutoff value used in contact analysis
112+
:type cutoff: float
113+
:param g: Gibbs skip parameter
114+
:type g: int
115+
:param from_combined: Whether data comes from combined contacts
116+
:type from_combined: bool
117+
"""
118+
x = np.array(time)
119+
if len(x) != 0:
120+
try:
121+
proc = int(multiprocessing.current_process().name.split('-')[-1])
122+
except ValueError:
123+
proc = 1
124+
125+
gib = Gibbs(times=x, residue=residue, loc=proc, ncomp=ncomp, niter=niter, cutoff=cutoff, g=g)
126+
gib._from_combined_contacts = from_combined
127+
gib.run()
128+
129+
99130
class Gibbs(object):
100131
r"""Gibbs sampler to estimate parameters of an exponential mixture for a set
101132
of data. Results are stored in :class:`gibbs.results`, which uses
@@ -301,8 +332,8 @@ def cluster(self, method="GaussianMixture", **kwargs):
301332
pindicator[tmpind, mapinds[i]] += 1
302333

303334
pindicator = (pindicator.T / pindicator.sum(axis=1)).T
304-
setattr(self.processed_results, 'indicator', pindicator)
305-
setattr(self.processed_results, 'labels', all_labels)
335+
self.processed_results.indicator = pindicator
336+
self.processed_results.labels = all_labels
306337

307338
def process_gibbs(self, show=True):
308339
r"""
@@ -329,9 +360,8 @@ def process_gibbs(self, show=True):
329360

330361
self.cluster(n_init=117, n_components=lmode)
331362
labels, presorts = mixture_and_plot(self, show=show)
332-
setattr(self.processed_results, 'labels', labels)
333-
setattr(self.processed_results, 'indicator',
334-
self.processed_results.indicator[:, presorts])
363+
self.processed_results.labels = labels
364+
self.processed_results.indicator = self.processed_results.indicator[:, presorts]
335365

336366
attrs = ["weights", "rates", "ncomp", "residue", "iteration", "niter"]
337367
values = [fweights, frates, lmode, self.residue, indices, self.niter]
@@ -364,7 +394,7 @@ def _sample_indicator(self):
364394
# sample indicator
365395
s = np.argmax(rng.multinomial(1, z), axis=1)
366396
indicator[i] = s
367-
setattr(self, 'indicator', indicator)
397+
self.indicator = indicator
368398
return indicator[burnin_ind::self.gskip]
369399

370400
def save(self):
@@ -719,8 +749,8 @@ def _estimate_params(self):
719749
params = np.array([[wh[1][np.argmax(wh[0])], rh[1][np.argmax(rh[0])]]
720750
for wh, rh in zip(whists, rhists)])
721751

722-
setattr(rp, 'parameters', params)
723-
setattr(rp, 'intervals', np.array([wbounds, rbounds]))
752+
rp.parameters = params
753+
rp.intervals = np.array([wbounds, rbounds])
724754

725755
def estimate_tau(self):
726756
r"""

basicrta/util.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -472,19 +472,6 @@ def plot_protein(residues, t_slow, bars, prot=None, label_cutoff=3, ylim=None,
472472
# gib.run()
473473

474474

475-
def run_residue(residue, time, proc, ncomp, niter, cutoff, g, from_combined=False):
476-
from basicrta.gibbs import Gibbs
477-
x = np.array(time)
478-
if len(x) != 0:
479-
try:
480-
proc = int(multiprocessing.current_process().name.split('-')[-1])
481-
except ValueError:
482-
proc = 1
483-
484-
gib = Gibbs(x, residue, proc, ncomp=ncomp, niter=niter, cutoff=cutoff)
485-
gib._from_combined_contacts = from_combined
486-
gib.run()
487-
488475

489476
def check_results(residues, times, ts):
490477
if not os.path.exists('result_check'):

0 commit comments

Comments
 (0)