Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auxiliary PR: Richardson Lucy Parallelization #271

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
375 changes: 375 additions & 0 deletions cosipy/image_deconvolution/RLparallelscript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,375 @@
import os
from pathlib import Path
import logging
import argparse

Check warning on line 4 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L1-L4

Added lines #L1 - L4 were not covered by tests

# logging setup
logger = logging.getLogger(__name__)

Check warning on line 7 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L7

Added line #L7 was not covered by tests

# argparse setup
parser = argparse.ArgumentParser()
parser.add_argument("--numrows", type=int, dest='numrows', help="Number of rows in the response matrix")
parser.add_argument("--numcols", type=int, dest='numcols', help="Number of columns in the response matrix")
parser.add_argument("--base_dir", type=str, dest='base_dir', help="Current working directory and where configuration file is assumed to lie")
parser.add_argument("--config_file", type=str, dest='config_file', help="Name of configuration file (assumed to lie in CWD)")
args = parser.parse_args()

Check warning on line 15 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L10-L15

Added lines #L10 - L15 were not covered by tests

# Import third party libraries
import numpy as np
from mpi4py import MPI
import h5py
from yayc import Configurator

Check warning on line 21 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L18-L21

Added lines #L18 - L21 were not covered by tests

# Load configuration file
config = Configurator.open(f'{args.base_dir}/{args.config_file}')

Check warning on line 24 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L24

Added line #L24 was not covered by tests

# Number of elements in data space (ROWS) and model space (COLS)
NUMROWS = args.numrows # TODO: Ideally, for row-major form to exploit caching, NUMROWS must be smaller than NUMCOLS
NUMCOLS = args.numcols

Check warning on line 28 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L27-L28

Added lines #L27 - L28 were not covered by tests

# Define MPI and iteration misc variables
MASTER = 0 # Indicates master process
MAXITER = config.get('deconvolution:parameter:iteration_max', 10)

Check warning on line 32 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L31-L32

Added lines #L31 - L32 were not covered by tests

# FILE_DIR = Path(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = Path(args.base_dir)
RESULTS_DIR = BASE_DIR / config.get('deconvolution:parameter:save_results_directory', './results')

Check warning on line 36 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L35-L36

Added lines #L35 - L36 were not covered by tests

'''

Check warning on line 38 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L38

Added line #L38 was not covered by tests
Response matrix
'''
def load_response_matrix(comm, start_row, end_row, filename='response_matrix.h5'):
with h5py.File(BASE_DIR / filename, "r", driver="mpio", comm=comm) as f1:
dataset = f1["response_matrix"]
R = dataset[start_row:end_row, :]
return R

Check warning on line 45 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L41-L45

Added lines #L41 - L45 were not covered by tests

'''

Check warning on line 47 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L47

Added line #L47 was not covered by tests
Response matrix transpose
'''
def load_response_matrix_transpose(comm, start_col, end_col, filename='response_matrix.h5'):
with h5py.File(BASE_DIR / filename, "r", driver="mpio", comm=comm) as f1:
dataset = f1["response_matrix"]
RT = dataset[:, start_col:end_col]
return RT

Check warning on line 54 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L50-L54

Added lines #L50 - L54 were not covered by tests

'''

Check warning on line 56 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L56

Added line #L56 was not covered by tests
Response matrix summed along axis=i
'''
def load_axis0_summed_response_matrix(filename='response_matrix.h5'):
with h5py.File(BASE_DIR / filename, "r") as f1:
dataset = f1["response_vector"]
Rj = dataset[:]
return Rj

Check warning on line 63 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L59-L63

Added lines #L59 - L63 were not covered by tests

'''

Check warning on line 65 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L65

Added line #L65 was not covered by tests
Sky model
'''
def initial_sky_model(model_init_val=[1e-4]):
M0 = np.ones(NUMCOLS, dtype=np.float64) * float(model_init_val[0]) # Initial guess according to image_deconvolution.py. TODO: Make this more general than element 0
return M0

Check warning on line 70 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L68-L70

Added lines #L68 - L70 were not covered by tests

