Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot saving to be specified in config.user #157

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
71 changes: 41 additions & 30 deletions examples/dataset/Example of doND wrappers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -24,7 +24,10 @@
"from qcodes.dataset.database import initialise_database\n",
"from qcodes.dataset.experiment_container import new_experiment\n",
"from qcodes.dataset.plotting import plot_by_id\n",
"import plottr\n",
"try:\n",
" import plottr\n",
"except ModuleNotFoundError:\n",
" pass\n",
"import qcodes.config\n",
"from qcodes.tests.instrument_mocks import DummyChannelInstrument"
]
Expand All @@ -46,17 +49,25 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Upgrading database: : 0it [00:00, ?it/s]\n",
"Upgrading database: : 0it [00:00, ?it/s]\n"
]
},
{
"data": {
"text/plain": [
"doNd-tutorial#no sample#10@C:\\Users\\jenielse/experiments.db\n",
"-----------------------------------------------------------"
"doNd-tutorial#no sample#1@/Users/natalie/experiments.db\n",
"-------------------------------------------------------"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -68,7 +79,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -81,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -100,7 +111,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -120,15 +131,15 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting experimental run with id: 27\n",
"plot by id took 0.19638419151306152\n"
"Starting experimental run with id: 1\n",
"plot by id took 0.037342071533203125\n"
]
}
],
Expand All @@ -138,7 +149,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {
"scrolled": false
},
Expand All @@ -147,8 +158,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Starting experimental run with id: 28\n",
"plot by id took 1.528921365737915\n"
"Starting experimental run with id: 2\n",
"plot by id took 0.3784291744232178\n"
]
}
],
Expand All @@ -175,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -197,15 +208,15 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"called before the loop\n",
"Starting experimental run with id: 29\n",
"Starting experimental run with id: 6\n",
"called at each step\n",
"called at each step\n",
"called at each step\n",
Expand All @@ -217,7 +228,7 @@
"called at each step\n",
"called at each step\n",
"called after the loop\n",
"plot by id took 0.1400294303894043\n"
"plot by id took 0.03285670280456543\n"
]
}
],
Expand All @@ -228,15 +239,15 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"called before the loop\n",
"Starting experimental run with id: 30\n",
"Starting experimental run with id: 7\n",
"called before inner loop\n",
"called at each step\n",
"called at each step\n",
Expand All @@ -250,7 +261,7 @@
"called at each step\n",
"called after inner loop\n",
"called after the loop\n",
"plot by id took 0.18652963638305664\n"
"plot by id took 0.06169295310974121\n"
]
}
],
Expand All @@ -272,7 +283,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -281,15 +292,15 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting experimental run with id: 31\n",
"plot by id took 0.15599322319030762\n"
"Starting experimental run with id: 8\n",
"plot by id took 0.03528785705566406\n"
]
}
],
Expand Down Expand Up @@ -328,7 +339,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -337,15 +348,15 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting experimental run with id: 35\n",
"plot by id took 0.07499814033508301\n"
"Starting experimental run with id: 9\n",
"plot by id took 0.045063018798828125\n"
]
}
],
Expand Down
87 changes: 56 additions & 31 deletions qdev_wrappers/dataset/doNd.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ def do0d(*param_meas: Union[_BaseParameter, Callable[[], None]],
dataid = datasaver.run_id

if do_plot is True:
ax, cbs = _save_image(datasaver)
plt.ioff()
start = time.time()
axes, cbs = plot_by_id(dataid)
stop = time.time()
print(f"plot by id took {stop-start}")
plt.ion()
exp_name = datasaver._dataset.exp_name
sample_name = datasaver._dataset.sample_name
save_image(axes, exp_name=exp_name,
sample_name=sample_name, run_id=dataid)
else:
ax = None,
axes = None,
cbs = None

return dataid, ax, cbs
return dataid, axes, cbs


def do1d(param_set: _BaseParameter, start: number, stop: number,
Expand Down Expand Up @@ -136,14 +145,23 @@ def do1d(param_set: _BaseParameter, start: number, stop: number,
dataid = datasaver.run_id # convenient to have for plotting

if do_plot is True:
ax, cbs = _save_image(datasaver)
plt.ioff()
start = time.time()
axes, cbs = plot_by_id(dataid)
stop = time.time()
print(f"plot by id took {stop-start}")
plt.ion()
exp_name = datasaver._dataset.exp_name
sample_name = datasaver._dataset.sample_name
save_image(axes, exp_name=exp_name,
sample_name=sample_name, run_id=dataid)
else:
ax = None,
axes = None,
cbs = None

if interrupted:
raise KeyboardInterrupt
return dataid, ax, cbs
return dataid, axes, cbs


def do2d(param_set1: _BaseParameter, start1: number, stop1: number,
Expand Down Expand Up @@ -235,17 +253,27 @@ def do2d(param_set1: _BaseParameter, start1: number, stop1: number,
dataid = datasaver.run_id

if do_plot is True:
ax, cbs = _save_image(datasaver)
plt.ioff()
start = time.time()
axes, cbs = plot_by_id(dataid)
stop = time.time()
print(f"plot by id took {stop-start}")
plt.ion()
exp_name = datasaver._dataset.exp_name
sample_name = datasaver._dataset.sample_name
save_image(axes, exp_name=exp_name,
sample_name=sample_name, run_id=dataid)
else:
ax = None,
axes = None,
cbs = None
if interrupted:
raise KeyboardInterrupt

return dataid, ax, cbs
return dataid, axes, cbs


def _save_image(datasaver) -> AxesTupleList:
def save_image(axes, exp_name=None, sample_name=None,
run_id=None, name_extension=None, **kwargs) -> AxesTupleList:
"""
Save the plots created by datasaver as pdf and png

Expand All @@ -254,35 +282,32 @@ def _save_image(datasaver) -> AxesTupleList:
as plot.

"""
plt.ioff()
dataid = datasaver.run_id
start = time.time()
axes, cbs = plot_by_id(dataid)
stop = time.time()
print(f"plot by id took {stop-start}")

run_id = run_id or 0
mainfolder = config.user.mainfolder
experiment_name = datasaver._dataset.exp_name
sample_name = datasaver._dataset.sample_name

storage_dir = os.path.join(mainfolder, experiment_name, sample_name)
os.makedirs(storage_dir, exist_ok=True)

png_dir = os.path.join(storage_dir, 'png')
pdf_dif = os.path.join(storage_dir, 'pdf')
if exp_name and sample_name:
storage_dir = os.path.join(mainfolder, exp_name, sample_name)
os.makedirs(storage_dir, exist_ok=True)
png_dir = os.path.join(storage_dir, 'png')
pdf_dir = os.path.join(storage_dir, 'pdf')
else:
png_dir = 'png'
pdf_dir = 'pdf'

os.makedirs(png_dir, exist_ok=True)
os.makedirs(pdf_dif, exist_ok=True)
os.makedirs(pdf_dir, exist_ok=True)

save_pdf = True
save_png = True
save_pdf = config.user.get('save_pdf', True)
save_png = config.user.get('save_png', True)

for i, ax in enumerate(axes):
filename = f'{run_id}'
if name_extension is not None:
filename += '_' + name_extension
filename += f'_{i}'
if save_pdf:
full_path = os.path.join(pdf_dif, f'{dataid}_{i}.pdf')
full_path = os.path.join(pdf_dir, filename + '.pdf')
ax.figure.savefig(full_path, dpi=500)
if save_png:
full_path = os.path.join(png_dir, f'{dataid}_{i}.png')
full_path = os.path.join(png_dir, filename + '.png')
ax.figure.savefig(full_path, dpi=500)
plt.ion()
return axes, cbs