From 66f074e25961ccfb02b11d4c19b3a024222737a2 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 28 Sep 2021 21:05:50 -0400 Subject: [PATCH 1/5] Add `-O3 -ffast-math` optimization to CMakeLists.txt --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e0d4006b2..0abe47dfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") set(Boost_NO_BOOST_CMAKE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden") + "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -ffast-math -O3") find_package(Graphviz) From 31cdb86c6f2bd72a9cc94addf223c61441dc4b87 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 28 Sep 2021 21:06:38 -0400 Subject: [PATCH 2/5] Inline `sqr` --- lib/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.cpp b/lib/utils.cpp index 3aeaa7ade..72cf747e1 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -12,7 +12,7 @@ namespace delphi::utils { /** * Returns the square of a number. */ -double sqr(double x) { return x * x; } +// double sqr(double x) { return x * x; } /** * Returns the sum of a vector of doubles. From d7e37a99dc723d6a8186d99f1a80284194acb799 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 28 Sep 2021 21:07:06 -0400 Subject: [PATCH 3/5] Inline `sqr` --- lib/utils.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.hpp b/lib/utils.hpp index bd7474f6b..e9be71eb2 100644 --- a/lib/utils.hpp +++ b/lib/utils.hpp @@ -47,7 +47,7 @@ template vector lmap(F f, vector vec) { /** * Returns the square of a number. */ -double sqr(double x); +inline double sqr(double x) {return x * x}; /** * Returns the sum of a vector of doubles. From 45b91a31f28639eac4b27ec3baaf35305c2d078b Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 28 Sep 2021 21:07:56 -0400 Subject: [PATCH 4/5] Create test.py --- test.py | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 000000000..a7bdcb3d4 --- /dev/null +++ b/test.py @@ -0,0 +1,152 @@ +#!/usr/bin/env python +""" + test.py + + Derived from: https://github.com/ml4ai/delphi/blob/c07eeae9394ab30ca8d984b2ec2e40ab4c2d2e08/scripts/debugger.py +""" + +import os +os.environ['DELPHI_DB'] = './data/delphi.db' + +import delphi.plotter as dp +from delphi.cpp.DelphiPython import AnalysisGraph, InitialBeta, InitialDerivative +import pandas as pd +import numpy as np + + +def set_indicator(G, concept, indicator_new, source): + G.delete_all_indicators(concept) + G.set_indicator(concept, indicator_new, source) + +def remove_node(G, concept, indicator_new, source): + G.delete_all_indicators(concept) + G.remove_node(concept) + + +def curate_indicators(G): + ''' + set_indicator( + G, + "wm/concept/indicator_and_reported_property/weather/rainfall", + "Average Precipitation", + "DSSAT", + ) + ''' + + set_indicator( + G, + "wm/concept/indicator_and_reported_property/agriculture/Crop_Production", + "Average Harvested Weight at Maturity (Maize)", + "DSSAT", + ) + + set_indicator( + G, + "wm/concept/causal_factor/condition/food_insecurity", + "IPC Phase Classification", + "FEWSNET", + ) + + ''' + set_indicator( + G, + "wm/concept/causal_factor/economic_and_commerce/economic_activity/market/price_or_cost/food_price", + "Consumer price index", + "WDI", + ) + + set_indicator( + G, + "wm/concept/indicator_and_reported_property/conflict/population_displacement", + "Internally displaced persons, total displaced by conflict and violence", + "WDI", + ) + + set_indicator( + G, + "wm/concept/causal_factor/condition/tension", + "Conflict incidences", + "None", + ) + #G.remove_node("wm/concept/indicator_and_reported_property/agriculture/Crop_Production") + G.remove_node("wm/concept/indicator_and_reported_property/weather/rainfall") + G.remove_node("wm/concept/indicator_and_reported_property/conflict/population_displacement") + G.remove_node("wm/concept/causal_factor/condition/tension") + ''' + +def create_base_CAG(causemos_create_model, + belief_score_cutoff=0, + grounding_score_cutoff=0, + kde_kernels=4): + if causemos_create_model: + G = AnalysisGraph.from_causemos_json_file(causemos_create_model, + belief_score_cutoff, + grounding_score_cutoff, + kde_kernels) + else: + statements = [ + ( + ("large", 1, "wm/concept/indicator_and_reported_property/agriculture/Crop_Production"), + ("small", -1, "wm/concept/causal_factor/condition/food_insecurity"), + ) + ] + G = AnalysisGraph.from_causal_fragments(statements) + G.map_concepts_to_indicators() + curate_indicators(G) + return G + + +def draw_CAG(G, file_name): + G.to_png( + file_name, + rankdir="TB", + simplified_labels=False, + ) + + + +json_inputs = [ + ["./tests/data/delphi/create_model_test.json", # 0. Missing data and mixed sampling frequency + "./tests/data/delphi/experiments_projection_test.json"], + ["./tests/data/delphi/create_model_ideal.json", # 1. Ideal data with gaps of 1 + "./tests/data/delphi/experiments_projection_ideal.json"], + ["./tests/data/delphi/create_model_input_2.json", # 2. Usual Data + "./tests/data/delphi/experiments_projection_input_2.json"], + ["./tests/data/delphi/create_model_ideal_10.json", # 3. Ideal data with gaps of 10 + "./tests/data/delphi/experiments_projection_ideal_2.json"], + ["./tests/data/delphi/create_model_ideal_3.json", # 4. Ideal data with real epochs + "./tests/data/delphi/experiments_projection_ideal_3.json"], + ["./tests/data/delphi/create_model_input_2_no_data.json", # 5. No data + "./tests/data/delphi/experiments_projection_input_2.json"], + ["./tests/data/delphi/create_model_input_2_partial_data.json", # 6. Partial data + "./tests/data/delphi/experiments_projection_input_2.json"], + ["./tests/data/delphi/create_model_input_new.json", # 7. Updated create model format + ""], + ["./tests/data/delphi/causemos_create.json", # 8. Oldest test data + "./tests/data/delphi/causemos_experiments_projection_input.json"], + ["./tests/data/delphi/create_model_rain--temperature--yield.json", # 9. rain-temperature CAG + "./tests/data/delphi/experiments_rain--temperature--yield.json"], + ["./tests/data/delphi/create_model_rain--temperature.json", # 10. rain-temperature CAG + "./tests/data/delphi/experiments_rain--temperature--yield.json"], +] + +input_idx = 9 +causemos_create_model = json_inputs[input_idx][0] +causemos_create_experiment = json_inputs[input_idx][1] + +G = AnalysisGraph.from_causemos_json_file( + causemos_create_model, + belief_score_cutoff=0, + grounding_score_cutoff=0, + kde_kernels=10 +) + +G.set_random_seed(81) + +G.run_train_model( + res = 200, + burn = 1000, + initial_beta = InitialBeta.ZERO, + initial_derivative = InitialDerivative.DERI_ZERO, + use_continuous = True +) From 14972e783551029ddf7db83961b73cf99c4c48e9 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 28 Sep 2021 21:08:16 -0400 Subject: [PATCH 5/5] Create build_and_run.sh --- build_and_run.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 build_and_run.sh diff --git a/build_and_run.sh b/build_and_run.sh new file mode 100644 index 000000000..680f3c477 --- /dev/null +++ b/build_and_run.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# build_and_run.sh + +rm -rf build +rm -rf delphi/cpp/* + +mkdir -p build +cd build +cmake .. +cmake --build . -- -j12 DelphiPython +cp *.so ../delphi/cpp +cd .. + +time python test.py