'''

Check warning on line 72 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L72

Added line #L72 was not covered by tests
Background model
'''
def load_bg_model(filename='bg.csv'):
bg = np.loadtxt(filename)
return bg

Check warning on line 77 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L75-L77

Added lines #L75 - L77 were not covered by tests

'''

Check warning on line 79 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L79

Added line #L79 was not covered by tests
Observed data
'''
def load_event_data(filename='event.csv'):
event = np.loadtxt(filename)
return event

Check warning on line 84 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L82-L84

Added lines #L82 - L84 were not covered by tests

def register_result(iter, M, delta):

Check warning on line 86 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L86

Added line #L86 was not covered by tests
"""
The values below are stored at the end of each iteration.
- iteration: iteration number
- model: updated image
- delta_model: delta map after M-step
- processed_delta_model: delta map after post-processing
- alpha: acceleration parameter in RL algirithm
- background_normalization: optimized background normalization
- loglikelihood: log-likelihood
"""

this_result = {"iteration": iter,

Check warning on line 98 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L98

Added line #L98 was not covered by tests
"model": M,
"delta_model": delta,
# "processed_delta_model": copy.deepcopy(self.processed_delta_model), TODO: The RL parallel implementation does not currently support smooth convergence through weighting, background normalization, or likelihood calculation
# "background_normalization": copy.deepcopy(self.dict_bkg_norm),
# "alpha": self.alpha,
# "loglikelihood": copy.deepcopy(self.loglikelihood_list)
}

# # show intermediate results
# logger.info(f' alpha: {this_result["alpha"]}')
# logger.info(f' background_normalization: {this_result["background_normalization"]}')
# logger.info(f' loglikelihood: {this_result["loglikelihood"]}')

return this_result

Check warning on line 112 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L112

Added line #L112 was not covered by tests

def save_results(results):

Check warning on line 114 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L114

Added line #L114 was not covered by tests
'''
NOTE: Copied from RichardsonLucy.py
'''
logger.info('Saving results in {RESULTS_DIR}')

Check warning on line 118 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L118

Added line #L118 was not covered by tests
# model
for this_result in results:
iteration_count = this_result["iteration"]

Check warning on line 121 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L120-L121

Added lines #L120 - L121 were not covered by tests
# this_result["model"].write(f"{RESULTS_DIR}/model_itr{iteration_count}.hdf5", overwrite = True) # TODO: numpy arrays do not support write_to_hdf5 as a method. Need to ensure rest of code is modified to support cosipy.image_deconvolution.allskyimage.AllSkyImageModel
# this_result["delta_model"].write(f"{RESULTS_DIR}/delta_model_itr{iteration_count}.hdf5", overwrite = True)
# this_result["processed_delta_model"].write(f"{RESULTS_DIR}/processed_delta_model_itr{iteration_count}.hdf5", overwrite = True) TODO: processed_delta_model here is not different from delta_model
np.savetxt(f'{RESULTS_DIR}/model_itr{iteration_count}.csv', this_result['model'], delimiter=',')
np.savetxt(f'{RESULTS_DIR}/delta_model_itr{iteration_count}.csv', this_result['delta_model'], delimiter=',')

Check warning on line 126 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L125-L126

Added lines #L125 - L126 were not covered by tests

# TODO: The following will be enabled once the respective calculations are incorporated
# #fits
# primary_hdu = fits.PrimaryHDU()

# col_iteration = fits.Column(name='iteration', array=[float(result['iteration']) for result in self.results], format='K')
# col_alpha = fits.Column(name='alpha', array=[float(result['alpha']) for result in self.results], format='D')
# cols_bkg_norm = [fits.Column(name=key, array=[float(result['background_normalization'][key]) for result in self.results], format='D')
# for key in self.dict_bkg_norm.keys()]
# cols_loglikelihood = [fits.Column(name=f"{self.dataset[i].name}", array=[float(result['loglikelihood'][i]) for result in self.results], format='D')
# for i in range(len(self.dataset))]

# table_alpha = fits.BinTableHDU.from_columns([col_iteration, col_alpha])
# table_alpha.name = "alpha"

# table_bkg_norm = fits.BinTableHDU.from_columns([col_iteration] + cols_bkg_norm)
# table_bkg_norm.name = "bkg_norm"

# table_loglikelihood = fits.BinTableHDU.from_columns([col_iteration] + cols_loglikelihood)
# table_loglikelihood.name = "loglikelihood"

# hdul = fits.HDUList([primary_hdu, table_alpha, table_bkg_norm, table_loglikelihood])
# hdul.writeto(f'{RESULTS_DIR}/results.fits', overwrite=True)

def main():

Check warning on line 151 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L151

Added line #L151 was not covered by tests
# Set up MPI
comm = MPI.COMM_WORLD
numtasks = comm.Get_size()
taskid = comm.Get_rank()

Check warning on line 155 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L153-L155

Added lines #L153 - L155 were not covered by tests

# Calculate the indices in Rij that the process has to parse. My hunch is that calculating these scalars individually will be faster than the MPI send broadcast overhead.
averow = NUMROWS // numtasks
extra_rows = NUMROWS % numtasks
start_row = taskid * averow
end_row = (taskid + 1) * averow if taskid < (numtasks - 1) else NUMROWS

Check warning on line 161 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L158-L161

Added lines #L158 - L161 were not covered by tests

# Calculate the indices in Rji, i.e., Rij transpose, that the process has to parse.
avecol = NUMCOLS // numtasks
extra_cols = NUMCOLS % numtasks
start_col = taskid * avecol
end_col = (taskid + 1) * avecol if taskid < (numtasks - 1) else NUMCOLS

Check warning on line 167 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L164-L167

Added lines #L164 - L167 were not covered by tests

# Initialise vectors required by all processes
epsilon = np.zeros(NUMROWS) # All gatherv-ed. Explicit variable declaration.
epsilon_fudge = 1e-12 # To prevent divide-by-zero and underflow errors. Value taken from `almost_zero = 1e-12` in dataIF_COSI_DC2.py

Check warning on line 171 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L170-L171

Added lines #L170 - L171 were not covered by tests

# Initialise epsilon_slice and C_slice. Explicit variable declarations.
epsilon_slice = np.zeros(end_row - start_row)
C_slice = np.zeros(end_col - start_col)

Check warning on line 175 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L174-L175

Added lines #L174 - L175 were not covered by tests

# Load R and RT into memory (single time if response matrix doesn't
# change with time)
R = load_response_matrix(comm, start_row, end_row)
RT = load_response_matrix_transpose(comm, start_col, end_col)

Check warning on line 180 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L179-L180

Added lines #L179 - L180 were not covered by tests

# Loaded and broadcasted by master.
M = np.empty(NUMCOLS, dtype=np.float64)
d = np.empty(NUMROWS, dtype=np.float64)
bg = np.zeros(NUMROWS)

Check warning on line 185 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L183-L185

Added lines #L183 - L185 were not covered by tests

# ****************************** MPI ******************************

# **************************** Part I *****************************

'''*************** Master ***************'''

Check warning on line 191 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L191

Added line #L191 was not covered by tests

if taskid == MASTER:

Check warning on line 193 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L193

Added line #L193 was not covered by tests

# Pretty print definitions
linebreak_stars = '**********************'
linebreak_dashes = '----------------------'

Check warning on line 197 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L196-L197

Added lines #L196 - L197 were not covered by tests

# Log input information (Only master node does this)
save_results_flag = config.get('deconvolution:parameter:save_results', False) # Extract from config file
logger.info(linebreak_stars)
logger.info(f'Number of elements in data space: {NUMROWS}')
logger.info(f'Number of elements in model space: {NUMCOLS}')
logger.info(f'Base directory: {BASE_DIR}')
if save_results_flag == True:
logger.info(f'Results directory (if save_results flag is set to True): {RESULTS_DIR}')
logger.info(f'Configuration filename: {args.config_file}')
logger.info(f'Master node: {MASTER}')
logger.info(f'Maximum number of RL iterations: {MAXITER}')

