Skip to content

Commit

Permalink
Merge pull request #32 from AllenNeuralDynamics:feat-support-for-x-en…
Browse files Browse the repository at this point in the history
…um-names

Feat-support-for-x-enum-names
  • Loading branch information
bruno-f-cruz authored Jun 6, 2024
2 parents 02ed87a + b255d09 commit ab6b926
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/DataSchemas/aind_behavior_services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.7.5"
__version__ = "0.7.6"

from .rig import AindBehaviorRigModel
from .session import AindBehaviorSessionModel
Expand Down
59 changes: 58 additions & 1 deletion src/DataSchemas/aind_behavior_services/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from pathlib import Path
from string import capwords
from subprocess import CompletedProcess, run
from typing import Dict, List, Optional, TypeVar
from typing import Dict, List, Optional, TypeVar, Any
import inspect

from pydantic import BaseModel, PydanticInvalidForJsonSchema
from pydantic.json_schema import (
Expand All @@ -23,6 +24,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.nullable_as_oneof = kwargs.get("nullable_as_oneof", True)
self.unions_as_oneof = kwargs.get("unions_as_oneof", True)
self.render_xenum_names = kwargs.get("render_xenum_names", True)

def nullable_schema(self, schema: core_schema.NullableSchema) -> JsonSchemaValue:
null_schema = {"type": "null"}
Expand All @@ -48,6 +50,47 @@ def get_flattened_oneof(self, schemas: list[JsonSchemaValue]) -> JsonSchemaValue
return members[0]
return {"oneOf": members}

def enum_schema(self, schema: core_schema.EnumSchema) -> JsonSchemaValue:
"""Generates a JSON schema that matches an Enum value.
Args:
schema: The core schema.
Returns:
The generated JSON schema.
"""
enum_type = schema['cls']
description = None if not enum_type.__doc__ else inspect.cleandoc(enum_type.__doc__)
if (
description == 'An enumeration.'
): # This is the default value provided by enum.EnumMeta.__new__; don't use it
description = None
result: dict[str, Any] = {'title': enum_type.__name__, 'description': description}
result = {k: v for k, v in result.items() if v is not None}

expected = [to_jsonable_python(v.value) for v in schema['members']]

result['enum'] = expected
if len(expected) == 1:
result['const'] = expected[0]

types = {type(e) for e in expected}
if isinstance(enum_type, str) or types == {str}:
result['type'] = 'string'
elif isinstance(enum_type, int) or types == {int}:
result['type'] = 'integer'
elif isinstance(enum_type, float) or types == {float}:
result['type'] = 'numeric'
elif types == {bool}:
result['type'] = 'boolean'
elif types == {list}:
result['type'] = 'array'

if (self.render_xenum_names) and (result['type'] != 'string'):
result['x-enumNames'] = [screaming_snake_case_to_pascal_case(v.name) for v in schema['members']]

return result

def literal_schema(self, schema: core_schema.LiteralSchema) -> JsonSchemaValue:
"""Generates a JSON schema that matches a literal value.
Expand Down Expand Up @@ -239,6 +282,20 @@ def pascal_to_snake_case(s: str) -> str:
return result


def screaming_snake_case_to_pascal_case(s: str) -> str:
"""
Converts a SCREAMING_SNAKE_CASE string to PascalCase.
Args:
s (str): The SCREAMING_SNAKE_CASE string to be converted.
Returns:
str: The PascalCase string.
"""
words = s.split('_')
return ''.join(word.capitalize() for word in words)


def _build_bonsai_process_command(
workflow_file: PathLike | str,
bonsai_exe: PathLike | str = "bonsai/bonsai.exe",
Expand Down
23 changes: 20 additions & 3 deletions src/DataSchemas/schemas/aind_manipulator_calibration_rig.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,14 @@
4
],
"title": "Axis",
"type": "integer"
"type": "integer",
"x-enumNames": [
"None",
"Y1",
"Y2",
"X",
"Z"
]
},
"AxisConfiguration": {
"description": "Axis configuration",
Expand Down Expand Up @@ -349,15 +356,25 @@
3
],
"title": "MicrostepResolution",
"type": "integer"
"type": "integer",
"x-enumNames": [
"Microstep8",
"Microstep16",
"Microstep32",
"Microstep64"
]
},
"MotorOperationMode": {
"enum": [
0,
1
],
"title": "MotorOperationMode",
"type": "integer"
"type": "integer",
"x-enumNames": [
"Quiet",
"Dynamic"
]
}
},
"properties": {
Expand Down
26 changes: 13 additions & 13 deletions src/Extensions/AindManipulatorCalibrationRig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,19 +494,19 @@ public enum Axis
{

[System.Runtime.Serialization.EnumMemberAttribute(Value="0")]
_0 = 0,
None = 0,

[System.Runtime.Serialization.EnumMemberAttribute(Value="1")]
_1 = 1,
Y1 = 1,

[System.Runtime.Serialization.EnumMemberAttribute(Value="2")]
_2 = 2,
Y2 = 2,

[System.Runtime.Serialization.EnumMemberAttribute(Value="3")]
_3 = 3,
X = 3,

[System.Runtime.Serialization.EnumMemberAttribute(Value="4")]
_4 = 4,
Z = 4,
}


Expand All @@ -526,11 +526,11 @@ public partial class AxisConfiguration

private int _stepInterval = 100;

private MicrostepResolution _microstepResolution = AindBehaviorServices.AindManipulatorCalibrationRig.MicrostepResolution._0;
private MicrostepResolution _microstepResolution = AindBehaviorServices.AindManipulatorCalibrationRig.MicrostepResolution.Microstep8;

private int _maximumStepInterval = 2000;

private MotorOperationMode _motorOperationMode = AindBehaviorServices.AindManipulatorCalibrationRig.MotorOperationMode._0;
private MotorOperationMode _motorOperationMode = AindBehaviorServices.AindManipulatorCalibrationRig.MotorOperationMode.Quiet;

private int _maxLimit = 24000;

Expand Down Expand Up @@ -866,16 +866,16 @@ public enum MicrostepResolution
{

[System.Runtime.Serialization.EnumMemberAttribute(Value="0")]
_0 = 0,
Microstep8 = 0,

[System.Runtime.Serialization.EnumMemberAttribute(Value="1")]
_1 = 1,
Microstep16 = 1,

[System.Runtime.Serialization.EnumMemberAttribute(Value="2")]
_2 = 2,
Microstep32 = 2,

[System.Runtime.Serialization.EnumMemberAttribute(Value="3")]
_3 = 3,
Microstep64 = 3,
}


Expand All @@ -884,10 +884,10 @@ public enum MotorOperationMode
{

[System.Runtime.Serialization.EnumMemberAttribute(Value="0")]
_0 = 0,
Quiet = 0,

[System.Runtime.Serialization.EnumMemberAttribute(Value="1")]
_1 = 1,
Dynamic = 1,
}


Expand Down
6 changes: 3 additions & 3 deletions src/aind_manipulator_calibration.bonsai
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<WorkflowBuilder Version="2.8.1"
<WorkflowBuilder Version="2.8.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p1="clr-namespace:AindBehaviorServices.AindManipulatorCalibrationRig;assembly=Extensions"
xmlns:rx="clr-namespace:Bonsai.Reactive;assembly=Bonsai.Core"
Expand Down Expand Up @@ -297,7 +297,7 @@
</Expression>
<Expression xsi:type="Subtract">
<Operand xsi:type="WorkflowProperty" TypeArguments="p1:Axis">
<Value>_1</Value>
<Value>Y1</Value>
</Operand>
</Expression>
<Expression xsi:type="Combinator">
Expand Down Expand Up @@ -525,7 +525,7 @@
</Expression>
<Expression xsi:type="Subtract">
<Operand xsi:type="WorkflowProperty" TypeArguments="p1:Axis">
<Value>_1</Value>
<Value>Y1</Value>
</Operand>
</Expression>
<Expression xsi:type="Combinator">
Expand Down

0 comments on commit ab6b926

Please sign in to comment.