Read and Write from JSON file Tutorial¶
[1]:
#
# IMPORTS
#
%matplotlib nbagg
import matplotlib as mpl
import broadbean as bb
from broadbean.plotting import plotter
mpl.rcParams["figure.figsize"] = (8, 3)
mpl.rcParams["figure.subplot.bottom"] = 0.15
Read and Write BluePrint from JSON file Tutorial¶
Building a BluePrint¶
[2]:
# The pulsebuilding module comes with a (small) collection of functions appropriate for being segments.
ramp = bb.PulseAtoms.ramp # args: start, stop
# Create the blueprints
bp_square = bb.BluePrint()
bp_square.setSR(1e9)
bp_square.insertSegment(0, ramp, (0, 0), dur=100e-9)
bp_square.insertSegment(1, ramp, (1e-3, 1e-3), name="top", dur=100e-9)
bp_square.insertSegment(2, ramp, (0, 0), dur=100e-9)
bp_boxes = bp_square + bp_square
plotter(bp_boxes)
Writing the BluePrint to a file
[3]:
bp_boxes.write_to_json("blue.json")
Reading the BluePrint back from the file
[4]:
bp_boxes_read = bb.BluePrint.init_from_json("blue.json")
bp_boxes_read.setSR(
1e9
) # note the blueprint readback do not have a sample rate attached
plotter(bp_boxes_read)
Test if the BluePrints are identical
[5]:
print(bp_boxes == bp_boxes_read)
True
Read and Write Element from JSON file Tutorial¶
Note only works if the Element is generated from BluePrints not if the Elements contains part generated directly form a numpy array
[6]:
######################################################
# The pulsebuilding module comes with a (small) collection of functions appropriate for being segments.
ramp = bb.PulseAtoms.ramp # args: start, stop
sine = bb.PulseAtoms.sine # args: freq, ampl, off, phase
seq1 = bb.Sequence()
# We fill up the sequence by adding elements at different sequence positions.
# A valid sequence is filled from 1 to N with NO HOLES, i.e. if position 4 is filled, so must be position 1, 2, and 3
#
# Make blueprints, make elements
# Create the blueprints
bp_square = bb.BluePrint()
bp_square.setSR(1e9)
bp_square.insertSegment(0, ramp, (0, 0), dur=100e-9)
bp_square.insertSegment(1, ramp, (1e-3, 1e-3), name="top", dur=100e-9)
bp_square.insertSegment(2, ramp, (0, 0), dur=100e-9)
bp_boxes = bp_square + bp_square
#
bp_sine = bb.BluePrint()
bp_sine.setSR(1e9)
bp_sine.insertSegment(0, sine, (3.333e6, 1.5e-3, 0, 0), dur=300e-9)
bp_sineandboxes = bp_sine + bp_square
bp_sineandboxes.setSegmentMarker(
"ramp", (-0.0, 100e-9), 1
) # segment name, (delay, duration), markerID
bp_sineandboxes.setSegmentMarker(
"sine", (-0.0, 100e-9), 2
) # segment name, (delay, duration), markerID
# make marker 2 go ON halfway through the sine
# bp_rtm.setSegmentMarker('mysine', (0.75, 0.1), 2)
# create elements
elem1 = bb.Element()
elem1.addBluePrint(1, bp_boxes)
elem1.addBluePrint(3, bp_sineandboxes)
plotter(elem1)
Writing the Element to a file
[7]:
elem1.write_to_json("elem.json")
plotter(elem1)
Reading the Element back from the file
[8]:
elem_read = bb.Element.init_from_json("elem.json")
Test if the Element.description are identical
[9]:
print(
elem1.description == elem_read.description
) ## note that the elements are not identical only the discription
True
Read and Write Sequence from JSON file Tutorial¶
Note only works if the Element is generated from BluePrints not if the Elements contains part generated directly form a numpy array
[10]:
######################################################
# The pulsebuilding module comes with a (small) collection of functions appropriate for being segments.
ramp = bb.PulseAtoms.ramp # args: start, stop
sine = bb.PulseAtoms.sine # args: freq, ampl, off, phase
seq1 = bb.Sequence()
# We fill up the sequence by adding elements at different sequence positions.
# A valid sequence is filled from 1 to N with NO HOLES, i.e. if position 4 is filled, so must be position 1, 2, and 3
#
# Make blueprints, make elements
# Create the blueprints
bp_square = bb.BluePrint()
bp_square.setSR(1e9)
bp_square.insertSegment(0, ramp, (0, 0), dur=100e-9)
bp_square.insertSegment(1, ramp, (1e-3, 1e-3), name="top", dur=100e-9)
bp_square.insertSegment(2, ramp, (0, 0), dur=100e-9)
bp_boxes = bp_square + bp_square
#
bp_sine = bb.BluePrint()
bp_sine.setSR(1e9)
bp_sine.insertSegment(0, sine, (3.333e6, 1.5e-3, 0, 0), dur=300e-9)
bp_sineandboxes = bp_sine + bp_square
bp_sineandboxes.setSegmentMarker(
"ramp", (-0.0, 100e-9), 1
) # segment name, (delay, duration), markerID
bp_sineandboxes.setSegmentMarker(
"sine", (-0.0, 100e-9), 2
) # segment name, (delay, duration), markerID
# make marker 2 go ON halfway through the sine
# bp_rtm.setSegmentMarker('mysine', (0.75, 0.1), 2)
# create elements
elem1 = bb.Element()
elem1.addBluePrint(1, bp_boxes)
elem1.addBluePrint(3, bp_sineandboxes)
#
elem2 = bb.Element()
elem2.addBluePrint(3, bp_boxes)
elem2.addBluePrint(1, bp_sineandboxes)
# Fill up the sequence
seq1.addElement(1, elem1) # Call signature: seq. pos., element
seq1.addElement(2, elem2)
seq1.addElement(3, elem1)
# set its sample rate
seq1.setSR(elem1.SR)
seq1.setChannelAmplitude(1, 10e-3) # Call signature: channel, amplitude (peak-to-peak)
seq1.setChannelOffset(1, 0)
seq1.setChannelAmplitude(3, 10e-3)
seq1.setChannelOffset(3, 0)
# Here we repeat each element twice and then proceed to the next, wrapping over at the end
seq1.setSequencingTriggerWait(1, 0)
seq1.setSequencingNumberOfRepetitions(1, 2)
seq1.setSequencingEventJumpTarget(1, 0)
seq1.setSequencingGoto(1, 2)
#
seq1.setSequencingTriggerWait(2, 0)
seq1.setSequencingNumberOfRepetitions(2, 2)
seq1.setSequencingEventJumpTarget(2, 0)
seq1.setSequencingGoto(2, 3)
#
seq1.setSequencingTriggerWait(3, 0)
seq1.setSequencingNumberOfRepetitions(3, 2)
seq1.setSequencingEventJumpTarget(3, 0)
seq1.setSequencingGoto(3, 1)
plotter(seq1)
Writing the Sequence to a file¶
[11]:
seq1.write_to_json("testdata.json")
[12]:
bp_boxes.write_to_json("blue.json")
Reading the Sequence back from the file¶
[13]:
seq = bb.Sequence.init_from_json("testdata.json")
[14]:
plotter(seq)
Test if the Sequences are identical¶
[15]:
print(seq == seq1)
True
Ekstras¶
Example of how read and write blueprint from/to Sequence json file if needed.
[16]:
def readblueprint(path: str, element: int = 1, channel: int = 1):
tempseq = bb.Sequence.init_from_json(path)
return tempseq.element(element)._data[channel]["blueprint"]
[17]:
def writeblueprint(
blueprint, path: str, SR: float = 1e9, SeqAmp: float = 10e-3, SeqOffset: float = 0
): # -> None
if blueprint.SR is None:
blueprint.setSR(SR)
elemtmp = bb.Element()
seqtmp = bb.Sequence()
elemtmp.addBluePrint(1, blueprint)
seqtmp.addElement(1, elemtmp)
seqtmp.setSR(blueprint.SR)
seqtmp.setChannelAmplitude(1, SeqAmp)
seqtmp.setChannelOffset(1, 0)
seqtmp.write_to_json(path)
[18]:
writeblueprint(bp_boxes, "boxes.json")
[19]:
bp_one_two = readblueprint("testdata.json", element=2, channel=1)
plotter(bp_one_two)
[ ]: