galmoss package

Subpackages

Submodules

galmoss.data module

class galmoss.data.DataSet(galaxy_index: List[str], image_path: List[str], sigma_path: List[str], psf_path: List[str] | None = None, mask_path: List[str] | None = None, image_hdu: int = 0, sigma_hdu: int = 0, psf_hdu: int = 0, mask_hdu: int = 0, mask_index: List[str] | None = None, mask_mode='bg', result_path=None, result_type='FITS', img_block_path=None, data_type: torch.dtype = torch.float32, device: str = 'cuda')

Bases: object

Load the images and Parameters in parallel

Parameters:

func – A function which returns the parameters need to be load in the optimism.

Return type:

A function that returns the scale-parameters need to be load in the optimism.

property data_size

To get the data size from the first galaxy’s image.

classmethod define_profiles(*args, **kwargs)

To define the profile class and set as an attribute in DataSet class.

param_matrix_from_(param_dict: dict, mode: str, item: int)

To concat the param value matrix following the param_dict, mode and item.

Parameters:
  • param_dict – The dictionary stores the keys (name of parameters) and values (the parameter objects). These parameters need to be cancated to a matrix that will be loaded into optimizer.

  • mode – The values of parameters will be called follow the mode in the parameter objects, e.g., updating_model.

  • item – The index of values, which will be given automaticly when the dataset is loaded in the data loader.

Returns:

The finial matrix, being transformed to the given data type.

Return type:

total_matrix

read_data(path: str, hdu: int)

To read the data from fits file.

sigma_pretreatment(sigma_datadata: torch.tensor)

To reset the illegal sigma values.

class galmoss.data.Profiles(**kwargs)

Bases: object

galmoss.fitting module

class galmoss.fitting.Fitting(dataset: ~galmoss.data.DataSet, batch_size: int, iteration: int, optimizer=<class 'torch_optimizer.diffgrad.DiffGrad'>, threshold=1e-05, early_stop=50)

Bases: FittingRepo, Uncertainty

This class owns functions that relates to fitting process.

Parameters:
  • dataset – The object instanced from DataSet class, used for Load the images and Parameters in parallel.

  • batch_size – The number of galaxies being fitted together in one batch. This number should as big as possible until the memory usage and GPU usage reach the limit.

  • iteration – The number of fitting iteration for the fitting process, the recommand number is 1,000 ~ 1,500.

  • optimizer – The type of optimizer. Galmoss supports any kind of optimizers in PyTorch. The defalt one is DiffGrad.

  • early_stop (threshold &) – The galaxy which (best_chi_mu - chi_mu) / best_chi_mu < threshold more than early_stop time will stop fitting. The defalt number is 10e-5 and 10.

add_convolution(to_conv_model)

Make the convolution to the input model image with the psf images.

Parameters:

to_conv_model – The images need to be convolved.

Return type:

The convolved images.

detect_update(fitting_metrics, chi_mu, iter)

Not every iteration in the optimization process results in a better outcome with a lower chi-square. Therefore, updates are made only in iterations that successfully reduce the chi-square, which are detected here in is_better.

improvement_small is galaxies their Δchi_mu is smaller than the thresholds. if early_stop number of iterations achive this statement continuously, this galaxy will be marked as early-stop galaxy. At present, only all the galaxies are early-stoped galaxy, the fitting process will finish.

Parameters:
  • fitting_metrics – A tuple includes stop_update_count, batched_eff_iter and batched_best_chi_mu. stop_update_count represents the number of iterations achive improvement_small statement continuously. batched_eff_iter represents how much iterations each galaxy used to achive the best value. batched_best_chi_mu represents the best chi_mu.

  • chi_mu – The chi_mu being calculated in this iteration.

  • iter – The value of this iteration.

Returns:

  • update – If True, continue fitting, otherwise break the loop.

  • fitting_metrics – The updated fitting_metrics.

fit()

The fitting process.

After setting the dataset mode in fitting, we load it into the data loader.

Then we make the grid for model generation.

The galaxies are loaded in batch using the data loader, and in each batch we initialize the fitting metrics, fixed parameter values and optim_list.

