Skip to content

Big idea: dump/load models by using pydantic #274

@connorferster

Description

@connorferster

Is your feature request related to a problem? Please describe.
When using Pynite for engineering work, there are times where it would be nice to be able to have "model files" instead of the commands to create the model living exclusively in a .py or .ipynb file. By using the pydantic library, this can be achieved readily by creating a model JSON file that can be used to populate an FEModel3D and run the analysis.

Here are some benefits that could be gained through this feature:

  • Speed of data input: it takes less time to type just the node coordinates than model.add_node() and the coordinates
  • Portability of models: A JSON file that represents a model can be used to power a Pynite analysis but it can also be used with another analysis engines (provided one has written functions to plug the data in to whatever API)
  • Applications can be built on-top of Pynite more readily since the Python library can now work more like an application (e.g. a command-line application): pass the model file (.json) to the Pynite analysis engine and generate your outputs.
  • Batch processing models: by having an input file-type, multiple models can be run in parallel by using multi-processing.
  • Debugging: if someone is having trouble with a model, they can upload/send the file to someone to help assist (although this is not much different than running someone's .py file).

Describe the solution you'd like

I have been using pydantic on several projects and I find it incredibly useful. pydantic is a data validation library for Python. One of its super-powers is that it can round-trip custom classes to JSON and, in the process, validate whether the JSON is correct or not (in the event that it was created/edited by the user).

To access this super power, any class can simply inherit from pydantic.BaseModel. If a class is composed of other custom classes, as long as they also inherit from pydantic.BaseModel, then the whole tree of classes become serializable to JSON.

Without pydantic, writing the code to allow for models to be "saved" and "reloaded" would be a tremendous effort, especially when it came to data validation. pydantic handles data validation by using type annotations on class attributes.

Describe alternatives you've considered

  • Do nothing - Pynite is not intended to have this feature.
  • Creating a fork of Pynite that has this feature included - I am not really interested in maintaining a fork :)
  • Creating a 3rd party library that modifies the Pynite classes on import and provides this functionality as a separate "add on" or "extension" feature (not sure if this is possible but might be). I am open to doing this but I think this feature is potentially too good to keep separate from the main project. If it seems that there is no interest in adding another dependency as a core project dependency or if folks are not interested in this feature then I will proceed with creating a 3rd party extension library to make this feature available.

Further thoughts

I think this could be a powerful new feature but it does require adding another dependency to the project and it would be a core dependency to the project since it would require modifying the Pynite classes.

Action steps

Since I have experience with pydantic, I would be happy to implement this on a new branch so that we could compare the two versions. Since the code change will mostly be changing classes from class FEModel3d to class FEModel3D(BaseModel), I do not think it will add much maintenance burden to the code base but, as always, actually implementing it will be how we know!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions