From 4d9ce1a204a8452d3821e30af481df3412801390 Mon Sep 17 00:00:00 2001
From: Guillaume <sagitta1618@gmail.com>
Date: Sat, 29 Oct 2022 10:19:15 +0100
Subject: [PATCH] adding test_ohmpi_mux.py and redirecting measure() to
 run_multiple_sequences()

---
 ohmpi.py          |  16 +++---
 test_ohmpi_mux.py | 131 +++++++++++++++++++++++++---------------------
 2 files changed, 80 insertions(+), 67 deletions(-)

diff --git a/ohmpi.py b/ohmpi.py
index 32369fe2..a3577a50 100644
--- a/ohmpi.py
+++ b/ohmpi.py
@@ -4,7 +4,7 @@ created on January 6, 2020.
 Updates May 2022, Oct 2022.
 Ohmpi.py is a program to control a low-cost and open hardware resistivity meter OhmPi that has been developed by
 Rémi CLEMENT (INRAE),Vivien DUBOIS (INRAE), Hélène GUYARD (IGE), Nicolas FORQUET (INRAE), Yannick FARGIER (IFSTTAR)
-Olivier KAUFMANN (UMONS), Arnaud WATELET (UMONS) and Guillaume BLANCHY (ILVO).
+Olivier KAUFMANN (UMONS), Arnaud WATELET (UMONS) and Guillaume BLANCHY (FNRS/ULiege).
 """
 
 import os
@@ -1042,10 +1042,7 @@ class OhmPi(object):
             reply = {'cmd_id': cmd_id, 'status': status}
             reply = json.dumps(reply)
             self.exec_logger.debug(f'Execution report: {reply}')
-
-    def measure(self, *args, **kwargs):
-        warnings.warn('This function is deprecated. Use load_sequence instead.', DeprecationWarning)
-        self.run_sequence(self, *args, **kwargs)
+        
 
     def set_sequence(self, args):
         try:
@@ -1056,7 +1053,8 @@ class OhmPi(object):
             status = False
 
     def run_sequence(self, cmd_id=None, **kwargs):
-        """Run sequence in sync mode
+        """Run sequence synchronously (=blocking on main thread).
+           Additional arguments are passed to run_measurement().
         """
         self.status = 'running'
         self.exec_logger.debug(f'Status: {self.status}')
@@ -1113,6 +1111,7 @@ class OhmPi(object):
 
     def run_sequence_async(self, cmd_id=None, **kwargs):
         """ Run the sequence in a separate thread. Can be stopped by 'OhmPi.interrupt()'.
+            Additional arguments are passed to run_measurement().
         """
         # self.run = True
         self.status = 'running'
@@ -1175,9 +1174,14 @@ class OhmPi(object):
         self.thread = threading.Thread(target=func)
         self.thread.start()
         
+    def measure(self, *args, **kwargs):
+        warnings.warn('This function is deprecated. Use run_multiple_sequences() instead.', DeprecationWarning)
+        self.run_sequence(self, *args, **kwargs)
+        
     def run_multiple_sequences(self, cmd_id=None, **kwargs):
         """ Run multiple sequences in a separate thread for monitoring mode.
             Can be stopped by 'OhmPi.interrupt()'.
+            Additional arguments are passed to run_measurement().
         """
         # self.run = True
         self.status = 'running'
diff --git a/test_ohmpi_mux.py b/test_ohmpi_mux.py
index 8883e9cb..cf09c17e 100644
--- a/test_ohmpi_mux.py
+++ b/test_ohmpi_mux.py
@@ -9,15 +9,27 @@ import os
 idps = False
 board_version = '22.10' # v2.0
 use_mux = True
-start_elec = 1  # start elec
+start_elec = 17  # start elec
 nelec = 16  # max elec in the sequence for testing
 
+# nelec electrodes Wenner sequence
+a = np.arange(nelec-3) + start_elec
+b = a + 3
+m = a + 1
+n = a + 2
+seq = np.c_[a, b, m, n]
+
 # testing measurement board only
 k = OhmPi(idps=idps, use_mux=use_mux)
+k.sequence = seq  # we need a sequence for possibly switching the mux
 k.reset_mux()  # just for safety
+if use_mux:
+    k.switch_mux_on(seq[:, 0])
 out1 = k.run_measurement(injection_duration=0.25, nb_stack=4, strategy='constant', tx_volt=12, autogain=True)
 out2 = k.run_measurement(injection_duration=0.5, nb_stack=2, strategy='vmin', tx_volt=5, autogain=True)
 out3 = k.run_measurement(injection_duration=1, nb_stack=1, strategy='vmax', tx_volt=5, autogain=True)
+if use_mux:
+    k.switch_mux_off(seq[:, 0])
 
 # visual figure of the full wave form
 fig, axs = plt.subplots(2, 1, sharex=True)
@@ -40,66 +52,63 @@ fig.savefig('check-fullwave.jpg')
 
 
 # test a sequence
