Skip to content

Commit f2e7a29

Browse files
committed
Cleaned up alpaca.py
1 parent 25fcdee commit f2e7a29

1 file changed

Lines changed: 7 additions & 119 deletions

File tree

ports/rp2/modules/alpaca.py

Lines changed: 7 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,22 @@
11
"""Module for using the Raspberry Pi Pico in the course NB2211 Electronic Instrumentation
22
By: Thijmen de Wolf & Thijn Hoekstra
33
"""
4-
# ___ __ .______ ___ ______ ___
5-
# / \ | | | _ \ / \ / | / \
6-
# / ^ \ | | | |_) | / ^ \ | ,----' / ^ \
7-
# / /_\ \ | | | ___/ / /_\ \ | | / /_\ \
8-
# / _____ \ | `----.| | / _____ \ | `----./ _____ \
9-
# /__/ \__\ |_______|| _| /__/ \__\ \______/__/ \__\
10-
version = '0.8' # Module version so students can retrieve their version for debugging
4+
5+
import os
6+
7+
_VERSION = '0.8' # Module version so students can retrieve their version
118

129

1310
def get_version():
1411
"""Retrieve the version of the `nb2211` module.
1512
"""
16-
print('You are currently running version ' + version + ' of alpaca.py')
17-
18-
def clear_disk(self):
19-
import os
20-
will_clear = False
13+
print('You are currently running version ' + _VERSION + ' of alpaca.py')
2114

22-
utime.sleep(5)
2315

16+
def clear_disk(self):
2417
print('Remove files...')
25-
for file in os.ilistdir():
18+
for file in os.listdir():
2619
os.remove(file[0])
2720
print('Removed ', file[0])
2821

2922
print('Done.')
30-
31-
32-
def __is_2D_array(self, object):
33-
result = False
34-
if type(object) is list:
35-
if type(object[0]) is list:
36-
result = True
37-
return result
38-
39-
def __is_scalar(self, object):
40-
return type(object) is float or type(object) is int
41-
42-
def store_data(self, filename, parameters=None, samples=None):
43-
"""Stores samples taken during a measurement as a text file on the memory of the Raspberry Pi Pico.
44-
45-
The data is stored in such a way that it is relatively easy to read later on. It is possible to store
46-
just the samples, but it is also possible to add information about the measurement (or parameters) to
47-
the text file.
48-
49-
Parameters
50-
----------
51-
filename : str
52-
Filename of text file in which to store data. Should include the file extension, e.g. `data.txt`.
53-
parameters : int or float or list, optional
54-
Information about the measurement, e.g. a single measurement parameter such as frequency stored as a float
55-
or multiple parameters stored as a list.
56-
samples : list
57-
A list of the samples taken during the measurment. Note that this can also be a nested list, e.g. a 2D array.
58-
59-
60-
Notes
61-
-----
62-
Stores the file as `filename` to the root directory of the Raspberry Pi Pico. Importing the data is best done using the `numpy` package. For example:
63-
64-
65-
Examples
66-
--------
67-
>>> pico = nb2211.Pico()
68-
>>> pico.store_data('data.txt', samples = [1,2,3]) # Store only three samples
69-
>>> pico.store_data('data.txt', 24.0, [600, 601, 602]) # Store three samples taken at a frequency of 24.0 Hertz.
70-
>>> pico.store_data('temperatures.txt', [3600, 12], samples = temperature_list) # Store list 'temperature_list' along with parameters, e.g. 3600 samples taken over the course of 12 days.
71-
>>> pico.store_data('data.txt', parameters = 10, samples = 3) # Store single parameter and single sample.
72-
>>> # Storing data and saving data using `numpy`.
73-
>>> # Saving data
74-
>>> from nb2211 import Pico
75-
>>> pico = Pico()
76-
>>> frequencies = [10, 11, 12]
77-
>>> data_2D = [[1, 2, 3, 4, 5, 6],
78-
>>> [7, 8, 9, 10, 11, 12]]
79-
>>> pico.store_data('data.txt', parameters=frequencies, samples=data_2D) # With parameters
80-
>>> #
81-
>>> # Retrieving data
82-
>>> import numpy as np
83-
>>> parameters_stored = True
84-
>>> data_2D = np.loadtxt('data.txt', skiprows = parameters_stored)
85-
>>> frequencies = np.loadtxt('data.txt', max_rows = parameters_stored)
86-
87-
"""
88-
file = open(filename, 'w')
89-
90-
if parameters is not None:
91-
# Write parameters
92-
if self.__is_scalar(parameters):
93-
file.write(str(parameters) + '\n')
94-
95-
elif self.__is_2D_array(parameters):
96-
raise TypeError('Error exporting parameters. Can only export 1D arrays')
97-
98-
elif type(parameters) is list:
99-
for item in parameters:
100-
file.write(str(item) + ' ')
101-
file.write('\n')
102-
103-
else:
104-
file.close()
105-
raise TypeError('Please input parameters as either a scalar value or a list.')
106-
107-
if isinstance(samples, (list, tuple, np.ndarray)):
108-
for ii, item in enumerate(samples):
109-
if isinstance(item, (list, tuple, np.ndarray)):
110-
samples[ii] = list(np.array(item).flatten())
111-
112-
samples = list(samples)
113-
114-
# ~~~~~~~~~~~~~~~~~~~ Write samples
115-
if self.__is_scalar(samples):
116-
file.write(str(samples) + '\n')
117-
118-
elif self.__is_2D_array(samples):
119-
for row in samples:
120-
for column in row:
121-
file.write(str(column) + ' ')
122-
file.write('\n')
123-
124-
elif type(samples) is list:
125-
for item in samples:
126-
file.write(str(item) + ' ')
127-
file.write('\n')
128-
129-
else:
130-
file.close()
131-
raise TypeError('Please input parameters as either a scalar value or a (nested) list.')
132-
133-
file.flush()
134-
file.close()

0 commit comments

Comments
 (0)