In version 7 of the Physics Derivation Graph I realized that I could use Python's Pickle format to serialize the data stored in memory without having to decide what storage format (CSV, XML, SQLite) is best. That insight lead to use of JSON because everything needed fits in dictionaries and lists.
The use of Pickle and then then JSON enabled development of many features, so it was a worthwhile investment. However, some operations are not well suited to the nested dictionaries and lists. A set of tables might be better for some operations. Converting from the current dictionaries and lists to tables would be a big rewrite, so I haven't started that yet.
If I move away from JSON for storage, the current candidates are Redis and PostgreSQL and SQLite3.
Use of a relational database would require a significant rewrite since most of the functions in the PDG rely on the nested dictionaries.
As a potentially easier transition, Redis has a plugin that supports JSON:
https://redislabs.com/blog/redis-as-a-json-store/
https://redislabs.com/redis-best-practices/data-storage-patterns/json-storage/
However, I'm not comfortable with the PDG being dependent on a plugin.
If I go with a relational database, I'll need to choose which one.
MySQL or PostgreSQL versus SQLite
https://stackoverflow.com/a/5102105/1164295
"SQLite can support multiple users at once. It does however lock the whole database when writing, so if you have lots of concurrent writes it is not the database you want (usually the time the database is locked is a few milliseconds - so for most uses this does not matter)."
https://www.sqlite.org/whentouse.html
"Any site that gets fewer than 100K hits/day should work fine with SQLite."
To improve concurrency, reads can happen without blocking writes: https://www.sqlite.org/wal.html
Monday, April 6, 2020
Saturday, April 4, 2020
why implementing a single feature took 12 hours
Yesterday I started investigating how to get d3.js working for the Physics Derivation Graph. I already had an implementation working on the live website, so I didn't expect the update to take too much effort or time.
Below is the sequence of challenges I encountered for this feature update.
Below is the sequence of challenges I encountered for this feature update.
- I learned that I had used v3; the current version is v5
- v5 doesn't support the .force() used in v3
- I found a v5-based force directed graph on https://observablehq.com/@d3/force-directed-graph
- Although I was able to get the code running locally, I found the files seemed to depend on remote resources.
- I found a better instance that was pure d3.v5.js instead of relying on observable code: https://bl.ocks.org/mapio/53fed7d84cd1812d6a6639ed7aa83868
- Figured out how to get images associated with nodes
- The JSON file needs images to have distinct and consistent names
- Instead of temporary image file names, use expr_global_id and expr_name
- The functions using "return False, error_message" meant the errors didn't propagate to the web interface. The "right" method is to use "raise Exception"
- With exceptions raised in compute, needed to add "try/except" in controller.py
- With Exceptions caught in controller.py, use flash() to tell the user there was a problem
- With Exceptions now sent to user via web interface, I learned that the PNG wasn't being created due to a missing command, "braket"
- I found that "braket" is a latex package available from CTAN
- I tried to install "braket" using "tlmgr install"; see https://tex.stackexchange.com/questions/73016/how-do-i-install-an-individual-package-on-a-linux-system
- I wasn't able to run "tlmgr" in Docker due to not having wget
- I wasn't able to install wget in Docker using "apt-get install -y wget", possible due to using phusion as a base image?
- Looked up instructions on installing packages manually; opened https://github.com/allofphysicsgraph/proofofconcept/issues/82
- In the process of debugging the PDF generation (notice that I strayed from the d3js effort), realized the migration of inference rules was incomplete -- new style is to have words separated by spaces in create_tmp_db.py
- Added an exception in compute.py to identify inconsistent inference rule names
- Manually fixed inference rule entries in create_tmp_db.py
- Altered the inference rule schema in compute.py -- use feeds+inputs+outputs
- Manually updated inference rules in create_tmp_db.py to reflect revised schema
- Compiling derivation PDF failed due to incorrect implementation of inference rule
- Realized that the "braket" issue wasn't a missing package, it was custom macros defined in an old version of the PDG
- Wrote function to generate JSON needed for d3js
- In the process of iterating that, added page latency measurement
Lessons learned:
- In the process of implementing a new feature or updating a feature, I uncovered a few bugs and a lot of technical debt that lead to the implementation taking longer than expected
- Some of the bugs were easy to fix (aka buy down the tech debt) as I discovered them, while others were sufficiently worthy of a new ticket.
- Some bugs were blockers -- I couldn't proceed with the desired work until I resolved architecture flaws; other issues were tangential and could be delayed.
Friday, March 20, 2020
data structure continues to evolve
I've experimented with seven different data structures for the Physics Derivation Graph:
- v1_plain_text
- v2_XML
- v3_CSV
- v4_file_per_expression
- v5_property_graph
- v6_sqlite
- v7_pickle_web_interface
Each of these have required a rewrite of the code from scratch, as well as transfer code (to move from n to n+1).
These changes progress concurrently with my knowledge of data structures. I didn't know about property graphs when I was implementing v1, v2, and v3. I wasn't comfortable with SQL when I implemented v4. I didn't know about Tidy data when I implemented v1 to v6. The data structures used in the PDG slightly lag my understanding of data structures.
Within a given implementation, there are design decisions with trade-offs to evaluate. I typically don't know all the options or consequences until I implement one of them and then determine what inefficiencies exist. Knowledge gained through evolutionary iteration is expensive and takes a lot of time.
Here's an example of two "minor" tweaks that incur a rewrite of all the code. My current data structure in v7 is
dat['derivations'] = {
'fun deriv': { # name of derivation
'4928482': { # key is "step ID"
'inf rule': 'declare initial expr',
'inputs': {},
'feeds': {},
'outputs': {'9428': '4928923942'}, # key is "expr local ID", value is "expr global ID"
'linear index': 1}, # linear index for PDF and for graph orientation
'2948592': {
'inf rule': 'add X to both sides',
'inputs': {'9428': '4928923942'},
'feeds': {'3190': '9494829190'},
'outputs': {'3921': '9499959299'},
'linear index': 2},
A better data structure would be
dat['derivations'] = {
'fun deriv': { # name of derivation
'4928482': { # key is "step ID"
'inf rule': 'declare initial expr',
'inputs': {},
'feeds': {},
'outputs': {1: '9428'}, # key is index, value is "expr local ID"
'linear index': 1}, # linear index for PDF and for graph orientation
'2948592': {
'inf rule': 'add X to both sides',
'inputs': {1: '9428'},
'feeds': {1: '3190'},
'outputs': {1: '3921'},
'linear index': 2},
dat['expr local to global'] = {
'9428': '4928923942',
'3190': '9494829190',
'3921': '9499959299',
'9128': '1492842000'}
The reasons this second data structure is an improvement is
- the global expression ID does not appear in the 'derivations' dict
- the inputs, feeds, and outputs have an index. The index is relevant for both printing in a PDF and use in inference rules.
I'm slowly evolving towards the likelihood that there will be a "v8" based on tables. The backend database would be something like SQLite3, and the internal representation in Python would be dataframes.
I'm not going to switch to v8 yet; I'll continue to invest effort in v7 for a bit longer to explore a few challenges (like implementation of inference rules).
Friday, March 13, 2020
notes from reading "A Step-by-Step Solution Methodology for Mathematical Expressions"
A Step-by-Step Solution Methodology for Mathematical Expressions
by Sahereh Hosseinpour, Mir Mohammad Reza Alavi Milani and Hüseyin Pehlivan
Symmetry 2018, 10(7), 285; https://doi.org/10.3390/sym10070285
https://www.mdpi.com/2073-8994/10/7/285/htm
The following sites parse input, take a specified action, and show the steps
(I'm not sure whether these two sites are related or clones of each other)
https://www.softmath.com/math-com-calculator/quadratic-equations/myalgebra.com.html
https://www.mathsite.org/maths-factors/perpendicular-lines/myalgebra.com.html
Also see https://www.mathway.com/Algebra and http://www.webmath.com/
I find it fascinating to read about someone else having an idea similar to mine:
"Our methodology uses a grammar-based approach to convert mathematical expressions into abstract syntax trees (AST) on which new methods can be developed for different evaluation or interpretation requirements. In this way, users can dynamically enter expressions and all the intermediate operations on them can successfully be achieved through AST nodes." ... "It can also be a good platform for the development of educational products in the field of mathematics, which can display all problem-solving steps."
I'm not clear on the distinctions of the three approaches to solvers, but it's helpful to know there's more than one approach
by Sahereh Hosseinpour, Mir Mohammad Reza Alavi Milani and Hüseyin Pehlivan
Symmetry 2018, 10(7), 285; https://doi.org/10.3390/sym10070285
https://www.mdpi.com/2073-8994/10/7/285/htm
The following sites parse input, take a specified action, and show the steps
(I'm not sure whether these two sites are related or clones of each other)
https://www.softmath.com/math-com-calculator/quadratic-equations/myalgebra.com.html
https://www.mathsite.org/maths-factors/perpendicular-lines/myalgebra.com.html
Also see https://www.mathway.com/Algebra and http://www.webmath.com/
I find it fascinating to read about someone else having an idea similar to mine:
"Our methodology uses a grammar-based approach to convert mathematical expressions into abstract syntax trees (AST) on which new methods can be developed for different evaluation or interpretation requirements. In this way, users can dynamically enter expressions and all the intermediate operations on them can successfully be achieved through AST nodes." ... "It can also be a good platform for the development of educational products in the field of mathematics, which can display all problem-solving steps."
I'm not clear on the distinctions of the three approaches to solvers, but it's helpful to know there's more than one approach
- computer algebra system
- rule based: "Starting with a mathematical expression, this approach transforms it into another equivalent expression and compares it with the intended target expression for equivalence verification."
- structural approach: "two mathematical expressions are first represented as two mathematical expression trees. Then, tree matching algorithm is used to compare the two trees using dynamic programming for equivalence verification.
Section 2.3 walks through the entire process of getting from BNF to an AST.
avoiding the need for logins in the Physics Derivation Graph website
Now that the PDG interface is a webpage, and users (not just developers) are the audience, there is a question of whether users of the website need accounts.
Downsides:
Data could be uploaded and download
User accounts
Having an account would enable persistence of data specific to one user.Downsides:
- Password management and security
- user account shenanigans
No users accounts
Live use without an account, modeled after services like https://repl.it/ and https://www.onlinecharttool.com/graph, enable users to explore the capabilities without need for registering and creating a password.Data could be uploaded and download
Source code
Historically the Physics Derivation Graph was developed on the command line using bash and Python. This implied a single user.
The source code was available, so other developers could pull a local copy to make edits.
The source code was available, so other developers could pull a local copy to make edits.
API calls
This would allow interfaces to be developed that are independent of the backend.
Sunday, March 8, 2020
why I'm excited about the Physics Derivation Graph
A few months ago I realized that rather than try to figure out what the best storage format was (MathML, Latex, Sympy, etc) and what database should store that representation (CSV, SQL, XML, etc), the "easy" solution was to simply store strings in dictionaries as a Python Pickle. No translation needed -- simply save the internal representation to disk and read it in as a Python variable.
Then I had an insight about how the front-end was supposed to work using the Model-View-Controller (MVC) approach. Now I felt comfortable about both the back-end and front-end aspects of the PDG! I had a backlog of features which were now easy and intuitive to implement. However, that didn't result in the excitement and motivation I now feel.
In the process of reviewing my hand-written notes from graduate school, I realized I now have actual hope of converting the notes to an electronic format. My reinvigorated interest in implementing the Physics Derivation graph is because I now have the relevant aspects figured out and see a well-defined end point.
That shifted my view of what I should be working on in the PDG code. Rather than working on arbitrary features, I am now focused on addressing aspects that are blocking me from converting my paper notes into PDG content.
Saturday, March 7, 2020
surveys of knowledge graphs and historical evolution
The Physics Derivation Graph is a domain specific knowledge graph
https://www.linkedin.com/pulse/knowledge-graphs-end-user-products-from-cyc-ai-part-daniel-kornev/
https://arxiv.org/abs/2003.02320
https://news.ycombinator.com/item?id=22504133
http://knowledgegraph.today/paper.html
https://www.linkedin.com/pulse/knowledge-graphs-end-user-products-from-cyc-ai-part-daniel-kornev/
https://arxiv.org/abs/2003.02320
https://news.ycombinator.com/item?id=22504133
http://knowledgegraph.today/paper.html
Subscribe to:
Posts (Atom)