-
-# nelec electrodes Wenner sequence
-a = np.arange(nelec-3) + start_elec
-b = a + 3
-m = a + 1
-n = a + 2
-seq = np.c_[a, b, m, n]
-
-# manually edit default settings
-k.settings['injection_duration'] = 1
-k.settings['nb_stack'] = 1
-#k.settings['nbr_meas'] = 1
-k.sequence = seq
-k.reset_mux()
-
-# set quadrupole manually
-k.switch_mux_on([1, 4, 2, 3])
-out = k.run_measurement(quad=[3, 3, 3, 3], nb_stack=1, tx_volt=12, strategy='constant', autogain=True)
-k.switch_mux_off([1, 4, 2, 3])
-print(out)
-
-# run rs_check() and save data
-k.rs_check()  # check all electrodes of the sequence
-
-# check values measured
-fname = sorted(os.listdir('data/'))[-1]
-print(fname)
-dfrs = pd.read_csv('data/' + fname)
-fig, ax = plt.subplots()
-ax.hist(dfrs['R [Ohm]']/1000)
-ax.set_xticks(np.arange(df.shape[0]))
-ax.set_xticklabels(df['A'].str + ' - ' + df['B'].str)
-ax.set_ylabel('Contact resistances [kOhm]')
-fig.tight_layout()
-fig.savefig('check-rs.jpg')
-
-# run sequence synchronously and save data to file
-k.run_sequence(nb_stack=1, injection_duration=0.25)
-
-# check values measured
-fname = sorted(os.listdir('data/'))[-1]
-print(fname)
-df = pd.read_csv('data/' + fname)
-fig, ax = plt.subplots()
-ax.hist(df['R [ohm]'])
-ax.set_ylabel('Transfer resistance [Ohm]')
-ax.set_xticks(np.arange(df.shape[0]))
-ax.set_xticklabels(df['A'] + ',' + df['B'] + ',' + df['M'] + ',' + df['N'])
-fig.tight_layou()
-fig.savefig('check-r.jpg')
-
-# run sequence asynchronously and save data to file
-k.run_sequence_async(nb_stack=1, injection_duration=0.25)
-time.sleep(2)
-k.interrupt()  # will kill the asynchronous sequence running
-
-# run a series of asynchronous sequences
-k.run_sequences(nb_stack=1, injection_duration=0.25)
-time.sleep(10)
-k.interrupt()
+if use_mux:
+    # manually edit default settings
+    k.settings['injection_duration'] = 1
+    k.settings['nb_stack'] = 1
+    #k.settings['nbr_meas'] = 1
+    k.sequence = seq
+    k.reset_mux()
+
+    # set quadrupole manually
+    k.switch_mux_on(seq[0, :])
+    out = k.run_measurement(quad=[3, 3, 3, 3], nb_stack=1, tx_volt=12, strategy='constant', autogain=True)
+    k.switch_mux_off(seq[0, :])
+    print(out)
+
+    # run rs_check() and save data
+    k.rs_check()  # check all electrodes of the sequence
+
+    # check values measured
+    fname = sorted(os.listdir('data/'))[-1]
+    print(fname)
+    dfrs = pd.read_csv('data/' + fname)
+    fig, ax = plt.subplots()
+    ax.hist(dfrs['RS [kOhm]'])
+    ax.set_xticks(np.arange(dfrs.shape[0]))
+    ax.set_xticklabels(dfrs['A'].astype(str) + ' - ' +
+                       dfrs['B'].astype(str), rotation=90)
+    ax.set_ylabel('Contact resistances [kOhm]')
+    fig.tight_layout()
+    fig.savefig('check-rs.jpg')
+
+    # run sequence synchronously and save data to file
+    k.run_sequence(nb_stack=1, injection_duration=0.25)
+
+    # check values measured
+    fname = sorted(os.listdir('data/'))[-1]
+    print(fname)
+    df = pd.read_csv('data/' + fname)
+    fig, ax = plt.subplots()
+    ax.hist(df['R [ohm]'])
+    ax.set_ylabel('Transfer resistance [Ohm]')
+    ax.set_xticks(np.arange(df.shape[0]))
+    ax.set_xticklabels(df['A'].astype(str) + ','
+                       + df['B'].astype(str) + ','
+                       + df['M'].astype(str) + ','
+                       + df['N'].astype(str), rotation=90)
+    fig.tight_layout()
+    fig.savefig('check-r.jpg')
+
+    # run sequence asynchronously and save data to file
+    k.run_sequence_async(nb_stack=1, injection_duration=0.25)
+    time.sleep(2)
+    k.interrupt()  # will kill the asynchronous sequence running
+
+    # run a series of asynchronous sequences
+    k.run_multiple_sequences(nb_stack=1, injection_duration=0.25)
+    time.sleep(2)
+    k.interrupt()
 
 
 # look at the noise frequency with FFT
-- 
GitLab