Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
workflow_dispatch:

jobs:
build:
unit-tests:
runs-on: ubuntu-latest

steps:
Expand Down
59 changes: 39 additions & 20 deletions spatialpy/Result.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
import filecmp
import math
import os
import re
import pickle
import shutil
import subprocess
import sys
import tempfile
import types
import warnings
import uuid


import numpy
import scipy.io
import scipy.sparse

from spatialpy.VTKReader import VTKReader
from spatialpy.Model import *

import inspect

import pickle
import json
import math


from spatialpy.VTKReader import VTKReader

common_rgb_values=['#1f77b4','#ff7f0e','#2ca02c','#d62728','#9467bd','#8c564b','#e377c2','#7f7f7f',
'#bcbd22','#17becf','#ff0000','#00ff00','#0000ff','#ffff00','#00ffff','#ff00ff',
Expand Down Expand Up @@ -94,6 +80,38 @@ def __init__(self, model=None, result_dir=None, loaddata=False):
# return model2


def __eq__(self, other):
""" Compare Result object's output for equality. This does _NOT_ compare objects themselves

Params:
self: Results object
other: Results object to compare against
Return:
bool """

if isinstance(other, Result) and self.result_dir != None and other.result_dir != None:
# Compare contents, not shallow compare
filecmp.cmpfiles.__defaults__ = (False,)
dircmp = filecmp.dircmp(self.result_dir, other.result_dir)
# Raise exception if funny_files
assert not dircmp.funny_files
if not (dircmp.left_only or dircmp.right_only or dircmp.funny_files or dircmp.diff_files):
return True
return False
return NotImplemented

def __ne__(self, other):
""" Compare Result object's output for inequality. This does _NOT_ compare objects themselves.
This inverts the logic in __eq__().

Params:
self: Results object
other: Results object to compare against
Return:
bool """

return not self.__eq__(other)

def __getstate__(self):
""" Used by pickle to get state when pickling. We need to read the contents of the
output file since we can't pickle file objects. """
Expand Down Expand Up @@ -156,6 +174,7 @@ def get_species(self, species, timepoints=None, concentration=False, determinist
#t_index_arr = numpy.linspace(0,self.model.num_timesteps,
# num=self.model.num_timesteps+1, dtype=int)
t_index_arr = self.get_timespan();
# Should this ; be here? ^

if timepoints is not None:
if isinstance(timepoints,float):
Expand Down Expand Up @@ -257,7 +276,7 @@ def plot_species(self, species, t_ndx=0, concentration=False, deterministic=Fals

if use_matplotlib:
import matplotlib.pyplot as plt

if (deterministic or not concentration):
d = data[spec_name]
else:
Expand Down Expand Up @@ -496,7 +515,7 @@ def plot_property(self, property_name, t_ndx=0, p_ndx=0, width=500, height=500,

if use_matplotlib:
import matplotlib.pyplot as plt

if (property_name == 'v'):
d = data[property_name]
d = [d[i][p_ndx] for i in range(0,len(d))]
Expand Down
97 changes: 27 additions & 70 deletions test/test_solver.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# import pickle
from spatialpy.Model import ModelError
import unittest
import spatialpy

# class SimpleDiffusion(spatialpy.Model):
# """ Initial condition is a delta function at the center voxel.
# The solution should be a Gaussian, up to the point where
# the BC becomes important. """

# def __init__(self, model_name="simple_diffusion"):
# spatialpy.Model.__init__(self, model_name)
# A = self.add_species(spatialpy.Species(
# name="A", diffusion_constant=0.01))
# # A unit square

# # System constants
# nxF, nyF = 50, 50 # number of particles in x and y-direction
# L = 1 # characteristic lenght of the cavity (= width = height)
# nW = 3 # number of wall points
# rho0 = 1 # reference fluid density

# # Compute domain bounds (including the boundary)
# dx, dy = L/(nxF-1), L/(nyF-1)
# xLim = ((0-(nW-1)*dx), 1+(nW-1)*dx)
# yLim = ((0-(nW-1)*dy), 1+(nW-1)*dy)

# # Discretization
# # total number of particles in x-direction (including walls)
# nxTot = nxF + 2*(nW-1)
# # total number of particles in y-direction (including walls)
# nyTot = nyF + 2*(nW-1)

# # Compute volume and mass per particle
# # in 2D simulations, consider z-lenght = 1
# vol = (xLim[1]-xLim[0])*(yLim[1]-yLim[0])*1.0
# # density * total volume / total number of particles
# mPP = rho0*vol/(nxTot*nyTot)

# self.mesh = spatialpy.Mesh.create_2D_domain(
# xlim=xLim, ylim=yLim, nx=nxTot, ny=nyTot, type_id=1, mass=mPP, rho0=rho0, fixed=True)
# # Place the A molecules in the voxel nearest the center of the square
# # self.set_initial_condition_place_near({A:10000},point=[0.5,0.5])
# self.add_initial_condition(spatialpy.ScatterInitialCondition(A, 10000))
# self.timestep_size = 1e-4
# self.timespan(numpy.linspace(0, 5, 200))


class diffusion_debug(spatialpy.Model):

Expand Down Expand Up @@ -97,32 +54,32 @@ def test_solver_io(self):
# A = result.get_species("A", -1)
# self.assertFalse((A - model.u0).any())

# def test_same_seed(self):
# """ Test that the output is the same if the same seed is used, edxplicit solver creation """
# solver = spatialpy.Solver(self.model)
# result1 = solver.run(seed=1)
# result2 = solver.run(seed=1)
# self.assertEqual(result1, result2)

# def test_same_seed2(self):
# """ Test that the output is the same if the same seed is used, use model.run() """
# result1 = self.model.run(seed=1)
# result2 = self.model.run(seed=1)
# self.assertEqual(result1, result2)

# def test_different_seeds(self):
# """ Test that the output is different if different seeds are used. """
# solver = spatialpy.Solver(self.model)
# result1 = solver.run(seed=1)
# result2 = solver.run(seed=100)
# self.assertNotEqual(result1, result2)

# def test_default_seed(self):
# """ Test that the output is different if no seed is given (default set on C level). """
# solver = spatialpy.Solver(self.model)
# result1 = solver.run()
# result2 = solver.run()
# self.assertNotEqual(result1, result2)
def test_same_seed(self):
""" Test that the output is the same if the same seed is used, edxplicit solver creation """
solver = spatialpy.Solver(self.model)
result1 = solver.run(seed=1)
result2 = solver.run(seed=1)
self.assertTrue(result1 == result2)

def test_same_seed2(self):
""" Test that the output is the same if the same seed is used, use model.run() """
result1 = self.model.run(seed=1)
result2 = self.model.run(seed=1)
self.assertTrue(result1 == result2)

def test_different_seeds(self):
""" Test that the output is different if different seeds are used. """
solver = spatialpy.Solver(self.model)
result1 = solver.run(seed=1)
result2 = solver.run(seed=100)
self.assertFalse(result1 == result2)

def test_default_seed(self):
""" Test that the output is different if no seed is given (default set on C level). """
solver = spatialpy.Solver(self.model)
result1 = solver.run()
result2 = solver.run()
self.assertFalse(result1 == result2)

# def test_mesh_pickle(self):
# meshstr = pickle.dumps(self.model.mesh)
Expand Down