-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_sampledata.py
251 lines (204 loc) · 8.7 KB
/
test_sampledata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import filecmp
import logging
import os
import tempfile
import unittest
from importlib.machinery import SourceFileLoader
from importlib.util import module_from_spec, spec_from_loader
import sbol3
import tyto
import xarray as xr
from numpy import nan
from tyto import OM
import labop
import uml
from labop.execution_engine import ExecutionEngine
from labop.utils.helpers import file_diff, initialize_protocol
from labop_convert.plate_coordinates import get_sample_list
OUT_DIR = os.path.join(os.path.dirname(__file__), "out")
if not os.path.exists(OUT_DIR):
os.mkdir(OUT_DIR)
# Save testfiles as artifacts when running in CI environment,
# else save them to a local temp directory
if "GH_TMPDIR" in os.environ:
TMPDIR = os.environ["GH_TMPDIR"]
else:
TMPDIR = tempfile.gettempdir()
protocol_def_file = os.path.join(
os.path.dirname(__file__), "../examples/LUDOX_protocol.py"
)
def load_ludox_protocol(protocol_filename):
loader = SourceFileLoader("ludox_protocol", protocol_filename)
spec = spec_from_loader(loader.name, loader)
module = module_from_spec(spec)
loader.exec_module(module)
return module
protocol_def = load_ludox_protocol(protocol_def_file)
class TestProtocolEndToEnd(unittest.TestCase):
def test_dataset_to_dataframe(self):
protocol, doc = initialize_protocol()
protocol.name = "sample_data_demo_protocol"
reagents = [
"fluorescene",
"sulforhodamine101",
"cascadeBlue",
"nanocym",
"water",
"pbs",
]
samples = get_sample_list(geometry="A1:H12")
def volume_in_sample(reagent, sample):
if "A" in sample:
if reagent == "fluorescene" or reagent == "water":
return 1.0
elif "B" in sample:
if reagent == "cascadeBlue" or reagent == "pbs":
return 1.0
return nan
# sample_data = xr.DataArray(
# [10.0 for sample in samples],
# dims=("sample"),
# coords={"sample": samples}
# )
# initial_contents = xr.DataArray(samples,
# dims=(labop.Strings.SAMPLE),
# coords={labop.Strings.SAMPLE: samples})
# sample_array = labop.SampleArray(container_type=labop.ContainerSpec("dummy_spec"),
# initial_contents=labop.serialize_sample_format(initial_contents))
# container_spec = labop.ContainerSpec('abstractPlateRequirement1',
# name='abstractPlateRequirement1')
# create_source = protocol.primitive_step('EmptyContainer', specification=container_spec, sample_array=sample_array)
# Define the type of container
container_type = container_type = labop.ContainerSpec("deep96")
# Create an activity to create the container.
create_source = protocol.primitive_step(
"EmptyContainer", specification=container_type
)
metadata_filename = "test/metadata/measure_absorbance.xlsx"
load_excel = protocol.primitive_step(
"ExcelMetadata",
for_samples=create_source.output_pin("samples"),
filename=metadata_filename,
)
create_coordinates = protocol.primitive_step(
"PlateCoordinates",
source=create_source.output_pin("samples"),
coordinates="A1:B12",
)
measure_absorbance = protocol.primitive_step(
"MeasureAbsorbance",
samples=create_coordinates.output_pin("samples"),
wavelength=sbol3.Measure(600, OM.nanometer),
)
meta1 = protocol.primitive_step(
"JoinMetadata",
dataset=measure_absorbance.output_pin("measurements"),
metadata=load_excel.output_pin("metadata"),
)
outnode = protocol.designate_output(
"dataset",
"http://bioprotocols.org/labop#Dataset",
source=meta1.output_pin("enhanced_dataset"),
)
protocol.order(outnode, protocol.final())
filename = protocol.name
protocol.to_dot().render(os.path.join(OUT_DIR, filename))
ee = ExecutionEngine(
failsafe=False,
sample_format="xarray",
use_ordinal_time=True,
dataset_file=f"{filename}_data", # name of xlsx file (w/o suffix)
out_dir=OUT_DIR,
)
execution = ee.execute(
protocol,
sbol3.Agent("test_agent"),
id="test_execution",
parameter_values=[],
)
execution.to_dot().render(os.path.join(OUT_DIR, f"{protocol.name}_execution"))
# dataset = execution.parameter_values[0].value.get_value()
# xr_dataset = labop.sort_samples(dataset.to_dataset())
# df = xr_dataset.to_dataframe()
# df.to_excel(os.path.join(OUT_DIR, "dataset.xlsx"))
print("Validating and writing protocol")
v = doc.validate()
assert len(v) == 0, "".join(f"\n {e}" for e in v)
nt_file = f"{filename}.nt"
temp_name = os.path.join(TMPDIR, nt_file)
# At some point, rdflib began inserting an extra newline into
# N-triple serializations, which breaks file comparison.
# Here we strip extraneous newlines, to maintain reverse compatibility
with open(temp_name, "w") as f:
f.write(doc.write_string(sbol3.SORTED_NTRIPLES).strip())
print(f"Wrote file as {temp_name}")
comparison_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"testfiles",
nt_file,
)
# with open(comparison_file, "w") as f:
# f.write(doc.write_string(sbol3.SORTED_NTRIPLES).strip())
print(f"Comparing against {comparison_file}")
diff = "".join(file_diff(comparison_file, temp_name))
print(f"Difference:\n{diff}")
assert filecmp.cmp(temp_name, comparison_file), "Files are not identical"
print("File identical with test file")
@unittest.skip(reason="tmp remove for dev")
def test_create_protocol(self):
protocol: labop.Protocol
doc: sbol3.Document
logger = logging.getLogger("LUDOX_protocol")
logger.setLevel(logging.INFO)
protocol, doc = protocol_def.ludox_protocol()
########################################
# Validate and write the document
agent = sbol3.Agent("test_agent")
# Execute the protocol
# In order to get repeatable timings, we use ordinal time in the test
# where each timepoint is one second after the previous time point
ee = ExecutionEngine(out_dir=OUT_DIR, use_ordinal_time=True)
parameter_values = [
labop.ParameterValue(
parameter=protocol.get_input("wavelength"),
value=uml.LiteralIdentified(
value=sbol3.Measure(100, tyto.OM.nanometer)
),
)
]
execution = ee.execute(
protocol, agent, id="test_execution", parameter_values=parameter_values
)
# Get the SampleData objects and attach values
# get_data() returns a dict of output parameter ids to SampleData objects
dataset = execution.get_data()
for id, ds in dataset.items():
for k, v in ds.data_vars.items():
for dimension in v.dims:
new_data = [8] * len(ds[k].data)
ds.update({k: (dimension, new_data)})
execution.set_data(dataset)
print("Validating and writing protocol")
v = doc.validate()
assert len(v) == 0, "".join(f"\n {e}" for e in v)
temp_name = os.path.join(TMPDIR, "igem_ludox_data_test.nt")
# At some point, rdflib began inserting an extra newline into
# N-triple serializations, which breaks file comparison.
# Here we strip extraneous newlines, to maintain reverse compatibility
with open(temp_name, "w") as f:
f.write(doc.write_string(sbol3.SORTED_NTRIPLES).strip())
print(f"Wrote file as {temp_name}")
comparison_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"testfiles",
"igem_ludox_data_test.nt",
)
diff = "".join(file_diff(comparison_file, temp_name))
print(f"Difference: {diff}")
# with open(comparison_file, 'w') as f:
# f.write(doc.write_string(sbol3.SORTED_NTRIPLES).strip())
print(f"Comparing against {comparison_file}")
assert filecmp.cmp(temp_name, comparison_file), "Files are not identical"
print("File identical with test file")
if __name__ == "__main__":
unittest.main()