-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_decayfit_json_string_api.py
More file actions
215 lines (163 loc) · 7.9 KB
/
test_decayfit_json_string_api.py
File metadata and controls
215 lines (163 loc) · 7.9 KB
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
"""
Test DecayFit JSON string API - to_json() returns str, from_json() accepts str
"""
import unittest
import json as json_module
import numpy as np
import tttrlib
class TestDecayFitJsonStringAPI(unittest.TestCase):
"""Test that to_json() returns strings and from_json() accepts strings"""
def test_decayfit_corrections_to_json_returns_string(self):
"""Test DecayFitCorrections.get_json() returns a string"""
corrections = tttrlib.DecayFitCorrections()
result = corrections.get_json()
# Should be a string, not a SWIG object
self.assertIsInstance(result, str)
# Should be valid JSON
parsed = json_module.loads(result)
self.assertIsInstance(parsed, dict)
def test_decayfit_corrections_from_json_accepts_string(self):
"""Test DecayFitCorrections.set_json() accepts a string"""
corrections1 = tttrlib.DecayFitCorrections()
# Serialize to JSON string
json_str = corrections1.get_json()
self.assertIsInstance(json_str, str)
# Create new object and deserialize from string
corrections2 = tttrlib.DecayFitCorrections()
corrections2.set_json(json_str)
# Both should produce the same JSON
json_str2 = corrections2.get_json()
self.assertEqual(json_str, json_str2)
def test_decayfit_settings_to_json_returns_string(self):
"""Test DecayFitSettings.get_json() returns a string"""
settings = tttrlib.DecayFitSettings()
result = settings.get_json()
# Should be a string
self.assertIsInstance(result, str)
# Should be valid JSON
parsed = json_module.loads(result)
self.assertIsInstance(parsed, dict)
def test_decayfit_settings_from_json_accepts_string(self):
"""Test DecayFitSettings.set_json() accepts a string"""
settings1 = tttrlib.DecayFitSettings()
# Serialize to JSON string
json_str = settings1.get_json()
self.assertIsInstance(json_str, str)
# Create new object and deserialize from string
settings2 = tttrlib.DecayFitSettings()
settings2.set_json(json_str)
# Both should produce the same JSON
json_str2 = settings2.get_json()
self.assertEqual(json_str, json_str2)
def test_decayfit_integrate_signals_to_json_returns_string(self):
"""Test DecayFitIntegrateSignals.get_json() returns a string"""
integrate = tttrlib.DecayFitIntegrateSignals()
result = integrate.get_json()
# Should be a string
self.assertIsInstance(result, str)
# Should be valid JSON
parsed = json_module.loads(result)
self.assertIsInstance(parsed, dict)
def test_decayfit_integrate_signals_from_json_accepts_string(self):
"""Test DecayFitIntegrateSignals.set_json() accepts a string"""
integrate1 = tttrlib.DecayFitIntegrateSignals()
# Serialize to JSON string
json_str = integrate1.get_json()
self.assertIsInstance(json_str, str)
# Create new object and deserialize from string
integrate2 = tttrlib.DecayFitIntegrateSignals()
integrate2.set_json(json_str)
# Both should produce the same JSON
json_str2 = integrate2.get_json()
self.assertEqual(json_str, json_str2)
def test_decayfit_has_static_json_methods(self):
"""Test DecayFit has static JSON methods for parameters, data, and model"""
# DecayFit only has static methods, not instance methods
self.assertTrue(hasattr(tttrlib.DecayFit, 'parameters_to_json'))
self.assertTrue(hasattr(tttrlib.DecayFit, 'data_to_json'))
self.assertTrue(hasattr(tttrlib.DecayFit, 'model_to_json'))
self.assertTrue(hasattr(tttrlib.DecayFit, 'parameters_from_json'))
self.assertTrue(hasattr(tttrlib.DecayFit, 'data_from_json'))
self.assertTrue(hasattr(tttrlib.DecayFit, 'model_from_json'))
def test_decayfit_from_json_accepts_string(self):
"""Test DecayFit static methods accept strings"""
# Test with parameters
param = np.array([2.0, 0.01, 0.38, 1.2])
json_str = tttrlib.DecayFit.parameters_to_json(param)
self.assertIsInstance(json_str, str)
# Test with data
data = np.array([0, 1, 2, 3, 4, 5], dtype=np.int32)
json_str = tttrlib.DecayFit.data_to_json(data)
self.assertIsInstance(json_str, str)
# Test with model
model = np.array([1.0, 2.0, 3.0, 4.0])
json_str = tttrlib.DecayFit.model_to_json(model)
self.assertIsInstance(json_str, str)
def test_json_roundtrip_corrections(self):
"""Test JSON roundtrip for DecayFitCorrections"""
corrections1 = tttrlib.DecayFitCorrections()
# Serialize and deserialize
json_str = corrections1.get_json()
corrections2 = tttrlib.DecayFitCorrections()
corrections2.set_json(json_str)
# Should be identical
self.assertEqual(corrections1.get_json(), corrections2.get_json())
def test_json_roundtrip_settings(self):
"""Test JSON roundtrip for DecayFitSettings"""
settings1 = tttrlib.DecayFitSettings()
# Serialize and deserialize
json_str = settings1.get_json()
settings2 = tttrlib.DecayFitSettings()
settings2.set_json(json_str)
# Should be identical
self.assertEqual(settings1.get_json(), settings2.get_json())
def test_json_roundtrip_integrate_signals(self):
"""Test JSON roundtrip for DecayFitIntegrateSignals"""
integrate1 = tttrlib.DecayFitIntegrateSignals()
# Serialize and deserialize
json_str = integrate1.get_json()
integrate2 = tttrlib.DecayFitIntegrateSignals()
integrate2.set_json(json_str)
# Should be identical
self.assertEqual(integrate1.get_json(), integrate2.get_json())
def test_json_roundtrip_decayfit_parameters(self):
"""Test JSON roundtrip for DecayFit parameters"""
param1 = np.array([2.0, 0.01, 0.38, 1.2])
# Serialize to JSON string
json_str = tttrlib.DecayFit.parameters_to_json(param1)
self.assertIsInstance(json_str, str)
# Deserialize from string
param2 = np.zeros(4)
tttrlib.DecayFit.parameters_from_json(json_str, param2)
# Should have valid data
self.assertTrue(np.any(param2 != 0))
def test_json_valid_structure_corrections(self):
"""Test that DecayFitCorrections JSON has expected structure"""
corrections = tttrlib.DecayFitCorrections()
json_str = corrections.get_json()
parsed = json_module.loads(json_str)
# Should be a dict
self.assertIsInstance(parsed, dict)
def test_json_valid_structure_settings(self):
"""Test that DecayFitSettings JSON has expected structure"""
settings = tttrlib.DecayFitSettings()
json_str = settings.get_json()
parsed = json_module.loads(json_str)
# Should be a dict
self.assertIsInstance(parsed, dict)
def test_json_valid_structure_integrate_signals(self):
"""Test that DecayFitIntegrateSignals JSON has expected structure"""
integrate = tttrlib.DecayFitIntegrateSignals()
json_str = integrate.get_json()
parsed = json_module.loads(json_str)
# Should be a dict
self.assertIsInstance(parsed, dict)
def test_json_valid_structure_decayfit_parameters(self):
"""Test that DecayFit parameters JSON has expected structure"""
param = np.array([2.0, 0.01, 0.38, 1.2])
json_str = tttrlib.DecayFit.parameters_to_json(param)
parsed = json_module.loads(json_str)
# Should be a dict
self.assertIsInstance(parsed, dict)
if __name__ == '__main__':
unittest.main()