When each batch finishes fitting, their img_block will be saved directly. When the total fitting process is finished, the fitted parameter values will be saved in the end.

make_grid()

Generate the grid for fitting process.

make_optim_list(P_dict, P_value)

Make the dictionary for being load in the optimizer.

Parameters:
  • P_dict – The dictionary of parameter objects.

  • P_value – The total value matrix for the parameters inside P_dict.

Returns:

The optimizer list, which includes dictionaries for each parameters, which contains values and learning rate.

Return type:

optim_list

modeling(profile_dict, mode='updating_model')

Generate the total model image matrix.

Parameters:
  • profile_dict – The dictionary of profile objects.

  • mode – The string representing which value to choose from the parameters.

Returns:

The total model matrix, with shape (profile_num, galaxy_num, n, m). (n, m) is the data shape.

Return type:

model_matrix

optimizing(data, model, sigma)

Calculate the residual for this fitting iteration.

Parameters:
  • data – The galaxy image data in this batch.

  • model – The galaxy model data in this batch.

  • sigma – The galaxy sigma data in this batch.

Returns:

  • total_residual – The total residual in this fitting iteration, acts as float.

  • chi_mu – The residual (acts as chi_mu) for each galaxy.

revise_mask()

To generage the mask image follows various mode. If the fitting process already loads mask image, then the original mask image will be call from cls.data_cube. Otherwise, it will be a tensor filled with the scalar value 1, with the shape same as the galaxy data image (In practical, the calculation will multiply 1 instead). The original mask image will be used In the fitting mode.

In bootstrap mode, the original mask data will be resampled.

Returns:

  • mask – The mask image that will be used in chi-square calculations.

  • mask_num – The effective pixel number of mask image, which will be used in freedom degree calculations.

class galmoss.fitting.FittingRepo

Bases: object

This class owns functions that relates to fitting metrics initializing, updating and storaging in fitting process.

append_param_best_value()

Append the best fitted values of each parameters after the completion of each batch fitting process.

property chi_mu_numpy: ndarray

Turns the chi_mu data after fitting into numpy.

Returns:

The chi_mu values as a NumPy array.

Return type:

np.ndarray

fitting_metrics_append(batched_eff_iter, batched_best_chi_mu)

Append the fitting_metrics for each batch of galaxies after the completion of each batch fitting process.

Parameters:
  • batched_eff_iter – The iteration being used when the belonging galaxy stop fitting when reaching the updating shreshold in this batch.

  • batched_best_chi_mu – The best chi_mu value of belonging galaxy in this batch.

initialize_fitting_metrics()

Initial the matrix stores fitting metrics.

Returns:

  • stop_update_count – The number of galaxies that already stop fitting.

  • batched_eff_iter – The iteration being used when the belonging galaxy stop fitting when reaching the updating shreshold in this batch.

  • batched_best_chi_mu – The best chi_mu value of belonging galaxy in this batch.

load_constant(P_dict, P_value)

This function is for set the fixed parameters (constant) before every batch-size of training.

Parameters:
  • P_dict – The dictionary of fixed parameters.

  • P_value – The total value matrix stores the initial mapped value of such parameters.

refresh_variable(P_dict, optim_list)

This function is for refresh the stepped value inside optimizer to the parameters instance, in sequence of the parameter diction being given.

Parameters:
  • P_dict – The dictionary of variable parameters.

  • optim_list – The optim list stores the stepped value of such parameters.

save_image()

This method saves the img block as FITS file, which includes galaxy image, model image, residual image and the image of sub-components. Each sub-components will be convoluted either.

save_param()

Save the best fitted values of each parameters after the total fitting process ends. A dictionary will be used to store the key (“{}_{}_(varaible/fixed)”.format(profile_name, param_name)) and the corresponding fitted value.

If the mode is not fitting, this function will save the uncertainty together, with the key:

(“{}_{}_err”.format(profile_name, param_name))

and the corresponding uncertainty value.

After the dictionary is defined, the file will be saved follows the attributes result_type.

update_param_best_value(index)

