Commit 7b85ae68 authored by whooie's avatar whooie
Browse files

keep add convenient records for data file paths and propagate through scripts...

keep add convenient records for data file paths and propagate through scripts and top-level imports; also adjust for file IO adjustments for sequences
parent f129c900
......@@ -2,10 +2,7 @@ from lib import *
from lib import CONNECTIONS as C
import datetime, sys, pathlib
outdir = (
pathlib.Path(r"C:\Users\EW\University of Illinois - Urbana\Covey Lab - Documents\Data\absorption imaging")
.joinpath(str(datetime.datetime.now()).replace(':', '').split('.')[0])
)
outdir = DATADIRS.absorption_imaging.joinpath(get_timestamp())
comments = """
62 MHz/26.41 dBm AOM, 4.00 A dispenser, 120.5 A MOT coil, keep MOT beams on
......@@ -14,7 +11,7 @@ comments = """
# BUILD SEQUENCE
t0 = 10.0 # turn off the MOT
t1 = t0 + 1.0e-3 # start imaging sequence
SEQ = SuperSequence({
SEQ = SuperSequence(outdir, "sequence", {
"Sequence": (Sequence
.digital_hilo(*C.dummy, 0.0, t0 + 0.2)
.with_color("k").with_stack_idx(0)
......@@ -93,7 +90,7 @@ class AbsorptionImaging(Controller):
def run_sequence(self, *args):
self.comp.run()
SEQ.save(outdir)
SEQ.save()
def run_camera(self, *args):
frames = self.cam.acquire_frames(3)
......
......@@ -2,10 +2,7 @@ from lib import *
from lib import CONNECTIONS as C
import datetime, sys, pathlib
outdir = (
pathlib.Path(r"C:\Users\EW\University of Illinois - Urbana\Covey Lab - Documents\Data\fluorescence imaging")
.joinpath(str(datetime.datetime.now()).replace(':', '').split('.')[0])
)
outdir = DATADIRS.fluorescence_imaging.joinpath(get_timestamp())
comments = """
62 MHz/26.41 dBm AOM, 4.00 A dispenser, 120.5 A MOT coil
......@@ -21,7 +18,7 @@ seq_camera = Sequence()
for tau in T_camera:
seq_camera += Sequence.digital_pulse(*C.camera, t0 + tau, 1000.0e-6)
SEQ = SuperSequence({
SEQ = SuperSequence(outdir, "sequence", {
"Sequence": (Sequence
.digital_hilo(*C.dummy, 0.0, t0 + T_camera.max() + 100e-3)
.with_color("k").with_stack_idx(0)
......@@ -79,7 +76,7 @@ class AtomTrajectory(Controller):
def run_sequence(self, *args):
self.comp.run()
SEQ.save(outdir)
SEQ.save()
def run_camera(self, *args):
frames = self.cam.acquire_frames(len(T_camera))
......
......@@ -2,10 +2,7 @@ from lib import *
from lib import CONNECTIONS as C
import datetime, sys, pathlib
outdir = (
pathlib.Path(r"C:\Users\EW\University of Illinois - Urbana\Covey Lab - Documents\Data\fluorescence imaging")
.joinpath(str(datetime.datetime.now()).replace(':', '').split('.')[0])
)
outdir = DATADIRS.fluorescence_imaging.joinpath(get_timestamp())
comments = """
62 MHz/26.41 dBm AOM, 4.00 A dispenser, 120.5 A MOT coil
......@@ -24,7 +21,7 @@ camera_config = {
# BUILD SEQUENCE
t0 = 10.0 # turn off the push beam
SEQ = SuperSequence({
SEQ = SuperSequence(outdir, "sequence", {
"Sequence": (Sequence
.digital_hilo(*C.dummy, 0.0, t0 + 100e-3)
.with_color("k").with_stack_idx(0)
......@@ -75,7 +72,7 @@ class FluorescenceImaging(Controller):
def run_sequence(self, *args):
self.comp.run()
SEQ.save(outdir)
SEQ.save()
def run_camera(self, *args):
frames = self.cam.acquire_frames(1)
......
......@@ -21,7 +21,15 @@ from lib.control import (
Controller,
)
from lib.system import (
DATADIRS,
MAIN,
CONNECTIONS,
FLIR,
)
from lib.dataio import (
get_timestamp,
fresh_filename,
DataPaths,
gen_table_fmt,
print_write,
)
import datetime
import pathlib
def gen_table_fmt(label_fmts, s=" ", L=12, P=5, K=2) -> (str, str):
"""
Generate the column labels and format string of a table from a list of
tuples following
(
'column label',
x in {'s','s>','i','f','g','e'},
{l: length override, p: precision override} (optional)
)
"""
head = ""
lines = ""
fmt = ""
names = list()
for label_fmt in label_fmts:
names.append(label_fmt[0])
overrides = dict() if len(label_fmt) < 3 else label_fmt[2]
l = overrides.get("l",
max(int((len(label_fmt[0])+K-1)/K)*K, L*(label_fmt[1] in ['e','f','g']))
)
p = overrides.get("p",
l-7 if (l-7 >= 1 and l-7 <= P) else P
)
head += "{:"+str(l)+"s}"+s
lines += l*"-" + s
if label_fmt[1] == 's':
fmt += "{:"+str(l)+"s}"+s
elif label_fmt[1] == 's>':
fmt += "{:>"+str(l)+"s}"+s
elif label_fmt[1] == 'i':
fmt += "{:"+str(l)+".0f}"+s
elif label_fmt[1] in ['e', 'f', 'g']:
fmt += "{:"+str(l)+"."+str(p)+label_fmt[1]+"}"+s
else:
raise Exception("Format is not one of {'s', 's>', 'i', 'f', 'g', 'e'}")
head = head[:-len(s)]
lines = lines[:-len(s)]
fmt = fmt[:-len(s)]
return head.format(*names)+"\n"+lines, fmt
def print_write(outfile, s, end="\n", flush=True) -> None:
print(s, end=end, flush=flush)
outfile.write(s+end)
if flush:
outfile.flush()
return None
def get_timestamp():
return format(datetime.datetime.now(), "%Y-%m-%d_%H%M%S")
class DataPaths:
def __init__(self, **paths: dict[str, pathlib.Path]):
self.paths = paths
def __getattr__(self, attr):
return self.paths[attr]
def fresh_filename(path: pathlib.Path, overwrite: bool=False) -> pathlib.Path:
if overwrite:
return path
_path = path
while _path.is_file():
print(f"WARNING: found existinf file {_path}")
_path = _path.with_stem(_path.stem + "_")
print(f"Write instead to {_path}")
return _path
......@@ -11,27 +11,18 @@ import toml
import warnings
import PIL.Image
import lib.plotdefs as pd
import lib.dataio as io
H = 6.626070040e-34
C = 2.99792458e+8
DEF_DX = 3.45 # pixel size for Flir Grasshopper [um]
def _fresh_filename(path: pathlib.Path, overwrite: bool=False) -> pathlib.Path:
if overwrite:
return path
_path = path
while _path.is_file():
print(f"WARNING: found existinf file {_path}")
_path = _path.with_stem(_path.stem + "_")
print(f"Write instead to {_path}")
return _path
class ImagingData:
def __init__(self, name: str, arrays: dict[str, np.ndarray],
def __init__(self, outdir: pathlib.Path, arrays: dict[str, np.ndarray],
config: dict[str, ...], comments: str=None,
results: dict[str, ...]=None):
self.name = name
self.outdir = outdir
self.arrays = arrays
self.config = config
self.comments = str() if comments is None else comments
......@@ -39,27 +30,27 @@ class ImagingData:
def save(self, target: pathlib.Path=None, overwrite: bool=False,
printflag: bool=True):
T = pathlib.Path(self.name) if target is None else target
if printflag: print(f"[imaging] Saving data to {T}:")
T = self.outdir if target is None else target
if printflag: print(f"[imaging] Saving data to '{T}':")
if not T.is_dir():
if printflag: print(f"[imaging] mkdir {T}")
T.mkdir(parents=True, exist_ok=True)
arrays_file = _fresh_filename(T.joinpath("arrays.npz"), overwrite)
arrays_file = io.fresh_filename(T.joinpath("arrays.npz"), overwrite)
if printflag: print("[imaging] save arrays")
np.savez_compressed(arrays_file, **self.arrays)
config_file = _fresh_filename(T.joinpath("config.toml"), overwrite)
config_file = io.fresh_filename(T.joinpath("config.toml"), overwrite)
if printflag: print("[imaging] save camera config")
with config_file.open('w') as outfile:
toml.dump(self.config, outfile)
comments_file = _fresh_filename(T.joinpath("comments.txt"), overwrite)
comments_file = io.fresh_filename(T.joinpath("comments.txt"), overwrite)
if printflag: print("[imaging] save comments")
comments_file.write_text(self.comments)
if self.results is not None:
results_file = _fresh_filename(
results_file = io.fresh_filename(
T.joinpath("results.toml"), overwrite)
if printflag: print("[imaging] save results")
with results_file.open('w') as outfile:
......@@ -68,22 +59,23 @@ class ImagingData:
return self
@staticmethod
def load(target: pathlib.Path, name: str=None, printflag: bool=True):
name = target.stem if name is None else name
if printflag: print(f"[imaging] Loading data from {target}:")
def load(target: pathlib.Path, outdir: pathlib.Path=None,
printflag: bool=True):
outdir = target if outdir is None else target
if printflag: print(f"[imaging] Loading data from target '{target}':")
if not target.is_dir():
raise Exception(f"Target {target} does not exist")
raise Exception(f"Target '{target}' does not exist")
arrays_file = target.joinpath("arrays.npz")
if printflag: print("[imaging] load arrays")
if not arrays_file.is_file():
raise Exception(f"Arrays file {arrays_file} does not exist")
raise Exception(f"Arrays file '{arrays_file}' does not exist")
arrays = np.load(arrays_file)
config_file = target.joinpath("config.toml")
if printflag: print("[imaging] load camera config")
if not config_file.is_file():
raise Exception(f"Config file {config_file} does not exist")
raise Exception(f"Config file '{config_file}' does not exist")
config = toml.load(config_file)
comments_file = target.joinpath("comments.txt")
......@@ -96,11 +88,11 @@ class ImagingData:
results = results_file.read_text() \
if results_file.is_file() else None
#if printflag: print(" Done.")
return AbsorptionData(name, arrays, config, comments, results)
return AbsorptionData(outdir, arrays, config, comments, results)
def render_arrays(self, target: pathlib.Path=None, dx=None,
printflag: bool=True):
T = pathlib.Path(self.name) if target is None else target
T = self.outdir if target is None else target
T = T.joinpath("images")
_dx = self.config.get("bin_size", 1) * (DEF_DX if dx is None else dx)
if printflag: print(f"[imaging] Render images to {T}:")
......@@ -108,7 +100,7 @@ class ImagingData:
if printflag: print(f"[imaging] mkdir {T}")
T.mkdir(parents=True, exist_ok=True)
for label, array in self.arrays.items():
if printflag: print(f"[imaging] render {label}")
if printflag: print(f"[imaging] render '{label}'")
if label == "od":
warnings.filterwarnings("ignore")
H, W = array.shape
......
......@@ -6,9 +6,19 @@ from lib.ew import (
AnalogConnection as Analog,
)
from lib.flir import Grasshopper
from lib.dataio import DataPaths
import pathlib
DATA_HOME = pathlib.Path(r"C:\Users\EW\University of Illinois - Urbana\Covey Lab - Documents\Data")
DATADIRS = DataPaths(
home = DATA_HOME,
absorption_imaging = DATA_HOME.joinpath("absorption imaging"),
fluorescence_imaging = DATA_HOME.joinpath("fluorescence imaging"),
)
CONNECTIONS = ConnectionLayout(
coils = Digital(0, 2, 1),
coil_cur = Analog(1, 4.01),
shims = Digital(0, 0, 0),
mot_laser = Digital(1, 7, 1),
push = Digital(0, 3, 1),
......
......@@ -2,10 +2,7 @@ from lib import *
from lib import CONNECTIONS as C
import datetime, sys, pathlib
outdir = (
pathlib.Path(r"C:\Users\EW\University of Illinois - Urbana\Covey Lab - Documents\Data\fluorescence imaging")
.joinpath(str(datetime.datetime.now()).replace(':', '').split('.')[0])
)
outdir = DATADIRS.fluorescence_imaging.joinpath(get_timestamp())
comments = """
62 MHz/26.41 dBm AOM, 4.00 A dispenser, 120.5 A MOT coil
......@@ -26,7 +23,7 @@ t0 = 5.0 # take a picure at t0
DT = np.linspace(0.0, 2.5, 50)
def make_sequence(dt):
SEQ = SuperSequence({
SEQ = SuperSequence(outdir.joinpath("sequences"), f"dt_{dt:.5f}", {
"Sequence": (Sequence
.digital_hilo(*C.dummy, 0.0, t0 + 100e-3)
.with_color("k").with_stack_idx(0)
......@@ -89,6 +86,7 @@ class LoadingRate(Controller):
.run()
.clear()
)
SEQ.save()
def run_camera(self, *args):
frames = self.cam.acquire_frames(len(DT))
......@@ -103,7 +101,8 @@ class LoadingRate(Controller):
data.render_arrays()
def cmd_visualize(self, *args):
SEQ.draw_simple().show().close()
make_sequence(float(args[0]) if len(args) > 0 else DT.max()) \
.draw_simple().show().close()
sys.exit(0)
if __name__ == "__main__":
......
......@@ -2,10 +2,7 @@ from lib import *
from lib import CONNECTIONS as C
import datetime, sys, pathlib
outdir = (
pathlib.Path(r"C:\Users\EW\University of Illinois - Urbana\Covey Lab - Documents\Data\fluorescence imaging")
.joinpath(str(datetime.datetime.now()).replace(':', '').split('.')[0])
)
outdir = DATADIRS.fluorescence_imaging.joinpath(get_timestamp())
comments = """
62 MHz/26.41 dBm AOM, 4.00 A dispenser, 120.5 A MOT coil
......@@ -26,7 +23,7 @@ t0 = 5.0 # let the MOT sit for t0 seconds
DT = np.append(np.array([0]), np.linspace(3e-3, 50e-3, 20))
def make_sequence(dt):
SEQ = SuperSequence({
SEQ = SuperSequence(outdir.joinpath("sequences"), f"dt_{dt:.5f}", {
"Sequence": (Sequence
.digital_hilo(*C.dummy, 0.0, t0 + DT.max() + 100e-3)
.with_color("k").with_stack_idx(0)
......@@ -97,7 +94,8 @@ class MOTSurvival(Controller):
data.render_arrays()
def cmd_visualize(self, *args):
SEQ.draw_simple().show().close()
make_sequence(float(args[0]) if len(args) > 0 else DT.max()) \
.draw_simple().show().close()
sys.exit(0)
if __name__ == "__main__":
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment