text: # coding: utf-8
  
  from __future__ import absolute_import, print_function
  
  """ Test ThereminModel Solver """
  
  __author__ = "Andy Casey <arc@ast.cam.ac.uk>"
  
  import logging
  from glob import glob
  
  import theremin
  
  logger = logging.getLogger("oracle")
  
  #[5954.0, 4.6630000000000003, -0.30499999999999999, 1.222] is [-0.00093788 -0.00217835  0.00108488  0.00045862]
  
  config = {
      "model": {
          "continuum": True,
          "redshift": False,
          "instrumental_broadening": True
      },
      "continuum": {
          "method": "polynomial",
          "order": [3, 3, 3, 3]
      },
      "ThereminModel": {
          "clean_line_list_filename": "../si/linedata/HERMES_clean.dat",
          "blend_line_list_filename": "../si/linedata/HERMES_blending.dat",
          "balance_mode": 2,
          "line_constraints": [
              "equivalent_width > 5", # mA 
              "150 >= equivalent_width", # mA
              "warnflag == 0",
              # Avoid the Balmer lines
              "np.abs(wavelength - 6563) > 100",
              "np.abs(wavelength - 4861) > 100"
          ]
      },
      "priors": {
          "instrumental_resolution": "uniform(20000,30000)",
      },
      "mask": [
          [4700, 4705.6588],
          [4705.7587, 4711.2],
          [4711.3, 4716.2429],
          [4716.6429, 4718.0748],
          [4718.1547, 4719.9489],
          [4720.3489, 4721.9528],
          [4722.3528, 4726.4575],
          [4726.6971, 4729.8288],
          [4730.2288, 4731.2531],
          [4731.6531, 4738.574],
          [4738.7537, 4738.8869],
          [4739.68, 4743.6301],
          [4744.0301, 4746.4162],
          [4746.8162, 4748.5261],
          [4748.9261, 4750.2712],
          [4750.7603, 4751.6218],
          [4752.0218, 4757.9182],
          [4758.3182, 4759.07],
          [4759.47, 4760.3311],
          [4760.6405, 4761.3122],
          [4761.7122, 4764.3239],
          [4764.7239, 4765.6462],
          [4766.0462, 4770.1514],
          [4770.4509, 4772.1232],
          [4772.5232, 4773.7409],
          [4774.1409, 4774.7304],
          [4775.3299, 4778.0549],
          [4778.4549, 4781.5109],
          [4781.9109, 4783.2268],
          [4783.6268, 4784.2668],
          [4784.6668, 4788.1247],
          [4788.4642, 4788.5568],
          [4788.9568, 4789.135],
          [4789.535, 4793.7619],
          [4794.5599, 4795.2795],
          [4795.4592, 4796.7141],
          [4797.1141, 4797.7751],
          [4798.1751, 4798.3322],
          [4798.7322, 4800.8249],
          [4801.2249, 4802.6799],
          [4803.0799, 4803.2029],
          [4803.5124, 4803.8391],
          [4804.2391, 4805.6892],
          [4806.0892, 4807.9479],
          [4808.3479, 4810.3278],
          [4810.7278, 4811.1418],
          [4811.5418, 4811.65],
          [4811.75, 4819.8518],
          [4820.0415, 4820.2111],
          [4820.6111, 4827.841],
          [4828.241, 4831.446],
          [4831.846, 4832.9968],
          [4833.3968, 4834.95],
          [4835.0898, 4836.029],
          [4836.429, 4854.6628],
          [4855.0628, 4868.9528],
          [4869.3528, 4873.8098],
          [4874.2098, 4883.4841],
          [4883.8841, 4887.9251],
          [4888.0349, 4890.5588],
          [4890.9588, 4891.2922],
          [4891.6922, 4894.94],
          [4895.3094, 5643.4325],
          [5643.6522, 5647.0339],
          [5647.4339, 5651.2692],
          [5651.6692, 5652.1179],
          [5652.5179, 5655.9584],
          [5656.6572, 5657.2351],
          [5657.6351, 5657.696],
          [5658.096, 5660.0],
          [5660.3, 5661.1462],
          [5661.5462, 5662.7248],
          [5663.1248, 5665.9384],
          [5666.2479, 5666.9489],
          [5667.3489, 5668.1608],
          [5668.5608, 5670.653],
          [5671.053, 5671.6208],
          [5672.0208, 5672.4242],
          [5672.7936, 5676.6172],
          [5677.3061, 5678.8229],
          [5679.2229, 5680.0402],
          [5680.4402, 5682.4328],
          [5682.8328, 5684.98],
          [5685.3893, 5686.6472],
          [5687.0472, 5688.0002],
          [5688.4002, 5689.26],
          [5689.66, 5690.2248],
          [5690.6248, 5693.9517],
          [5694.371, 5695.8898],
          [5696.2898, 5700.0368],
          [5700.4368, 5700.904],
          [5701.304, 5702.1062],
          [5702.5062, 5704.0415],
          [5704.371, 5705.2648],
          [5705.6648, 5710.1979],
          [5710.5474, 5710.8879],
          [5711.2879, 5716.2502],
          [5716.6502, 5716.75],
          [5717.0, 5719.6159],
          [5720.0159, 5720.236],
          [5720.636, 5722.7435],
          [5723.3227, 5723.9069],
          [5724.3069, 5724.8],
          [5725.2, 5726.8479],
          [5727.2479, 5727.4519],
          [5727.8519, 5728.6901],
          [5729.0901, 5730.0981],
          [5730.4675, 5731.0412],
          [5731.4412, 5731.5622],
          [5731.9622, 5732.0959],
          [5732.4959, 5735.1],
          [5735.4, 5736.8591],
          [5737.2591, 5740.3],
          [5740.6, 5740.6579],
          [5741.0579, 5741.6481],
          [5742.0481, 5745.3593],
          [5746.0582, 5755.6289],
          [5756.0981, 5764.0915],
          [5765.5989, 5770.2888],
          [5770.6888, 5770.9367],
          [5771.1563, 5772.8],
          [5773.2, 5774.8811],
          [5775.2811, 5778.2531],
          [5778.6531, 5779.173],
          [5779.3926, 5781.927],
          [5782.327, 5787.346],
          [5787.5856, 5787.718],
          [5788.118, 5792.35],
          [5792.65, 5792.8732],
          [5793.2732, 5799.0132],
          [5799.5424, 5801.549],
          [5801.949, 5802.1048],
          [5802.7137, 5805.5729],
          [5805.9729, 5810.15],
          [5810.45, 5811.3718],
          [5811.7718, 5820.3976],
          [5820.9567, 5826.8],
          [5827.2, 5832.7],
          [5833.0, 5840.0316],
          [5840.381, 5841.6655],
          [5842.0848, 5842.1662],
          [5842.5662, 5844.3952],
          [5844.7952, 5846.7858],
          [5847.1858, 5849.4841],
          [5849.8841, 5851.4459],
          [5851.8452, 5852.9479],
          [5853.3479, 5853.468],
          [5853.868, 5854.8771],
          [5855.2771, 5857.2512],
          [5857.6512, 5858.5778],
          [5858.9778, 5861.4],
          [5861.8, 5866.2512],
          [5866.6512, 5867.362],
          [5867.762, 5868.4741],
          [5868.7337, 5874.2612],
          [5874.8003, 6472.3943],
          [6472.6638, 6479.75],
          [6480.0, 6480.4775],
          [6480.7071, 6481.6701],
          [6482.0701, 6482.5959],
          [6482.9959, 6485.4392],
          [6485.7786, 6492.1846],
          [6492.4242, 6493.5808],
          [6493.9808, 6494.78],
          [6495.18, 6496.697],
          [6497.097, 6498.739],
          [6499.139, 6499.4499],
          [6499.8499, 6500.1214],
          [6500.5109, 6505.7986],
          [6506.0083, 6508.6501],
          [6509.0501, 6513.3627],
          [6513.732, 6515.8801],
          [6516.2801, 6518.1672],
          [6518.5672, 6525.2695],
          [6525.6588, 6531.215],
          [6531.615, 6546.0388],
          [6546.4388, 6550.0402],
          [6550.4402, 6587.4099],
          [6587.8099, 6590.2479],
          [6590.9467, 6592.7141],
          [6593.1141, 6593.6701],
          [6594.0701, 6597.361],
          [6597.761, 6598.905],
          [6599.305, 6603.7121],
          [6604.0815, 6604.4011],
          [6604.8011, 6605.0],
          [6605.25, 6608.3],
          [6608.7, 6608.9099],
          [6609.3099, 6615.3194],
          [6616.2678, 6620.0615],
          [6620.6106, 6625.4991],
          [6625.8984, 6626.5],
          [6627.1, 6627.3449],
          [6627.7449, 6629.8112],
          [6630.2112, 6630.3675],
          [6630.7171, 6632.2331],
          [6632.6331, 6632.75],
          [6633.1, 6638.7534],
          [6639.1231, 6641.6855],
          [6641.9151, 6643.4289],
          [6643.8289, 6644.864],
          [6645.264, 6647.8801],
          [6648.2801, 6648.7936],
          [6649.0432, 6649.7487],
          [6649.9583, 6657.8252],
          [6658.1247, 6663.8618],
          [6664.0216, 6669.8518],
          [6670.0515, 6677.7868],
          [6678.1868, 6689.5723],
          [6690.0016, 6691.25],
          [6692.0, 6695.8229],
          [6696.2229, 6698.4729],
          [6698.8729, 6698.9421],
          [6699.3421, 6699.5124],
          [6699.9217, 6702.75],
          [6703.05, 6703.3669],
          [6703.7669, 6707.5612],
          [6707.9612, 6713.5451],
          [6713.9451, 6716.466],
          [6716.866, 6718.5241],
          [6719.0232, 6721.6481],
          [6722.0481, 6723.0532],
          [6723.3427, 6727.4],
          [6727.7, 6730.7],
          [6731.0, 6732.9509],
          [6733.3509, 6736.1],
          [6736.35, 6739.321],
          [6739.721, 6744.1613],
          [6744.8103, 7680.0661],
          [7680.4661, 7691.3332],
          [7691.7332, 7694.1114],
          [7694.5108, 7698.7741],
          [7699.1741, 7706.5673],
          [7706.9766, 7710.1638],
          [7710.5638, 7711.5231],
          [7711.9231, 7712.465],
          [7712.865, 7716.0083],
          [7716.6771, 7721.1464],
          [7721.6655, 7723.01],
          [7723.41, 7727.4128],
          [7727.8128, 7728.3444],
          [7729.0232, 7735.1297],
          [7735.5291, 7740.2379],
          [7741.1364, 7747.969],
          [7748.569, 7749.0911],
          [7749.0911, 7754.1],
          [7754.8, 7755.8785],
          [7756.4375, 7762.3144],
          [7762.8136, 7766.1],
          [7767.1, 7771.7409],
          [7772.1409, 7773.25],
          [7773.75, 7773.9611],
          [7774.3611, 7775.1901],
          [7775.5901, 7775.8],
          [7776.25, 7777.9051],
          [7778.4043, 7779.3],
          [7780.0, 7783.0432],
          [7783.4525, 7791.6054],
          [7792.4044, 7796.2578],
          [7796.9766, 7797.3859],
          [7797.7859, 7798.1246],
          [7798.604, 7800.0588],
          [7800.4588, 7801.5056],
          [7802.1049, 7804.9],
          [7805.25, 7808.6036],
          [7809.3229, 7817.6355],
          [7818.2845, 7822.1545],
          [7822.7238, 7823.3],
          [7823.9, 7826.5661],
          [7826.9661, 7829.3],
          [7829.8, 7835.1091],
          [7835.5091, 7835.9338],
          [7836.3338, 7837.9338],
          [7838.3338, 7838.5],
          [7839.3, 7846.8768],
          [7847.8352, 7852.4768],
          [7852.8768, 7858.0],
          [7858.4, 7859.7986],
          [7860.5074, 7866.2],
          [7866.7, 7870.3],
          [7870.7, 7878.0149],
          [7878.4841, 7885.7786],
          [7886.4375, 7891.0964],
          [7891.6156, 7900]
      ]
  }
  
  benchmarks = [each.split("/")[-2] for each in glob("../tests/data/benchmarks/*/*blue_noresample.txt")]
  
  for benchmark in benchmarks:
  
      filenames = glob("../tests/data/benchmarks/{0}/{0}_narval*noresample*".format(benchmark))
      data = map(theremin.specutils.Spectrum1D.load, filenames)
      data.sort(key=lambda s: s.disp[0])
  
      model = theremin.ThereminModel(config)
      spectrum_model = theremin.SpectrumModel(config, data)
      break
  
      # Optimise the model parameters and plot the transition fits at every 10th
      # iteration of stellar parameters
      parameters, state, converged, transitions, spectra, sampled_parameters, \
          sampled_states = model.optimise(data, plotting=True, 
          plot_transition_frequency=10, plot_filename_prefix=benchmark,
          balance_mode=model.config["ThereminModel"].get("balance_mode", 1))
  
      if converged:
          # Do something with the results?
          print("Start {0} has converged with {1}".format(benchmark, parameters))
  
      
  