-
Notifications
You must be signed in to change notification settings - Fork 1
/
MeasurementComponents.py
1118 lines (952 loc) · 56.9 KB
/
MeasurementComponents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""This module packages up the MeasurementController class to control the measuring hardware on an abstract level as
well as the Measurement class which takes out and implements the actual measuring process"""
__copyright__ = "Copyright 2015 - 2017, Justin Scholz"
__author__ = "Justin Scholz"
import queue
from threading import Thread
from abc import ABCMeta, abstractmethod
import math
import time
from MeasurementSetups import MeasurementSetup
import MeasurementSetups
import UserInput
from DataStorage import main_db
class Task(metaclass=ABCMeta):
@abstractmethod
def do(self):
pass
@abstractmethod
def generate_one_line_summary(self):
pass
class Trigger(Thread, Task):
"""Class that provides functionality for triggering a sub task on a time or measurable base
"""
def __init__(self, identifier: [], measurement_setup: MeasurementSetup, trigger: dict, global_task_list: []):
"""
:param identifier:
:param measurement_setup:
:param trigger: {"total_time_span": 300, "trigger_separation": 3} or
{"acquis_triggering_measurable": measurable_dict, "acquis_triggering_value": 200}
:param global_task_list:
"""
super().__init__()
self.should_do_now = False
self.should_be_running = True
self.global_task_list = global_task_list
self.trigger = trigger
self.measurement_setup = measurement_setup
self.identifier = identifier
self.mode = None # type: str
self.total_time_span = None
self.trigger_separation = None
self.acquis_triggering_measurable = None
self.acquis_triggering_value = None
if "total_time_span" in trigger:
self.total_time_span = float(trigger["total_time_span"])
self.trigger_separation = trigger["trigger_separation"]
self.mode = "time"
elif "acquis_triggering_measurable" in trigger:
self.acquis_triggering_measurable = trigger["acquis_triggering_measurable"]
self.acquis_triggering_value = trigger["acquis_triggering_value"]
self.trigger_when_below = trigger["trigger_when_below"]
self.mode = "measurable"
main_db.make_storage(identifier, "Trigger", self.generate_one_line_summary())
def run(self):
while self.should_be_running:
if self.should_do_now:
self.do()
self.should_do_now = False
time.sleep(0.05)
def do(self):
if self.mode == "time":
self._time_based_triggering()
elif self.mode == "measurable":
self._measurable_value_based_triggering()
def _time_based_triggering(self):
sub_tasks = Helper.check_for_sub_tasks(self.identifier, self.global_task_list)
start_time = time.perf_counter()
end_time = start_time + (self.total_time_span * 60.0)
next_trigger_time = start_time
UserInput.post_status("{0}: Started Trigger task '{1}'.".format(time.strftime("%c"), str(self.name)))
# Only while the end time isn't reached
while end_time > time.perf_counter():
# check for next trigger time. If it is time to trigger, then run all direct_sub_tasks after each other
if time.perf_counter() > next_trigger_time:
datapackage_start_time = time.strftime("%H %M %S")
for task in sub_tasks:
task.should_do_now = True
while task.should_do_now:
pass
UserInput.post_status(time.strftime("%c") + ": Waiting for new trigger time to be reached.")
datapackage_end_time = time.strftime("%H %M %S")
datapoint = {"start_time": datapackage_start_time, "end_time": datapackage_end_time}
main_db.add_point(self.identifier, datapoint)
# we only calculate the time of when to trigger next if we reached the previous one!
next_trigger_time = time.perf_counter() + self.trigger_separation * 60
return
def _measurable_value_based_triggering(self): # TODO: One could think of optionally implementing a time out
# TODO: Implement measurable triggered code path. Problem to solve is: how does the user select which one of the
# potentially many values inside a datapoint (look at ALPHA - it's R,X and freq) to use for comparison
UserInput.confirm_warning("Not implemented yet!!")
pass
def generate_one_line_summary(self):
if self.mode == "time":
summary = "Total time is {0} minutes. Firing off sub_taks every {1} minutes.".format(
str(self.total_time_span), str(self.trigger_separation))
else:
summary = "meh, doesn't work with measurables yet!"
return summary
class DataAcquisition(Thread, Task):
"""This class is for an individual _acquire_point, so multiple frequencies are measured"""
def __init__(self, identifier: [],
measurement_setup: MeasurementSetup, measurable: dict,
average_through_sub_controlable: bool, global_task_list: []):
"""initializes the measurement object with a frequency list (remember, this is essentially a single _acquire_point!)
and the measurementDeviceController so it can actually start the measurement on device. Should later be
initialized with a temp device controller optionally
:type identifier: []
:type measurable: dict
:param measurable: A dictionary created by a measurement setup
:param measurement_setup:
:return: """
super().__init__()
self.should_do_now = False
self.should_be_running = True
self.global_task_list = global_task_list
self.identifier = identifier
self.average_through_sub_task = average_through_sub_controlable
self.measurable = measurable
self.measurement_setup = measurement_setup
self.sub_tasks = []
main_db.make_storage(identifier, "DataAcq", self.generate_one_line_summary())
return
def generate_one_line_summary(self):
"""
:returns One-line summary
:rtype: str
"""
text = "Acquires data from" + self.measurable["dev"].name
text += " and is "
if self.average_through_sub_task:
average = ""
else:
average = "no "
summary = text + average + "averaging datapoints while sub_tasks are run."
return summary
def run(self):
while self.should_be_running:
if self.should_do_now:
self.do()
self.should_do_now = False
time.sleep(0.01)
def acquire_point(self):
"""This method performs a sweep with the measurables stored in the DataAcquisition objects frequ_list variable (set
during initialization, will probably be calls as a thread to not have to wait for the
"""
if not self.has_sub_tasks: # This means we can eg just pass a measuring command to a device and
# acquire data instead of having to make sure that a specific condition eg a temperature is reached
datapoint = self.measurement_setup.measure_measurable(self.measurable)
main_db.add_point(self.identifier, datapoint)
elif self.has_sub_tasks: # if we have a task
start_datapackage = self.measurement_setup.measure_measurable(self.measurable) # type: dict
# initialize the averaging logic if needed
if self.average_through_sub_task:
max_negative_deviation_datapackage = start_datapackage.copy()
max_positive_deviation_datapackage = start_datapackage.copy()
average_datapackage = start_datapackage.copy()
averager = 1 # the variable used to calculate the true average
for task in self.sub_tasks: # execute every sub_task
task.should_do_now = True
if self.average_through_sub_task:
while task.should_do_now:
# we need the current datapackage
current_datapoint = self.measurement_setup.measure_measurable(self.measurable) # type: dict
# if needed refresh the max_negative deviation
for key in max_negative_deviation_datapackage.keys():
# if it's the time key, it's a str, we can't really calculate with strings
if type(max_negative_deviation_datapackage[key]) == float:
# If the currently maximum negative deviation is bigger than the current value, it's
# not the maximum so it's rewritten
if max_negative_deviation_datapackage[key] > current_datapoint[key]:
max_negative_deviation_datapackage[key] = current_datapoint[key]
# if needed refresh the max_positive deviation
for key in max_positive_deviation_datapackage.keys():
if type(max_negative_deviation_datapackage[key]) == float:
# If the currently maximum positive deviation is bigger than the current value, it's
# not the maximum so it's rewritten
if max_positive_deviation_datapackage[key] < current_datapoint[key]:
max_positive_deviation_datapackage[key] = current_datapoint[key]
# calculate the current average throughout the whole thingy
for key in average_datapackage.keys():
if type(average_datapackage[key]) == float:
current_average = average_datapackage[key]
expanded_average = current_average * averager
expanded_average += current_datapoint[key]
new_average = expanded_average / (averager + 1)
average_datapackage[key] = new_average
averager += 1
# Make points every 1 seconds. If you want that to be a setting, include in the
# acquisition Class as a parameter, eg "averaging point frequency
time.sleep(5)
while task.should_do_now:
pass
if self.average_through_sub_task:
final_max_negative_deviation_datapackage = {}
# now append all the relevant postfixes:
for key in max_negative_deviation_datapackage.keys():
if type(max_negative_deviation_datapackage[key]) == float:
new_key = key + "_max_-"
# we set the new suffixed key to the then removed item at place key. Essentially renaming the
# key
final_max_negative_deviation_datapackage[new_key] = max_negative_deviation_datapackage[key]
final_max_positive_deviation_datapackage = {}
for key in max_positive_deviation_datapackage.keys():
if type(max_positive_deviation_datapackage[key]) == float:
new_key = key + "_max_+"
# we set the new suffixed key to the then removed item at place key. Essentially renaming the
# key
final_max_positive_deviation_datapackage[new_key] = max_positive_deviation_datapackage[key]
final_average_datapackage = {}
for key in average_datapackage.keys():
if type(average_datapackage[key]) == float:
new_key = key + "_aver"
# we set the new suffixed key to the then removed item at place key. Essentially renaming the
# key
final_average_datapackage[new_key] = average_datapackage[key]
# Now update the starting_point dict with the now calculated thingies
start_datapackage.update(final_max_negative_deviation_datapackage)
start_datapackage.update(final_max_positive_deviation_datapackage)
start_datapackage.update(final_average_datapackage)
# And in every case add the starting data package to the thingy
main_db.add_point(self.identifier, start_datapackage)
return
def do(self):
self.sub_tasks = Helper.check_for_sub_tasks(self.identifier, self.global_task_list)
if len(self.sub_tasks) == 0:
self.has_sub_tasks = False
else:
self.has_sub_tasks = True
self.acquire_point()
class ParameterController(Thread, Task):
"""
Objects will be Threads that control a controlable, eg the temperature and use classic ramping in currently the
ramping method layout in the README to trigger a sweep at specific points. If one wanted to implement different ramping of
a controlable (eg temperature, moisture or something), one could introduce a new switch and then implement the ramp
here.
"""
def __init__(self, identifier: [], measurement_setup: MeasurementSetup, meas_setup_controlable,
trigger: {}, global_task_list: []):
"""
:type identifier: [int]
:param controlable: A dictionary created by the meas_setup to specify device and controlable
:param trigger_separation: when we want to measure every 3 K, we need to trigger every 3 K. So value would be 3
:param measurement_setup: that's the ms for the controlable we are talking about
:param start_value: eg starting temperature (200K)
:param rate_for_controllable: eg 0.15 [K]
:param end_value: eg end temperature (300)
"""
# acquis_triggering_measurable, start_value, rate_for_controllable, end_value,
# trigger_separation, specific_values: [])
super().__init__()
self.should_be_running = True
self.should_do_now = False
self.global_task_list = global_task_list
self.sub_tasks = []
self.identifier = identifier
self.meas_setup_controlable = meas_setup_controlable
self.ms = measurement_setup
self.mode = None
self.all_values_reached = False
if "start_value" in trigger:
self.start_value = trigger["start_value"]
self.trigger_separation = trigger["trigger_separation"]
self.rate_for_controllable = trigger["rate_for_controlable"]
self.milisecond_rate = self.rate_for_controllable / (60) # minutes and seconds ->miliseconds
self.end_value = trigger["end_value"]
self.end_value_reached_when_below = None # type: bool # whether ramp goes up or down
if self.end_value > self.start_value:
self.end_value_reached_when_below = False
else:
self.end_value_reached_when_below = True
self.mode = "ramp"
elif "specific_values" in trigger:
self.specific_values = trigger["specific_values"]
self.mode = "spec_values"
main_db.make_storage(self.identifier, "ParamContr", self.generate_one_line_summary())
def generate_one_line_summary(self):
"""
:returns Oneline summary
:rtype: str
"""
text = self.mode + " :"
dev_name = self.meas_setup_controlable["dev"].name + " "
controled_param = self.meas_setup_controlable["name"] + " "
summary = ""
if self.mode == "ramp":
summary = str(text) + str(dev_name) + str(controled_param) + "from " + str(self.start_value) + " to " + \
str(self.end_value) + " triggering every " + str(self.trigger_separation) + \
" and controlling at a rate of " + str(self.rate_for_controllable) + "."
elif self.mode == "spec_values":
summary = str(text) + str(dev_name) + str(
controled_param) + "setting specified values and triggering sub_tasks then"
return summary
def run(self):
"""starts a thread of ParameterController when called by calling "start" (!! don't call "run"!)
:return:
"""
while self.should_be_running:
if self.should_do_now:
self.do()
self.should_do_now = False
time.sleep(0.01)
return
def _start_and_stop_sub_tasks(self):
# self.sub´_tasks gets updates when the Thread is started with the run method
for task in self.sub_tasks:
# start the action on the thread. If the property "should_do_now" is changed to False again, it is finished
task.should_do_now = True
while task.should_do_now:
pass
def do(self):
self.sub_tasks = Helper.check_for_sub_tasks(self.identifier, self.global_task_list)
if self.mode == "ramp":
start_time = time.perf_counter()
current_value = self.start_value
most_recent_value = self.start_value
# The very first temperature should also be sweepin'
datapoint = self.ms.change_value_of_controlable_to(self.meas_setup_controlable, current_value)
main_db.add_point(self.identifier, datapoint)
self._start_and_stop_sub_tasks()
while not self.all_values_reached:
sweeped_this_cycle = False
# when we get the relative time to the start of this controllable, we can calculate our expected setpoint
# according to the rate specified, therefore we first gather the current time
current_relative_time = (time.perf_counter() - start_time) # some float in milliseconds
# new setpoint value is start value * relative time in milliseconds * millisecond_rate as this is a linear
# function and we want to use the mentioned ramping method.
if self.end_value_reached_when_below:
setpoint_value = self.start_value - current_relative_time * self.milisecond_rate
if setpoint_value < self.end_value:
setpoint_value = self.end_value
else:
setpoint_value = self.start_value + current_relative_time * self.milisecond_rate
if setpoint_value > self.end_value:
setpoint_value = self.end_value
UserInput.post_status(time.strftime("%c") + ": Halting " + self.generate_one_line_summary())
# Actually send the temperature controller a new value
datapoint = self.ms.change_value_of_controlable_to(self.meas_setup_controlable, setpoint_value)
current_value = setpoint_value
# This is the if condition to trigger sub_tasks
if abs(current_value - most_recent_value) >= self.trigger_separation:
most_recent_value = current_value
# we only add a datapoint if we are triggering sub_tasks:
main_db.add_point(self.identifier, datapoint)
self._start_and_stop_sub_tasks()
sweeped_this_cycle = True
UserInput.post_status(time.strftime("%c") + ": Resuming: " + self.generate_one_line_summary())
if self.end_value_reached_when_below:
if current_value <= self.end_value:
self.all_values_reached = True
# and we sweep when we reach the final value, but only if we didn't already sweep
if not sweeped_this_cycle:
main_db.add_point(self.identifier, datapoint)
self._start_and_stop_sub_tasks()
UserInput.post_status(time.strftime("%c") + ": Ramp " + self.generate_one_line_summary() + " now done!")
elif not self.end_value_reached_when_below:
if current_value >= self.end_value:
self.all_values_reached = True
# and we sweep when we reach the final value, but only if we didn't already sweep
if not sweeped_this_cycle:
UserInput.post_status(time.strftime("%c") + ": Ramp " + self.generate_one_line_summary() + " now done!")
main_db.add_point(self.identifier, datapoint)
self._start_and_stop_sub_tasks()
# TODO: Do we need to introduce a time out/sleep because we are setting temperatures to quickly?
time.sleep(0.01)
elif self.mode == "spec_values":
UserInput.post_status(time.strftime("%c") + ": Started " + self.generate_one_line_summary())
for specific_controlable_value in self.specific_values:
datapoint = self.ms.change_value_of_controlable_to(self.meas_setup_controlable,
specific_controlable_value)
main_db.add_point(self.identifier, datapoint)
self._start_and_stop_sub_tasks()
class Measurement:
"""This class has all the relevant info for one Measurement step (eg from 10 to 300 K _acquire_point every 3 seconds the
frequencies x,y,z
"""
def __init__(self):
self.tasks = [] # type: [Task]
self.task_input = [] #User input to questions. Variable helps with setting up a new template
self.meas_setup = None # type: MeasurementSetups.MeasurementSetup
self._choose_meas_setup()
self.meas_setup.init_after_creation()
return
def _choose_meas_setup(self):
self.meas_setup_chooser = MeasurementSetups.MeasurementSetupHelper()
self.list_of_setups = self.meas_setup_chooser.list_available_setups()
question = {"question_title": "Measurement Setup",
"question_text": "Please choose your current measurement setup",
"default_answer": 0,
"optiontype": "multi_choice", "valid_options": self.list_of_setups}
answer = UserInput.ask_user_for_input(question)["answer"]
self.meas_setup = self.meas_setup_chooser.select_setup(self.list_of_setups[answer])
def new_task(self,custom_type=True,template=[]):
# We need measurement setup controlables and measurables
template=template
available_raw_controlables = self.meas_setup.get_controlables()
available_controlables = [] # mainly used to be human readable in questions
for controlable in available_raw_controlables:
text_option = controlable["name"] + " at device: " + controlable["dev"].name
available_controlables.append(text_option)
available_raw_measurables = self.meas_setup.get_measurables()
available_measurables = [] # mainly used to be human readable in questions
for meausurable in available_raw_measurables:
text_option = meausurable["name"] + " at device: " + meausurable["dev"].name
available_measurables.append(text_option)
question = {"question_title": "Kind of task",
"question_text": "Do you want to create a Parameter Controller or a Data Acquisition or a Trigger?",
"default_answer": 0,
"optiontype": "multi_choice",
"valid_options": ["Parameter Controller", "Data Acquisition", "Trigger"]}
answer = self._get_input(custom_type,question,template)
# 0 = ParameterController, code path to create a new parameter controller
if answer["answer"] == 0:
param_controller = None
question = {"question_title": "Choosing the controlable",
"question_text": "Which Controlable of the following available ones do you want to use? "
"(Irrelevant if you want to use a time-triggered parameter controller",
"default_answer": 0,
"optiontype": "multi_choice",
"valid_options": available_controlables}
answer = self._get_input(custom_type,question,template)
desired_controlable = available_raw_controlables[answer["answer"]]
UserInput.post_status("Beware of the following limits on this measurement setup!")
limits = self.meas_setup.get_limits()
for limit in limits:
UserInput.post_status(limit)
# We have four kinds of ParamControllers: ramp-based, specific values, measurable- and time-triggered
question = {"question_title": "ParamController type",
"question_text": "Do you want to create a ramp-based, specific values, measurable-triggered or"
" time triggered controller?",
"default_answer": 0,
"optiontype": "multi_choice",
"valid_options": ["ramp-based (eg temperature)",
"specific values (eg frequencies)"]}
answer = self._get_input(custom_type,question,template)
# answer being 0 means ramp-based is wanted
if answer["answer"] == 0:
# Get a start value for the controlable
# TODO: One could think of imposing more sensible limits on the controlable here
question = {"question_title": "Start Value for " + desired_controlable["name"],
"question_text": "What is the desired start value?",
"default_answer": 300,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e1}
answer = self._get_input(custom_type,question,template)
start_value = answer["answer"]
question = {"question_title": "End Value",
"question_text": "What is the end value?",
"default_answer": 20,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1000.0,
"valid_options_steplength": 1e1}
answer = self._get_input(custom_type,question,template)
end_value = answer["answer"]
# trigger spearation is how often the inner part of the task list should be triggered
question = {"question_title": "Trigger separation ",
"question_text": "What shall be the interval after which a sub_task is triggered? "
"(Eg measure every 3 K",
"default_answer": 3,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1000.0,
"valid_options_steplength": 1e3}
answer = self._get_input(custom_type,question,template)
trigger_separation = answer["answer"]
# The rate in change per minute of the controlable
question = {"question_title": "Controlable rate",
"question_text": "By what amount should the controlable change per minute?",
"default_answer": 0.6,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1000.0,
"valid_options_steplength": 1e3}
answer = self._get_input(custom_type,question,template)
rate_for_controlable = answer["answer"]
identifier = self._get_id_for_task_insert_into_queue(custom_type,template)
trigger = {"start_value": start_value,
"end_value": end_value,
"trigger_separation": trigger_separation,
"rate_for_controlable": rate_for_controlable}
param_controller = ParameterController(identifier, self.meas_setup, desired_controlable, trigger,
self.tasks)
# answer being 1 means "specific values" parameter controller should be used
elif answer["answer"] == 1:
specific_values_list = []
UserInput.post_status("We will generate a nice and shiny list for you. But first, I need some "
"answers")
UserInput.post_status("You are able to create an unlimited amount of lists, not just 1 separate "
"low frequency list")
# We promised unlimited lists, now we have to deliver
user_wants_one_more_list = True
while user_wants_one_more_list:
question = {"question_title": "Distribution of list",
"question_text": "Do you want a linear, a logarithmic distribution or single point?",
"default_answer": 1,
"optiontype": "multi_choice",
"valid_options": ["linear",
"logarithmic",
"single point",
"cancel"]}
answer = self._get_input(custom_type,question,template)
generated_values = []
if answer["answer"] == 0:
# linear distribution
# I don't know whether there will ever be the case where a negative value is desired.
# Omitting right now
UserInput.post_status("-------- Lists always include upper boundary --------")
question = {"question_title": "Start value",
"question_text": "What is the desired start value? Please enter the value, both "
"scientific and standard notation supported (eg 1234.56 or 1e7)",
"default_answer": 1,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
start_value = answer["answer"]
question = {"question_title": "End value",
"question_text": "What is the desired end value? Please enter the value, both "
"scientific and standard notation supported (eg 1234.56 or 1e7)",
"default_answer": 1e7,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
end_value = answer["answer"]
question = {"question_title": "Interval or number of values",
"question_text": "Do you want interval based or number of points based?",
"default_answer": 1,
"optiontype": "multi_choice",
"valid_options": ["interval based",
"number of points based"]}
answer = self._get_input(custom_type,question,template)
if answer["answer"] == 0: # interval should be used to generate the list
question = {"question_title": "Step size",
"question_text": "What step interval shall be used dear all mighty user?",
"default_answer": 30.0,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
step_interval = answer["answer"]
generated_values = Helper.create_valuelist_according_to_distribution(start_value, end_value,
None, step_interval,
"linear")
elif answer["answer"] == 1: # amount of steps is defined and should be used
question = {"question_title": "Amount of steps",
"question_text": "How many steps do you want?",
"default_answer": 10.0,
"optiontype": "free_choice",
"valid_options_lower_limit": 1.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e0}
answer = self._get_input(custom_type,question,template)
amount_of_values = answer["answer"]
generated_values = Helper.create_valuelist_according_to_distribution(start_value, end_value,
amount_of_values, None,
"linear")
elif answer["answer"] == 1:
# Default case - logarithmic distribution
UserInput.post_status("-------- Lists always include upper boundary --------")
question = {"question_title": "Start value",
"question_text": "What is the desired start value? Please enter the value, both "
"scientific and standard notation supported (eg 1234.56 or 1e7)",
"default_answer": 1,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
start_value = answer["answer"]
question = {"question_title": "End value",
"question_text": "What is the desired end value? Please enter the value, both "
"scientific and standard notation supported (eg 1234.56 or 1e7)",
"default_answer": 1e7,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
end_value = answer["answer"]
question = {"question_title": "Amount of steps",
"question_text": "How many steps do you want?",
"default_answer": 30.0,
"optiontype": "free_choice",
"valid_options_lower_limit": 1.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e0}
answer = self._get_input(custom_type,question,template)
amount_of_values = answer["answer"]
generated_values = Helper.create_valuelist_according_to_distribution(start_value, end_value,
amount_of_values, None,
"logarithmic")
elif answer["answer"] == 2:
# single point
question = {"question_title": "Enter desired point",
"question_text": "What is your desired point?",
"default_answer": 45.0,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
generated_values.append(answer["answer"])
elif answer["answer"] == 3:
user_wants_one_more_list = False
# Show the user the list and let him confirm it before adding it to the thing
UserInput.post_status("---------- List as follows ----------")
for index, value in enumerate(generated_values):
UserInput.post_status(str(index) + ": " + str(value))
question = {"question_title": "List health status",
"question_text": "Is this list fine?",
"default_answer": True,
"optiontype": "yes_no"}
answer = self._get_input(custom_type,question,template)
if answer: # means: User finds this list to suit his needs
for value in generated_values:
specific_values_list.append(value)
# We catch a cancel call in the original question, we then shouldn't ask the user whether he wants
# another list then again
if user_wants_one_more_list:
question = {"question_title": "One more?!",
"question_text": "Do you want to create another list/point?",
"default_answer": True,
"optiontype": "yes_no"}
answer = self._get_input(custom_type,question,template)
user_wants_one_more_list = answer["answer"]
if user_wants_one_more_list:
UserInput.post_status("---------- Current list is as follows ----------")
for index, value in enumerate(specific_values_list):
UserInput.post_status(str(index) + ": " + str(value))
identifier = self._get_id_for_task_insert_into_queue(custom_type,template)
trigger = {"specific_values": specific_values_list}
param_controller = ParameterController(identifier, self.meas_setup, desired_controlable, trigger,
self.tasks)
self.tasks.append(param_controller)
# 1 = DataAcquisition, code path to create a new Data Acquisition task
elif answer["answer"] == 1:
question = {"question_title": "Choosing the measurable",
"question_text": "What do you want to measure?",
"default_answer": 0,
"optiontype": "multi_choice",
"valid_options": available_measurables}
answer = self._get_input(custom_type,question,template)
measurable_to_measure = available_raw_measurables[answer["answer"]]
question = {"question_title": "max deviation values during sub_tasks",
"question_text": "It is possible to output the maximum deviation while sub_tasks were run. "
"Do you want that?",
"default_answer": True,
"optiontype": "yes_no"}
answer = self._get_input(custom_type,question,template)
user_wants_max_deviation = answer["answer"]
identifier = self._get_id_for_task_insert_into_queue(custom_type,template)
new_data_acqu = DataAcquisition(identifier, self.meas_setup, measurable_to_measure,
user_wants_max_deviation, self.tasks)
self.tasks.append(new_data_acqu)
# 2 = Trigger - either measurable triggered or time triggered for now
elif answer["answer"] == 2:
question = {"question_title": "Time or Measurable triggered",
"question_text": "Do you want to have it triggered by time or a measurable "
"(not implemented yet)?",
"default_answer": 0,
"optiontype": "multi_choice",
"valid_options": ["time", "meausurable"]}
answer = self._get_input(custom_type,question,template)
# 0 means time triggered
if answer["answer"] == 0:
question = {"question_title": "Total time",
"question_text": "Please enter the total time in minutes",
"default_answer": 10.0,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
total_time = answer["answer"]
question = {"question_title": "Trigger separation",
"question_text": "Please enter the trigger separation in minutes. "
"Eg every 3 minutes",
"default_answer": 3.0,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
trigger_separation = answer["answer"]
trigger = {"total_time_span": total_time, "trigger_separation": trigger_separation}
identifier = self._get_id_for_task_insert_into_queue(custom_type,template)
new_trigger = Trigger(identifier, self.meas_setup, trigger, self.tasks)
self.tasks.append(new_trigger)
# 1 means measurable triggered acquisition
elif answer["answer"] == 1:
question = {"question_title": "Measurable used to trigger sub task",
"question_text": "Which measurable do you want to use to trigger a sub task?",
"default_answer": 0,
"optiontype": "multi_choice",
"valid_options": available_raw_measurables}
answer = self._get_input(custom_type,question,template)
measurable_to_use_as_trigger = available_measurables[answer["answer"]]
question = {"question_title": "Trigger value",
"question_text": "At what value should it trigger?",
"default_answer": 310.0,
"optiontype": "free_choice",
"valid_options_lower_limit": 0.0,
"valid_options_upper_limit": 1e64,
"valid_options_steplength": 1e16}
answer = self._get_input(custom_type,question,template)
triggering_value = answer["answer"]
question = {"question_title": "Trigger threshold direction",
"question_text": "Should it be triggered when below the specified value? "
"(saying NO means it triggers above the value",
"default_answer": True,
"optiontype": "yes_no"}
answer = self._get_input(custom_type,question,template)
trigger_when_below = answer["answer"]
trigger = {"acquis_triggering_measurable": measurable_to_use_as_trigger,
"acquis_triggering_value": triggering_value,
"acquis_triggered_when_below_value": trigger_when_below}
identifier = self._get_id_for_task_insert_into_queue(custom_type,template)
# new_trigger = Trigger...
# self.tasks.append(new_trigger)...
# a task itself is an object and not the identifier but we need to sort by identifier - lambda is a way to
# access the underlying identifier for sorting.
# As we have a sort of arbitrary insertion point for tasks into the list, we append and then sort the list. To
# never screw up, we directly sort after each insert/append.
self.tasks.sort(key=lambda x: x.identifier)
def _get_id_for_task_insert_into_queue(self,custom_type=True,template=[]):
if len(self.tasks) == 0:
id_for_task = [0]
else:
available_ids = []
task_id_list = []
for item in self.tasks:
current_identifier = item.identifier.copy()
task_id_list.append(current_identifier)
# After this, we have a nice list containing all the identifiers and also knowledge about
# what the deepest level is
for index, item in enumerate(task_id_list):
own_length = len(item)
later_own_index_length_exists = False
has_sub_identifier = False
smaller_list = task_id_list[(index + 1):]
for subitem in smaller_list:
if len(subitem) < own_length:
break
elif len(subitem) == own_length:
later_own_index_length_exists = True
elif len(subitem) > own_length:
has_sub_identifier = True
if not has_sub_identifier:
new_sub_identifier = item.copy()
new_sub_identifier.append(0)
available_ids.append(new_sub_identifier)
if not later_own_index_length_exists:
new_identifier_one = item.copy()
new_identifier_one[len(new_identifier_one) - 1] += 1
available_ids.append(new_identifier_one)
available_string_ids = []
available_ids.sort()
for item in available_ids:
available_string_ids.append(str(item))
self.print_current_task_list()
question = {"question_title": "Insertion point",
"question_text": "Which insertion point do you want to use?",
"default_answer": 0,
"optiontype": "multi_choice",
"valid_options": available_string_ids}
answer = answer = self._get_input(custom_type,question,template)
id_for_task = available_ids[answer["answer"]]
return id_for_task
def print_current_task_list(self):
UserInput.post_status("This is the current task list:")
for task in self.tasks:
UserInput.post_status(str(task.identifier) + "task " + task.generate_one_line_summary())
def measure(self):
"""
We start going through all tasks and every task starts sub_tasks accordingly
"""
first_temp_file = True
self._prepare_before_measuring()
for task in self.tasks:
task.start()
for task in self.tasks:
if len(task.identifier) == 1:
task.should_do_now = True
while task.should_do_now:
if first_temp_file:
main_db.pickle_database("_autosave1")
first_temp_file = False
time.sleep(300)
else:
main_db.pickle_database("_autosave2")
first_temp_file = True
time.sleep(300)
task.should_do_now = False
for task in self.tasks:
task.should_be_running = False
task.join()
self.meas_setup.measurement_done()
def _prepare_before_measuring(self):
# save the current task list in the database. Crucial for later data manipulation
task_list = []
for task in self.tasks:
task_list.append(str(task.identifier) + "task " + task.generate_one_line_summary())
main_db.tasks = task_list
def remove_task(self):
"""Method to remove a task from the task list
"""
one_line_summary_of_tasks = []
for task in self.tasks:
one_line_summary_of_tasks.append(str(task.identifier) + "task " + task.generate_one_line_summary())
question = {"question_title": "Remove task",
"question_text": "Please select the task you want to have removed.",
"default_answer": 2,
"optiontype": "multi_choice",
"valid_options": one_line_summary_of_tasks}
to_be_removed_index = UserInput.ask_user_for_input(question)["answer"]
sub_tasks = Helper.check_for_sub_tasks(self.tasks[to_be_removed_index].identifier, self.tasks)
if len(sub_tasks) == 0:
self.tasks.pop(to_be_removed_index)
else:
question = {"question_title": "Warning, Sub_tasks detected!",
"question_text": "The selected task has sub_tasks. Do you really want to delete it?",
"default_answer": True,
"optiontype": "yes_no"}
user_wants_removal = UserInput.ask_user_for_input(question)["answer"]
if user_wants_removal:
amount_of_subtasks = len(sub_tasks)
for index in range(amount_of_subtasks):
self.tasks.pop(to_be_removed_index + 1 + index)
self.tasks.pop(to_be_removed_index)
else:
UserInput.post_status("Didn't remove a thing. Carry on!")
def _get_input(self,custom,question,template=[]):
"""
Parameters
----------
custom : Boolean
Decide wether to customize your measurement or use a template instead.
question :
Enter the posed question.
template :
Enter a template, if custom=False.
Returns chosen task.
-------
Private function in order to implement template measurements. A list with all input parameters