FenTechSolutions / CausalDiscoveryToolbox

Package for causal inference in graphs and in the pairwise settings. Tools for graph structure recovery and dependencies are included.
https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/index.html
MIT License
1.12k stars 198 forks source link

Example #7

Closed snowde closed 6 years ago

snowde commented 6 years ago

Hey man in trying your example, I have run into an error. I am not sure what to make of it, as it might be device related. I will include the message here and the file link.

https://github.com/snowde/firmai.github.io/blob/master/Discovery_LUCAS.ipynb


# So the question is, if you only have the data can you find the
# structure of the graph
from cdt.independence.graph import FSGNN

Fsgnn = FSGNN()

start_time = time.time()
ugraph = Fsgnn.predict(data, train_epochs=2000, test_epochs=1000, threshold=5e-4, l1=0.01)
print("--- Execution time : %4.4s seconds ---" % (time.time() - start_time))
nx.draw_networkx(ugraph, font_size=8) # The plot function allows for quick visualization of the graph.
plt.show()
# List results
pd.DataFrame(list(ugraph.edges(data='weight')))

`--------------------------------------------------------------------------- RemoteTraceback Traceback (most recent call last) RemoteTraceback: """ Traceback (most recent call last): File "/Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/_parallel_backends.py", line 350, in call return self.func(*args, kwargs) File "/Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py", line 131, in call return [func(*args, *kwargs) for func, args, kwargs in self.items] File "/Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py", line 131, in return [func(args, kwargs) for func, args, kwargs in self.items] File "/Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/model.py", line 43, in run_feature_selection return self.predict_features(df_features, df_target, **kwargs) File "/Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/FSGNN.py", line 79, in predict_features x = th.FloatTensor(scale(df_features.as_matrix())).to(device) AttributeError: 'torch.FloatTensor' object has no attribute 'to'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/dereksnow/anaconda/envs/py36/lib/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/_parallel_backends.py", line 359, in call raise TransportableException(text, e_type) joblib.my_exceptions.TransportableException: TransportableException


AttributeError Sat Jun 30 16:19:48 2018 PID: 92373 Python 3.6.3: /Users/dereksnow/anaconda/envs/py36/bin/python ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in call(self=) 126 def init(self, iterator_slice): 127 self.items = list(iterator_slice) 128 self._size = len(self.items) 129 130 def call(self): --> 131 return [func(*args, **kwargs) for func, args, kwargs in self.items] self.items = [(<bound method FeatureSelectionModel.runfeature...n of >, ( Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579

[500 rows x 11 columns], 'Allergy', 0), {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000})] 132 133 def len(self): 134 return self._size 135

........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in (.0=) 126 def init(self, iterator_slice): 127 self.items = list(iterator_slice) 128 self._size = len(self.items) 129 130 def call(self): --> 131 return [func(*args, **kwargs) for func, args, kwargs in self.items] func = <bound method FeatureSelectionModel.runfeature...n of > args = ( Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579

[500 rows x 11 columns], 'Allergy', 0) kwargs = {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000} 132 133 def len(self): 134 return self._size 135

........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/model.py in run_feature_selection(self=, df_data= Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579

[500 rows x 11 columns], target='Allergy', idx=0, kwargs={'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000}) 38 list_features = list(df_data.columns.values) 39 list_features.remove(target) 40 df_target = pd.DataFrame(df_data[target], columns=[target]) 41 df_features = df_data[list_features] 42 ---> 43 return self.predict_features(df_features, df_target, kwargs) self.predict_features = <bound method FSGNN.predict_features of > df_features = Anxiety Genetics Peer_Pressure Attentio...0.858699 -1.037579

[500 rows x 10 columns] df_target = Allergy 0 -0.266076 1 -0.579084 2 -0...8 -0.064685 499 -0.638704

[500 rows x 1 columns] kwargs = {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000} 44 45 def predict(self, df_data, threshold=0.05, **kwargs): 46 """Get the skeleton of the graph from raw data. 47

........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/FSGNN.py in predict_features(self=, df_features= Anxiety Genetics Peer_Pressure Attentio...0.858699 -1.037579

[500 rows x 10 columns], df_target= Allergy 0 -0.266076 1 -0.579084 2 -0...8 -0.064685 499 -0.638704

[500 rows x 1 columns], nh=20, idx=0, dropout=0.0, activation_function=<class 'torch.nn.modules.activation.ReLU'>, lr=0.01, l1=0.01, train_epochs=2000, test_epochs=1000, device='cpu', verbose=False, nb_runs=3) 74 activation_function=th.nn.ReLU, lr=0.01, l1=0.1, # batch_size=-1, 75 train_epochs=1000, test_epochs=1000, device=None, 76 verbose=None, nb_runs=3): 77 """For one variable, predict its neighbours.""" 78 device, verbose = SETTINGS.get_default(('device', device), ('verbose', verbose)) ---> 79 x = th.FloatTensor(scale(df_features.as_matrix())).to(device) x = undefined df_features.as_matrix.to = undefined device = 'cpu' 80 y = th.FloatTensor(scale(df_target.as_matrix())).to(device) 81 out = [] 82 for i in range(nb_runs): 83 model = FSGNN_model([x.size()[1], nh, 1],

AttributeError: 'torch.FloatTensor' object has no attribute 'to'


"""

The above exception was the direct cause of the following exception:

TransportableException Traceback (most recent call last) ~/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in retrieve(self) 698 if getattr(self._backend, 'supports_timeout', False): --> 699 self._output.extend(job.get(timeout=self.timeout)) 700 else:

~/anaconda/envs/py36/lib/python3.6/multiprocessing/pool.py in get(self, timeout) 643 else: --> 644 raise self._value 645

TransportableException: TransportableException


AttributeError Sat Jun 30 16:19:48 2018 PID: 92373 Python 3.6.3: /Users/dereksnow/anaconda/envs/py36/bin/python ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in call(self=) 126 def init(self, iterator_slice): 127 self.items = list(iterator_slice) 128 self._size = len(self.items) 129 130 def call(self): --> 131 return [func(*args, **kwargs) for func, args, kwargs in self.items] self.items = [(<bound method FeatureSelectionModel.runfeature...n of >, ( Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579

[500 rows x 11 columns], 'Allergy', 0), {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000})] 132 133 def len(self): 134 return self._size 135

........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in (.0=) 126 def init(self, iterator_slice): 127 self.items = list(iterator_slice) 128 self._size = len(self.items) 129 130 def call(self): --> 131 return [func(*args, **kwargs) for func, args, kwargs in self.items] func = <bound method FeatureSelectionModel.runfeature...n of > args = ( Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579

[500 rows x 11 columns], 'Allergy', 0) kwargs = {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000} 132 133 def len(self): 134 return self._size 135

........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/model.py in run_feature_selection(self=, df_data= Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579

[500 rows x 11 columns], target='Allergy', idx=0, kwargs={'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000}) 38 list_features = list(df_data.columns.values) 39 list_features.remove(target) 40 df_target = pd.DataFrame(df_data[target], columns=[target]) 41 df_features = df_data[list_features] 42 ---> 43 return self.predict_features(df_features, df_target, kwargs) self.predict_features = <bound method FSGNN.predict_features of > df_features = Anxiety Genetics Peer_Pressure Attentio...0.858699 -1.037579

[500 rows x 10 columns] df_target = Allergy 0 -0.266076 1 -0.579084 2 -0...8 -0.064685 499 -0.638704

[500 rows x 1 columns] kwargs = {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000} 44 45 def predict(self, df_data, threshold=0.05, **kwargs): 46 """Get the skeleton of the graph from raw data. 47

........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/FSGNN.py in predict_features(self=, df_features= Anxiety Genetics Peer_Pressure Attentio...0.858699 -1.037579

[500 rows x 10 columns], df_target= Allergy 0 -0.266076 1 -0.579084 2 -0...8 -0.064685 499 -0.638704

[500 rows x 1 columns], nh=20, idx=0, dropout=0.0, activation_function=<class 'torch.nn.modules.activation.ReLU'>, lr=0.01, l1=0.01, train_epochs=2000, test_epochs=1000, device='cpu', verbose=False, nb_runs=3) 74 activation_function=th.nn.ReLU, lr=0.01, l1=0.1, # batch_size=-1, 75 train_epochs=1000, test_epochs=1000, device=None, 76 verbose=None, nb_runs=3): 77 """For one variable, predict its neighbours.""" 78 device, verbose = SETTINGS.get_default(('device', device), ('verbose', verbose)) ---> 79 x = th.FloatTensor(scale(df_features.as_matrix())).to(device) x = undefined df_features.as_matrix.to = undefined device = 'cpu' 80 y = th.FloatTensor(scale(df_target.as_matrix())).to(device) 81 out = [] 82 for i in range(nb_runs): 83 model = FSGNN_model([x.size()[1], nh, 1],

AttributeError: 'torch.FloatTensor' object has no attribute 'to'


During handling of the above exception, another exception occurred:

JoblibAttributeError Traceback (most recent call last)

in () 6 7 start_time = time.time() ----> 8 ugraph = Fsgnn.predict(data, train_epochs=2000, test_epochs=1000, threshold=5e-4, l1=0.01) 9 print("--- Execution time : %4.4s seconds ---" % (time.time() - start_time)) 10 nx.draw_networkx(ugraph, font_size=8) # The plot function allows for quick visualization of the graph. ~/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/model.py in predict(self, df_data, threshold, **kwargs) 53 result_feature_selection = Parallel(n_jobs=nb_jobs)(delayed(self.run_feature_selection) 54 (df_data, node, idx, **kwargs) ---> 55 for idx, node in enumerate(list_nodes)) 56 else: 57 result_feature_selection = [self.run_feature_selection(df_data, node, idx, **kwargs) for idx, node in enumerate(list_nodes)] ~/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in __call__(self, iterable) 787 # consumption. 788 self._iterating = False --> 789 self.retrieve() 790 # Make sure that we get a last message telling us we are done 791 elapsed_time = time.time() - self._start_time ~/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in retrieve(self) 738 exception = exception_type(report) 739 --> 740 raise exception 741 742 def __call__(self, iterable): JoblibAttributeError: JoblibAttributeError ___________________________________________________________________________ Multiprocessing exception: ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/runpy.py in _run_module_as_main(mod_name='ipykernel.__main__', alter_argv=1) 188 sys.exit(msg) 189 main_globals = sys.modules["__main__"].__dict__ 190 if alter_argv: 191 sys.argv[0] = mod_spec.origin 192 return _run_code(code, main_globals, None, --> 193 "__main__", mod_spec) mod_spec = ModuleSpec(name='ipykernel.__main__', loader=<_f...b/python3.6/site-packages/ipykernel/__main__.py') 194 195 def run_module(mod_name, init_globals=None, 196 run_name=None, alter_sys=False): 197 """Execute a module's code without importing it ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/runpy.py in _run_code(code= at 0x10f262a50, file "/Use...3.6/site-packages/ipykernel/__main__.py", line 1>, run_globals={'__annotations__': {}, '__builtins__': , '__cached__': '/Users/dereksnow/anaconda/envs/py36/lib/python3....ges/ipykernel/__pycache__/__main__.cpython-36.pyc', '__doc__': None, '__file__': '/Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/__main__.py', '__loader__': <_frozen_importlib_external.SourceFileLoader object>, '__name__': '__main__', '__package__': 'ipykernel', '__spec__': ModuleSpec(name='ipykernel.__main__', loader=<_f...b/python3.6/site-packages/ipykernel/__main__.py'), 'app': }, init_globals=None, mod_name='__main__', mod_spec=ModuleSpec(name='ipykernel.__main__', loader=<_f...b/python3.6/site-packages/ipykernel/__main__.py'), pkg_name='ipykernel', script_name=None) 80 __cached__ = cached, 81 __doc__ = None, 82 __loader__ = loader, 83 __package__ = pkg_name, 84 __spec__ = mod_spec) ---> 85 exec(code, run_globals) code = at 0x10f262a50, file "/Use...3.6/site-packages/ipykernel/__main__.py", line 1> run_globals = {'__annotations__': {}, '__builtins__': , '__cached__': '/Users/dereksnow/anaconda/envs/py36/lib/python3....ges/ipykernel/__pycache__/__main__.cpython-36.pyc', '__doc__': None, '__file__': '/Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/__main__.py', '__loader__': <_frozen_importlib_external.SourceFileLoader object>, '__name__': '__main__', '__package__': 'ipykernel', '__spec__': ModuleSpec(name='ipykernel.__main__', loader=<_f...b/python3.6/site-packages/ipykernel/__main__.py'), 'app': } 86 return run_globals 87 88 def _run_module_code(code, init_globals=None, 89 mod_name=None, mod_spec=None, ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/__main__.py in () 1 if __name__ == '__main__': 2 from ipykernel import kernelapp as app ----> 3 app.launch_new_instance() ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/traitlets/config/application.py in launch_instance(cls=, argv=None, **kwargs={}) 653 654 If a global instance already exists, this reinitializes and starts it 655 """ 656 app = cls.instance(**kwargs) 657 app.initialize(argv) --> 658 app.start() app.start = > 659 660 #----------------------------------------------------------------------------- 661 # utility functions, for convenience 662 #----------------------------------------------------------------------------- ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/kernelapp.py in start(self=) 472 return self.subapp.start() 473 if self.poller is not None: 474 self.poller.start() 475 self.kernel.start() 476 try: --> 477 ioloop.IOLoop.instance().start() 478 except KeyboardInterrupt: 479 pass 480 481 launch_new_instance = IPKernelApp.launch_instance ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/tornado/ioloop.py in start(self=) 883 self._events.update(event_pairs) 884 while self._events: 885 fd, events = self._events.popitem() 886 try: 887 fd_obj, handler_func = self._handlers[fd] --> 888 handler_func(fd_obj, events) handler_func = .null_wrapper> fd_obj = events = 1 889 except (OSError, IOError) as e: 890 if errno_from_exception(e) == errno.EPIPE: 891 # Happens when the client closes the connection 892 pass ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/tornado/stack_context.py in null_wrapper(*args=(, 1), **kwargs={}) 272 # Fast path when there are no active contexts. 273 def null_wrapper(*args, **kwargs): 274 try: 275 current_state = _state.contexts 276 _state.contexts = cap_contexts[0] --> 277 return fn(*args, **kwargs) args = (, 1) kwargs = {} 278 finally: 279 _state.contexts = current_state 280 null_wrapper._wrapped = True 281 return null_wrapper ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py in _handle_events(self=, fd=, events=1) 445 return 446 zmq_events = self.socket.EVENTS 447 try: 448 # dispatch events: 449 if zmq_events & zmq.POLLIN and self.receiving(): --> 450 self._handle_recv() self._handle_recv = > 451 if not self.socket: 452 return 453 if zmq_events & zmq.POLLOUT and self.sending(): 454 self._handle_send() ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py in _handle_recv(self=) 475 else: 476 raise 477 else: 478 if self._recv_callback: 479 callback = self._recv_callback --> 480 self._run_callback(callback, msg) self._run_callback = > callback = .null_wrapper> msg = [, , , , , , ] 481 482 483 def _handle_send(self): 484 """Handle a send event.""" ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py in _run_callback(self=, callback=.null_wrapper>, *args=([, , , , , , ],), **kwargs={}) 427 close our socket.""" 428 try: 429 # Use a NullContext to ensure that all StackContexts are run 430 # inside our blanket exception handler rather than outside. 431 with stack_context.NullContext(): --> 432 callback(*args, **kwargs) callback = .null_wrapper> args = ([, , , , , , ],) kwargs = {} 433 except: 434 gen_log.error("Uncaught exception in ZMQStream callback", 435 exc_info=True) 436 # Re-raise the exception so that IOLoop.handle_callback_exception ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/tornado/stack_context.py in null_wrapper(*args=([, , , , , , ],), **kwargs={}) 272 # Fast path when there are no active contexts. 273 def null_wrapper(*args, **kwargs): 274 try: 275 current_state = _state.contexts 276 _state.contexts = cap_contexts[0] --> 277 return fn(*args, **kwargs) args = ([, , , , , , ],) kwargs = {} 278 finally: 279 _state.contexts = current_state 280 null_wrapper._wrapped = True 281 return null_wrapper ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/kernelbase.py in dispatcher(msg=[, , , , , , ]) 278 if self.control_stream: 279 self.control_stream.on_recv(self.dispatch_control, copy=False) 280 281 def make_dispatcher(stream): 282 def dispatcher(msg): --> 283 return self.dispatch_shell(stream, msg) msg = [, , , , , , ] 284 return dispatcher 285 286 for s in self.shell_streams: 287 s.on_recv(make_dispatcher(s), copy=False) ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/kernelbase.py in dispatch_shell(self=, stream=, msg={'buffers': [], 'content': {'allow_stdin': True, 'code': "# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))", 'silent': False, 'stop_on_error': True, 'store_history': True, 'user_expressions': {}}, 'header': {'date': datetime.datetime(2018, 6, 30, 4, 19, 48, 623932, tzinfo=tzutc()), 'msg_id': '3F664F4DF8994E9980115EFD76A70918', 'msg_type': 'execute_request', 'session': '1DCE294733F74AD0BBF17671DE4E5820', 'username': 'username', 'version': '5.2'}, 'metadata': {}, 'msg_id': '3F664F4DF8994E9980115EFD76A70918', 'msg_type': 'execute_request', 'parent_header': {}}) 230 self.log.warn("Unknown message type: %r", msg_type) 231 else: 232 self.log.debug("%s: %s", msg_type, msg) 233 self.pre_handler_hook() 234 try: --> 235 handler(stream, idents, msg) handler = > stream = idents = [b'1DCE294733F74AD0BBF17671DE4E5820'] msg = {'buffers': [], 'content': {'allow_stdin': True, 'code': "# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))", 'silent': False, 'stop_on_error': True, 'store_history': True, 'user_expressions': {}}, 'header': {'date': datetime.datetime(2018, 6, 30, 4, 19, 48, 623932, tzinfo=tzutc()), 'msg_id': '3F664F4DF8994E9980115EFD76A70918', 'msg_type': 'execute_request', 'session': '1DCE294733F74AD0BBF17671DE4E5820', 'username': 'username', 'version': '5.2'}, 'metadata': {}, 'msg_id': '3F664F4DF8994E9980115EFD76A70918', 'msg_type': 'execute_request', 'parent_header': {}} 236 except Exception: 237 self.log.error("Exception in message handler:", exc_info=True) 238 finally: 239 self.post_handler_hook() ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/kernelbase.py in execute_request(self=, stream=, ident=[b'1DCE294733F74AD0BBF17671DE4E5820'], parent={'buffers': [], 'content': {'allow_stdin': True, 'code': "# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))", 'silent': False, 'stop_on_error': True, 'store_history': True, 'user_expressions': {}}, 'header': {'date': datetime.datetime(2018, 6, 30, 4, 19, 48, 623932, tzinfo=tzutc()), 'msg_id': '3F664F4DF8994E9980115EFD76A70918', 'msg_type': 'execute_request', 'session': '1DCE294733F74AD0BBF17671DE4E5820', 'username': 'username', 'version': '5.2'}, 'metadata': {}, 'msg_id': '3F664F4DF8994E9980115EFD76A70918', 'msg_type': 'execute_request', 'parent_header': {}}) 394 if not silent: 395 self.execution_count += 1 396 self._publish_execute_input(code, parent, self.execution_count) 397 398 reply_content = self.do_execute(code, silent, store_history, --> 399 user_expressions, allow_stdin) user_expressions = {} allow_stdin = True 400 401 # Flush output before sending the reply. 402 sys.stdout.flush() 403 sys.stderr.flush() ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/ipkernel.py in do_execute(self=, code="# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))", silent=False, store_history=True, user_expressions={}, allow_stdin=True) 191 192 self._forward_input(allow_stdin) 193 194 reply_content = {} 195 try: --> 196 res = shell.run_cell(code, store_history=store_history, silent=silent) res = undefined shell.run_cell = > code = "# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))" store_history = True silent = False 197 finally: 198 self._restore_input() 199 200 if res.error_before_exec is not None: ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/ipykernel/zmqshell.py in run_cell(self=, *args=("# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))",), **kwargs={'silent': False, 'store_history': True}) 528 ) 529 self.payload_manager.write_payload(payload) 530 531 def run_cell(self, *args, **kwargs): 532 self._last_traceback = None --> 533 return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) self.run_cell = > args = ("# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))",) kwargs = {'silent': False, 'store_history': True} 534 535 def _showtraceback(self, etype, evalue, stb): 536 # try to preserve ordering of tracebacks and print statements 537 sys.stdout.flush() ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_cell(self=, raw_cell="# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))", store_history=True, silent=False, shell_futures=True) 2723 self.displayhook.exec_result = result 2724 2725 # Execute the user code 2726 interactivity = "none" if silent else self.ast_node_interactivity 2727 has_raised = self.run_ast_nodes(code_ast.body, cell_name, -> 2728 interactivity=interactivity, compiler=compiler, result=result) interactivity = 'last_expr' compiler = 2729 2730 self.last_execution_succeeded = not has_raised 2731 self.last_execution_result = result 2732 ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_ast_nodes(self=, nodelist=[<_ast.ImportFrom object>, <_ast.Assign object>, <_ast.Assign object>, <_ast.Assign object>, <_ast.Expr object>, <_ast.Expr object>, <_ast.Expr object>, <_ast.Expr object>], cell_name='', interactivity='last', compiler=, result=) 2845 2846 try: 2847 for i, node in enumerate(to_run_exec): 2848 mod = ast.Module([node]) 2849 code = compiler(mod, cell_name, "exec") -> 2850 if self.run_code(code, result): self.run_code = > code = at 0x1a1f3f18a0, file "", line 8> result = 2851 return True 2852 2853 for i, node in enumerate(to_run_interactive): 2854 mod = ast.Interactive([node]) ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_code(self=, code_obj= at 0x1a1f3f18a0, file "", line 8>, result=) 2905 outflag = True # happens in more places, so it's easier as default 2906 try: 2907 try: 2908 self.hooks.pre_run_code_hook() 2909 #rprint('Running code', repr(code_obj)) # dbg -> 2910 exec(code_obj, self.user_global_ns, self.user_ns) code_obj = at 0x1a1f3f18a0, file "", line 8> self.user_global_ns = {'FSGNN': , 'Fsgnn': , 'In': ['', '#Import libraries\nimport cdt\nfrom cdt import SET...pandas as pd\nfrom matplotlib import pyplot as plt', '#Import libraries\nimport cdt\nfrom cdt import SET...pandas as pd\nfrom matplotlib import pyplot as plt', '# Load data and graph solution\ndata = pd.read_cs...sualization of the graph. \nplt.show()\ndata.head()', 'solution', "# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))"], 'Out': {3: Allergy Anxiety Genetics Peer_Pressure ... -0.733240 -0.149308 0.854195 -0.633940 , 4: }, 'SETTINGS': , '_': , '_3': Allergy Anxiety Genetics Peer_Pressure ... -0.733240 -0.149308 0.854195 -0.633940 , '_4': , '__': Allergy Anxiety Genetics Peer_Pressure ... -0.733240 -0.149308 0.854195 -0.633940 , '___': '', ...} self.user_ns = {'FSGNN': , 'Fsgnn': , 'In': ['', '#Import libraries\nimport cdt\nfrom cdt import SET...pandas as pd\nfrom matplotlib import pyplot as plt', '#Import libraries\nimport cdt\nfrom cdt import SET...pandas as pd\nfrom matplotlib import pyplot as plt', '# Load data and graph solution\ndata = pd.read_cs...sualization of the graph. \nplt.show()\ndata.head()', 'solution', "# So the question is, if you only have the data ...s\npd.DataFrame(list(ugraph.edges(data='weight')))"], 'Out': {3: Allergy Anxiety Genetics Peer_Pressure ... -0.733240 -0.149308 0.854195 -0.633940 , 4: }, 'SETTINGS': , '_': , '_3': Allergy Anxiety Genetics Peer_Pressure ... -0.733240 -0.149308 0.854195 -0.633940 , '_4': , '__': Allergy Anxiety Genetics Peer_Pressure ... -0.733240 -0.149308 0.854195 -0.633940 , '___': '', ...} 2911 finally: 2912 # Reset our crash handler in place 2913 sys.excepthook = old_excepthook 2914 except SystemExit as e: ........................................................................... /Volumes/extra/FirmAI/Causal Inference/CausalDiscoveryToolbox-master/examples/ in () 3 from cdt.independence.graph import FSGNN 4 5 Fsgnn = FSGNN() 6 7 start_time = time.time() ----> 8 ugraph = Fsgnn.predict(data, train_epochs=2000, test_epochs=1000, threshold=5e-4, l1=0.01) 9 print("--- Execution time : %4.4s seconds ---" % (time.time() - start_time)) 10 nx.draw_networkx(ugraph, font_size=8) # The plot function allows for quick visualization of the graph. 11 plt.show() 12 # List results ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/model.py in predict(self=, df_data= Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579 [500 rows x 11 columns], threshold=0.0005, **kwargs={'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000}) 50 nb_jobs = kwargs.get("nb_jobs", SETTINGS.NB_JOBS) 51 list_nodes = list(df_data.columns.values) 52 if nb_jobs != 1: 53 result_feature_selection = Parallel(n_jobs=nb_jobs)(delayed(self.run_feature_selection) 54 (df_data, node, idx, **kwargs) ---> 55 for idx, node in enumerate(list_nodes)) idx = undefined node = undefined list_nodes = ['Allergy', 'Anxiety', 'Genetics', 'Peer_Pressure', 'Attention_Disorder', 'Smoking', 'Lung_Cancer', 'Yellow_Fingers', 'Coughing', 'Fatigue', 'Car_Accident'] 56 else: 57 result_feature_selection = [self.run_feature_selection(df_data, node, idx, **kwargs) for idx, node in enumerate(list_nodes)] 58 for idx, i in enumerate(result_feature_selection): 59 try: ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in __call__(self=Parallel(n_jobs=4), iterable=.>) 784 if pre_dispatch == "all" or n_jobs == 1: 785 # The iterable was consumed all at once by the above for loop. 786 # No need to wait for async callbacks to trigger to 787 # consumption. 788 self._iterating = False --> 789 self.retrieve() self.retrieve = 790 # Make sure that we get a last message telling us we are done 791 elapsed_time = time.time() - self._start_time 792 self._print('Done %3i out of %3i | elapsed: %s finished', 793 (len(self._output), len(self._output), --------------------------------------------------------------------------- Sub-process traceback: --------------------------------------------------------------------------- AttributeError Sat Jun 30 16:19:48 2018 PID: 92373 Python 3.6.3: /Users/dereksnow/anaconda/envs/py36/bin/python ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in __call__(self=) 126 def __init__(self, iterator_slice): 127 self.items = list(iterator_slice) 128 self._size = len(self.items) 129 130 def __call__(self): --> 131 return [func(*args, **kwargs) for func, args, kwargs in self.items] self.items = [(>, ( Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579 [500 rows x 11 columns], 'Allergy', 0), {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000})] 132 133 def __len__(self): 134 return self._size 135 ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/joblib/parallel.py in (.0=) 126 def __init__(self, iterator_slice): 127 self.items = list(iterator_slice) 128 self._size = len(self.items) 129 130 def __call__(self): --> 131 return [func(*args, **kwargs) for func, args, kwargs in self.items] func = > args = ( Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579 [500 rows x 11 columns], 'Allergy', 0) kwargs = {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000} 132 133 def __len__(self): 134 return self._size 135 ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/model.py in run_feature_selection(self=, df_data= Allergy Anxiety Genetics Peer_Pressure...0.858699 -1.037579 [500 rows x 11 columns], target='Allergy', idx=0, **kwargs={'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000}) 38 list_features = list(df_data.columns.values) 39 list_features.remove(target) 40 df_target = pd.DataFrame(df_data[target], columns=[target]) 41 df_features = df_data[list_features] 42 ---> 43 return self.predict_features(df_features, df_target, **kwargs) self.predict_features = > df_features = Anxiety Genetics Peer_Pressure Attentio...0.858699 -1.037579 [500 rows x 10 columns] df_target = Allergy 0 -0.266076 1 -0.579084 2 -0...8 -0.064685 499 -0.638704 [500 rows x 1 columns] kwargs = {'l1': 0.01, 'test_epochs': 1000, 'train_epochs': 2000} 44 45 def predict(self, df_data, threshold=0.05, **kwargs): 46 """Get the skeleton of the graph from raw data. 47 ........................................................................... /Users/dereksnow/anaconda/envs/py36/lib/python3.6/site-packages/cdt/independence/graph/FSGNN.py in predict_features(self=, df_features= Anxiety Genetics Peer_Pressure Attentio...0.858699 -1.037579 [500 rows x 10 columns], df_target= Allergy 0 -0.266076 1 -0.579084 2 -0...8 -0.064685 499 -0.638704 [500 rows x 1 columns], nh=20, idx=0, dropout=0.0, activation_function=, lr=0.01, l1=0.01, train_epochs=2000, test_epochs=1000, device='cpu', verbose=False, nb_runs=3) 74 activation_function=th.nn.ReLU, lr=0.01, l1=0.1, # batch_size=-1, 75 train_epochs=1000, test_epochs=1000, device=None, 76 verbose=None, nb_runs=3): 77 """For one variable, predict its neighbours.""" 78 device, verbose = SETTINGS.get_default(('device', device), ('verbose', verbose)) ---> 79 x = th.FloatTensor(scale(df_features.as_matrix())).to(device) x = undefined df_features.as_matrix.to = undefined device = 'cpu' 80 y = th.FloatTensor(scale(df_target.as_matrix())).to(device) 81 out = [] 82 for i in range(nb_runs): 83 model = FSGNN_model([x.size()[1], nh, 1], AttributeError: 'torch.FloatTensor' object has no attribute 'to' ___________________________________________________________________________`
diviyank commented 6 years ago

