def test_make_balanced_slices_ValueError(n_frames, n_blocks, start, stop, step): with pytest.raises(ValueError): make_balanced_slices(n_frames, n_blocks, start=start, stop=stop, step=step)
def conclude(total, io, compute, univ, w): '''Function to gather the total time, prepare time, universe time and maximum IO, computation, and wait time. total: path to the total benchmark data io: path to the io data compute: pate to the compute data ''' data_t = np.loadtxt(total) all_io = np.loadtxt(io) all_compute = np.loadtxt(compute) all_univese = np.load(univ) all_wait = np.load(w) ndx = data_t[:, 0] total = data_t[:, 1] prepare = data_t[:, 4] conclude = data_t[:, 7] universe = [] wait = [] IO = [] com = [] stop = len(all_io[1, :]) - 1 start = 0 for i, j in enumerate(ndx): n_frames = len(all_io[1, :]) n_blocks = int(j) slices = make_balanced_slices(n_frames, n_blocks, start=start, stop=stop, step=1) io_block = np.zeros(n_blocks) com_block = np.zeros(n_blocks) for k, bslice in enumerate(slices): k_io = all_io[i, bslice.start:bslice.stop] k_com = all_compute[i, bslice.start:bslice.stop] io_block[k] = np.sum(k_io) com_block[k] = np.sum(k_com) main = all_univese[i] + all_wait[i] + io_block + com_block n = np.argmax(main) IO.append(io_block[n]) com.append(com_block[n]) universe.append(all_univese[i][n]) wait.append(all_wait[i][n]) d = { 'n': ndx, 'total': total, 'prepare': prepare, 'conclude': conclude, 'universe': universe, 'wait': wait, 'IO': IO, 'compute': com } df = pd.DataFrame(data=d) return df
def test_make_balanced_slices_step1(n_frames, n_blocks, start, result, step=1): assert step in (None, 1), "This test can only test step None or 1" _start = start if start is not None else 0 _result = [slice(sl.start + _start, sl.stop + _start if sl.stop is not None else None, sl.step) for sl in result] slices = make_balanced_slices(n_frames, n_blocks, start=start, step=step) assert_equal(slices, _result)
def test_blocks(analysis, n_blocks): analysis.run(n_blocks=n_blocks) u = mda.Universe(analysis._top, analysis._traj) n_frames = u.trajectory.n_frames start, stop, step = u.trajectory.check_slice_indices( None, None, None) slices = make_balanced_slices(n_frames, n_blocks, start, stop, step) blocks = [ range(bslice.start, bslice.stop, bslice.step) for bslice in slices ] assert analysis._blocks == blocks
def _test_make_balanced_slices(n_blocks, start, stop, step, scale): _start = start if start is not None else 0 traj_frames = range(scale * stop) frames = traj_frames[start:stop:step] n_frames = len(frames) if n_frames >= n_blocks: slices = make_balanced_slices(n_frames, n_blocks, start=start, stop=stop, step=step) assert len(slices) == n_blocks # assemble frames again by blocks and show that we have all # the original frames; get the sizes of the blocks block_frames = [] block_sizes = [] for bslice in slices: bframes = traj_frames[bslice] block_frames.extend(list(bframes)) block_sizes.append(len(bframes)) block_sizes = np.array(block_sizes) # check that we have all the frames accounted for assert_equal(np.asarray(block_frames), np.asarray(frames)) # check that the distribution is balanced assert np.all(block_sizes > 0) minsize = n_frames // n_blocks assert len(np.setdiff1d(block_sizes, [minsize, minsize+1])) == 0, \ "For n_blocks <= n_frames, block sizes are not balanced" else: with pytest.raises(ValueError, match="n_blocks must be smaller"): slices = make_balanced_slices(n_frames, n_blocks, start=start, stop=stop, step=step)
def _test_make_balanced_slices(n_blocks, start, stop, step, scale): _start = start if start is not None else 0 traj_frames = range(scale * stop) frames = traj_frames[start:stop:step] n_frames = len(frames) slices = make_balanced_slices(n_frames, n_blocks, start=start, stop=stop, step=step) assert len(slices) == n_blocks # assemble frames again by blocks and show that we have all # the original frames; get the sizes of the blocks block_frames = [] block_sizes = [] for bslice in slices: bframes = traj_frames[bslice] block_frames.extend(list(bframes)) block_sizes.append(len(bframes)) block_sizes = np.array(block_sizes) # check that we have all the frames accounted for assert_equal(np.asarray(block_frames), np.asarray(frames)) # check that the distribution is balanced if n_frames >= n_blocks: assert np.all(block_sizes > 0) minsize = n_frames // n_blocks assert not np.setdiff1d(block_sizes, [minsize, minsize+1]), \ "For n_blocks <= n_frames, block sizes are not balanced" else: # pathological case; we will have blocks with length 0 # and n_blocks with 1 frame zero_blocks = block_sizes == 0 assert np.sum(zero_blocks) == n_blocks - n_frames assert np.sum(~zero_blocks) == n_frames assert not np.setdiff1d(block_sizes[~zero_blocks], [1]), \ "For n_blocks>n_frames, some blocks contain != 1 frame"
def test_make_balanced_slices_empty(n_blocks, start, step): slices = make_balanced_slices(0, n_blocks, start=start, step=step) assert slices == []