Skip to content

Commit

Permalink
Removed the use of quamash which is outdated. Single works fine but i…
Browse files Browse the repository at this point in the history
…s delayed in IPython (jupyter)
  • Loading branch information
michaelcroquette committed Apr 9, 2024
1 parent 77e1eff commit 7f67ce2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 42 deletions.
40 changes: 16 additions & 24 deletions pyrpl/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
It provides the 3 functions:
* ensure_future(coroutine): schedules the task described by the coroutine and
returns a Future that can be used as argument of
the follo<ing functions:
the following functions:
* sleep(time_s): blocks the commandline for time_s, without blocking other
tasks such as gui update...
* wait(future, timeout=None): blocks the commandline until future is set or
Expand Down Expand Up @@ -36,10 +36,11 @@
import logging
from qtpy import QtCore, QtWidgets
import asyncio
from asyncio import Future, iscoroutine
from asyncio import Future, iscoroutine, TimeoutError, get_event_loop, wait_for
import quamash
import sys

import nest_asyncio
nest_asyncio.apply()

logger = logging.getLogger(name=__name__)

Expand All @@ -57,24 +58,26 @@
APP = QtWidgets.QApplication(['pyrpl'])


LOOP = quamash.QEventLoop() # Since tasks scheduled in this loop seem to
#LOOP = quamash.QEventLoop() # Since tasks scheduled in this loop seem to
# fall in the standard QEventLoop, and we never explicitly ask to run this
# loop, it might seem useless to send all tasks to LOOP, however, a task
# scheduled in the default loop seem to never get executed with IPython
# kernel integration.
#asyncio.set_event_loop(LOOP)

async def sleep_async(time_s):
"""
Replaces asyncio.sleep(time_s) inside coroutines. Deals properly with
IPython kernel integration.
"""
await asyncio.sleep(time_s, loop=LOOP)
await asyncio.sleep(time_s)

def ensure_future(coroutine):
"""
Schedules the task described by the coroutine. Deals properly with
IPython kernel integration.
"""
LOOP = get_event_loop()
return asyncio.ensure_future(coroutine, loop=LOOP)

def wait(future, timeout=None):
Expand All @@ -89,25 +92,14 @@ def curve(self):
BEWARE: never use wait in a coroutine (use builtin await instead)
"""
assert isinstance(future, Future) or iscoroutine(future)
new_future = ensure_future(asyncio.wait({future},
timeout=timeout,
loop=LOOP))
#if sys.version>='3.7': # this way, it was not possible to execute wait
# behind a qt slot !!!
# LOOP.run_until_complete(new_future)
# done, pending = new_future.result()
#else:
loop = QtCore.QEventLoop()
def quit(*args):
loop.quit()
new_future.add_done_callback(quit)
loop.exec_()
done, pending = new_future.result()
if future in done:
return future.result()
else:
raise TimeoutError("Timout exceeded")
#assert isinstance(future, Future) or iscoroutine(future)
new_future = wait_for(future, timeout)
LOOP = get_event_loop()
try:
return LOOP.run_until_complete(new_future)
except TimeoutError:
print(("Timout exceeded"))


def sleep(time_s):
"""
Expand Down
8 changes: 4 additions & 4 deletions pyrpl/hardware_modules/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,29 +495,29 @@ def _data_ch1(self):
return np.array(
np.roll(self._rawdata_ch1, - (self._write_pointer_trigger +
self._trigger_delay_register + 1)),
dtype=np.float) / 2 ** 13
dtype=float) / 2 ** 13

@property
def _data_ch2(self):
""" acquired (normalized) data from ch2"""
return np.array(
np.roll(self._rawdata_ch2, - (self._write_pointer_trigger +
self._trigger_delay_register + 1)),
dtype=np.float) / 2 ** 13
dtype=float) / 2 ** 13

@property
def _data_ch1_current(self):
""" (unnormalized) data from ch1 while acquisition is still running"""
return np.array(
np.roll(self._rawdata_ch1, -(self._write_pointer_current + 1)),
dtype=np.float) / 2 ** 13
dtype=float) / 2 ** 13

@property
def _data_ch2_current(self):
""" (unnormalized) data from ch2 while acquisition is still running"""
return np.array(
np.roll(self._rawdata_ch2, -(self._write_pointer_current + 1)),
dtype=np.float) / 2 ** 13
dtype=float) / 2 ** 13

@property
def times(self):
Expand Down
72 changes: 58 additions & 14 deletions pyrpl/test/test_ipython_notebook/test_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": [
Expand All @@ -14,9 +17,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#define hostname\n",
"HOSTNAME = '_FAKE_'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'python_sys_version'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[2], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msys\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m sys\u001b[38;5;241m.\u001b[39mversion\u001b[38;5;241m==\u001b[39m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43menviron\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpython_sys_version\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\n",
"File \u001b[1;32m~\\AppData\\Local\\miniconda3\\lib\\os.py:680\u001b[0m, in \u001b[0;36m_Environ.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 677\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_data[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mencodekey(key)]\n\u001b[0;32m 678\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m:\n\u001b[0;32m 679\u001b[0m \u001b[38;5;66;03m# raise KeyError with the original key value\u001b[39;00m\n\u001b[1;32m--> 680\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 681\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdecodevalue(value)\n",
"\u001b[1;31mKeyError\u001b[0m: 'python_sys_version'"
]
}
],
"source": [
"import os\n",
"import sys\n",
Expand All @@ -25,9 +51,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\michael.croquette\\AppData\\Local\\miniconda3\\lib\\site-packages\\paramiko\\transport.py:219: CryptographyDeprecationWarning: Blowfish has been deprecated\n",
" \"class\": algorithms.Blowfish,\n",
"WARNING:pyrpl.redpitaya:Simulating RedPitaya because (hostname==_FAKE_). Incomplete functionality possible. \n"
]
}
],
"source": [
"from pyrpl import Pyrpl\n",
"p = Pyrpl(hostname=HOSTNAME,\n",
Expand All @@ -39,9 +75,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:pyrpl.modules:Register input1 of module scope has value 1, which does not correspond to selected option 10. Setting to 'in1'. \n"
]
}
],
"source": [
"p.rp.scope.setup(duration=0.0001, trace_average=1)\n",
"curve = p.rp.scope.single()\n",
Expand All @@ -50,17 +94,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": []
"source": [
"a=2"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -74,9 +118,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}

0 comments on commit 7f67ce2

Please sign in to comment.