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

Commit

Permalink
added data model demo
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPatrie committed Mar 1, 2024
1 parent 42c3c84 commit 7d19249
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 24 deletions.
41 changes: 27 additions & 14 deletions biosimulator_processes/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# TODO: You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
class BaseModel(Base):
model_config = ConfigDict(arbitrary_types_allowed=True)
protected_namespaces: Tuple = Field(default=())
# 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 @@ -61,7 +61,7 @@ class ReactionChanges(BaseModel):
reaction_scheme: Union[NoneType, str] = Field(default=None)


class ModelChanges:
class ModelChanges(BaseModel):
species_changes: Union[NoneType, List[SpeciesChanges]] = Field(default=None)
global_parameter_changes: Union[NoneType, List[GlobalParameterChanges]] = Field(default=None)
reaction_changes: Union[NoneType, List[ReactionChanges]] = Field(default=None)
Expand All @@ -72,7 +72,7 @@ class ModelSource(BaseModel):


class BiomodelId(ModelSource):
value: str = Field(default='')
value: str

@classmethod
@field_validator('value')
Expand All @@ -82,7 +82,7 @@ def check_value(cls, v):


class ModelFilepath(BaseModel):
value: str = Field(default='')
value: str

@classmethod
@field_validator('value')
Expand All @@ -96,15 +96,13 @@ class Model(BaseModel):
Parameters:
model_id: `str`
input_source: `str`
model_source: `Union[biosimulator_processes.data_model.ModelFilepath, biosimulator_processes.data_model.BiomodelId]`
model_language: `str`
model_name: `str`
model_changes: `biosimulator_processes.data_model.ModelChanges`
"""
model_id: str = Field(default='')
input_source: str # <-- user input
model_source: Union[ModelFilepath, BiomodelId] # <-- SED type validated by constructor
model_source: str # <-- SED type validated by constructor
model_language: str = Field(default='sbml')
model_name: str = Field(default='Unnamed Composite Process Model')
model_changes: ModelChanges
Expand All @@ -113,19 +111,34 @@ class Model(BaseModel):
@field_validator('model_source')
def set_value(cls):
"""Verify that the model source is set to only either a path or a biomodels id"""
if '/' in cls.input_source:
return ModelFilepath(value=cls.input_source)
elif 'BIO' in cls.input_source:
return BiomodelId(value=cls.input_source)
# if '/' in cls.model_source:
# return ModelFilepath(value=cls.model_source)
# elif 'BIO' in cls.input_source:
# return BiomodelId(value=cls.model_source)
if '/' not in cls.model_source or 'BIO' not in cls.model_source:
raise AttributeError('You must pass either a model filepath or valid biomodel id.')
else:
return cls.model_source


class ProcessConfigSchema(BaseModel):
config: Dict
config: Dict = Field(default={})


class CopasiProcessConfigSchema(ProcessConfigSchema):
model: Union[Dict, Model]
class CopasiProcessConfigSchema(BaseModel):
method: str = Field(default='deterministic')
model: Union[Model, Dict]

@classmethod
@field_serializer('model')
def serialize_model(cls):
return cls.model.model_dump()

'''@classmethod
@field_validator('model')
def set_model(cls):
if isinstance(cls.model, Model):
return cls.model.model_dump()'''


class PortSchema(BaseModel):
Expand Down
125 changes: 115 additions & 10 deletions notebooks/data_model_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-03-01T16:58:02.687656Z",
"start_time": "2024-03-01T16:58:02.684480Z"
"end_time": "2024-03-01T17:36:11.752324Z",
"start_time": "2024-03-01T17:36:11.749069Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -69,13 +69,14 @@
}
],
"source": [
"from biosimulator_processes.data_model import *"
"from biosimulator_processes.data_model import *\n",
"from process_bigraph import pf"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T16:58:04.483343Z",
"start_time": "2024-03-01T16:58:02.800619Z"
"end_time": "2024-03-01T17:36:13.415658Z",
"start_time": "2024-03-01T17:36:11.894800Z"
}
},
"id": "3cd5053431aed7e1",
Expand All @@ -85,21 +86,125 @@
"cell_type": "code",
"outputs": [],
"source": [
"speciesChangesA = SpeciesChanges(\n",
" species_name='A',\n",
" initial_concentration=20.017\n",
")\n",
"\n",
"speciesChangesB = SpeciesChanges(\n",
" species_name='B',\n",
" expression='A + B = C'\n",
")\n",
"\n",
"process_changes = ModelChanges(\n",
" species_changes=[speciesChangesA, speciesChangesB]\n",
")\n",
"\n",
"# process_biomodel_id = BiomodelId(value='BIOMD0000000861')\n",
"\n",
"process_model = Model(\n",
" model_id='BioModel',\n",
" input_source='BIOMD0000000861',\n",
" model_source='BIOMD0000000861',\n",
" model_name='Bachmann2011',\n",
" model_changes=''\n",
" model_changes=process_changes\n",
")"
],
"metadata": {
"collapsed": false
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T17:36:13.418537Z",
"start_time": "2024-03-01T17:36:13.415607Z"
}
},
"id": "6bf18cbc74f1f95f",
"execution_count": null
"execution_count": 3
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": "biosimulator_processes.data_model.Model"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(process_model)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T17:36:15.889304Z",
"start_time": "2024-03-01T17:36:15.883583Z"
}
},
"id": "6dd932af0c8358aa",
"execution_count": 4
},
{
"cell_type": "code",
"outputs": [],
"source": [
"copasi_process = CopasiProcessConfigSchema(\n",
" model=process_model,\n",
" method='stochastic'\n",
")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T17:36:16.365986Z",
"start_time": "2024-03-01T17:36:16.363866Z"
}
},
"id": "909c4fc3a751659",
"execution_count": 5
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ 'method': 'stochastic',\n",
" 'model': { 'model_changes': { 'global_parameter_changes': None,\n",
" 'reaction_changes': None,\n",
" 'species_changes': [ { 'expression': None,\n",
" 'initial_concentration': 20.017,\n",
" 'initial_expression': None,\n",
" 'initial_particle_number': None,\n",
" 'species_name': 'A',\n",
" 'unit': None},\n",
" { 'expression': 'A + B = '\n",
" 'C',\n",
" 'initial_concentration': None,\n",
" 'initial_expression': None,\n",
" 'initial_particle_number': None,\n",
" 'species_name': 'B',\n",
" 'unit': None}]},\n",
" 'model_id': 'BioModel',\n",
" 'model_language': 'sbml',\n",
" 'model_name': 'Bachmann2011',\n",
" 'model_source': 'sdfadf'}}\n"
]
}
],
"source": [
"print(pf(copasi_process.model_dump()))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-01T17:36:16.957634Z",
"start_time": "2024-03-01T17:36:16.954386Z"
}
},
"id": "2be623d00e2fc495",
"execution_count": 6
},
{
"cell_type": "code",
Expand All @@ -108,7 +213,7 @@
"metadata": {
"collapsed": false
},
"id": "6dd932af0c8358aa"
"id": "7bca99082a8df8ef"
}
],
"metadata": {
Expand Down

0 comments on commit 7d19249

Please sign in to comment.