Home > Back-end >  Is there a way to extend a list of time values in seconds given a new list of time values starting a
Is there a way to extend a list of time values in seconds given a new list of time values starting a

Time:09-21

I have a python program running a serial port connected to a teensy board. The program receives three lists from the board: t, volt, and curr once one method is complete. I have three lists that I want to build using these values: t_list, volt_list, and curr_list. The board is connected to a iorodeo potentiostat which can only run 25 cycles per method cycle, so I need to pass the values it gives from t, volt, and curr to t_list, volt_list, and curr_list for x number of method cycles to get around the 25 cycle limit. The problem is that the time list t restarts at 0.003 seconds each method cycle so I end up with a repeating list of times in the final t_list. Is there a way to take each new t and have the python program calculate a new list which starts at the last time and ends with the total time in seconds? The list t gives a list of times in seconds with the same length as the curr and volt lists. For the script below, the first method returns a list t with a length of 250 starting at 0.003 seconds and ending at 0.75 seconds. It should be the same every method cycle.

Here is the script:

# -*- coding: utf-8 -*-
"""
Created on Sat Sep 18 18:34:59 2021

@author: dallo
"""
from potentiostat import Potentiostat
import sys
import matplotlib.pyplot as plt
from serial import Serial

port = 'COM7'

num_pulse = 25  # Number of pulses (max = 25)
lo_pulse = (10, -1.0)  
hi_pulse = (20, -0.1)  
num_cycles = 3
dev = Potentiostat(port)
hw_variant = dev.get_hardware_variant()

dev.set_curr_range('1000uA')
dev.set_sample_rate(300)

step_list = []
curr_list = []
volt_list = []
t_list = []
# Add pulses to step list
for i in range(num_pulse):
    step_list.append(lo_pulse)
    step_list.append(hi_pulse)


# Test name and test parameters
test_name = 'multiStep'
test_param = {
        'quietValue' : 0.0,
        'quietTime'  : 0,
        'step'       : step_list,
        }
for i in range(num_cycles):
    volt = []
    curr = []
    dev.set_param(test_name,test_param)
    t,volt,curr = dev.run_test(test_name,display='pbar')
    curr_list.extend(curr)
    volt_list.extend(volt)
    t_list.extend(t)

volt_ax = plt.subplot(2,1,1)
plt.plot(t_list,volt_list)
plt.ylabel('potential (V)')
plt.grid('on')

plt.subplot(2,1,2, sharex=volt_ax)
plt.plot(t_list,curr_list)
plt.ylabel('current (uA)')
plt.xlabel('t (sec)')
plt.grid('on')

plt.show()

CodePudding user response:

You could try to keep track of the time by adding at your t_list a dictionary having this form:

tmp_dict = {time_of_the_iteration: t}

So that the code became:

....

from datetime import datetime

....

for i in range(num_cycles):
    volt = []
    curr = []
    dev.set_param(test_name,test_param)
    t,volt,curr = dev.run_test(test_name,display='pbar')
    curr_list.extend(curr)
    volt_list.extend(volt)
    
    
    actual_time = datetime.now().strftime('%H:%M:%S')
    tmp_dict = {actual_time: t}
    
    t_list.extend(tmp_dict)

In this way you have different time for each t.

CodePudding user response:

I was able to figure it out. I added the following code to the for loop:

for i in range(num_cycles):
    volt = []
    curr = []
    t = []
    dev.set_param(test_name,test_param)
    t,volt,curr = dev.run_test(test_name,display='pbar')
    curr_list.extend(curr)
    volt_list.extend(volt)
    
    if len(t_list) > 0:
        last_time = t_list[-1]
        new_t_list = [x last_time for x in t]
        t_list.extend(new_t_list)
    else:
        t_list.extend(t)

This takes the last (latest) time value from t_list and adds it to each of the new values in the next t list for each new method cycle. For example, if the first t list was [1,2,3,4,5] and the next t list was the same [1,2,3,4,5] it takes the last value from the first t and adds it to each element of the second t list and adds those new elements to the t_list, giving a final t_list of [1,2,3,4,5,6,7,8,9,10]

  • Related