Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
fix: updated data model for class methods and error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPatrie committed Mar 1, 2024
1 parent a905e54 commit 56e248a
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 112 deletions.
76 changes: 52 additions & 24 deletions biosimulator_processes/data_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
from typing import Dict, List, Union
from typing import Dict, List, Union, Tuple
from types import NoneType
from abc import ABC, abstractmethod
from pydantic import BaseModel, field_validator, field_serializer, Field
from pydantic import BaseModel as Base, field_validator, field_serializer, Field, ConfigDict


__all__ = [
'SpeciesChanges',
'GlobalParameterChanges',
'ReactionParameter',
'ReactionChanges',
'ModelChanges',
'ModelSource',
'BiomodelId',
'ModelFilepath',
'Model',
'ProcessConfigSchema',
'CopasiProcessConfigSchema',
'PortSchema',
'EmittedType',
'EmitterInstance',
'ProcessInstance',
'FromDict',
'BasicoModelChanges',
'SedModel'
]


class BaseModel(Base):
model_config = ConfigDict(arbitrary_types_allowed=True)
protected_namespaces: Tuple = Field(default=())


class SpeciesChanges(BaseModel): # <-- this is done like set_species('B', kwarg=) where the inner most keys are the kwargs
Expand Down Expand Up @@ -39,29 +66,26 @@ class ModelChanges:
reaction_changes: List[ReactionChanges]


class ModelSource(ABC, BaseModel):
class ModelSource(BaseModel):
value: str = None

@field_validator('value')
@abstractmethod
def check_value(self, v):
pass


class BiomodelId(ModelSource):
value: str = None

@classmethod
@field_validator('value')
def check_value(self, v):
def check_value(cls, v):
assert '/' not in v, "value must not contain a path but rather a valid BioModels id like: 'BIOMODELS...'"
return v


class ModelFilepath(BaseModel):
value: str = None

@classmethod
@field_validator('value')
def check_value(self, v):
def check_value(cls, v):
assert '/' in v, "value must contain a path"
return v

Expand All @@ -75,13 +99,14 @@ class Model(BaseModel):
model_name: str = Field(default='Unnamed Composite Process Model')
model_changes: ModelChanges

@classmethod
@field_validator('model_source')
def set_value(self):
def set_value(cls):
"""Verify that the model source is set to only either a path or a biomodels id"""
if '/' in self.input_source:
return ModelFilepath(value=self.input_source)
elif 'BIO' in self.input_source:
return BiomodelId(value=self.input_source)
if '/' in cls.input_source:
return ModelFilepath(value=cls.input_source)
elif 'BIO' in cls.input_source:
return BiomodelId(value=cls.input_source)


class ProcessConfigSchema(BaseModel):
Expand All @@ -94,14 +119,15 @@ class CopasiProcessConfigSchema(ProcessConfigSchema):


class PortSchema(BaseModel):
input_value_names: [List[str]] # user input
schema: Dict[str, List[str]]
input_value_names: List[str] # user input
_schema: Dict[str, List[str]]

@field_validator('schema')
def set_value(self):
@classmethod
@field_validator('_schema')
def set_value(cls):
return {
input_value: [f'{input_value}_store']
for input_value in self.input_value_names}
for input_value in cls.input_value_names}


class EmittedType:
Expand All @@ -116,12 +142,13 @@ class EmitterInstance:
config: Dict[str, Dict[str, str]]
inputs: PortSchema # these names might be the same as self.config

@classmethod
@field_validator('config')
def set_value(self):
def set_value(cls):
config = {
'emit': {
emit_type.value_name: emit_type._type
for emit_type in self.emit_types
for emit_type in cls.emit_types
}
}

Expand All @@ -134,12 +161,13 @@ class ProcessInstance:
outputs: PortSchema
emitter: Union[EmitterInstance, NoneType] = Field(default=None)

@classmethod
@field_validator('address')
def check_value(self, v):
def check_value(cls, v):
pass