Update the best value of each parameters during the optimization process.

Parameters:

index – Specifies the position in the best value matrix to be refreshed. If None, refreshes the entire matrix.

galmoss.uncertainty module

class galmoss.uncertainty.Bootstrap

Bases: object

Class for uncertainty calculation method Bootstrap.

bootstrap(n_resample: int = 100, iteration: int = 100, center_fix: bool = True)

Refit the resampled galaxies, as a way to calculating fitting uncertianties.

Parameters:
  • n_resample – The time of the resampling process. The suggest value is 100.

  • iteration – The maximum fitting iteration, the fitting process will be shut down when reaching this number.

  • center_fix – To decide whether set the fit statement of paramter (Center_x & Center_y) to False. The suggest value is True.

calculate_uncertainty(P_dict)

Calculate the fitting uncertainty via calculating the variance among the fitted value of parameters.

prepair_data(center_fix: bool)

Refitting in bootstrap process often meets challenge if the pixels near the galaxy center is not be resampled. This function is for changing the fix statement of center_x and center_y. If center_fix == True, the profile center will be fixed, which will help to make the refitting process more stable.

Parameters:

center_fix – Bool augument, to determin the center parameters will be fixed or not.

refitted_param_append(P_dict: dict, total: bool = False, sampling_idx: int = 999)

This function is for storing the parameters refitting value inside bootstrapping process. If total is True, which means the process is appending batch refitted value to total refitted value. If total is False, which means the process is appending total refitted value with the previous total refitted value (in principle each galaxy’s each parameters will have n_sampled values)

Parameters:
  • sampling_idx – The idx of sampling process, if zero, need to reset bsp_batch_best_value

  • P_dict – Dictionary of variable parameters

  • total – Determines to running which kind of appending process.

resample_mask(mask_data: torch.tensor)

this function is for resampling the pixels of galaxy image data.

Parameters:

mask_data – The mask data will be resampled.

Return type:

The resampled mask data.

class galmoss.uncertainty.CovarianceMatrix

Bases: object

Class for uncertainty calculation method CovarianceMatrix.

CM_uncertainty_append(batched_uncertainty: torch.tensor)

Append the uncertainty values for each batch of galaxies after the completion of each uncertainty calculation process.

Parameters:

batched_uncertainty – The uncertainty for each batch of galaxies.

calculate_uncertainty_from_J(jacobian_matrix: torch.tensor)

Use Jacobian matrix to appraxmate Hessian matrix, then use the diagnose of the inverse of Hessian matrix to calculate parameter fitting uncertainty.

Parameters:

jacobian_matrix – The corresponding jacobian matrix.

Returns:

The uncertainty calculated from Jacobian matrix.

Return type:

uncertainty

covar_mat(bs: int = 2)

Calculate the fitting uncertainty for each parameters.

Parameters:

bs – the batch-size for calculating covariance matrix. Cause the huge calculation cost in Jacobian matrix, the bs here should be much smaller than the fitting batch size.

jacobian(param: torch.Tensor) torch.Tensor

This function would not be used to calculate model value, only to be load in jacrevm for calculating the Jacobian matrix.

Parameters:
  • param – The fitted parameters after fitting process.

  • model – The model image calculated by fitted parameters.

reconstruct(J: torch.Tensor) [torch.Tensor, torch.Tensor]

To extract the effective value from weight matrix and Jacobian matrix based on the mask matrix, and resize to flat matrix.

Parameters:

J – The original Jacobian matrix.

Returns:

  • new_J – The effective and flat Jacobian matrix.

  • new_eyed_W – The eye matrix, with values equals flat weight matrix.

store_uncertainty(param_value)

This function is responsible for separating and storing the uncertainties associated with each fitting parameter in the corresponding parameter object. It iterates through each parameter involved in the fitting process, assigning the relevant uncertainty values from the provided tensor.

class galmoss.uncertainty.Uncertainty

Bases: Bootstrap, CovarianceMatrix

uncertainty(method='covar_mat', *args, **kwargs)

Start calculating fitting uncertainty follows the method, and resave the fitted value.

Module contents