.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_basics/plot_05_import_export.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_basics_plot_05_import_export.py: Import and export: pandas, SPMF, networkx, Gephi, Cytoscape =========================================================== Event logs usually arrive as long tables (case, activity, timestamp). They become a :class:`~spmviz.SequenceDatabase`; results go to pandas tables, SPMF files, JSON, and every graph to networkx and on to GraphML, GEXF (Gephi, with positions and colors), Cytoscape JSON or igraph — node attributes included. .. GENERATED FROM PYTHON SOURCE LINES 10-20 .. code-block:: Python import tempfile from pathlib import Path import pandas as pd import spmviz as sv tmp = Path(tempfile.mkdtemp()) .. GENERATED FROM PYTHON SOURCE LINES 21-23 From an event table ------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-33 .. code-block:: Python log = pd.DataFrame({ "case": [1, 1, 1, 2, 2, 3, 3, 3], "activity": ["login", "search", "buy", "login", "search", "login", "search", "buy"], "time": pd.to_datetime(["2026-01-01 10:00", "2026-01-01 10:02", "2026-01-01 10:09", "2026-01-02 09:00", "2026-01-02 09:01", "2026-01-03 18:00", "2026-01-03 18:05", "2026-01-03 18:06"]), }) db = sv.SequenceDatabase.from_event_log(log, case="case", activity="activity", timestamp="time") ps = sv.mine(db, min_support=2) print(ps.to_frame()[["pattern", "support", "confidence"]]) .. rst-class:: sphx-glr-script-out .. code-block:: none pattern support confidence 0 buy 2 1.000000 1 login 3 1.000000 2 search 3 1.000000 3 login → buy 2 0.666667 4 login → search 3 1.000000 5 search → buy 2 0.666667 6 login → search → buy 2 0.666667 .. GENERATED FROM PYTHON SOURCE LINES 34-36 Files ----- .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python sv.io.write_spmf(db, tmp / "db.txt") # SPMF input format (text items coded) ps.write(tmp / "patterns.txt") # SPMF output: #SUP and #SID ps.write(tmp / "patterns.json") back = sv.io.read_patterns(tmp / "patterns.json", db) print((tmp / "patterns.txt").read_text()) .. rst-class:: sphx-glr-script-out .. code-block:: none @CONVERTED_FROM_TEXT @ITEM=1=buy @ITEM=2=login @ITEM=3=search 1 -1 #SUP: 2 #SID: 0 2 2 -1 #SUP: 3 #SID: 0 1 2 3 -1 #SUP: 3 #SID: 0 1 2 2 -1 1 -1 #SUP: 2 #SID: 0 2 2 -1 3 -1 #SUP: 3 #SID: 0 1 2 3 -1 1 -1 #SUP: 2 #SID: 0 2 2 -1 3 -1 1 -1 #SUP: 2 #SID: 0 2 .. GENERATED FROM PYTHON SOURCE LINES 42-44 Graphs ------ .. GENERATED FROM PYTHON SOURCE LINES 44-51 .. code-block:: Python G = ps.to_networkx("extension") # a networkx.DiGraph print(G.nodes["login → search"]) wn = sv.NBVFS("wn").fit(ps) for ext in ("gexf", "graphml", "cyjs", "json"): wn.write(tmp / f"network.{ext}") # with x, y, support, confidence, ... H = sv.io.read_graph(tmp / "network.graphml") print(sorted(H.nodes(data=True))[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none {'label': 'login → search', 'support': 3, 'rel_support': 1.0, 'length': 2, 'confidence': 1.0, 'cum_confidence': 1.0, 'start': 'login', 'added': 'search', 'index': 4} ('buy', {'label': 'buy', 'support': 2, 'rel_support': 0.6666666666666666, 'length': 1, 'confidence': 1.0, 'cum_confidence': 1.0, 'start': 'buy', 'added': 'buy', 'index': 0, 'x': -0.7071067811865475, 'y': -0.7071067811865476}) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.012 seconds) .. _sphx_glr_download_auto_examples_01_basics_plot_05_import_export.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/spmviz/main?urlpath=lab/tree/notebooks/auto_examples/01_basics/plot_05_import_export.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_import_export.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_import_export.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_import_export.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_