Repository navigation
Expand file tree
/
Copy pathtest_export.py
More file actions
432 lines (329 loc) · 13.7 KB
/
Copy pathtest_export.py
File metadata and controls
432 lines (329 loc) · 13.7 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""
Unit tests for network export functionality.
Tests all network types (FeedForward, Recurrent, CTRNN, IZNN) and validates
the JSON structure, metadata handling, and function detection.
"""
import os
import json
import tempfile
import neat
from neat.export import export_network_json
from neat.export.json_format import validate_json, is_builtin_activation, is_builtin_aggregation
def create_simple_genome(config):
"""Create a simple genome for testing."""
# Create a population to properly initialize innovation tracking
p = neat.Population(config)
# Get a genome from initial population
genome_id, genome = list(p.population.items())[0]
genome.fitness = 1.0
return genome
def get_test_config():
"""Load test configuration."""
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'test_configuration')
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
return config
def test_feedforward_export():
"""Test exporting a FeedForwardNetwork."""
config = get_test_config()
genome = create_simple_genome(config)
# Create network
net = neat.nn.FeedForwardNetwork.create(genome, config)
# Export to JSON
json_str = export_network_json(net)
data = json.loads(json_str)
# Validate structure
assert data['format_version'] == '1.0'
assert data['network_type'] == 'feedforward'
assert 'metadata' in data
assert 'topology' in data
assert 'nodes' in data
assert 'connections' in data
# Validate topology
assert data['topology']['num_inputs'] == config.genome_config.num_inputs
assert data['topology']['num_outputs'] == config.genome_config.num_outputs
assert len(data['topology']['input_keys']) == config.genome_config.num_inputs
assert len(data['topology']['output_keys']) == config.genome_config.num_outputs
def test_recurrent_export():
"""Test exporting a RecurrentNetwork."""
config = get_test_config()
genome = create_simple_genome(config)
# Create recurrent network
net = neat.nn.RecurrentNetwork.create(genome, config)
# Export to JSON
json_str = export_network_json(net)
data = json.loads(json_str)
# Validate structure
assert data['format_version'] == '1.0'
assert data['network_type'] == 'recurrent'
assert 'metadata' in data
assert 'topology' in data
assert 'nodes' in data
assert 'connections' in data
def test_ctrnn_export():
"""Test exporting a CTRNN."""
config = get_test_config()
genome = create_simple_genome(config)
# Create CTRNN
net = neat.ctrnn.CTRNN.create(genome, config)
# Export to JSON
json_str = export_network_json(net)
data = json.loads(json_str)
# Validate structure
assert data['format_version'] == '1.0'
assert data['network_type'] == 'ctrnn'
# Check for time_constant field in nodes
for node in data['nodes']:
assert 'time_constant' in node
def test_iznn_export():
"""Test exporting an IZNN."""
# Skip this test if IZNN config not available
# IZNN requires special configuration that may not be present in test_configuration
try:
# For now, we'll create a simple IZNN manually to test export
# This is a minimal test - full IZNN testing would require proper config
from neat.iznn import IZNeuron, IZNN
# Create minimal IZNN manually
neurons = {
0: IZNeuron(bias=5.0, a=0.02, b=0.20, c=-65.0, d=8.0, inputs=[])
}
inputs = [-1]
outputs = [0]
net = IZNN(neurons, inputs, outputs)
# Export to JSON
json_str = export_network_json(net)
data = json.loads(json_str)
# Validate structure
assert data['format_version'] == '1.0'
assert data['network_type'] == 'iznn'
# Check for Izhikevich parameters in output node
output_nodes = [n for n in data['nodes'] if n['type'] == 'output']
assert len(output_nodes) > 0
for node in output_nodes:
assert 'a' in node
assert 'b' in node
assert 'c' in node
assert 'd' in node
except Exception as e:
# If there's any issue with IZNN, we'll skip this test gracefully
print(f"Skipping IZNN export test: {e}")
pass
def test_json_validation():
"""Test JSON structure validation."""
config = get_test_config()
genome = create_simple_genome(config)
net = neat.nn.FeedForwardNetwork.create(genome, config)
json_str = export_network_json(net)
data = json.loads(json_str)
# Should pass validation
assert validate_json(data) == True
def test_metadata_inclusion():
"""Test that metadata is correctly included."""
config = get_test_config()
genome = create_simple_genome(config)
net = neat.nn.FeedForwardNetwork.create(genome, config)
metadata = {
'fitness': 99.5,
'generation': 42,
'genome_id': 123,
'custom_field': 'test_value'
}
json_str = export_network_json(net, metadata=metadata)
data = json.loads(json_str)
# Check metadata
assert data['metadata']['fitness'] == 99.5
assert data['metadata']['generation'] == 42
assert data['metadata']['genome_id'] == 123
assert data['metadata']['custom_field'] == 'test_value'
def test_export_to_file():
"""Test exporting directly to a file."""
config = get_test_config()
genome = create_simple_genome(config)
net = neat.nn.FeedForwardNetwork.create(genome, config)
# Export to temporary file
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as f:
temp_path = f.name
try:
json_str = export_network_json(net, filepath=temp_path)
# Verify file was created
assert os.path.exists(temp_path)
# Read file and verify content
with open(temp_path, 'r') as f:
file_content = f.read()
assert file_content == json_str
# Verify it's valid JSON
data = json.loads(file_content)
validate_json(data)
finally:
# Clean up
if os.path.exists(temp_path):
os.remove(temp_path)
def test_builtin_activation_detection():
"""Test detection of built-in activation functions."""
# Test built-in functions
assert is_builtin_activation(neat.activations.sigmoid_activation) == True
assert is_builtin_activation(neat.activations.tanh_activation) == True
assert is_builtin_activation(neat.activations.relu_activation) == True
assert is_builtin_activation(neat.activations.identity_activation) == True
# Test None
assert is_builtin_activation(None) == False
# Test custom function
def custom_activation(x):
return x * 2
assert is_builtin_activation(custom_activation) == False
def test_builtin_aggregation_detection():
"""Test detection of built-in aggregation functions."""
# Test built-in functions
assert is_builtin_aggregation(neat.aggregations.sum_aggregation) == True
assert is_builtin_aggregation(neat.aggregations.product_aggregation) == True
assert is_builtin_aggregation(neat.aggregations.max_aggregation) == True
assert is_builtin_aggregation(neat.aggregations.mean_aggregation) == True
# Test None
assert is_builtin_aggregation(None) == False
# Test custom function
def custom_aggregation(x):
return sum(x) / 2
assert is_builtin_aggregation(custom_aggregation) == False
def test_activation_functions_marked_correctly():
"""Test that various activation functions are correctly identified in export."""
config = get_test_config()
# Test with different activation functions
for act_name in ['sigmoid', 'tanh', 'relu', 'identity']:
genome = create_simple_genome(config)
# Set activation function for output nodes
for node in genome.nodes.values():
node.activation = act_name
net = neat.nn.FeedForwardNetwork.create(genome, config)
json_str = export_network_json(net)
data = json.loads(json_str)
# Find output node and check activation
for node in data['nodes']:
if node['type'] == 'output':
assert node['activation']['name'] == act_name
assert node['activation']['custom'] == False
def test_aggregation_functions_marked_correctly():
"""Test that various aggregation functions are correctly identified in export."""
config = get_test_config()
# Test with different aggregation functions
for agg_name in ['sum', 'product', 'max', 'min']:
genome = create_simple_genome(config)
# Set aggregation function for output nodes
for node in genome.nodes.values():
node.aggregation = agg_name
net = neat.nn.FeedForwardNetwork.create(genome, config)
json_str = export_network_json(net)
data = json.loads(json_str)
# Find output node and check aggregation
for node in data['nodes']:
if node['type'] == 'output':
assert node['aggregation']['name'] == agg_name
assert node['aggregation']['custom'] == False
def test_node_structure():
"""Test that exported nodes have all required fields."""
config = get_test_config()
genome = create_simple_genome(config)
net = neat.nn.FeedForwardNetwork.create(genome, config)
json_str = export_network_json(net)
data = json.loads(json_str)
required_fields = ['id', 'type', 'activation', 'aggregation', 'bias', 'response']
for node in data['nodes']:
for field in required_fields:
assert field in node, f"Node missing required field: {field}"
# Check node type is valid
assert node['type'] in ['input', 'hidden', 'output']
# Check activation structure
assert 'name' in node['activation']
assert 'custom' in node['activation']
# Check aggregation structure
assert 'name' in node['aggregation']
assert 'custom' in node['aggregation']
def test_connection_structure():
"""Test that exported connections have all required fields."""
config = get_test_config()
genome = create_simple_genome(config)
net = neat.nn.FeedForwardNetwork.create(genome, config)
json_str = export_network_json(net)
data = json.loads(json_str)
required_fields = ['from', 'to', 'weight', 'enabled']
for conn in data['connections']:
for field in required_fields:
assert field in conn, f"Connection missing required field: {field}"
# Check that weight is a number
assert isinstance(conn['weight'], (int, float))
# Check that enabled is a boolean
assert isinstance(conn['enabled'], bool)
def test_export_without_metadata():
"""Test exporting without optional metadata."""
config = get_test_config()
genome = create_simple_genome(config)
net = neat.nn.FeedForwardNetwork.create(genome, config)
# Export without metadata
json_str = export_network_json(net)
data = json.loads(json_str)
# Should still have metadata dict with timestamp
assert 'metadata' in data
assert 'created_timestamp' in data['metadata']
def test_invalid_network_type():
"""Test that invalid network type raises appropriate error."""
try:
export_network_json(None)
assert False, "Should have raised TypeError"
except TypeError as e:
assert "cannot be None" in str(e)
# Test with invalid object
try:
export_network_json("not a network")
assert False, "Should have raised ValueError"
except ValueError as e:
assert "Unsupported network type" in str(e)
def test_json_is_well_formatted():
"""Test that exported JSON is properly formatted."""
config = get_test_config()
genome = create_simple_genome(config)
net = neat.nn.FeedForwardNetwork.create(genome, config)
json_str = export_network_json(net)
# Should be pretty-printed (check for indentation)
assert '\n' in json_str
assert ' ' in json_str # Check for indentation
# Should be valid JSON
data = json.loads(json_str)
assert data is not None
def test_numeric_precision():
"""Test that numeric values are preserved correctly."""
config = get_test_config()
genome = create_simple_genome(config)
# Set specific bias value
for node in genome.nodes.values():
node.bias = 0.123456789
node.response = 0.987654321
net = neat.nn.FeedForwardNetwork.create(genome, config)
json_str = export_network_json(net)
data = json.loads(json_str)
# Check that precision is preserved
for node in data['nodes']:
if node['type'] == 'output':
# JSON may have slight rounding, but should be very close
assert abs(node['bias'] - 0.123456789) < 1e-9
assert abs(node['response'] - 0.987654321) < 1e-9
if __name__ == '__main__':
# Run tests
test_feedforward_export()
test_recurrent_export()
test_ctrnn_export()
test_iznn_export()
test_json_validation()
test_metadata_inclusion()
test_export_to_file()
test_builtin_activation_detection()
test_builtin_aggregation_detection()
test_activation_functions_marked_correctly()
test_aggregation_functions_marked_correctly()
test_node_structure()
test_connection_structure()
test_export_without_metadata()
test_invalid_network_type()
test_json_is_well_formatted()
test_numeric_precision()
print("All tests passed!")