def filter_all(data_dir): """Removes STORM localizations from neighbouring cells and removes cell objects with too few for all conditions.""" gt_cells = load( os.path.join(data_dir, 'cell_obj', 'cells_final_selected.hdf5')) for ph in [10000, 1000, 500]: print('Photons', ph) with open( os.path.join(data_dir, 'matched_names', 'm_cells_ph_{}_match.txt'.format(ph)), 'r') as f: m_names = f.readlines() m_names = list([n.rstrip() for n in m_names]) m_cells = load( os.path.join(data_dir, 'cell_obj', 'cell_ph_{}_raw.hdf5'.format(ph))) with open( os.path.join(data_dir, 'matched_names', 'gt_cells_ph_{}_match.txt'.format(ph)), 'r') as f: gt_names = f.readlines() gt_names = list([n.rstrip() for n in gt_names]) m_final, gt_final, m_cells, gt_cells = filter_cells( m_names, gt_names, m_cells, gt_cells) with open( os.path.join(data_dir, 'matched_names', 'gt_cells_ph_{}_match_filter.txt'.format(ph)), 'w') as f: f.writelines(gt_final) with open( os.path.join(data_dir, 'matched_names', 'm_cells_ph_{}_match_filter.txt'.format(ph)), 'w') as f: f.writelines(m_final) for i, (m_, gt_) in tqdm(enumerate(zip(m_final, gt_final))): m_i = m_cells.name.tolist().index(m_.rstrip()) g_i = gt_cells.name.tolist().index(gt_.rstrip()) try: assert len(m_cells[m_i].data.data_dict['storm_inner']) == len( gt_cells[g_i].data.data_dict['storm_inner']) except AssertionError: print('Assertion error:', i) save( os.path.join(data_dir, 'cell_obj', 'cell_ph_{}_filtered.hdf5'.format(ph)), m_cells)
def gen_cells(data_dir): storm_i = np.load(os.path.join(data_dir, 'images', 'storm_inner.npy')) storm_o = np.load(os.path.join(data_dir, 'images', 'storm_outer.npy')) foci_i = np.load(os.path.join(data_dir, 'images', 'foci_inner.npy')) foci_o = np.load(os.path.join(data_dir, 'images', 'foci_outer.npy')) for ph in [10000, 1000, 500]: print('Photons {}'.format(ph)) bin_predicted = tifffile.imread( os.path.join(data_dir, 'images', 'binary_{}photons_predicted.tif'.format(ph))) bf = np.load( os.path.join(data_dir, 'images', 'bf_noise_{}_photons.npy'.format(ph))) print('Filtering') filtered_pred = filter_binaries(bin_predicted, min_size=495, max_size=2006.4, min_minor=7.57, max_minor=17.3, min_major=15.41, max_major=54.97) data = Data() data.add_data(filtered_pred, 'binary') data.add_data(bf, 'brightfield') data.add_data(storm_i, 'storm', 'storm_inner') data.add_data(storm_o, 'storm', 'storm_outer') data.add_data(foci_i, 'fluorescence', 'foci_inner') data.add_data(foci_o, 'fluorescence', 'foci_outer') print('Making cells') m_cells = data_to_cells(data, remove_multiple_cells=False, remove_bordering=False) print('Saving') save( os.path.join(data_dir, 'cell_obj', 'cell_ph_{}_raw.hdf5'.format(ph)), m_cells)
def optimize_all(data_dir): """Optimize the cell's coordinate systems for each condition based on different data elements""" for ph in [10000, 1000, 500]: print('Photons {}'.format(ph)) m_cells = load( os.path.join(data_dir, 'cell_obj', 'cell_ph_{}_filtered.hdf5'.format(ph))) print('Measured cells loaded') print('binary') optimize_cells = m_cells.copy() res = optimize_cells.optimize_mp() obj_vals = [r.objective_value for r in res] np.savetxt( os.path.join(data_dir, 'minimize_res', 'm_cells_ph_{}_binary.txt'.format(ph)), obj_vals) save( os.path.join(data_dir, 'cell_obj', 'm_cells_ph_{}_filtered_binary.hdf5'.format(ph)), optimize_cells) print('brightfield') optimize_cells = m_cells.copy() res = optimize_cells.optimize_mp('brightfield') obj_vals = [r.objective_value for r in res] np.savetxt( os.path.join(data_dir, 'minimize_res', 'm_cells_ph_{}_brightfield.txt'.format(ph)), obj_vals) save( os.path.join(data_dir, 'cell_obj', 'm_cells_ph_{}_filtered_brightfield.hdf5'.format(ph)), optimize_cells) print('storm inner') optimize_cells = m_cells.copy() res = optimize_cells.optimize_mp('storm_inner') obj_vals = [r.objective_value for r in res] np.savetxt( os.path.join(data_dir, 'minimize_res', 'm_cells_ph_{}_storm.txt'.format(ph)), obj_vals) save( os.path.join(data_dir, 'cell_obj', 'm_cells_ph_{}_filtered_storm_inner.hdf5'.format(ph)), optimize_cells)
if reload: for ph in photons: im = np.load( os.path.join(data_dir, 'images', 'bf_noise_{}_photons.npy'.format(ph))) np.save( os.path.join(data_dir, 'plot_vars', 'bf_noise_{}_photons.npy'.format(ph)), im[0]) for condition in conditions: cells = load( os.path.join( data_dir, 'cell_obj', 'm_cells_ph_{}_filtered_{}.hdf5'.format(ph, condition))) save( os.path.join(data_dir, 'plot_vars', 'cells_{}_{}_photons.hdf5'.format(condition, ph)), cells[:50]) cell_dict = {} for ph in photons: cell_dict[ph] = {} for condition in conditions: cell_dict[ph][condition] = load( os.path.join(data_dir, 'plot_vars', 'cells_{}_{}_photons.hdf5'.format(condition, ph))) imgs = { ph: np.load( os.path.join(data_dir, 'plot_vars', 'bf_noise_{}_photons.npy'.format(ph))) for ph in photons
def measure_r(file_path): bf_rdist = np.loadtxt(file_path) x, y = bf_rdist.T mid_val = (np.min(y) + np.max(y)) / 2 imin = np.argmin(y) imax = np.argmax(y) y_select = y[imin:imax] if imax > imin else y[imax:imin][::-1] x_select = x[imin:imax] if imax > imin else x[imax:imin][::-1] try: assert np.all(np.diff(y_select) > 0) except AssertionError: print('Radial distribution not monotonically increasing') r = np.interp(mid_val, y_select, x_select) return r if __name__ == '__main__': np.random.seed(42) cell_list = gen_synthcells(25000) # Remove cells with incorrect amount of data elements (missing STORM data) b = np.array([len(cell.data.names) == 6 for cell in cell_list]) data_dir = r'.' if not os.path.exists(os.path.join(data_dir, 'cell_obj')): os.mkdir(os.path.join(data_dir, 'cell_obj')) save(os.path.join(data_dir, 'cell_obj', 'cells_final_selected.hdf5'), cell_list[b])
import matplotlib.pyplot as plt from colicoords import Cell, load, save, CellPlot import os cell = load(r'img191c002.hdf5') data = cell.data.copy() cell_raw = Cell(data[:, 1:-1]) reload = False if reload: cell_bin = cell_raw.copy() cell_bin.optimize() save('cell_bin.hdf5', cell_bin) cell_bf = cell_raw.copy() cell_bf.optimize('brightfield') cell_bf.measure_r() save('cell_bf.hdf5', cell_bf) cell_flu = cell_raw.copy() cell_flu.optimize('gain50') cell_flu.measure_r() save('cell_flu.hdf5', cell_flu) else: cell_bin = load('cell_bin.hdf5') cell_bf = load('cell_bf.hdf5') cell_flu = load('cell_flu.hdf5') fig_width = 8.53534 / 2.54 fig, axes = plt.subplots(3, 3, figsize=(fig_width, fig_width))