def test_get_models(self): client = getclient() models = client.get_models() self.assertTrue(len(models) > 0) log.info('test %i models', len(models)) for model in models: # type: dict self.assertIsNotNone(model.get('id')) self.assertIsNotNone(model.get('name')) self.assertIsNotNone(model.get('location'))
def test_direct_impacts(self): """Test the reproducibility of the direct impact matrix: D = C B""" client = getclient() for model in client.get_models(): model_id = model['id'] log.info('check the direct impact matrix D in model %s', model_id) B = client.get_matrix(model_id, "B") C = client.get_matrix(model_id, "C") D = C @ B self.compare_matrices(D, client.get_matrix(model_id, 'D'))
def test_check_leontief_inverse(self): """Test the reproducibility of the Leontief inverse: L = (I - A)^{-1} """ client = getclient() for model in client.get_models(): model_id = model['id'] log.info('check the Leontief inverse L in model %s', model_id) A = client.get_matrix(model_id, "A") _, n = A.shape L = np.linalg.inv(np.eye(n) - A) self.compare_matrices(L, client.get_matrix(model_id, 'L'))
def test_upstream_impacts(self): """Test the reproducibility of the upstream impact matrix: U = D L""" client = getclient() for model in client.get_models(): model_id = model['id'] log.info('check the upstream impact matrix U in model %s', model_id) D = client.get_matrix(model_id, "D") L = client.get_matrix(model_id, "L") U = D @ L self.compare_matrices(U, client.get_matrix(model_id, 'U'))
def build_test_demand(self, model_id: str): """Creates a test demand for the given model. For each sector with index i, the demand vector d gets an entry of d[i] = i + 1.0.""" client = getclient() sectors = client.get_sectors(model_id) entries = [] for sector in sectors: entries.append({ 'sector': sector['id'], 'amount': sector['index'] + 1.0 }) return {'demand': entries}
def test_get_indicator(self): client = getclient() for model in client.get_models(): model_id = model['id'] indicators = client.get_indicators(model_id) i = random.randint(0, len(indicators) - 1) log.info('check indicator %i in model %s', i, model_id) indicator = indicators[i] # type: dict same = client.get_indicator(model_id, indicator['id']) self.assertEqual(same['id'], indicator['id']) self.assertEqual(same['index'], indicator['index']) self.assertEqual(same['name'], indicator['name']) self.assertEqual(same['code'], indicator['code'])
def test_get_demands(self): client = getclient() for model in client.get_models(): model_id = model['id'] demands = client.get_demands(model_id) self.assertTrue(len(demands) > 0) log.info('test %i demands in model %s', len(demands), model_id) for demand in demands: self.assertIsNotNone(demand.get('id')) self.assertIsNotNone(demand.get('year')) self.assertIsNotNone(demand.get('type')) self.assertIsNotNone(demand.get('system')) self.assertIsNotNone(demand.get('location'))
def test_get_sectors(self): client = getclient() for model in client.get_models(): model_id = model['id'] sectors = client.get_sectors(model_id) self.assertTrue(len(sectors) > 0) log.info('test %i sectors in model %s', len(sectors), model_id) for s in sectors: # type: dict self.assertIsNotNone(s.get('id')) self.assertIsNotNone(s.get('index')) self.assertIsNotNone(s.get('name')) self.assertIsNotNone(s.get('code')) self.assertIsNotNone(s.get('location'))
def test_get_flows(self): client = getclient() for model in client.get_models(): model_id = model['id'] flows = client.get_flows(model_id) self.assertTrue(len(flows) > 0) log.info('test %i flows in model %s', len(flows), model_id) for f in flows: # type: dict self.assertIsNotNone(f.get('id')) self.assertIsNotNone(f.get('index')) self.assertIsNotNone(f.get('flowable')) self.assertIsNotNone(f.get('context')) self.assertIsNotNone(f.get('unit')) self.assertIsNotNone(f.get('uuid'))
def test_get_indicators(self): client = getclient() for model in client.get_models(): model_id = model['id'] indicators = client.get_indicators(model_id) self.assertTrue(len(indicators) > 0) log.info('test %i indicators in model %s', len(indicators), model_id) for indicator in indicators: # type: dict self.assertIsNotNone(indicator.get('id')) self.assertIsNotNone(indicator.get('index')) self.assertIsNotNone(indicator.get('name')) self.assertIsNotNone(indicator.get('code')) self.assertIsNotNone(indicator.get('unit')) self.assertIsNotNone(indicator.get('group'))
def test_get_flow(self): client = getclient() for model in client.get_models(): model_id = model['id'] flows = client.get_flows(model_id) i = random.randint(0, len(flows) - 1) log.info('check flow %i in model %s', i, model_id) flow = flows[i] # type: dict same = client.get_flow(model_id, flow['id']) self.assertEqual(same['id'], flow['id']) self.assertEqual(same['index'], flow['index']) self.assertEqual(same['name'], flow['name']) self.assertEqual(same['category'], flow['category']) self.assertEqual(same['unit'], flow['unit']) self.assertEqual(same['uuid'], flow['uuid'])
def test_get_demand(self): """Test that each demand vector is a non-empty list of sector ID-amount-pairs with valid sector IDs.""" client = getclient() for model in client.get_models(): model_id = model['id'] sectors = client.get_sectors(model_id) sector_ids = set([s['id'] for s in sectors]) for info in client.get_demands(model_id): demand = client.get_demand(model_id, info['id']) log.debug('test demand %s in model %s', info['id'], model_id) self.assertTrue(len(demand) > 0) for entry in demand: sector_id = entry.get('sector') self.assertIsNotNone(sector_id) self.assertTrue(sector_id in sector_ids) self.assertTrue(isinstance( entry.get('amount'), (int, float)))
def test_final_perspective(self): """Test the calculation of a result of the final perspecitve.""" client = getclient() for model in client.get_models(): model_id = model['id'] log.info('test the final perspective calculation in model %s', model_id) # first, calculate the expected result matrix R U = client.get_matrix(model_id, "U") _, n = U.shape d = np.zeros(n) for j in range(0, n): d[j] = j + 1.0 R = U @ np.diag(d) # compare this with the result from the API demand = self.build_test_demand(model_id) demand['perspective'] = 'final' r = client.calculate(model_id, demand) self.compare_matrices(R, np.asarray(r['data'], dtype=np.float))
def test_direct_perspective(self): """Test the calculation of a result of the direct perspecitve.""" client = getclient() for model in client.get_models(): model_id = model['id'] log.info('test the direct perspective calculation in model %s', model_id) # first, calculate the expected result matrix R L = client.get_matrix(model_id, "L") _, n = L.shape s = np.zeros(n) for j in range(0, n): s += L[:, j] * (j + 1.0) D = client.get_matrix(model_id, "D") R = D @ np.diag(s) # compare this with the result from the API demand = self.build_test_demand(model_id) demand['perspective'] = 'direct' r = client.calculate(model_id, demand) self.compare_matrices(R, np.asarray(r['data'], dtype=np.float))
def test_matrices(self): """For each numeric matrix this test first fetches the complete matrix and a random row and column from the server and checks that the data of the matrix, row, and column data match.""" client = getclient() for model in client.get_models(): model_id = model['id'] for matrix in ['A', 'B', 'C', 'D', 'L', 'U']: log.info('check matrix %s in model %s', matrix, model_id) M = client.get_matrix(model_id, matrix) rows, cols = M.shape row = random.randint(0, rows - 1) log.info('check row %i in matrix %s/%s', row, matrix, model_id) r = client.get_matrix_row(model_id, matrix, row) for col in range(0, cols): self.assertAlmostEqual(M[row, col], r[col]) col = random.randint(0, cols - 1) log.info('check column %i in matrix %s/%s', col, matrix, model_id) c = client.get_matrix_column(model_id, matrix, col) for row in range(0, rows): self.assertAlmostEqual(M[row, col], c[row])
def test_total_results(self): """All perspectives should give the same total result.""" client = getclient() for model in client.get_models(): model_id = model['id'] log.info( 'test the total results in the perspectives calculation' ' of model %s', model_id) # first, calculate the expected total result t U = client.get_matrix(model_id, "U") m, n = U.shape t = np.zeros(m) for j in range(0, n): t += U[:, j] * (j + 1.0) # compare this with the result from the API demand = self.build_test_demand(model_id) for p in ['direct', 'intermediate', 'final']: demand['perspective'] = p r = client.calculate(model_id, demand) totals = r['totals'] for i in range(0, m): self.assertAlmostEqual(t[i], totals[i])