Skip to content

Commit

Permalink
address linting diff
Browse files Browse the repository at this point in the history
  • Loading branch information
20DM committed Oct 3, 2023
1 parent a133e6b commit a31e7e9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ jobs:

- name: Report results
run: |
git diff || (echo '## NOTE: your code is not linted properly - please commit the suggested changes'; false)
git diff --exit-code || (echo '## NOTE: your code is not linted properly - please commit the suggested changes'; false)
106 changes: 53 additions & 53 deletions cpp/purify/logging.enabled.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,64 @@ using spdlogPtr = std::shared_ptr<spdlog::logger>;

namespace purify::logging {

void set_level(std::string const &level, std::string const &name = "");
void set_level(std::string const &level, std::string const &name = "");

//! \brief Initializes a logger.
//! \details Logger only exists as long as return is kept alive.
inline spdlogPtr initialize(std::string const &name = "") {
const std::string loggerName = default_logger_name() + name;
const spdlogPtr result = spdlog::stdout_logger_mt(default_logger_name() + name);
if (!spdlog::get(loggerName)) spdlog::register_logger(result);
set_level(default_logging_level(), name);
return result;
}
//! \brief Initializes a logger.
//! \details Logger only exists as long as return is kept alive.
inline spdlogPtr initialize(std::string const &name = "") {
const std::string loggerName = default_logger_name() + name;
const spdlogPtr result = spdlog::stdout_logger_mt(default_logger_name() + name);
if (!spdlog::get(loggerName)) spdlog::register_logger(result);
set_level(default_logging_level(), name);
return result;
}

//! Returns shared pointer to logger or null if it does not exist
inline spdlogPtr get(std::string const &name = "") {
return spdlog::get(default_logger_name() + name);
}
//! Returns shared pointer to logger or null if it does not exist
inline spdlogPtr get(std::string const &name = "") {
return spdlog::get(default_logger_name() + name);
}

//! \brief Sets loggin level
//! \details Levels can be one of
//! - "trace"
//! - "debug"
//! - "info"
//! - "warn"
//! - "err"
//! - "critical"
//! - "off"
inline void set_level(std::string const &level, std::string const &name) {
const spdlogPtr logger = get(name);
if (not logger) throw std::runtime_error("No logger by the name of " + std::string(name));
#define PURIFY_MACRO(LEVEL) \
if (level == #LEVEL) logger->set_level(spdlog::level::LEVEL)
PURIFY_MACRO(trace);
else PURIFY_MACRO(debug);
else PURIFY_MACRO(info);
else PURIFY_MACRO(warn);
else PURIFY_MACRO(err);
else PURIFY_MACRO(critical);
else PURIFY_MACRO(off);
#undef PURIFY_MACRO
else throw std::runtime_error("Unknown logging level " + std::string(level));
}
//! \brief Sets logging level
//! \details Levels can be one of
//! - "trace"
//! - "debug"
//! - "info"
//! - "warn"
//! - "err"
//! - "critical"
//! - "off"
inline void set_level(std::string const &level, std::string const &name) {
const spdlogPtr logger = get(name);
if (not logger) throw std::runtime_error("No logger by the name of " + std::string(name));
#define PURIFY_MACRO(LEVEL) \
if (level == #LEVEL) logger->set_level(spdlog::level::LEVEL)
PURIFY_MACRO(trace);
else PURIFY_MACRO(debug);
else PURIFY_MACRO(info);
else PURIFY_MACRO(warn);
else PURIFY_MACRO(err);
else PURIFY_MACRO(critical);
else PURIFY_MACRO(off);
#undef PURIFY_MACRO
else throw std::runtime_error("Unknown logging level " + std::string(level));
}

inline bool has_level(std::string const &level, std::string const &name = "") {
const spdlogPtr logger = get(name);
if (not logger) return false;
inline bool has_level(std::string const &level, std::string const &name = "") {
const spdlogPtr logger = get(name);
if (not logger) return false;

#define PURIFY_MACRO(LEVEL) \
if (level == #LEVEL) return logger->level() >= spdlog::level::LEVEL
PURIFY_MACRO(trace);
else PURIFY_MACRO(debug);
else PURIFY_MACRO(info);
else PURIFY_MACRO(warn);
else PURIFY_MACRO(err);
else PURIFY_MACRO(critical);
else PURIFY_MACRO(off);
#undef PURIFY_MACRO
else throw std::runtime_error("Unknown logging level " + std::string(level));
}
#define PURIFY_MACRO(LEVEL) \
if (level == #LEVEL) return logger->level() >= spdlog::level::LEVEL
PURIFY_MACRO(trace);
else PURIFY_MACRO(debug);
else PURIFY_MACRO(info);
else PURIFY_MACRO(warn);
else PURIFY_MACRO(err);
else PURIFY_MACRO(critical);
else PURIFY_MACRO(off);
#undef PURIFY_MACRO
else throw std::runtime_error("Unknown logging level " + std::string(level));
}

} // namespace purify::logging

Expand Down
174 changes: 87 additions & 87 deletions cpp/purify/pfitsio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,94 @@

namespace purify::pfitsio {

void write_key(fitsfile *fptr, const std::string &key, const std::string &value,
const std::string &comment, int *status) {
std::string entry = value;
if (fits_write_key(fptr, TSTRING, const_cast<char *>(key.c_str()),
reinterpret_cast<void *>(const_cast<char *>(entry.c_str())),
const_cast<char *>(comment.c_str()), status))
throw std::runtime_error("Problem writing key in fits file: " + key);
}


void write_key(fitsfile *fptr, const std::string &key, const char *value,
const std::string &comment, int *status) {
write_key(fptr, key, std::string(value), comment, status);
}


void write_history(fitsfile *fptr, const std::string &context, const std::string &history,
int *status) {
const std::string entry = context + ": " + history;
if (fits_write_history(fptr, const_cast<char *>(entry.c_str()), status))
throw std::runtime_error("Problem writing comments in fits file");
}


//! Write image to fits file
void write2d(const Image<t_real> &eigen_image, const pfitsio::header_params &header,
const bool &overwrite) {
/*
Writes an image to a fits file.
image:: image data, a 2d Image.
header:: structure containing header information
overwrite:: if true, overwrites old fits file with same name
*/

write3d(std::vector<Image<t_real>>(1, eigen_image), header, overwrite);
}

void write2d(const Image<t_real> &eigen_image, const std::string &fits_name,
const std::string &pix_units, const bool &overwrite) {
/*
Writes an image to a fits file.
image:: image data, a 2d Image.
fits_name:: string containing the file name of the fits file.
pix_units:: units of flux
ra:: centre pixel coordinate in ra
dec:: centre pixel coordinate in dec
*/
pfitsio::header_params header;
header.fits_name = fits_name;
header.pix_units = pix_units;

write2d(eigen_image, header, overwrite);
}


void write3d(const std::vector<Image<t_real>> &eigen_images, const pfitsio::header_params &header,
const bool &overwrite) {
std::vector<long> naxes = {static_cast<long>(eigen_images.at(0).rows()),
static_cast<long>(eigen_images.at(0).cols()),
static_cast<long>(eigen_images.size()), 1};
std::vector<long> fpixel = {1, 1, 1, 1};

Vector<t_real> image = Vector<t_real>::Zero(naxes.at(0) * naxes.at(1) * naxes.at(2));
for (int i = 0; i < eigen_images.size(); i++)
image.segment(i * naxes.at(0) * naxes.at(1), naxes.at(0) * naxes.at(1)) =
Vector<t_real>::Map(eigen_images.at(i).data(), naxes.at(0) * naxes.at(1));
write3d<Vector<t_real>>(image, naxes.at(0), naxes.at(1), naxes.at(2), header, overwrite);
}

void write3d(const std::vector<Image<t_real>> &eigen_images, const std::string &fits_name,
const std::string &pix_units, const bool &overwrite) {
/*
Writes a vector of images to a fits file.
image:: image data, a 3d Image.
fits_name:: string containing the file name of the fits file.
pix_units:: units of flux
ra:: centre pixel coordinate in ra
dec:: centre pixel coordinate in dec
void write_key(fitsfile *fptr, const std::string &key, const std::string &value,
const std::string &comment, int *status) {
std::string entry = value;
if (fits_write_key(fptr, TSTRING, const_cast<char *>(key.c_str()),
reinterpret_cast<void *>(const_cast<char *>(entry.c_str())),
const_cast<char *>(comment.c_str()), status))
throw std::runtime_error("Problem writing key in fits file: " + key);
}


void write_key(fitsfile *fptr, const std::string &key, const char *value,
const std::string &comment, int *status) {
write_key(fptr, key, std::string(value), comment, status);
}


void write_history(fitsfile *fptr, const std::string &context, const std::string &history,
int *status) {
const std::string entry = context + ": " + history;
if (fits_write_history(fptr, const_cast<char *>(entry.c_str()), status))
throw std::runtime_error("Problem writing comments in fits file");
}


//! Write image to fits file
void write2d(const Image<t_real> &eigen_image, const pfitsio::header_params &header,
const bool &overwrite) {
/*
Writes an image to a fits file.
image:: image data, a 2d Image.
header:: structure containing header information
overwrite:: if true, overwrites old fits file with same name
*/

write3d(std::vector<Image<t_real>>(1, eigen_image), header, overwrite);
}

void write2d(const Image<t_real> &eigen_image, const std::string &fits_name,
const std::string &pix_units, const bool &overwrite) {
/*
Writes an image to a fits file.
image:: image data, a 2d Image.
fits_name:: string containing the file name of the fits file.
pix_units:: units of flux
ra:: centre pixel coordinate in ra
dec:: centre pixel coordinate in dec
*/
pfitsio::header_params header;
header.fits_name = fits_name;
header.pix_units = pix_units;
pfitsio::header_params header;
header.fits_name = fits_name;
header.pix_units = pix_units;

write2d(eigen_image, header, overwrite);
}


void write3d(const std::vector<Image<t_real>> &eigen_images, const pfitsio::header_params &header,
const bool &overwrite) {
std::vector<long> naxes = {static_cast<long>(eigen_images.at(0).rows()),
static_cast<long>(eigen_images.at(0).cols()),
static_cast<long>(eigen_images.size()), 1};
std::vector<long> fpixel = {1, 1, 1, 1};

write3d(eigen_images, header, overwrite);
}
Vector<t_real> image = Vector<t_real>::Zero(naxes.at(0) * naxes.at(1) * naxes.at(2));
for (int i = 0; i < eigen_images.size(); i++)
image.segment(i * naxes.at(0) * naxes.at(1), naxes.at(0) * naxes.at(1)) =
Vector<t_real>::Map(eigen_images.at(i).data(), naxes.at(0) * naxes.at(1));
write3d<Vector<t_real>>(image, naxes.at(0), naxes.at(1), naxes.at(2), header, overwrite);
}

void write3d(const std::vector<Image<t_real>> &eigen_images, const std::string &fits_name,
const std::string &pix_units, const bool &overwrite) {
/*
Writes a vector of images to a fits file.
image:: image data, a 3d Image.
fits_name:: string containing the file name of the fits file.
pix_units:: units of flux
ra:: centre pixel coordinate in ra
dec:: centre pixel coordinate in dec
*/
pfitsio::header_params header;
header.fits_name = fits_name;
header.pix_units = pix_units;

write3d(eigen_images, header, overwrite);
}

}
19 changes: 7 additions & 12 deletions cpp/purify/pfitsio.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct header_params {
};
//! create empty fits header
header_params(){};
#define PURIFY_MACRO(VAR, H2) (this->VAR == H2.VAR)
#define PURIFY_MACRO(VAR, H2) (this->VAR == H2.VAR)
bool operator==(const header_params &h2) const {
return PURIFY_MACRO(mean_frequency, h2) and PURIFY_MACRO(cell_x, h2) and
PURIFY_MACRO(cell_y, h2) and PURIFY_MACRO(ra, h2) and PURIFY_MACRO(dec, h2) and
Expand All @@ -70,7 +70,7 @@ struct header_params {
PURIFY_MACRO(relative_variation, h2) and PURIFY_MACRO(residual_convergence, h2) and
PURIFY_MACRO(epsilon, h2);
}
#undef PURIFY_MACRO
#undef PURIFY_MACRO
bool operator!=(const header_params &h2) const { return not(*this == h2); }
};

Expand Down Expand Up @@ -120,7 +120,6 @@ typename std::enable_if<std::is_scalar<T>::value, void>::type write_key(fitsfile
throw std::runtime_error("Problem writing key in fits file: " + key);
}


//! read fits key and return as tuple
template <typename T>
T read_key(fitsfile *fptr, const std::string& key, int *status) {
Expand All @@ -130,8 +129,7 @@ T read_key(fitsfile *fptr, const std::string& key, int *status) {
if (fits_read_key(fptr, TSTRING, key.data(), value.data(), comment, status)) {
throw std::runtime_error("Error reading value from key " + key);
}
}
else {
} else {
int datatype = 0;
if (std::is_same<T, double>::value)
datatype = TDOUBLE;
Expand All @@ -152,7 +150,6 @@ T read_key(fitsfile *fptr, const std::string& key, int *status) {
return value;
}


//! Write image to fits file using header information
void write2d(const Image<t_real> &image, const pfitsio::header_params &header,
const bool &overwrite = true);
Expand All @@ -161,7 +158,6 @@ void write2d(const Image<t_real> &image, const pfitsio::header_params &header,
void write2d(const Image<t_real> &image, const std::string &fits_name,
const std::string &pix_units = "Jy/Beam", const bool &overwrite = true);


template <typename DERIVED>
void write2d(const Eigen::EigenBase<DERIVED> &input, int nx, int ny, const std::string &fits_name,
const std::string &pix_units = "Jy/Beam", const bool &overwrite = true) {
Expand All @@ -183,7 +179,6 @@ void write3d(const std::vector<Image<t_real>> &image, const pfitsio::header_para
void write3d(const std::vector<Image<t_real>> &image, const std::string &fits_name,
const std::string &pix_units = "Jy/Beam", const bool &overwrite = true);


template <typename DERIVED>
void write3d(const std::vector<Eigen::EigenBase<DERIVED>> &input, int nx, int ny,
const std::string &fits_name, const std::string &pix_units = "Jy/Beam",
Expand Down Expand Up @@ -249,7 +244,7 @@ typename std::enable_if<std::is_same<t_real, typename T::Scalar>::value, void>::
const_cast<t_real *>(image.derived().data()), &status))
throw std::runtime_error("Problem writing image in fits file:" + header.fits_name);

#define PURIFY_MACRO(KEY, VALUE, COMMENT) write_key(fptr, KEY, VALUE, COMMENT, &status);
#define PURIFY_MACRO(KEY, VALUE, COMMENT) write_key(fptr, KEY, VALUE, COMMENT, &status);

// Writing to fits header
PURIFY_MACRO("BUNIT", header.pix_units, ""); // units
Expand Down Expand Up @@ -277,7 +272,7 @@ typename std::enable_if<std::is_same<t_real, typename T::Scalar>::value, void>::
PURIFY_MACRO("BTYPE", "intensity", "");
PURIFY_MACRO("EQUINOX", 2000, "");

#undef PURIFY_MACRO
#undef PURIFY_MACRO
write_history(fptr, "SOFTWARE", "Purify", &status);
write_history(fptr, "PURIFY-NITERS", header.niters, &status);
if (header.hasconverged) {
Expand All @@ -296,8 +291,8 @@ typename std::enable_if<std::is_same<t_real, typename T::Scalar>::value, void>::
}

template <typename T, typename = std::enable_if_t<std::is_same_v<double, typename T::Scalar>>>
void read3d(const std::string& fits_name, Eigen::EigenBase<T>& output,
int& rows, int& cols, int& channels, int& pols) {
void read3d(const std::string& fits_name, Eigen::EigenBase<T>& output, int& rows, int& cols,
int& channels, int& pols) {
/*
Reads in a cube from a fits file and returns the vector of images.
fits_name:: name of fits file
Expand Down

0 comments on commit a31e7e9

Please sign in to comment.