Skip to content

Commit

Permalink
Feat: save and load for alpha and conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
MagedMohamedTurk committed Feb 25, 2022
1 parent 8d61187 commit b673e82
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
5 changes: 5 additions & 0 deletions pyproject.toml
Expand Up @@ -4,3 +4,8 @@ requires = [
"wheel"
]
build-backend = "setuptools.build_meta"
[tool.pytest.ini_options]
log_cli=true
log_level='NOTSET'
log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="hsbalance",
version="0.5.3",
version="0.5.4",
author="Maged M.Eltorkoman",
author_email="newmaged@gmail.com",
description="Python tools for Practical Modeling and Solving High Speed Rotor Unbalance Problem",
Expand Down
28 changes: 18 additions & 10 deletions src/hsbalance/CI_matrix.py
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import hsbalance.tools as tools
import pandas as pd
import pickle

_logger = logging.getLogger(__name__)
_logger.propagate = False
Expand Down Expand Up @@ -256,17 +257,24 @@ def __repr__(self):

return ''.join(formatter.info())

def test_save():
alpha = Alpha(name='Model_IC')
alpha.add(np.random.rand(3, 3))
alpha.save('my_alpha')

def save(self, file:str):
'''
Method to save condition instance.
'''
if isinstance(file, str):
self.file = file
with open(self.file, 'wb') as f:
pickle.dump(self, f)

if __name__ == '__main__':
test_save()
my_alpha = Alpha()
my_alpha.load('my_alpha')
print(my_alpha)


def load(self, file:str):
'''
Method to load condition instance.
'''
if isinstance(file, str):
self.file = file
with open(self.file, 'rb') as f:
_loaded_instance = pickle.load(f)
self.add(_loaded_instance.alpha, _loaded_instance.A)

2 changes: 2 additions & 0 deletions src/hsbalance/model.py
Expand Up @@ -8,6 +8,7 @@

class _Model:
"""Abstract class for models"""
# TODO init model by only name to be able to load from previous saved instance

def __init__(self, A:'initial_vibration'=None, alpha: 'instance of Alpha class'=None,
conditions: 'list of condition instances'=None, name:'string'=''):
Expand Down Expand Up @@ -291,6 +292,7 @@ def info(self):
return ''.join(formatter.info())



class LeastSquares(_Model):
"""subclass of Model
solving the model using Least squares method, The objective function
Expand Down
Binary file removed src/hsbalance/my_alpha.npy
Binary file not shown.
12 changes: 12 additions & 0 deletions test/test_ALPHA.py
Expand Up @@ -5,6 +5,10 @@
import test_tools
import warnings
import hsbalance as hs
import tempfile
import logging
logger = logging.getLogger(__name__)
temp_file = tempfile.NamedTemporaryFile()

'''This module is for testing ALPHA class'''
# Reading the test cases from config.yaml file
Expand Down Expand Up @@ -150,3 +154,11 @@ def test_info():
condition1 = hs.Condition(name='Speed 2500')
condition1.add(alpha2, A=np.random.rand(m,1))
print(alpha1, alpha2, condition1)

def test_alpha_save_load(test_alpha):
np.random.seed(42)
test_alpha.add(np.random.rand(6,4))
test_alpha.save(temp_file.name)
loaded_alpha = hs.Alpha()
loaded_alpha.load(temp_file.name)
np.testing.assert_allclose(loaded_alpha.value, test_alpha.value)
22 changes: 19 additions & 3 deletions test/test_model.py
@@ -1,13 +1,14 @@
import time
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt
import scipy.optimize as opt
import numpy as np
import sys
import yaml
import pytest
import test_tools
import hsbalance as hs
import tempfile
import logging
logger = logging.getLogger(__name__)
temp_file = tempfile.NamedTemporaryFile()

def test_model_with_no_argument():
with pytest.raises(TypeError) as e_info:
Expand Down Expand Up @@ -195,8 +196,23 @@ def test_info_model():
split2.update(confirm=True)
print(model.info())

# Testing save and load conditions

def test_Condition_save_load(test_A, test_alpha):
condition = hs.Condition()
condition.add(test_alpha, test_A)
condition.save(temp_file.name)
loaded_condition = hs.Condition()
loaded_condition.load(temp_file.name)
np.testing.assert_allclose(loaded_condition.alpha.value, test_alpha.value)
np.testing.assert_allclose(loaded_condition.A, test_A)


if __name__ == '__main__':
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt
import scipy.optimize as opt

def test_performance(n):
alpha = hs.Alpha()
real = np.random.uniform(0, 10, [n, n])
Expand Down

0 comments on commit b673e82

Please sign in to comment.