XSyncMapAcquisition.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. namespace OINA.Extender.WPF.Testharness
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Threading;
  11. using OINA.Extender.Acquisition;
  12. using OINA.Extender.Acquisition.Ed;
  13. using OINA.Extender.Acquisition.XSync;
  14. using OINA.Extender.Data.Ed;
  15. using OINA.Extender.Data.Image;
  16. using OINA.Extender.Processing;
  17. using OINA.Extender.Processing.Ed;
  18. /// <summary>
  19. /// Interaction logic for XSyncMapAcquisition.xaml
  20. /// </summary>
  21. public partial class XSyncMapAcquisition : UserControl, INotifyPropertyChanged
  22. {
  23. private readonly List<DetectorEnableModel> enabledDetectors;
  24. private EdHardwareId primaryHardwareId;
  25. private List<DetectorEnableModel> secondaryHardwareIds;
  26. private IXSyncEdMapAcquisitionController controller;
  27. private IEdSpectrum spectrum;
  28. private bool isPaused;
  29. private string pauseResumeState;
  30. private int completedFrames;
  31. private ObservableCollection<IEdMap> acquiredMaps;
  32. private IEdMap selectedMap;
  33. private IReadOnlyList<IEdMapImage> elementMaps;
  34. private double progressPercentage;
  35. private IEdMapAcquisitionData mapData;
  36. private IEdMapProcessor mapProcessor;
  37. private IAutoIdSettings autoIdSettings;
  38. private IEdSpectrumProcessing spectrumProcessing;
  39. public XSyncMapAcquisition()
  40. {
  41. this.InitializeComponent();
  42. this.DataContext = this;
  43. this.autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
  44. this.spectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
  45. this.Settings = AcquireFactory.CreateXSyncEdMapSettings();
  46. this.Settings.XSyncSettings.SynchronizationMode = XSyncMode.CASLineSync;
  47. // pixel size is the number of data pixels in x
  48. double pixelSize = 1.0 / 120;
  49. // assume that there are also 120 data pixels in y
  50. Size size = new Size(1.0, 1.0);
  51. this.Settings.XSyncSettings.AcquisitionRegion.CreateRectangleRegion(new Rect(size), pixelSize);
  52. this.Settings.XSyncSettings.FrameCount = 1;
  53. // assume that there are 2040 lines scanned, this gives a binning factor of 17
  54. this.Settings.XSyncSettings.LineSync.BinFactorY = 17;
  55. this.Settings.XSyncSettings.LineSync.LinesPerFrame = 2040; // = (120 * 17)
  56. // set the calibrated field width to set the image viewer scale bar
  57. this.Settings.XSyncSettings.CalibratedFieldWidthmm = 180;
  58. this.enabledDetectors = this.Settings.EdSettings.IsHardwareEnabled
  59. .Select(i => new DetectorEnableModel(i.Key, i.Value)).ToList();
  60. this.PrimaryHardwareId = this.Settings.EdSettings.PrimaryHardwareId;
  61. this.controller = AcquireFactory.CreateXSyncEdMapServer();
  62. this.controller.ExperimentStarted += this.OnExperimentStarted;
  63. this.controller.ExperimentFinished += this.OnExperimentFinished;
  64. this.controller.PauseRequested += this.OnPauseRequested;
  65. this.controller.ResumeRequested += this.OnResumeRequested;
  66. this.controller.FrameCompleted += this.OnFrameCompleted;
  67. this.acquiredMaps = new ObservableCollection<IEdMap>();
  68. }
  69. /// <summary>
  70. /// Event that notifies that the processing progress has changed
  71. /// </summary>
  72. public event PropertyChangedEventHandler PropertyChanged;
  73. private void RaisePropertyChanged(string propertyName)
  74. {
  75. this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  76. }
  77. private void StartButton_Click(object sender, System.Windows.RoutedEventArgs e)
  78. {
  79. this.PauseResumeState = string.Empty;
  80. this.IsPaused = false;
  81. this.CompletedFrames = 0;
  82. this.ElementMaps = null;
  83. this.ProgressPercentage = 0;
  84. var edSettings = this.Settings.EdSettings;
  85. var xsyncSettings = this.Settings.XSyncSettings;
  86. foreach (var detectorEnabled in this.secondaryHardwareIds)
  87. {
  88. edSettings.EnableDevice(detectorEnabled.EdHardwareId, detectorEnabled.IsDetectorEnabled);
  89. }
  90. edSettings.PrimaryHardwareId = this.primaryHardwareId;
  91. if (xsyncSettings.SynchronizationMode == XSyncMode.CASLineSync)
  92. {
  93. edSettings.AcquisitionMode = EdAcquireMode.RealTime;
  94. edSettings.AcquisitionTime = TimeSpan.FromTicks((long)(xsyncSettings.LineSync.DwellTimeMicroSeconds * TimeSpan.TicksPerMillisecond / 1000));
  95. edSettings.TimeMode = EdTimeMode.Pixel;
  96. }
  97. var results = this.Settings.Validate();
  98. if (!results.IsValid)
  99. {
  100. string errors = string.Join(Environment.NewLine, results.ValidationErrors);
  101. MessageBox.Show(errors, @"Invalid acquisition settings");
  102. return;
  103. }
  104. this.controller.StartAcquisition(this.Settings);
  105. }
  106. private void StopButton_Click(object sender, System.Windows.RoutedEventArgs e)
  107. {
  108. this.controller.StopAcquisition();
  109. }
  110. private void OnExperimentStarted(object sender, AcquisitionStartedEventArgs<IEdMapAcquisitionData> e)
  111. {
  112. this.Spectrum = e.Value.EdSpectrum;
  113. this.mapData = e.Value;
  114. }
  115. private void OnExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdMapAcquisitionData> e)
  116. {
  117. var edMap = e.Value?.EdMap;
  118. if (edMap != null)
  119. {
  120. this.Dispatcher.BeginInvoke(() =>
  121. {
  122. this.acquiredMaps.Add(edMap);
  123. this.SelectedMap = edMap;
  124. });
  125. }
  126. }
  127. public IEdSpectrum Spectrum
  128. {
  129. get
  130. {
  131. return this.spectrum;
  132. }
  133. set
  134. {
  135. this.spectrum = value;
  136. this.RaisePropertyChanged(nameof(this.Spectrum));
  137. }
  138. }
  139. public IXSyncEdMapSettings Settings { get; }
  140. public EdHardwareId PrimaryHardwareId
  141. {
  142. get
  143. {
  144. return this.primaryHardwareId;
  145. }
  146. set
  147. {
  148. if (this.primaryHardwareId == value)
  149. {
  150. return;
  151. }
  152. this.primaryHardwareId = value;
  153. this.RaisePropertyChanged(nameof(this.PrimaryHardwareId));
  154. this.SecondaryHardwareIds = this.enabledDetectors.Where(i => i.EdHardwareId != value).ToList();
  155. }
  156. }
  157. public IReadOnlyList<DetectorEnableModel> SecondaryHardwareIds
  158. {
  159. get
  160. {
  161. return this.secondaryHardwareIds;
  162. }
  163. set
  164. {
  165. this.secondaryHardwareIds = value.ToList();
  166. this.RaisePropertyChanged(nameof(this.SecondaryHardwareIds));
  167. }
  168. }
  169. #region Pause / Resume
  170. private void OnPauseRequested(object sender, PauseScanEventArgs e)
  171. {
  172. this.PauseResumeState = @"Pause requested";
  173. }
  174. private void OnResumeRequested(object sender, ResumeScanEventArgs e)
  175. {
  176. this.PauseResumeState = @"Resume requested";
  177. }
  178. /// <summary>
  179. /// Gets or sets a value indicating whether the external scan hardware is paused
  180. /// </summary>
  181. public bool IsPaused
  182. {
  183. get
  184. {
  185. return this.isPaused;
  186. }
  187. set
  188. {
  189. if (this.isPaused == value)
  190. {
  191. return;
  192. }
  193. this.isPaused = value;
  194. this.RaisePropertyChanged(nameof(this.IsPaused));
  195. this.controller.NotifyPauseResume(value);
  196. }
  197. }
  198. /// <summary>
  199. /// Gets or sets a value indicating whether a pause or resume is requested
  200. /// </summary>
  201. public string PauseResumeState
  202. {
  203. get
  204. {
  205. return this.pauseResumeState;
  206. }
  207. set
  208. {
  209. if (this.pauseResumeState == value)
  210. {
  211. return;
  212. }
  213. this.pauseResumeState = value;
  214. this.RaisePropertyChanged(nameof(this.PauseResumeState));
  215. }
  216. }
  217. #endregion
  218. private void OnFrameCompleted(object sender, FrameCompletedEventArgs e)
  219. {
  220. this.CompletedFrames = e.FrameNumber;
  221. }
  222. /// <summary>
  223. /// Gets or sets the number of completed frames
  224. /// </summary>
  225. public int CompletedFrames
  226. {
  227. get
  228. {
  229. return this.completedFrames;
  230. }
  231. set
  232. {
  233. if (this.completedFrames == value)
  234. {
  235. return;
  236. }
  237. this.completedFrames = value;
  238. this.RaisePropertyChanged(nameof(this.CompletedFrames));
  239. }
  240. }
  241. /// <summary>
  242. /// Gets the acquired maps
  243. /// </summary>
  244. public ObservableCollection<IEdMap> AcquiredMaps
  245. {
  246. get
  247. {
  248. return this.acquiredMaps;
  249. }
  250. }
  251. /// <summary>
  252. /// Gets or sets the selected map
  253. /// </summary>
  254. public IEdMap SelectedMap
  255. {
  256. get
  257. {
  258. return this.selectedMap;
  259. }
  260. set
  261. {
  262. this.selectedMap = value;
  263. this.RaisePropertyChanged(nameof(this.SelectedMap));
  264. }
  265. }
  266. /// <summary>
  267. /// Deletes the selected map
  268. /// </summary>
  269. private void DeleteMapButton_Click(object sender, RoutedEventArgs e)
  270. {
  271. // Maps should be deleted when they are no longer required as the data is saved in the
  272. // AppData\Local\Oxford Instruments NanoAnalysis\Extender\TempProject folder of the current user
  273. // Note these projects cannot be used with AZtec.
  274. var edMap = this.selectedMap;
  275. if (edMap != null)
  276. {
  277. this.controller.Delete(edMap);
  278. edMap.Dispose();
  279. this.acquiredMaps.Remove(edMap);
  280. this.SelectedMap = this.acquiredMaps.LastOrDefault();
  281. }
  282. }
  283. #region FLS Processing
  284. /// <summary>
  285. /// Starts FLS map processing
  286. /// </summary>
  287. /// <param name="mapData">The map data</param>
  288. private void StartFLSProcessing(IEdMapAcquisitionData mapData)
  289. {
  290. this.PerformAutoId(mapData);
  291. var processor = ProcessingFactory.CreateEdMapFLSProcessor();
  292. var settings = ProcessingFactory.CreateEdMapFLSSettings();
  293. try
  294. {
  295. this.MapProcessor = processor;
  296. this.ElementMaps = processor.StartProcessing(mapData, settings);
  297. }
  298. catch (Exception ex)
  299. {
  300. MessageBox.Show(ex.Message, @"Error starting FLS processing");
  301. }
  302. }
  303. #endregion
  304. #region window integral processing
  305. /// <summary>
  306. /// Starts window integral map processing
  307. /// </summary>
  308. /// <param name="mapData">The map data</param>
  309. private void StartWindowIntegralProcessing(IEdMapAcquisitionData mapData)
  310. {
  311. this.PerformAutoId(mapData);
  312. var processor = ProcessingFactory.CreateEdMapWindowIntegralProcessor();
  313. var settings = ProcessingFactory.CreateEdMapWindowIntegralSettings();
  314. try
  315. {
  316. this.MapProcessor = processor;
  317. this.ElementMaps = processor.StartProcessing(mapData, settings);
  318. }
  319. catch (Exception ex)
  320. {
  321. MessageBox.Show(ex.Message, @"Error starting window integral processing");
  322. }
  323. }
  324. #endregion
  325. #region common map processing
  326. /// <summary>
  327. /// Gets or sets the Ed Map processor
  328. /// </summary>
  329. private IEdMapProcessor MapProcessor
  330. {
  331. get
  332. {
  333. return this.mapProcessor;
  334. }
  335. set
  336. {
  337. if (this.mapProcessor == value)
  338. {
  339. return;
  340. }
  341. var processor = this.mapProcessor;
  342. if (processor != null)
  343. {
  344. processor.CancelProcessing();
  345. processor.ProgressChanged -= this.OnProcessingProgressChanged;
  346. processor.Dispose();
  347. }
  348. this.mapProcessor = value;
  349. if (value != null)
  350. {
  351. value.ProgressChanged += this.OnProcessingProgressChanged;
  352. }
  353. }
  354. }
  355. private void OnProcessingProgressChanged(object sender, ProcessingProgressEventArgs e)
  356. {
  357. this.ProgressPercentage = e.ProgressPercentage;
  358. if (e.State == ProcessingProgressState.Completed)
  359. {
  360. this.elementMaps.ToList().ForEach(i => i.AutoBrightness());
  361. // calculate total counts in the first element map
  362. var elementMap = this.elementMaps.OfType<IEdMapImage>().FirstOrDefault();
  363. if (elementMap != null)
  364. {
  365. int pixels = elementMap.Height * elementMap.Width;
  366. float[] countsPerSecond = new float[pixels];
  367. elementMap.GetData(countsPerSecond);
  368. float[] liveTimes = new float[pixels];
  369. this.mapData.EdMap.GetLiveTimes(elementMap.BinFactor, liveTimes);
  370. double totalCounts = Enumerable.Range(0, pixels).Sum(i => countsPerSecond[i] * liveTimes[i]);
  371. }
  372. }
  373. }
  374. /// <summary>
  375. /// Gets or sets the processing percentage (0 - 100)
  376. /// </summary>
  377. public double ProgressPercentage
  378. {
  379. get
  380. {
  381. return this.progressPercentage;
  382. }
  383. set
  384. {
  385. this.progressPercentage = value;
  386. this.RaisePropertyChanged(nameof(this.ProgressPercentage));
  387. }
  388. }
  389. /// <summary>
  390. /// Gets or sets the element maps
  391. /// </summary>
  392. public IReadOnlyList<IEdMapImage> ElementMaps
  393. {
  394. get
  395. {
  396. return this.elementMaps;
  397. }
  398. set
  399. {
  400. if (this.elementMaps == value)
  401. {
  402. return;
  403. }
  404. var elementMaps = this.elementMaps;
  405. elementMaps?.ToList().ForEach(i => i.Dispose());
  406. this.elementMaps = value;
  407. this.RaisePropertyChanged(nameof(this.ElementMaps));
  408. }
  409. }
  410. private void PerformAutoId(IEdMapAcquisitionData mapData)
  411. {
  412. try
  413. {
  414. // Identify the elements in the sum spectrum
  415. var elements = this.spectrumProcessing.IdentifyElements(mapData.EdSpectrum, this.autoIdSettings);
  416. if (elements.Any())
  417. {
  418. // Update the identified elements in the map. Used to create the element maps.
  419. mapData.EdMap.ClearIdentifiedElements();
  420. mapData.EdMap.SetIdentifiedElements(elements, true);
  421. // Update the peak labels
  422. this.spectrumProcessing.UpdatePeakLabels(mapData.EdSpectrum, null);
  423. }
  424. }
  425. catch (Exception ex)
  426. {
  427. MessageBox.Show(ex.Message, @"Error processing spectrum");
  428. }
  429. }
  430. private void CancelButton_Click(object sender, RoutedEventArgs e)
  431. {
  432. this.MapProcessor?.CancelProcessing();
  433. }
  434. #endregion
  435. private void WindowIntegral_Click(object sender, RoutedEventArgs e)
  436. {
  437. if (this.mapData != null)
  438. {
  439. this.StartWindowIntegralProcessing(this.mapData);
  440. }
  441. }
  442. private void FLS_Click(object sender, RoutedEventArgs e)
  443. {
  444. if (this.mapData != null)
  445. {
  446. this.StartFLSProcessing(this.mapData);
  447. }
  448. }
  449. }
  450. }