Skip to content

Commit

Permalink
Merge pull request #83 from kateharborne/dev-noise_fix
Browse files Browse the repository at this point in the history
dev-noise_fix
  • Loading branch information
kateharborne committed Apr 26, 2023
2 parents 8eef4e6 + 8ab0f30 commit fb904cd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SimSpin
Type: Package
Title: SimSpin - A package for the kinematic analysis of galaxy simulations
Version: 2.4.6
Version: 2.4.7
Author: Katherine Harborne
Co-author: Alice Serene
Maintainer: <katherine.harborne@icrar.org>
Expand Down
7 changes: 4 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SimSpin v2.4.6 News
# SimSpin v2.4.7 News

### Last edit: 23/04/23
### Last edit: 26/04/23

Below is a table containing a summary of all changes made to SimSpin, since the date this file was created on 26/08/2021.

Expand All @@ -16,7 +16,8 @@ All changes are noted in the changelog table below.

| Date | Summary of change | Version | Commit | Author |
|---------- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |--------- |------------------------------------------ | ----------------- |
| 23/04/23 | Fixing the noise implementation in methods spectral and velocity. Adding inverse variance cubes to the output when a S/N value is specified. Incorporating tests to check proper incorporation and behaviour. | 2.4.6 | | Kate Harborne |
| 26/04/23 | Inverse variance cubes have large values that do not save to FITS successfully. Dividing by 1e40 to remove this issue and incorporating this with the units. Also modifying S/N description to give the MINIMUM S/N in the image, not the maximum. | 2.4.7 | Kate Harborne |
| 23/04/23 | Fixing the noise implementation in methods spectral and velocity. Adding inverse variance cubes to the output when a S/N value is specified. Incorporating tests to check proper incorporation and behaviour. | 2.4.6 | 8eef4e6dea8f9901b83ced8f2b43cee1438fe034 | Kate Harborne |
| 18/04/23 | Updating the methodology for method = "velocity" (details below) and fixing dependencies (resolving the Rfits and ProFit versions clash). Added header information to the SimSpin file made such that the whole file can be recreated using the information in the file rather than having to retain the code used to build the file. | 2.4.5 | cdf5522fb6040b7a5fdcf5ea4a54df7e576f57d5 | Kate Harborne |
| | 1. Modified the Gauss-Hermite fitting function to use the mean and standard deviation of the LOSVD as priors on the fit, but reporting the observed velocity and dispersion as those returned by the fit Gauss-Hermite along with h3 and h4. | | | |
| | 2. Added measure of the residual between the best fit Gauss-Hermite and the true LOSVD at the velocity scale of the observing telescope. This is now returned to the user as an image in `observed_images$residual`. | | | |
Expand Down
6 changes: 3 additions & 3 deletions R/build_datacube.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ build_datacube = function(simspin_file, telescope, observing_strategy,
output$variance_cube = noise_cube # initialising empty arrays

noise_cube = .add_noise(output$spectral_cube,
sqrt(max(raw_images$flux_image, na.rm=T))/
sqrt(min(raw_images$flux_image[raw_images$flux_image > 0], na.rm=T))/
(observation$signal_to_noise*sqrt(raw_images$flux_image)))
output$spectral_cube = output$spectral_cube + noise_cube
output$variance_cube = 1/(noise_cube)^2
Expand Down Expand Up @@ -369,7 +369,7 @@ build_datacube = function(simspin_file, telescope, observing_strategy,
output$variance_cube = noise_cube # initialising empty arrays

noise_cube = .add_noise(output$velocity_cube,
sqrt(max(raw_images$flux_image, na.rm=T))/
sqrt(min(raw_images$flux_image[raw_images$flux_image > 0], na.rm=T))/
(observation$signal_to_noise*sqrt(raw_images$flux_image)))
noise_image = output$observed_images$flux_image*(rowSums(noise_cube, dims=2)/rowSums(output$velocity_cube, dims=2))

Expand Down Expand Up @@ -476,7 +476,7 @@ build_datacube = function(simspin_file, telescope, observing_strategy,
output$variance_cube = noise_cube # initialising empty arrays

noise_cube = .add_noise(output$velocity_cube,
sqrt(max(raw_images$mass_image, na.rm=T))/
sqrt(min(raw_images$mass_image[raw_images$mass_image > 0], na.rm=T))/
(observation$signal_to_noise*sqrt(raw_images$mass_image)))
noise_image = output$observed_images$flux_image*(rowSums(noise_cube, dims=2)/rowSums(output$velocity_cube, dims=2))

Expand Down
20 changes: 11 additions & 9 deletions R/write_simspin_FITS.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ write_simspin_FITS = function(output_file, simspin_datacube, object_name,
"PI-COI"="PI-COI name.", "OBSERVER"="Name of observer.",
"REDSHIFT"="Observed redshift.",
"PIPEFILE"="Filename of data product",
"BUNIT"="Angstrom",
"BUNIT"="units of values in image or cube",
"ARCFILE"="Archive File Name",
"DATAMD5"="MD5 checksum",
"OBJECT"="Original target.")
Expand Down Expand Up @@ -156,7 +156,7 @@ write_simspin_FITS = function(output_file, simspin_datacube, object_name,
"HDUCLAS2"="this extension contains the data itself",
"ERRDATA"="pointer to the variance extension",
"OBJECT"="simulation and galaxy ID number",
"BUNIT"="Angstrom",
"BUNIT"="units of values in image or cube",
"CRPIX1"="Pixel coordinate of reference point",
"CRPIX2"="Pixel coordinate of reference point",
"CDELT1"="Coordinate transformation matrix element",
Expand Down Expand Up @@ -397,6 +397,7 @@ write_simspin_FITS = function(output_file, simspin_datacube, object_name,
if (!is.na(observation$signal_to_noise)){
# Adding variance cube to FITS file ----
data_keyvalues$EXTNAME = "STAT"
data_keyvalues$BUNIT = "1**40 (erg/s/cm**2)**-2"

if (split_save){
stat_summary_file_name = paste0(output_dir, "/", output_file_root, "_inv_variance_cube.FITS")
Expand All @@ -405,13 +406,13 @@ write_simspin_FITS = function(output_file, simspin_datacube, object_name,
keyvalues = header_keyvalues,
keycomments = header_keycomments, ext=1, create_file = T,
overwrite_file = TRUE)
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube,
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube/1e40,
filename = stat_summary_file_name, ext=2,
keyvalues = data_keyvalues, keycomments = data_keycomments,
create_ext = TRUE, create_file = FALSE, overwrite_file = FALSE)

} else {
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube,
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube/1e40,
filename = cube_file_name, ext=(max(extnum)+1),
keyvalues = data_keyvalues,
keycomments = data_keycomments,
Expand Down Expand Up @@ -581,21 +582,21 @@ write_simspin_FITS = function(output_file, simspin_datacube, object_name,
if (!is.na(observation$signal_to_noise)){
# Adding variance cube to FITS file ----
data_keyvalues$EXTNAME = "STAT"

data_keyvalues$BUNIT = "1**40 (erg/s/cm**2)**-2"
if (split_save){
stat_summary_file_name = paste0(output_dir, "/", output_file_root, "_inv_variance_cube.FITS")

Rfits::Rfits_write_header(filename = stat_summary_file_name,
keyvalues = header_keyvalues,
keycomments = header_keycomments, ext=1, create_file = T,
overwrite_file = TRUE)
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube,
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube/1e40,
filename = stat_summary_file_name, ext=2,
keyvalues = data_keyvalues, keycomments = data_keycomments,
create_ext = TRUE, create_file = FALSE, overwrite_file = FALSE)

} else {
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube,
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube/1e40,
filename = cube_file_name, ext=(max(extnum)+1),
keyvalues = data_keyvalues,
keycomments = data_keycomments,
Expand Down Expand Up @@ -738,6 +739,7 @@ write_simspin_FITS = function(output_file, simspin_datacube, object_name,
if (!is.na(observation$signal_to_noise)){
# Adding variance cube to FITS file ----
data_keyvalues$EXTNAME = "STAT"
data_keyvalues$BUNIT = "1**40 (erg/s/cm**2)**-2"

if (split_save){
stat_summary_file_name = paste0(output_dir, "/", output_file_root, "_inv_variance_cube.FITS")
Expand All @@ -746,13 +748,13 @@ write_simspin_FITS = function(output_file, simspin_datacube, object_name,
keyvalues = header_keyvalues,
keycomments = header_keycomments, ext=1, create_file = T,
overwrite_file = TRUE)
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube,
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube/1e40,
filename = stat_summary_file_name, ext=2,
keyvalues = data_keyvalues, keycomments = data_keycomments,
create_ext = TRUE, create_file = FALSE, overwrite_file = FALSE)

} else {
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube,
Rfits::Rfits_write_cube(data = simspin_datacube$variance_cube/1e40,
filename = cube_file_name, ext=(max(extnum)+1),
keyvalues = data_keyvalues,
keycomments = data_keycomments,
Expand Down
10 changes: 4 additions & 6 deletions tests/testthat/test_build_datacube.R
Original file line number Diff line number Diff line change
Expand Up @@ -1394,9 +1394,8 @@ test_that("Noise increases as expected in velocity cubes", {
observing_strategy = observing_strategy(dist_z = 0.03, inc_deg = 45, blur = T),
method = "velocity")

expect_true(sd(gadget_velocity_nonoise$observed_images$flux_image,na.rm = T) < sd(gadget_velocity_sn30$observed_images$flux_image,na.rm = T))
expect_true(sd(gadget_velocity_nonoise$observed_images$flux_image,na.rm = T) < sd(gadget_velocity_sn5$observed_images$flux_image,na.rm = T))
expect_true(sd(gadget_velocity_sn30$observed_images$flux_image,na.rm = T) < sd(gadget_velocity_sn5$observed_images$flux_image,na.rm = T))
expect_true(sd(gadget_velocity_sn30$observed_images$flux_image - gadget_velocity_nonoise$observed_images$flux_image, na.rm = T) <
sd(gadget_velocity_sn5$observed_images$flux_image - gadget_velocity_nonoise$observed_images$flux_image, na.rm = T))

})

Expand All @@ -1417,8 +1416,7 @@ test_that("Noise increases as expected in spectral cubes", {
observing_strategy = observing_strategy(dist_z = 0.03, inc_deg = 45, blur = T),
method = "spectral")

expect_true(sd(gadget_spectra_nonoise$spectral_cube[15,15,]) < sd(gadget_spectra_sn30$spectral_cube[15,15,]))
expect_true(sd(gadget_spectra_nonoise$spectral_cube[15,15,]) < sd(gadget_spectra_sn5$spectral_cube[15,15,]))
expect_true(sd(gadget_spectra_sn30$spectral_cube[15,15,]) < sd(gadget_spectra_sn5$spectral_cube[15,15,]))
expect_true(sd(gadget_spectra_sn30$spectral_cube[15,15,] - gadget_spectra_nonoise$spectral_cube[15,15,]) <
sd(gadget_spectra_sn5$spectral_cube[15,15,] - gadget_spectra_nonoise$spectral_cube[15,15,]))

})

0 comments on commit fb904cd

Please sign in to comment.