Check warning on line 209 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L200-L209

Added lines #L200 - L209 were not covered by tests

# Load Rj vector (response matrix summed along axis=i)
Rj = load_axis0_summed_response_matrix()

Check warning on line 212 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L212

Added line #L212 was not covered by tests

# Generate initial sky model from configuration file
M = initial_sky_model(model_init_val=config.get('model_definition:initialization:parameter:value', [1e-4]))

Check warning on line 215 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L215

Added line #L215 was not covered by tests

# Load event data and background model (intermediate files created in RichardsonLucyParallel.py)
bg = load_bg_model()
d = load_event_data()

Check warning on line 219 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L218-L219

Added lines #L218 - L219 were not covered by tests

# Sanity check: print d
print()
print('Observed data-space d vector:')
print(d)

Check warning on line 224 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L222-L224

Added lines #L222 - L224 were not covered by tests
## Pretty print
print()
print(linebreak_stars)

Check warning on line 227 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L226-L227

Added lines #L226 - L227 were not covered by tests

# Initialise C vector. Only master requires full length. Explicit variable declaration.
C = np.empty(NUMCOLS, dtype=np.float64)

Check warning on line 230 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L230

Added line #L230 was not covered by tests

# Initialise update delta vector. Explicit variable declaration.
delta = np.empty(NUMCOLS, dtype=np.float64)

Check warning on line 233 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L233

Added line #L233 was not covered by tests

# Initialise list for results. See function register_result() for list elements.
results = []

Check warning on line 236 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L236

Added line #L236 was not covered by tests

'''*************** Worker ***************'''

Check warning on line 238 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L238

Added line #L238 was not covered by tests

if taskid > MASTER:

Check warning on line 240 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L240

Added line #L240 was not covered by tests
# Only separate if... clause for NON-MASTER processes.
# Initialise C vector to None. Only master requires full length.
C = None

Check warning on line 243 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L243

Added line #L243 was not covered by tests

# Broadcast d vector
comm.Bcast([d, MPI.DOUBLE], root=MASTER)

Check warning on line 246 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L246

Added line #L246 was not covered by tests

# Scatter bg vector to epsilon_BG
comm.Bcast([bg, MPI.DOUBLE], root=MASTER)

Check warning on line 249 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L249

Added line #L249 was not covered by tests
# comm.Scatter(bg, [epsilon_BG, recvcounts, displacements, MPI.DOUBLE])

# print(f"TaskID {taskid}, gathered broadcast")

# Sanity check: print epsilon
# if taskid == MASTER:
# print('epsilon_BG')
# print(bg)
# print()

# **************************** Part IIa *****************************

'''***************** Begin Iterative Segment *****************'''

Check warning on line 262 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L262

Added line #L262 was not covered by tests
# Set up initial values for iterating variables.
# Exit if:
## 1. Max iterations are reached
# for iter in tqdm(range(MAXITER)):
for iter in range(MAXITER):

Check warning on line 267 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L267

Added line #L267 was not covered by tests

'''*************** Master ***************'''
if taskid == MASTER:

Check warning on line 270 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L269-L270

Added lines #L269 - L270 were not covered by tests
# Pretty print - starting
print(f"Starting iteration {iter + 1}")

Check warning on line 272 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L272

Added line #L272 was not covered by tests
# logger.info(f"## Iteration {self.iteration_count}/{self.iteration_max} ##")
# logger.info("<< E-step >>")


# Calculate epsilon vector and all gatherv

'''**************** All *****************'''

Check warning on line 279 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L279

Added line #L279 was not covered by tests

'''Synchronization Barrier 1'''

Check warning on line 281 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L281

Added line #L281 was not covered by tests
# Broadcast M vector
comm.Bcast([M, MPI.DOUBLE], root=MASTER)

Check warning on line 283 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L283

Added line #L283 was not covered by tests

