@@ -74,7 +74,7 @@ def _calculate_default_grid_settings(surface_points: List[SurfacePoint], orienta
74
74
z_padding = z_range * 0.1
75
75
76
76
return {
77
- "regular_grid_resolution" : [10 , 10 , 10 ],
77
+ "regular_grid_resolution" : [20 , 20 , 20 ],
78
78
"regular_grid_extent" : [
79
79
x_min - x_padding , x_max + x_padding ,
80
80
y_min - y_padding , y_max + y_padding ,
@@ -413,115 +413,90 @@ def save_model_to_json(model, file_path: str) -> None:
413
413
json .dump (json_data , f , indent = 4 )
414
414
415
415
@staticmethod
416
- def _validate_json_schema (data : Dict [str , Any ]) -> bool :
417
- """
418
- Validate the JSON data against the expected schema.
419
-
420
- Args:
421
- data (Dict[str, Any]): The JSON data to validate
422
-
423
- Returns:
424
- bool: True if valid, False otherwise
425
- """
426
- # Check required top-level keys (metadata, grid_settings, interpolation_options, and series are optional)
427
- required_keys = {'surface_points' , 'orientations' }
428
- if not all (key in data for key in required_keys ):
429
- return False
430
-
416
+ def _validate_json_schema (data : Dict ) -> None :
417
+ """Validate the JSON schema and set default values."""
418
+ # Required top-level keys
419
+ required_keys = ['surface_points' , 'orientations' , 'grid_settings' ]
420
+ for key in required_keys :
421
+ if key not in data :
422
+ raise ValueError (f"Missing required key: { key } " )
423
+
431
424
# Validate surface points
432
- if not isinstance (data ['surface_points' ], list ):
433
- return False
434
-
435
- for sp in data ['surface_points' ]:
436
- required_sp_keys = {'x' , 'y' , 'z' , 'id' , 'nugget' }
437
- if not all (key in sp for key in required_sp_keys ):
438
- return False
439
- if not all (isinstance (sp [key ], (int , float )) for key in ['x' , 'y' , 'z' , 'nugget' ]):
440
- return False
441
- if not isinstance (sp ['id' ], int ):
442
- return False
443
-
425
+ for point in data ['surface_points' ]:
426
+ required_point_keys = ['x' , 'y' , 'z' ]
427
+ for key in required_point_keys :
428
+ if key not in point :
429
+ raise ValueError (f"Missing required key in surface point: { key } " )
430
+ # Set default nugget if not provided
431
+ if 'nugget' not in point :
432
+ point ['nugget' ] = 0.0 # Default nugget for surface points
433
+
444
434
# Validate orientations
445
- if not isinstance (data ['orientations' ], list ):
446
- return False
447
-
448
- for ori in data ['orientations' ]:
449
- required_ori_keys = {'x' , 'y' , 'z' , 'G_x' , 'G_y' , 'G_z' , 'id' , 'nugget' , 'polarity' }
450
- if not all (key in ori for key in required_ori_keys ):
451
- return False
452
- if not all (isinstance (ori [key ], (int , float )) for key in ['x' , 'y' , 'z' , 'G_x' , 'G_y' , 'G_z' , 'nugget' ]):
453
- return False
454
- if not isinstance (ori ['id' ], int ):
455
- return False
456
- if not isinstance (ori ['polarity' ], int ):
457
- return False
458
-
459
- # Validate series if present
435
+ for orientation in data ['orientations' ]:
436
+ required_orientation_keys = ['x' , 'y' , 'z' , 'G_x' , 'G_y' , 'G_z' ]
437
+ for key in required_orientation_keys :
438
+ if key not in orientation :
439
+ raise ValueError (f"Missing required key in orientation: { key } " )
440
+ # Set default nugget if not provided
441
+ if 'nugget' not in orientation :
442
+ orientation ['nugget' ] = 0.01 # Default nugget for orientations
443
+ # Set default polarity if not provided
444
+ if 'polarity' not in orientation :
445
+ orientation ['polarity' ] = 1
446
+
447
+ # Validate grid settings
448
+ grid_settings = data ['grid_settings' ]
449
+ required_grid_keys = ['regular_grid_resolution' , 'regular_grid_extent' ]
450
+ for key in required_grid_keys :
451
+ if key not in grid_settings :
452
+ raise ValueError (f"Missing required key in grid_settings: { key } " )
453
+
454
+ # Validate series if provided
460
455
if 'series' in data :
461
- if not isinstance (data ['series' ], list ):
462
- return False
463
-
464
456
for series in data ['series' ]:
465
- # Only name and surfaces are required
466
- required_series_keys = {'name' , 'surfaces' }
467
- if not all (key in series for key in required_series_keys ):
468
- return False
469
- if not isinstance (series ['name' ], str ):
470
- return False
471
- if not isinstance (series ['surfaces' ], list ):
472
- return False
473
- # Validate optional fields if present
474
- if 'structural_relation' in series and not isinstance (series ['structural_relation' ], str ):
475
- return False
457
+ required_series_keys = ['name' , 'surfaces' ]
458
+ for key in required_series_keys :
459
+ if key not in series :
460
+ raise ValueError (f"Missing required key in series: { key } " )
461
+ # Set default structural relation if not provided
462
+ if 'structural_relation' not in series :
463
+ series ['structural_relation' ] = "ERODE"
464
+ # Validate colors if provided
476
465
if 'colors' in series :
477
466
if not isinstance (series ['colors' ], list ):
478
- return False
479
- if not all (isinstance (color , str ) for color in series ['colors' ]):
480
- return False
481
-
482
- # Validate grid settings if present
483
- if 'grid_settings' in data :
484
- if not isinstance (data ['grid_settings' ], dict ):
485
- return False
486
-
487
- required_grid_keys = {'regular_grid_resolution' , 'regular_grid_extent' }
488
- if not all (key in data ['grid_settings' ] for key in required_grid_keys ):
489
- return False
490
-
491
- if not isinstance (data ['grid_settings' ]['regular_grid_resolution' ], list ):
492
- return False
493
- if not isinstance (data ['grid_settings' ]['regular_grid_extent' ], list ):
494
- return False
467
+ raise ValueError ("Colors must be a list" )
468
+ if not all (isinstance (color , str ) and color .startswith ('#' ) for color in series ['colors' ]):
469
+ raise ValueError ("Colors must be hex color codes starting with #" )
495
470
496
471
# Validate interpolation options if present
497
472
if 'interpolation_options' in data :
498
473
if not isinstance (data ['interpolation_options' ], dict ):
499
- return False
474
+ raise ValueError ( "Interpolation options must be a dictionary" )
500
475
if 'kernel_options' in data ['interpolation_options' ]:
501
476
kernel_options = data ['interpolation_options' ]['kernel_options' ]
502
477
if not isinstance (kernel_options , dict ):
503
- return False
478
+ raise ValueError ( "Kernel options must be a dictionary" )
504
479
if 'range' in kernel_options and not isinstance (kernel_options ['range' ], (int , float )):
505
- return False
480
+ raise ValueError ( "Kernel range must be a number" )
506
481
if 'c_o' in kernel_options and not isinstance (kernel_options ['c_o' ], (int , float )):
507
- return False
482
+ raise ValueError ( "Kernel c_o must be a number" )
508
483
if 'mesh_extraction' in data ['interpolation_options' ] and not isinstance (data ['interpolation_options' ]['mesh_extraction' ], bool ):
509
- return False
484
+ raise ValueError ( "Mesh extraction must be a boolean" )
510
485
if 'number_octree_levels' in data ['interpolation_options' ] and not isinstance (data ['interpolation_options' ]['number_octree_levels' ], int ):
511
- return False
486
+ raise ValueError ( "Number of octree levels must be an integer" )
512
487
513
488
# Validate metadata if present
514
489
if 'metadata' in data :
515
490
metadata = data ['metadata' ]
516
491
if not isinstance (metadata , dict ):
517
- return False
492
+ raise ValueError ( "Metadata must be a dictionary" )
518
493
if 'name' in metadata and not isinstance (metadata ['name' ], str ):
519
- return False
494
+ raise ValueError ( "Metadata name must be a string" )
520
495
if 'creation_date' in metadata and not isinstance (metadata ['creation_date' ], str ):
521
- return False
496
+ raise ValueError ( "Metadata creation date must be a string" )
522
497
if 'last_modification_date' in metadata and not isinstance (metadata ['last_modification_date' ], str ):
523
- return False
498
+ raise ValueError ( "Metadata last modification date must be a string" )
524
499
if 'owner' in metadata and not isinstance (metadata ['owner' ], str ):
525
- return False
500
+ raise ValueError ( "Metadata owner must be a string" )
526
501
527
502
return True
0 commit comments