Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:14

0001 // ./NPFold_test.sh
0002 
0003 #include "NPFold.h"
0004 
0005 void test_0()
0006 {
0007     NP* a = NP::Make<float>(1,4,4) ; 
0008     a->fillIndexFlat(); 
0009 
0010     NP* b = NP::Make<float>(1,4,4) ; 
0011     b->fillIndexFlat(); 
0012 
0013     NPFold nf0 ; 
0014     nf0.add("a.npy", a ); 
0015     nf0.add("some/relative/path/a.npy", a ); 
0016     nf0.add("b.npy", b ); 
0017 
0018     std::cout << "nf0" << std::endl << nf0.desc()  ; 
0019 
0020     const char* base = "/tmp/NPFold_test/base" ; 
0021     nf0.save(base); 
0022 
0023     NPFold* nf1 = NPFold::Load(base); 
0024 
0025     std::cout << "nf1" << std::endl << nf1->desc()  ; 
0026 
0027     int cf = NPFold::Compare(&nf0, nf1);
0028     if( cf != 0 ) std::cout << NPFold::DescCompare( &nf0, nf1) ; 
0029     assert( cf == 0 ); 
0030 }
0031 
0032 void test_add_without_ext()
0033 {
0034     NP* a = NP::Make<float>(1,4,4) ; 
0035     a->fillIndexFlat(); 
0036 
0037     NP* b = NP::Make<float>(1,4,4) ; 
0038     b->fillIndexFlat(); 
0039 
0040     NPFold nf0 ; 
0041     nf0.add("a", a ); 
0042     nf0.add("b", b ); 
0043     std::cout << "nf0" << std::endl << nf0.desc()  ; 
0044 
0045     const char* base = "/tmp/NPFold_test/test_add_without_ext" ; 
0046     nf0.save(base); 
0047 
0048     NPFold* nf1 = NPFold::Load(base); 
0049 
0050     std::cout << "nf1" << std::endl << nf1->desc()  ; 
0051     int cf = NPFold::Compare(&nf0, nf1 ); 
0052     if( cf != 0 ) std::cout << NPFold::DescCompare( &nf0, nf1) ; 
0053     assert( cf == 0 ); 
0054 }
0055 
0056 NPFold* make_NPFold(const char* opt)
0057 {
0058     NPFold* nf = new NPFold ; 
0059     if( strchr( opt, 'a') )
0060     {
0061         NP* a = NP::Make<float>(1,4,4) ; 
0062         a->fillIndexFlat(); 
0063         nf->add("a", a); 
0064     }
0065     if( strchr( opt, 'b') )
0066     {
0067         NP* b = NP::Make<float>(2,4,4) ; 
0068         b->fillIndexFlat(); 
0069         nf->add("b", b); 
0070     }
0071     if( strchr( opt, 'c') )
0072     {
0073         NP* c = NP::Make<float>(3,4,4) ; 
0074         c->fillIndexFlat(); 
0075         nf->add("c", c);
0076     } 
0077     return nf ; 
0078 }
0079 
0080 /**
0081 
0082     nf0
0083        a.npy
0084        b.npy
0085        c.npy
0086        nf1 
0087            a.npy
0088            b.npy 
0089            nf2
0090                b.npy
0091                c.npy 
0092 
0093 **/
0094 
0095 NPFold* make_compound()
0096 {
0097     NPFold* nf0 = make_NPFold("abc"); 
0098 
0099         NPFold* nf1 = make_NPFold("ab"); 
0100         nf0->add_subfold( "nf1", nf1 ); 
0101 
0102             NPFold* nf2 = make_NPFold("bc"); 
0103             NPFold* nf3 = make_NPFold("ac"); 
0104             NPFold* nf4 = make_NPFold("b"); 
0105             NPFold* nf5 = make_NPFold("a"); 
0106 
0107             nf1->add_subfold("nf2", nf2 ); 
0108             nf1->add_subfold("nf3", nf3 ); 
0109             nf1->add_subfold("nf4", nf4 ); 
0110             {
0111                 NPFold* nf6 = make_NPFold("ac") ;
0112                 nf4->add_subfold("nf6", nf6 ); 
0113             }
0114             nf1->add_subfold("nf5", nf5 ); 
0115 
0116     return nf0 ; 
0117 }
0118 
0119 
0120 void test_subfold_save_load()
0121 {
0122     const char* base = "/tmp/NPFold_test/test_subfold" ; 
0123 
0124     NPFold* nf0 = make_compound(); 
0125     std::cout << "nf0" << std::endl << nf0->desc()  ; 
0126     nf0->save(base); 
0127 
0128     NPFold* nfl = NPFold::Load(base); 
0129     std::cout << "nfl" << std::endl << nfl->desc()  ; 
0130 
0131     NPFold* nfl_nf1 = nfl->get_subfold("nf1"); 
0132     std::cout << "nfl_nf1" << std::endl << nfl_nf1->desc()  ; 
0133     NPFold* nfl_nf1_nf2 = nfl_nf1->get_subfold("nf2"); 
0134 
0135     std::cout << "nfl_nf1_nf2" << std::endl << nfl_nf1_nf2->desc()  ; 
0136 }
0137 
0138 void test_desc_subfold()
0139 {
0140     NPFold* nf0 = make_compound(); 
0141     std::cout << "[nf0.desc_subfold\n " << nf0->desc_subfold("nf0") << "]" << std::endl ; 
0142 }
0143 void test_find_subfold_0()
0144 {
0145     NPFold* nf0 = make_compound(); 
0146     const NPFold* nf4 = nf0->find_subfold("nf1/nf4"); 
0147     std::cout << "[nf0->find_subfold('nf1/nf4')\n " << ( nf4 ? nf4->desc() : "-" ) << "]" << std::endl ; 
0148 }
0149 void test_find_subfold_1()
0150 {
0151     NPFold* nf0 = make_compound(); 
0152     const NPFold* nf6 = nf0->find_subfold("nf1/nf4/nf6"); 
0153     std::cout << "[nf0->find_subfold('nf1/nf4/nf6')\n " << ( nf6 ? nf6->desc() : "-" ) << "]" << std::endl ; 
0154 }
0155 
0156 
0157 void test_add_same_key()
0158 {
0159     NPFold nf0 ; 
0160 
0161     nf0.add("a", NP::MakeFlat<float>(1,4,4) ); 
0162     nf0.add("a", NP::MakeFlat<float>(2,4,4) ); 
0163     nf0.add("a", NP::MakeFlat<float>(3,4,4) ); 
0164     nf0.add("a", NP::MakeFlat<float>(4,4,4) ); 
0165 
0166     std::cout << "nf0.desc" << std::endl << nf0.desc()  ; 
0167     std::cout << "nf0.get_num(\"a\") " << nf0.get_num("a") << " (get_num finds the first matching key) " <<  std::endl ; 
0168 }
0169 
0170 void test_add_same_key_clear()
0171 {
0172     NPFold nf0 ; 
0173 
0174     nf0.add("a", NP::MakeFlat<float>(1,4,4) ); 
0175     nf0.clear(); 
0176 
0177     nf0.add("a", NP::MakeFlat<float>(2,4,4) ); 
0178     nf0.clear(); 
0179 
0180     nf0.add("a", NP::MakeFlat<float>(3,4,4) ); 
0181     nf0.clear(); 
0182 
0183     nf0.add("a", NP::MakeFlat<float>(4,4,4) ); 
0184 
0185     std::cout << "nf0.desc" << std::endl << nf0.desc()  ; 
0186     std::cout << "nf0.get_num(\"a\") " << nf0.get_num("a") << " (get_num finds the first matching key) " <<  std::endl ; 
0187 }
0188 
0189 
0190 
0191 
0192 void test_set_same_key()
0193 {
0194     NPFold nf0 ; 
0195 
0196     nf0.set("a", NP::MakeFlat<float>(1,4,4) ); 
0197     nf0.set("a", NP::MakeFlat<float>(2,4,4) ); 
0198     nf0.set("a", NP::MakeFlat<float>(3,4,4) ); 
0199     nf0.set("a", NP::MakeFlat<float>(4,4,4) ); 
0200 
0201     std::cout << "nf0.desc" << std::endl << nf0.desc()  ; 
0202     std::cout << "nf0.get_num(\"a\") " << nf0.get_num("a") << " (get_num finds the first matching key) " <<  std::endl ; 
0203 }
0204 
0205 
0206 void test_recursive_txt_load(const char* name)
0207 {
0208     const char* base = U::Path(getenv("NP_PROP_BASE"), name) ; 
0209 
0210     NPFold* fold = NPFold::Load(base) ; 
0211 
0212     std::cout << fold->desc() << std::endl ;  
0213 
0214     const char* tmpd = U::Path("/tmp", name ) ; 
0215     fold->save(tmpd); 
0216 }
0217 
0218 void test_accessors()
0219 {
0220     NPFold* pmt = NPFold::LoadProp("PMTProperty") ; 
0221 
0222     // look for an array in all the PMTProperty subfold 
0223     const char* name = "THICKNESS" ; 
0224 
0225     unsigned num = pmt->get_num_subfold() ; 
0226     for(unsigned idx=0 ; idx < num ; idx++)
0227     {
0228         const char* key = pmt->get_subfold_key(idx);
0229         const NP* a     = pmt->find_array(key, name) ; 
0230         if( a == nullptr ) continue ; 
0231 
0232         std::cout << " key " << key << " name " << name << std::endl ;  
0233         std::cout << " a.lpath " << a->get_lpath() << std::endl ;
0234         std::cout << " a.sstr " << ( a ? a->sstr() : "-" ) << std::endl ; 
0235         std::string units = a->get_meta<std::string>("units", "") ; 
0236         std::cout << " units " << units << std::endl;
0237 
0238         std::vector<std::string> qtys = {"ARC_THICKNESS", "PHC_THICKNESS" } ; 
0239         for(unsigned q=0 ; q < qtys.size() ; q++)
0240         {
0241             const char* qty = qtys[q].c_str(); 
0242             double d = a->get_named_value<double>(qty, -1 );  
0243             std::cout << " qty " << qty << " d " << std::scientific << d << std::endl ;
0244         }
0245         std::cout << std::endl << std::endl ; 
0246     }
0247 }
0248 
0249 void test_clear_partial()
0250 {
0251     NPFold nf0 ; 
0252 
0253     nf0.add("a", NP::MakeFlat<float>(1,4,4) ); 
0254     nf0.add("b", NP::MakeFlat<float>(2,4,4) ); 
0255     nf0.add("c", NP::MakeFlat<float>(3,4,4) ); 
0256     nf0.add("d", NP::MakeFlat<float>(4,4,4) ); 
0257 
0258     std::cout << nf0.desc() << std::endl ;  
0259 
0260     nf0.clear_partial("c,a"); 
0261 
0262     std::cout << "after clear_partial c,a " << std::endl << nf0.desc() << std::endl ;  
0263 
0264 
0265 }
0266 
0267 
0268 int main()
0269 {
0270     /*
0271     test_0();
0272     test_add_without_ext();
0273     test_subfold_save_load(); 
0274     test_desc_subfold(); 
0275     test_find_subfold_0(); 
0276     test_find_subfold_1(); 
0277 
0278     test_add_same_key(); 
0279     test_set_same_key(); 
0280     test_add_same_key_clear(); 
0281 
0282     test_recursive_txt_load("Material"); 
0283     test_recursive_txt_load("PMTProperty"); 
0284     test_accessors(); 
0285     */
0286     test_clear_partial(); 
0287 
0288 
0289 
0290     return 0 ; 
0291 }