# Calculate epsilon slice
epsilon_BG = bg[start_row:end_row] # TODO: Change the way epsilon_BG is loaded. Make it taskID dependent through MPI.Scatter for example. Use `recvcounts`
epsilon_slice = np.dot(R, M) + epsilon_BG + epsilon_fudge # TODO: For a more general implementation, see calc_expectation() in dataIF_COSI_DC2.py

Check warning on line 287 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L286-L287

Added lines #L286 - L287 were not covered by tests

'''Synchronization Barrier 2'''

Check warning on line 289 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L289

Added line #L289 was not covered by tests
# All vector gather epsilon slices
recvcounts = [averow] * (numtasks-1) + [averow + extra_rows]
displacements = np.arange(numtasks) * averow
comm.Allgatherv(epsilon_slice, [epsilon, recvcounts, displacements, MPI.DOUBLE])

Check warning on line 293 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L291-L293

Added lines #L291 - L293 were not covered by tests

# Sanity check: print epsilon
# if taskid == MASTER:
# print('epsilon')
# print(epsilon)
# print(epsilon.min(), epsilon.max())
# print()

# **************************** Part IIb *****************************

# Calculate C vector and gatherv

'''**************** All *****************'''

Check warning on line 306 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L306

Added line #L306 was not covered by tests

# Calculate C slice
C_slice = np.dot(RT.T, d/epsilon)

Check warning on line 309 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L309

Added line #L309 was not covered by tests

'''Synchronization Barrier 3'''

Check warning on line 311 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L311

Added line #L311 was not covered by tests
# All vector gather C slices
recvcounts = [avecol] * (numtasks-1) + [avecol + extra_cols]
displacements = np.arange(numtasks) * avecol
comm.Gatherv(C_slice, [C, recvcounts, displacements, MPI.DOUBLE], root=MASTER)

Check warning on line 315 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L313-L315

Added lines #L313 - L315 were not covered by tests

# **************************** Part IIc *****************************

# Iterative update of model-space M vector

if taskid == MASTER:

Check warning on line 321 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L321

Added line #L321 was not covered by tests

# logger.info("<< M-step >>")

# Sanity check: print C
# print('C')
# print(C)
# print(C.min(), C.max())
# print()

delta = C / Rj - 1
M = M + delta * M # Allows for optimization features presented in Siegert et al. 2020

Check warning on line 332 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L331-L332

Added lines #L331 - L332 were not covered by tests

# Sanity check: print M
# print('M')
# print(np.round(M, 5))
# print(np.round(M.max(), 5))

# Sanity check: print delta
# print('delta')
# print(delta)

# Pretty print - completion
print(f"Done")
print(linebreak_dashes)

Check warning on line 345 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L344-L345

Added lines #L344 - L345 were not covered by tests

# Save iteration
if save_results_flag == True:
results.append(register_result(iter, M, delta))

Check warning on line 349 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L348-L349

Added lines #L348 - L349 were not covered by tests

'''****************** End Iterative Segment ******************'''

Check warning on line 351 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L351

Added line #L351 was not covered by tests

# Print converged M
if taskid == MASTER:

Check warning on line 354 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L354

Added line #L354 was not covered by tests
# logger.info("<< Registering Result >>")
print('Converged M vector:')
print(np.round(M, 5))
print(np.round(M.max(), 5))
print(np.sum(M))
print()

Check warning on line 360 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L356-L360

Added lines #L356 - L360 were not covered by tests

if save_results_flag == True:
save_results(results)

Check warning on line 363 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L362-L363

Added lines #L362 - L363 were not covered by tests

# MAXITER
if iter == (MAXITER - 1):
print(f'Reached maximum iterations = {MAXITER}')
print(linebreak_stars)
print()

Check warning on line 369 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L366-L369

Added lines #L366 - L369 were not covered by tests

# MPI Shutdown
MPI.Finalize()

Check warning on line 372 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L372

Added line #L372 was not covered by tests

if __name__ == "__main__":
main()

Check warning on line 375 in cosipy/image_deconvolution/RLparallelscript.py

View check run for this annotation

Codecov / codecov/patch

cosipy/image_deconvolution/RLparallelscript.py#L374-L375

Added lines #L374 - L375 were not covered by tests
Loading