Hi, It seems like your pytorch version is not 0.4. Could you update it and try again? Best, Diviyan

snowde commented 6 years ago

Awesome, that sorted the first one out! On the next tab, I have this issue.

Thanks for taking the time.

# Orient the edges of the graph
from cdt.causality.graph import CGNN
Cgnn = CGNN()
start_time = time.time()
dgraph = Cgnn.predict(data, graph=ugraph, nb_runs=12, nb_max_runs=20, train_epochs=1500, test_epochs=1000)
print("--- Execution time : %4.4s seconds ---" % (time.time() - start_time))

# Plot the output graph
nx.draw_networkx(dgraph, font_size=8) # The plot function allows for quick visualization of the graph.
plt.show()
# Print output results : 
pd.DataFrame(list(dgraph.edges(data='weight')), columns=['Cause', 'Effect', 'Score'])

The pairwise GNN model is computed on each edge of the UMG to initialize the model and start CGNN with a DAG

TypeError Traceback (most recent call last)

in () 3 Cgnn = CGNN() 4 start_time = time.time() ----> 5 dgraph = Cgnn.predict(data, graph=ugraph, nb_runs=12, nb_max_runs=20, train_epochs=1500, test_epochs=1000) 6 print("--- Execution time : %4.4s seconds ---" % (time.time() - start_time)) 7 ~/anaconda/envs/py36/lib/python3.6/site-packages/cdt/causality/graph/model.py in predict(self, df_data, graph, **kwargs) 31 return self.orient_directed_graph(df_data, graph, **kwargs) 32 elif type(graph) == nx.Graph: ---> 33 return self.orient_undirected_graph(df_data, graph, **kwargs) 34 else: 35 print('Unknown Graph type') ~/anaconda/envs/py36/lib/python3.6/site-packages/cdt/causality/graph/CGNN.py in orient_undirected_graph(self, data, umg, nh, nb_runs, nb_jobs, gpu, lr, train_epochs, test_epochs, verbose, nb_max_runs) 257 og = gnn.orient_graph(data, umg, nb_runs=nb_runs, nb_max_runs=nb_max_runs, 258 nb_jobs=nb_jobs, train_epochs=train_epochs, --> 259 test_epochs=test_epochs, verbose=verbose, gpu=gpu) # Pairwise method 260 # print(nx.adj_matrix(og).todense().shape) 261 ~/anaconda/envs/py36/lib/python3.6/site-packages/cdt/causality/pairwise/model.py in orient_graph(self, df_data, graph, printout, nb_runs, **kwargs) 91 92 elif type(graph) == nx.Graph: ---> 93 edges = list(graph.edges) 94 output = nx.DiGraph() 95 TypeError: 'method' object is not iterable
diviyank commented 6 years ago

