Commit a7abb93f97175910de90cd33deffd6eb9f3a51a9
1 parent
e724faa8
Autodetect file format from filename (#1863)
Showing
11 changed files
with
385 additions
and
139 deletions
.gitignore
ChangeLog
| 1 | -2017-04-05 | |
| 1 | +2017-04-10 | |
| 2 | 2 | |
| 3 | 3 | * Version 1.3.0 released |
| 4 | 4 | ========================= |
| 5 | 5 | |
| 6 | + Add GFilename::type() method (#1863) | |
| 6 | 7 | Copy shared Python libraries instead of creating a symbolic link (#1987) |
| 7 | 8 | Added FITS image conversion operators (#1986) |
| 8 | 9 | If code originates from Git, always compile the Python bindings (#1985) | ... | ... |
NEWS
| 1 | 1 | New Features and Important Changes in GammaLib 1.3.0.dev1 |
| 2 | 2 | |
| 3 | -5 April 2017 | |
| 3 | +10 April 2017 | |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | 1. Introduction |
| ... | ... | @@ -28,6 +28,7 @@ The following methods have been added: |
| 28 | 28 | - GCTAEdisp2D::fetch |
| 29 | 29 | - GEbounds::set |
| 30 | 30 | - GEnergies::set |
| 31 | +- GFilename::type() | |
| 31 | 32 | |
| 32 | 33 | The following methods have been removed: |
| 33 | 34 | - ... |
| ... | ... | @@ -117,7 +118,8 @@ The GWcs base class now also supports CD keywords instead of CDELT keywords |
| 117 | 118 | |
| 118 | 119 | 13. Support module |
| 119 | 120 | ------------------ |
| 120 | -None | |
| 121 | +A GFilename::type() method has been added that returns the type of the file | |
| 122 | +based on the file type extension (e.g. ".fits") (#1863). | |
| 121 | 123 | |
| 122 | 124 | |
| 123 | 125 | 14. XML module | ... | ... |
include/GFilename.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GFilename.hpp - Filename class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2015-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2015-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -62,8 +62,10 @@ |
| 62 | 62 | class GFilename : public GBase { |
| 63 | 63 | |
| 64 | 64 | // Friend functions |
| 65 | - friend std::string operator+(const GFilename& filename, const std::string& string); | |
| 66 | - friend std::string operator+(const std::string& string, const GFilename& filename); | |
| 65 | + friend std::string operator+(const GFilename& filename, | |
| 66 | + const std::string& string); | |
| 67 | + friend std::string operator+(const std::string& string, | |
| 68 | + const GFilename& filename); | |
| 67 | 69 | friend bool operator==(const GFilename &a, const GFilename &b); |
| 68 | 70 | friend bool operator!=(const GFilename &a, const GFilename &b); |
| 69 | 71 | |
| ... | ... | @@ -89,6 +91,7 @@ public: |
| 89 | 91 | std::string protocol(void) const; |
| 90 | 92 | std::string path(void) const; |
| 91 | 93 | std::string file(void) const; |
| 94 | + std::string type(void) const; | |
| 92 | 95 | bool exists(void) const; |
| 93 | 96 | bool is_fits(void) const; |
| 94 | 97 | void remove(void) const; | ... | ... |
include/GObservations.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GObservations.hpp - Observation container class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2009-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2009-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -158,13 +158,16 @@ public: |
| 158 | 158 | double npred(void) const; |
| 159 | 159 | GMatrixSparse hessian(const GOptimizerPars& pars); |
| 160 | 160 | GMatrixSparse covariance(void) const; |
| 161 | - void save(const GFilename& filename, bool csv = true) const; | |
| 161 | + void save(const GFilename& filename) const; | |
| 162 | 162 | |
| 163 | 163 | protected: |
| 164 | 164 | // Protected methods |
| 165 | - void init_members(void); | |
| 166 | - void copy_members(const likelihood& fct); | |
| 167 | - void free_members(void); | |
| 165 | + void init_members(void); | |
| 166 | + void copy_members(const likelihood& fct); | |
| 167 | + void free_members(void); | |
| 168 | + void save_csv(const GFilename& filename) const; | |
| 169 | + void save_fits(const GFilename& filename) const; | |
| 170 | + std::vector<std::string> covariance_names(void) const; | |
| 168 | 171 | |
| 169 | 172 | // Protected data members |
| 170 | 173 | double m_value; //!< Function value | ... | ... |
pyext/GFilename.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GFilename.i - Filename class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2015-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2015-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -54,6 +54,7 @@ public: |
| 54 | 54 | std::string protocol(void) const; |
| 55 | 55 | std::string path(void) const; |
| 56 | 56 | std::string file(void) const; |
| 57 | + std::string type(void) const; | |
| 57 | 58 | bool exists(void) const; |
| 58 | 59 | bool is_fits(void) const; |
| 59 | 60 | void remove(void) const; | ... | ... |
src/obs/GObservations_likelihood.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GObservations_likelihood.cpp - Likelihood function class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2009-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2009-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -621,88 +621,27 @@ GMatrixSparse GObservations::likelihood::covariance(void) const |
| 621 | 621 | |
| 622 | 622 | |
| 623 | 623 | /***********************************************************************//** |
| 624 | - * @brief Save likelihood fit results into CSV or FITS file. | |
| 624 | + * @brief Save likelihood fit results into a CSV or FITS file. | |
| 625 | 625 | * |
| 626 | - * @param[in] filename CSV/FITS filename. | |
| 627 | - * @param[in] csv save as CSV? (default: true) | |
| 626 | + * @param[in] filename CSV or FITS filename. | |
| 628 | 627 | * |
| 629 | - * Saves the likelihood fit results into a CSV/FITS file. For the moment the | |
| 630 | - * method only writes the covariance matrix. | |
| 628 | + * Saves the likelihood fit results into a CSV or FITS file. The result | |
| 629 | + * format depends on the filename extension. If the extension is `.fits` or | |
| 630 | + * `.fit` the file is written into a FITS file, otherwise it is written into | |
| 631 | + * a CSV file. | |
| 631 | 632 | ***************************************************************************/ |
| 632 | -void GObservations::likelihood::save(const GFilename& filename, bool csv) const | |
| 633 | +void GObservations::likelihood::save(const GFilename& filename) const | |
| 633 | 634 | { |
| 634 | - // Get covariance matrix | |
| 635 | - GMatrixSparse covmat = covariance(); | |
| 635 | + // Get file type | |
| 636 | + std::string filetype = filename.type(); | |
| 636 | 637 | |
| 637 | - // Create covariance matrix entry names | |
| 638 | - std::vector<std::string> covmat_entries; | |
| 639 | - for (int i = 0 ; i < m_this->m_models.size(); ++i) { | |
| 640 | - for (int j = 0; j < m_this->m_models[i]->size(); ++j) { | |
| 641 | - covmat_entries.push_back(m_this->m_models[i]->at(j).name() + "(" + | |
| 642 | - m_this->m_models[i]->name() + ")"); | |
| 643 | - } | |
| 638 | + // If file type is a FITS file then write save covariance matrix into a | |
| 639 | + // FITS file, otherwise save the covariance matrix into a CSV file | |
| 640 | + if (filetype == "fits") { | |
| 641 | + save_fits(filename); | |
| 644 | 642 | } |
| 645 | - | |
| 646 | - // Create binary table and columns | |
| 647 | - int size = covmat_entries.size(); | |
| 648 | - | |
| 649 | - if (csv) { | |
| 650 | - GCsv table(size + 1, size); | |
| 651 | - | |
| 652 | - // Fill CSV header | |
| 653 | - for (int i = 0; i < size; ++i) { | |
| 654 | - table.string(0, i, covmat_entries[i]); | |
| 655 | - } | |
| 656 | - | |
| 657 | - // Fill CSV table | |
| 658 | - for (int i = 0; i < size; ++i) { | |
| 659 | - for (int j = 0; j < size; j ++) { | |
| 660 | - table.real(i+1, j, covmat(i,j)); | |
| 661 | - } | |
| 662 | - } | |
| 663 | - | |
| 664 | - // Save CSV table | |
| 665 | - table.save(filename, " ", true); | |
| 666 | - } else { | |
| 667 | - | |
| 668 | - GFitsBinTable covmat_table; | |
| 669 | - GFitsTableStringCol par("Parameters", 1, 50, size); | |
| 670 | - GFitsTableDoubleCol cov("Covariance", 1, size*size); | |
| 671 | - | |
| 672 | - // Fill tables | |
| 673 | - int counter = 0; | |
| 674 | - for (int i = 0; i < size; ++i) { | |
| 675 | - par(0, i) = covmat_entries[i]; | |
| 676 | - for (int j = 0; j < size; ++j) { | |
| 677 | - cov(0, counter) = covmat(i,j); | |
| 678 | - ++counter; | |
| 679 | - } | |
| 680 | - } | |
| 681 | - | |
| 682 | - // Set dimension for covariance matrix column | |
| 683 | - std::vector<int> dim; | |
| 684 | - dim.push_back(size); | |
| 685 | - dim.push_back(size); | |
| 686 | - cov.dim(dim); | |
| 687 | - | |
| 688 | - // Append columns to table | |
| 689 | - covmat_table.append(par); | |
| 690 | - covmat_table.append(cov); | |
| 691 | - | |
| 692 | - // Set extension name | |
| 693 | - covmat_table.extname("Covariance Matrix"); | |
| 694 | - | |
| 695 | - // Allocate FITS object | |
| 696 | - GFits fits; | |
| 697 | - | |
| 698 | - // Append covariance matrix table to FITS object | |
| 699 | - fits.append(covmat_table); | |
| 700 | - | |
| 701 | - // Save FITS file to disk | |
| 702 | - fits.saveto(filename, true); | |
| 703 | - | |
| 704 | - // Close FITS object | |
| 705 | - fits.close(); | |
| 643 | + else { | |
| 644 | + save_csv(filename); | |
| 706 | 645 | } |
| 707 | 646 | |
| 708 | 647 | // Return |
| ... | ... | @@ -776,3 +715,134 @@ void GObservations::likelihood::free_members(void) |
| 776 | 715 | // Return |
| 777 | 716 | return; |
| 778 | 717 | } |
| 718 | + | |
| 719 | + | |
| 720 | +/***********************************************************************//** | |
| 721 | + * @brief Save likelihood fit results into CSV file. | |
| 722 | + * | |
| 723 | + * @param[in] filename CSV filename. | |
| 724 | + * | |
| 725 | + * Saves the likelihood fit results into a CSV file. For the moment the | |
| 726 | + * method only writes the covariance matrix. | |
| 727 | + ***************************************************************************/ | |
| 728 | +void GObservations::likelihood::save_csv(const GFilename& filename) const | |
| 729 | +{ | |
| 730 | + // Get covariance matrix | |
| 731 | + GMatrixSparse covmat = covariance(); | |
| 732 | + | |
| 733 | + // Get covariance matrix row and column names | |
| 734 | + std::vector<std::string> names = covariance_names(); | |
| 735 | + | |
| 736 | + // Create binary table and columns | |
| 737 | + int size = names.size(); | |
| 738 | + | |
| 739 | + // Create binary table and columns | |
| 740 | + GCsv table(size + 1, size); | |
| 741 | + | |
| 742 | + // Fill CSV header | |
| 743 | + for (int i = 0; i < size; ++i) { | |
| 744 | + table.string(0, i, names[i]); | |
| 745 | + } | |
| 746 | + | |
| 747 | + // Fill CSV table | |
| 748 | + for (int i = 0; i < size; ++i) { | |
| 749 | + for (int j = 0; j < size; j ++) { | |
| 750 | + table.real(i+1, j, covmat(i,j)); | |
| 751 | + } | |
| 752 | + } | |
| 753 | + | |
| 754 | + // Save CSV table | |
| 755 | + table.save(filename, " ", true); | |
| 756 | + | |
| 757 | + // Return | |
| 758 | + return; | |
| 759 | +} | |
| 760 | + | |
| 761 | + | |
| 762 | +/***********************************************************************//** | |
| 763 | + * @brief Save likelihood fit results into FITS file. | |
| 764 | + * | |
| 765 | + * @param[in] filename FITS filename. | |
| 766 | + * | |
| 767 | + * Saves the likelihood fit results into a FITS file. For the moment the | |
| 768 | + * method only writes the covariance matrix. | |
| 769 | + ***************************************************************************/ | |
| 770 | +void GObservations::likelihood::save_fits(const GFilename& filename) const | |
| 771 | +{ | |
| 772 | + // Get covariance matrix | |
| 773 | + GMatrixSparse covmat = covariance(); | |
| 774 | + | |
| 775 | + // Get covariance matrix row and column names | |
| 776 | + std::vector<std::string> names = covariance_names(); | |
| 777 | + | |
| 778 | + // Create binary table and columns | |
| 779 | + int size = names.size(); | |
| 780 | + | |
| 781 | + // Create binary table and columns | |
| 782 | + GFitsBinTable covmat_table; | |
| 783 | + GFitsTableStringCol par("Parameters", 1, 50, size); | |
| 784 | + GFitsTableDoubleCol cov("Covariance", 1, size*size); | |
| 785 | + | |
| 786 | + // Fill tables | |
| 787 | + int counter = 0; | |
| 788 | + for (int i = 0; i < size; ++i) { | |
| 789 | + par(0, i) = names[i]; | |
| 790 | + for (int j = 0; j < size; ++j) { | |
| 791 | + cov(0, counter) = covmat(i,j); | |
| 792 | + ++counter; | |
| 793 | + } | |
| 794 | + } | |
| 795 | + | |
| 796 | + // Set dimension for covariance matrix column | |
| 797 | + std::vector<int> dim; | |
| 798 | + dim.push_back(size); | |
| 799 | + dim.push_back(size); | |
| 800 | + cov.dim(dim); | |
| 801 | + | |
| 802 | + // Append columns to table | |
| 803 | + covmat_table.append(par); | |
| 804 | + covmat_table.append(cov); | |
| 805 | + | |
| 806 | + // Set extension name | |
| 807 | + covmat_table.extname("Covariance Matrix"); | |
| 808 | + | |
| 809 | + // Allocate FITS object | |
| 810 | + GFits fits; | |
| 811 | + | |
| 812 | + // Append covariance matrix table to FITS object | |
| 813 | + fits.append(covmat_table); | |
| 814 | + | |
| 815 | + // Save FITS file to disk | |
| 816 | + fits.saveto(filename, true); | |
| 817 | + | |
| 818 | + // Close FITS object | |
| 819 | + fits.close(); | |
| 820 | + | |
| 821 | + // Return | |
| 822 | + return; | |
| 823 | +} | |
| 824 | + | |
| 825 | + | |
| 826 | +/***********************************************************************//** | |
| 827 | + * @brief Return covariance matrix row and column names | |
| 828 | + * | |
| 829 | + * @return Covariance matrix row and column names. | |
| 830 | + * | |
| 831 | + * Returns the row and column names of the covariance matrix. | |
| 832 | + ***************************************************************************/ | |
| 833 | +std::vector<std::string> GObservations::likelihood::covariance_names(void) const | |
| 834 | +{ | |
| 835 | + // Initialise covariance matrix row and column names | |
| 836 | + std::vector<std::string> names; | |
| 837 | + | |
| 838 | + // Create covariance matrix row and column names | |
| 839 | + for (int i = 0 ; i < m_this->m_models.size(); ++i) { | |
| 840 | + for (int j = 0; j < m_this->m_models[i]->size(); ++j) { | |
| 841 | + names.push_back(m_this->m_models[i]->at(j).name() + "(" + | |
| 842 | + m_this->m_models[i]->name() + ")"); | |
| 843 | + } | |
| 844 | + } | |
| 845 | + | |
| 846 | + // Return covariance matrix row and column names | |
| 847 | + return names; | |
| 848 | +} | ... | ... |
src/support/GFilename.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GFilename.cpp - Filename class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2015-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2015-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -248,6 +248,61 @@ bool GFilename::exists(void) const |
| 248 | 248 | |
| 249 | 249 | |
| 250 | 250 | /***********************************************************************//** |
| 251 | + * @brief Return file type | |
| 252 | + * | |
| 253 | + * @return File type. | |
| 254 | + * | |
| 255 | + * Returns the file type based on the file extension name. Possible file | |
| 256 | + * types are | |
| 257 | + * | |
| 258 | + * Type Extension names | |
| 259 | + * fits .fits / .fit | |
| 260 | + * | |
| 261 | + * For any other file types an empty string will be returned. Any compression | |
| 262 | + * extensions such as `.gz`, `.Z`, `.z` or `.zip` will be stripped. | |
| 263 | + ***************************************************************************/ | |
| 264 | +std::string GFilename::type(void) const | |
| 265 | +{ | |
| 266 | + // Initialise empty file type | |
| 267 | + std::string type = ""; | |
| 268 | + | |
| 269 | + // Initialise type with the name of the file | |
| 270 | + std::string file = m_file; | |
| 271 | + | |
| 272 | + // Strip compression extensions | |
| 273 | + size_t start = file.rfind(".gz"); | |
| 274 | + if (start != std::string::npos) { | |
| 275 | + file = file.substr(0, start); | |
| 276 | + } | |
| 277 | + start = file.rfind(".Z"); | |
| 278 | + if (start != std::string::npos) { | |
| 279 | + file = file.substr(0, start); | |
| 280 | + } | |
| 281 | + start = file.rfind(".z"); | |
| 282 | + if (start != std::string::npos) { | |
| 283 | + file = file.substr(0, start); | |
| 284 | + } | |
| 285 | + start = file.rfind(".zip"); | |
| 286 | + if (start != std::string::npos) { | |
| 287 | + file = file.substr(0, start); | |
| 288 | + } | |
| 289 | + | |
| 290 | + // Extract file type | |
| 291 | + start = file.rfind(".fits"); | |
| 292 | + if (start != std::string::npos) { | |
| 293 | + type = "fits"; | |
| 294 | + } | |
| 295 | + start = file.rfind(".fit"); | |
| 296 | + if (start != std::string::npos) { | |
| 297 | + type = "fits"; | |
| 298 | + } | |
| 299 | + | |
| 300 | + // Return file type | |
| 301 | + return type; | |
| 302 | +} | |
| 303 | + | |
| 304 | + | |
| 305 | +/***********************************************************************//** | |
| 251 | 306 | * @brief Checks whether file is a FITS file |
| 252 | 307 | * |
| 253 | 308 | * @return True if file is a FITS file. | ... | ... |
test/test_GObservation.cpp
| ... | ... | @@ -55,14 +55,24 @@ void TestGObservation::set(void) |
| 55 | 55 | name("Observation module"); |
| 56 | 56 | |
| 57 | 57 | // Append tests |
| 58 | - append(static_cast<pfunction>(&TestGObservation::test_time_reference), "Test GTimeReference class"); | |
| 59 | - append(static_cast<pfunction>(&TestGObservation::test_time), "Test GTime class"); | |
| 60 | - append(static_cast<pfunction>(&TestGObservation::test_times), "Test GTimes class"); | |
| 61 | - append(static_cast<pfunction>(&TestGObservation::test_gti), "Test GGti class"); | |
| 62 | - append(static_cast<pfunction>(&TestGObservation::test_energy), "Test GEnergy class"); | |
| 63 | - append(static_cast<pfunction>(&TestGObservation::test_energies), "Test GEnergies class"); | |
| 64 | - append(static_cast<pfunction>(&TestGObservation::test_ebounds), "Test GEbounds class"); | |
| 65 | - append(static_cast<pfunction>(&TestGObservation::test_photons), "Test GPhotons class"); | |
| 58 | + append(static_cast<pfunction>(&TestGObservation::test_time_reference), | |
| 59 | + "Test GTimeReference class"); | |
| 60 | + append(static_cast<pfunction>(&TestGObservation::test_time), | |
| 61 | + "Test GTime class"); | |
| 62 | + append(static_cast<pfunction>(&TestGObservation::test_times), | |
| 63 | + "Test GTimes class"); | |
| 64 | + append(static_cast<pfunction>(&TestGObservation::test_gti), | |
| 65 | + "Test GGti class"); | |
| 66 | + append(static_cast<pfunction>(&TestGObservation::test_energy), | |
| 67 | + "Test GEnergy class"); | |
| 68 | + append(static_cast<pfunction>(&TestGObservation::test_energies), | |
| 69 | + "Test GEnergies class"); | |
| 70 | + append(static_cast<pfunction>(&TestGObservation::test_ebounds), | |
| 71 | + "Test GEbounds class"); | |
| 72 | + append(static_cast<pfunction>(&TestGObservation::test_photons), | |
| 73 | + "Test GPhotons class"); | |
| 74 | + append(static_cast<pfunction>(&TestGObservation::test_observations_optimizer), | |
| 75 | + "Test GObservations::likelihood class"); | |
| 66 | 76 | |
| 67 | 77 | // Return |
| 68 | 78 | return; |
| ... | ... | @@ -1262,6 +1272,112 @@ void TestGObservation::test_photons(void) |
| 1262 | 1272 | } |
| 1263 | 1273 | |
| 1264 | 1274 | |
| 1275 | +/***********************************************************************//** | |
| 1276 | + * @brief Test GObservations::likelihood | |
| 1277 | + ***************************************************************************/ | |
| 1278 | +void TestGObservation::test_observations_optimizer(void) | |
| 1279 | +{ | |
| 1280 | + // Create Test Model | |
| 1281 | + GTestModelData model; | |
| 1282 | + | |
| 1283 | + // Create Models conteners | |
| 1284 | + GModels models; | |
| 1285 | + models.append(model); | |
| 1286 | + | |
| 1287 | + // Time iterval | |
| 1288 | + GTime tmin(0.0); | |
| 1289 | + GTime tmax(1800.0); | |
| 1290 | + | |
| 1291 | + // Rate : events/sec | |
| 1292 | + double rate = RATE; | |
| 1293 | + | |
| 1294 | + // Create observations | |
| 1295 | + GObservations obs; | |
| 1296 | + | |
| 1297 | + // Add observations to container | |
| 1298 | + for (int i = 0; i < 6; ++i) { | |
| 1299 | + | |
| 1300 | + // Set random Generator | |
| 1301 | + GRan ran(i); | |
| 1302 | + | |
| 1303 | + // Create an event list | |
| 1304 | + GEvents *events = model.generateList(rate, tmin, tmax, ran); | |
| 1305 | + | |
| 1306 | + // Create a observation | |
| 1307 | + GTestObservation observation; | |
| 1308 | + observation.id(gammalib::str(i)); | |
| 1309 | + | |
| 1310 | + // Add events to the observation | |
| 1311 | + observation.events(*events); | |
| 1312 | + observation.ontime(tmax.secs()-tmin.secs()); | |
| 1313 | + | |
| 1314 | + // Append observation to container | |
| 1315 | + obs.append(observation); | |
| 1316 | + | |
| 1317 | + // Free the event list | |
| 1318 | + delete events; | |
| 1319 | + | |
| 1320 | + } // endfor: added observations to container | |
| 1321 | + | |
| 1322 | + // Add the model to the observation | |
| 1323 | + obs.models(models); | |
| 1324 | + | |
| 1325 | + // Create a GLog for show the interations of optimizer. | |
| 1326 | + GLog log; | |
| 1327 | + | |
| 1328 | + // Create an optimizer. | |
| 1329 | + GOptimizerLM opt(&log); | |
| 1330 | + | |
| 1331 | + // Set number of stalls | |
| 1332 | + opt.max_stalls(50); | |
| 1333 | + | |
| 1334 | + // Optimize | |
| 1335 | + obs.optimize(opt); | |
| 1336 | + | |
| 1337 | + // Compute errors | |
| 1338 | + obs.errors(opt); | |
| 1339 | + | |
| 1340 | + // Get the result | |
| 1341 | + GModelPar result = (*(obs.models()[0]))[0]; | |
| 1342 | + | |
| 1343 | + // Check if converged | |
| 1344 | + test_value(opt.status(), 0, "Check if converged", | |
| 1345 | + "Optimizer did not converge"); | |
| 1346 | + | |
| 1347 | + // Check if value is correct | |
| 1348 | + test_value(result.factor_value(), RATE, result.factor_error()*3.0); | |
| 1349 | + | |
| 1350 | + // Save covariance matrix as FITS file | |
| 1351 | + obs.function().save("test_observations_optimizer.fits"); | |
| 1352 | + | |
| 1353 | + // Try loading covariance matrix as FITS file | |
| 1354 | + test_try("Load covariance matrix as FITS file"); | |
| 1355 | + try { | |
| 1356 | + GFits file("test_observations_optimizer.fits"); | |
| 1357 | + test_try_success(); | |
| 1358 | + } | |
| 1359 | + catch (std::exception &e) { | |
| 1360 | + test_try_failure(e); | |
| 1361 | + } | |
| 1362 | + | |
| 1363 | + // Save covariance matrix as CSV file | |
| 1364 | + obs.function().save("test_observations_optimizer.csv"); | |
| 1365 | + | |
| 1366 | + // Try loading covariance matrix as CSV file | |
| 1367 | + test_try("Load covariance matrix as CSV file"); | |
| 1368 | + try { | |
| 1369 | + GCsv file("test_observations_optimizer.csv"); | |
| 1370 | + test_try_success(); | |
| 1371 | + } | |
| 1372 | + catch (std::exception &e) { | |
| 1373 | + test_try_failure(e); | |
| 1374 | + } | |
| 1375 | + | |
| 1376 | + // Return | |
| 1377 | + return; | |
| 1378 | +} | |
| 1379 | + | |
| 1380 | + | |
| 1265 | 1381 | #ifdef _OPENMP |
| 1266 | 1382 | /***********************************************************************//** |
| 1267 | 1383 | * @brief Set tests |
| ... | ... | @@ -1363,6 +1479,7 @@ void TestOpenMP::test_observations_optimizer(const int& mode) |
| 1363 | 1479 | // Create an optimizer. |
| 1364 | 1480 | GOptimizerLM opt(&log); |
| 1365 | 1481 | |
| 1482 | + // Set number of stalls | |
| 1366 | 1483 | opt.max_stalls(50); |
| 1367 | 1484 | |
| 1368 | 1485 | // Optimize | ... | ... |
test/test_GObservation.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * test_GObservation.hpp - Test observation module * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2012-2014 by Jean-Baptiste Cayrou * | |
| 4 | + * copyright (C) 2012-2017 by Jean-Baptiste Cayrou * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -55,6 +55,7 @@ public: |
| 55 | 55 | void test_times(void); |
| 56 | 56 | void test_energy(void); |
| 57 | 57 | void test_energies(void); |
| 58 | + void test_observations_optimizer(void); | |
| 58 | 59 | }; |
| 59 | 60 | |
| 60 | 61 | ... | ... |
test/test_GSupport.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * test_GSupport.cpp - test support module * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2016 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2010-2017 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -710,31 +710,21 @@ void TestGSupport::test_filename(void) |
| 710 | 710 | |
| 711 | 711 | // Set filename without extension |
| 712 | 712 | GFilename filename = "myfile.fits"; |
| 713 | - test_assert(filename.url() == "myfile.fits", | |
| 714 | - "Expected \"myfile.fits\" filename, " | |
| 715 | - "found \""+filename.url()+"\""); | |
| 713 | + test_value(filename.url(), "myfile.fits"); | |
| 716 | 714 | test_assert(!filename.has_extname(), |
| 717 | 715 | "Expected that extension name is not set."); |
| 718 | 716 | |
| 719 | 717 | // Set filename with extension name |
| 720 | 718 | filename = "myfile.fits[EVENTS]"; |
| 721 | - test_assert(filename.url() == "myfile.fits", | |
| 722 | - "Expected \"myfile.fits\" filename, " | |
| 723 | - "found \""+filename.url()+"\""); | |
| 724 | - test_assert(filename.extname() == "EVENTS", | |
| 725 | - "Expected \"EVENTS\" extension name, " | |
| 726 | - "found \""+filename.extname()+"\""); | |
| 719 | + test_value(filename.url(), "myfile.fits"); | |
| 720 | + test_value(filename.extname(), "EVENTS"); | |
| 727 | 721 | test_assert(filename.has_extname(), |
| 728 | 722 | "Expected that extension name is set."); |
| 729 | 723 | |
| 730 | 724 | // Set filename with extension name and version |
| 731 | 725 | filename = "myfile.fits[EVENTS,2]"; |
| 732 | - test_assert(filename.url() == "myfile.fits", | |
| 733 | - "Expected \"myfile.fits\" filename, " | |
| 734 | - "found \""+filename.url()+"\""); | |
| 735 | - test_assert(filename.extname() == "EVENTS", | |
| 736 | - "Expected \"EVENTS\" extension name, " | |
| 737 | - "found \""+filename.extname()+"\""); | |
| 726 | + test_value(filename.url(), "myfile.fits"); | |
| 727 | + test_value(filename.extname(), "EVENTS"); | |
| 738 | 728 | test_assert(filename.has_extname(), |
| 739 | 729 | "Expected that extension name is set."); |
| 740 | 730 | test_value(filename.extver(), 2); |
| ... | ... | @@ -743,21 +733,15 @@ void TestGSupport::test_filename(void) |
| 743 | 733 | |
| 744 | 734 | // Set filename with lower case extension name |
| 745 | 735 | filename = "myfile.fits[events]"; |
| 746 | - test_assert(filename.extname() == "EVENTS", | |
| 747 | - "Expected \"EVENTS\" extension name, " | |
| 748 | - "found \""+filename.extname()+"\""); | |
| 736 | + test_value(filename.extname(), "EVENTS"); | |
| 749 | 737 | |
| 750 | 738 | // Set filename with number in extension name |
| 751 | 739 | filename = "myfile.fits[EVENTS_2D]"; |
| 752 | - test_assert(filename.extname() == "EVENTS_2D", | |
| 753 | - "Expected \"EVENTS_2D\" extension name, " | |
| 754 | - "found \""+filename.extname()+"\""); | |
| 740 | + test_value(filename.extname(), "EVENTS_2D"); | |
| 755 | 741 | |
| 756 | 742 | // Set filename with extension number and version |
| 757 | 743 | filename = "myfile.fits[1,2]"; |
| 758 | - test_assert(filename.url() == "myfile.fits", | |
| 759 | - "Expected \"myfile.fits\" filename, " | |
| 760 | - "found \""+filename.url()+"\""); | |
| 744 | + test_value(filename.url(), "myfile.fits"); | |
| 761 | 745 | test_value(filename.extno(), 1); |
| 762 | 746 | test_assert(filename.has_extno(), |
| 763 | 747 | "Expected that extension number is set."); |
| ... | ... | @@ -767,15 +751,9 @@ void TestGSupport::test_filename(void) |
| 767 | 751 | |
| 768 | 752 | // Set filename with extension name and expression |
| 769 | 753 | filename = "myfile.fits[EVENTS][ENERGY>0.1]"; |
| 770 | - test_assert(filename.url() == "myfile.fits", | |
| 771 | - "Expected \"myfile.fits\" filename, " | |
| 772 | - "found \""+filename.url()+"\""); | |
| 773 | - test_assert(filename.extname() == "EVENTS", | |
| 774 | - "Expected \"EVENTS\" extension name, " | |
| 775 | - "found \""+filename.extname()+"\""); | |
| 776 | - test_assert(filename.expression() == "ENERGY>0.1", | |
| 777 | - "Expected \"EVENTS>0.1\" expression, " | |
| 778 | - "found \""+filename.expression()+"\""); | |
| 754 | + test_value(filename.url(), "myfile.fits"); | |
| 755 | + test_value(filename.extname(), "EVENTS"); | |
| 756 | + test_value(filename.expression(), "ENERGY>0.1"); | |
| 779 | 757 | |
| 780 | 758 | // Test missing closing symbol |
| 781 | 759 | test_try("Missing ] symbol"); |
| ... | ... | @@ -854,13 +832,9 @@ void TestGSupport::test_filename(void) |
| 854 | 832 | |
| 855 | 833 | // Test default extension name |
| 856 | 834 | filename = "myfile.fits"; |
| 857 | - test_assert(filename.extname("EVENTS") == "EVENTS", | |
| 858 | - "Expected \"EVENTS\" extension name, " | |
| 859 | - "found \""+filename.extname()+"\""); | |
| 835 | + test_value(filename.extname("EVENTS"), "EVENTS"); | |
| 860 | 836 | filename = "myfile.fits[LUCKY]"; |
| 861 | - test_assert(filename.extname("EVENTS") == "LUCKY", | |
| 862 | - "Expected \"LUCKY\" extension name, " | |
| 863 | - "found \""+filename.extname()+"\""); | |
| 837 | + test_value(filename.extname("EVENTS"), "LUCKY"); | |
| 864 | 838 | |
| 865 | 839 | // Test default extension number |
| 866 | 840 | filename = "myfile.fits"; |
| ... | ... | @@ -874,6 +848,24 @@ void TestGSupport::test_filename(void) |
| 874 | 848 | filename = "myfile.fits[3,3]"; |
| 875 | 849 | test_value(filename.extver(1), 3); |
| 876 | 850 | |
| 851 | + // Test type method | |
| 852 | + filename = "myfile.fits"; | |
| 853 | + test_value(filename.type(), "fits"); | |
| 854 | + filename = "myfile.fit"; | |
| 855 | + test_value(filename.type(), "fits"); | |
| 856 | + filename = "myfile.fits.gz"; | |
| 857 | + test_value(filename.type(), "fits"); | |
| 858 | + filename = "myfile.fits.Z"; | |
| 859 | + test_value(filename.type(), "fits"); | |
| 860 | + filename = "myfile.fits.z"; | |
| 861 | + test_value(filename.type(), "fits"); | |
| 862 | + filename = "myfile.fits.zip"; | |
| 863 | + test_value(filename.type(), "fits"); | |
| 864 | + filename = "myfile.csv"; | |
| 865 | + test_value(filename.type(), ""); | |
| 866 | + filename = "myfile.csv.gz"; | |
| 867 | + test_value(filename.type(), ""); | |
| 868 | + | |
| 877 | 869 | // Return |
| 878 | 870 | return; |
| 879 | 871 | } | ... | ... |