Skip to content

Commit

Permalink
Improved error checking point coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchmitz committed Feb 15, 2024
1 parent 0d03d3b commit 8f5d4d2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.18)

project(campo
VERSION 0.3.9
VERSION 0.3.10
DESCRIPTION "Modelling framework for fields and agents"
HOMEPAGE_URL "https://campo.computationalgeography.org/"
LANGUAGES NONE
Expand Down
2 changes: 2 additions & 0 deletions source/campo/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def read(self, filename):
self.nr_items = len(content)

self.p1 = Points()
self.p1.nr_items = self.nr_items
self.p2 = Points()
self.p2.nr_items = self.nr_items

x1 = numpy.zeros(self.nr_items)
y1 = numpy.zeros(self.nr_items)
Expand Down
45 changes: 26 additions & 19 deletions source/campo/points.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import csv
import numpy
import numpy as np


class Points(object):
class Points():
def __init__(self, mobile=False):

self.nr_items = None
Expand Down Expand Up @@ -40,16 +40,34 @@ def xcoord(self):
return self._xcoord

@xcoord.setter
def xcoord(self, value):
self._xcoord = value
def xcoord(self, values):
new_values = values
if new_values is not None:
if isinstance(values, list):
new_values = np.array(values)

if new_values.shape != (self.nr_items,):
msg = f"Number of provided coordinates ({new_values.shape[0]}) does not match number of agents ({self.nr_items})"
raise RuntimeError(msg)

self._xcoord = new_values

@property
def ycoord(self):
return self._ycoord

@ycoord.setter
def ycoord(self, value):
self._ycoord = value
def ycoord(self, values):
new_values = values
if new_values is not None:
if isinstance(values, list):
new_values = np.array(values)

if new_values.shape != (self.nr_items,):
msg = f"Number of provided coordinates ({new_values.shape[0]}) does not match number of agents ({self.nr_items})"
raise RuntimeError(msg)

self._ycoord = new_values

@property
def nr_items(self):
Expand All @@ -67,26 +85,15 @@ def read(self, filename):

self.nr_items = len(content)

#x = numpy.zeros(self.nr_items)
#y = numpy.zeros(self.nr_items)

#for idx, item in enumerate(content):
#x[idx] = item[0]
#y[idx] = item[1]

#self.xcoord = x
#self.ycoord = y

v = numpy.empty((self.nr_items, 2))
v = np.empty((self.nr_items, 2))
for idx, item in enumerate(content):
v[idx, 0] = item[0]
v[idx, 1] = item[1]
# numpy.random.shuffle(v)

self.xcoord = v[:, 0]
self.ycoord = v[:, 1]

self._coordinates = numpy.empty((self.nr_items, 2))
self._coordinates = np.empty((self.nr_items, 2))

def __iter__(self):
return self
Expand Down
2 changes: 1 addition & 1 deletion source/test/test_mobile_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_1(self):
curr_mp = self.a.mobile_points.get_space_domain(timestep)
# set coordinates of current timestep
curr_mp.xcoord = x_coords + timestep
curr_mp.ycoord = y_coords + timestep
curr_mp.ycoord = list(y_coords + timestep)

self.a.mobile_points.set_space_domain(curr_mp, timestep)

Expand Down

0 comments on commit 8f5d4d2

Please sign in to comment.