Oh yes, I had this bug corrected, could you update your toolbox to master?
Best, Diviyan

snowde commented 6 years ago

Sorry I got one problem after the update. - Running the same code.

The pairwise GNN model is computed on each edge of the UMG to initialize the model and start CGNN with a DAG

AttributeError Traceback (most recent call last)

in () 3 Cgnn = CGNN() 4 start_time = time.time() ----> 5 dgraph = Cgnn.predict(data, graph=ugraph, nb_runs=5, nb_max_runs=6, train_epochs=15, test_epochs=8) 6 print("--- Execution time : %4.4s seconds ---" % (time.time() - start_time)) 7 /Volumes/extra/FirmAI/Causal Inference/CausalDiscoveryToolbox-master/examples/cdt/causality/graph/model.py in predict(self, df_data, graph, **kwargs) 31 return self.orient_directed_graph(df_data, graph, **kwargs) 32 elif type(graph) == nx.Graph: ---> 33 return self.orient_undirected_graph(df_data, graph, **kwargs) 34 else: 35 print('Unknown Graph type') /Volumes/extra/FirmAI/Causal Inference/CausalDiscoveryToolbox-master/examples/cdt/causality/graph/CGNN.py in orient_undirected_graph(self, data, umg, nh, nb_runs, nb_jobs, gpu, lr, train_epochs, test_epochs, verbose, nb_max_runs) 269 # print(nx.adj_matrix(og).todense().shape) 270 # print(list(og.edges())) --> 271 dag = dagify_min_edge(og) 272 # print(nx.adj_matrix(dag).todense().shape) 273 /Volumes/extra/FirmAI/Causal Inference/CausalDiscoveryToolbox-master/examples/cdt/utils/graph_utils.py in dagify_min_edge(g) 14 """ 15 while not nx.is_directed_acyclic_graph(g): ---> 16 cycle = nx.simple_cycles(g).next() 17 scores = [] 18 edges = [] AttributeError: 'generator' object has no attribute 'next'
snowde commented 6 years ago

It might be a python 3 thing, I will jsut change it locally. Thank, one last question - more personal - would you recommend any packages for automated causal effects from observational data after I have done the casual discovery? I have had a look at the following, https://github.com/laurencium/Causalinference, and https://github.com/akelleh/causality. If you are unsure then please just ignore the question, thanks so much for your help :)

diviyank commented 6 years ago

Hi again, glad that some errors are sorted out. Please keep me updated about the generator error. Concerning causal inference, I've got no great experience in python packages. You could look into the "IDA" algorithm (implemented in the pcalg R package).

We plan to bring tools for causal effect evaluation in this toolbox, but it will be at a later date =) Best, Diviyan

diviyank commented 6 years ago

I'll be closing this issue, don't hesitate to open it if a bug pops again. Best, Diviyan