# FromDict classes
# Non-Pydantic FromDict classes
class FromDict(dict):
def __init__(self, value: Dict):
super().__init__(value)
Expand Down
92 changes: 4 additions & 88 deletions notebooks/builder_composer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -577,96 +577,12 @@
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6c11fc1f-23ee-45e5-9975-f3d3b2e1dff9",
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-01T14:49:48.391394Z",
"start_time": "2024-03-01T14:49:46.902231Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CobraProcess registered successfully.\n",
"CopasiProcess not available. Error: No module named 'biosimulator_processes.process_types'\n",
"SmoldynProcess registered successfully.\n",
"TelluriumProcess registered successfully.\n"
]
}
],
"source": [
"from biosimulator_processes.data_model import BasicoModelChanges"
]
},
{
"cell_type": "code",
"outputs": [],
"source": [
"changes = BasicoModelChanges()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T14:49:48.392183Z",
"start_time": "2024-03-01T14:49:48.384674Z"
}
},
"id": "8bfc0e4e8a5a33dc",
"execution_count": 2
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "{'species_changes': {'species_name': {'unit': 'maybe[string]',\n 'initial_concentration': 'maybe[float]',\n 'initial_particle_number': 'maybe[float]',\n 'initial_expression': 'maybe[string]',\n 'expression': 'maybe[string]'}},\n 'global_parameter_changes': {'global_parameter_name': {'initial_value': 'maybe[float]',\n 'initial_expression': 'maybe[string]',\n 'expression': 'maybe[string]',\n 'status': 'maybe[string]',\n 'type': 'maybe[string]'}},\n 'reaction_changes': {'reaction_name': {'parameters': {'reaction_parameter_name': 'maybe[int]'},\n 'reaction_scheme': 'maybe[string]'}}}"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"changes"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T14:49:48.392944Z",
"start_time": "2024-03-01T14:49:48.387744Z"
}
},
"id": "85395c6be6b05aa4",
"execution_count": 3
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "biosimulator_processes.data_model.BasicoModelChanges"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(changes)"
],
"cell_type": "markdown",
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T14:51:08.922231Z",
"start_time": "2024-03-01T14:51:08.905949Z"
}
"collapsed": false
},
"id": "371623197a002e07",
"execution_count": 4
"id": "b379609c7bc217c0"
},
{
"cell_type": "code",
Expand Down
120 changes: 120 additions & 0 deletions notebooks/data_model_demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-03-01T16:47:21.583961Z",
"start_time": "2024-03-01T16:47:21.581681Z"
}
},
"outputs": [],
"source": [
"import sys \n",
"\n",
"sys.path.insert(0, '..')"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CobraProcess registered successfully.\n",
"CopasiProcess not available. Error: No module named 'biosimulator_processes.process_types'\n",
"SmoldynProcess registered successfully.\n",
"TelluriumProcess registered successfully.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/alex/Library/Caches/pypoetry/virtualenvs/biosimulator-processes-KVuYbFzt-py3.10/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning:\n",
"\n",
"Field \"model_id\" has conflict with protected namespace \"model_\".\n",
"\n",
"You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.\n",
"\n",
"/Users/alex/Library/Caches/pypoetry/virtualenvs/biosimulator-processes-KVuYbFzt-py3.10/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning:\n",
"\n",
"Field \"model_source\" has conflict with protected namespace \"model_\".\n",
"\n",
"You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.\n",
"\n",
"/Users/alex/Library/Caches/pypoetry/virtualenvs/biosimulator-processes-KVuYbFzt-py3.10/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning:\n",
"\n",
"Field \"model_language\" has conflict with protected namespace \"model_\".\n",
"\n",
"You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.\n",
"\n",
"/Users/alex/Library/Caches/pypoetry/virtualenvs/biosimulator-processes-KVuYbFzt-py3.10/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning:\n",
"\n",
"Field \"model_name\" has conflict with protected namespace \"model_\".\n",
"\n",
"You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.\n",
"\n",
"/Users/alex/Library/Caches/pypoetry/virtualenvs/biosimulator-processes-KVuYbFzt-py3.10/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning:\n",
"\n",
"Field \"model_changes\" has conflict with protected namespace \"model_\".\n",
"\n",
"You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.\n",
"\n"
]
}
],
"source": [
"from biosimulator_processes.data_model import *"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T16:47:23.232487Z",
"start_time": "2024-03-01T16:47:21.584280Z"
}
},
"id": "3cd5053431aed7e1",
"execution_count": 2
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T16:47:23.234568Z",
"start_time": "2024-03-01T16:47:23.232709Z"
}
},
"id": "6bf18cbc74f1f95f",
"execution_count": 2
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 56e248a

Please sign in to comment.