Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request : Save adjacency matrix in a standard data table/format ? #4

Closed
woodoxherd opened this issue Jan 14, 2022 · 8 comments
Closed

Comments

@woodoxherd
Copy link

@woodoxherd woodoxherd commented Jan 14, 2022

Hi SkepticMystic,

Recently, i runned a Google research with "adjacency matrix" and "Obsidian" as keywords, and naturally stumble on this plugin of yours, which i missed into the app plugin browser : Thanks for making this feature available, this is very interesting !

As i would need it quite soon in the near future, for professional foresight use, my initial research was to know if there would be a way to convert an Obsidian graph into an adjacency matrix.. but the latter one having numbers instead of colors :)

i.e. from something like this:

Prospective Graphe

to something like this:

Prospective Table

Would you think this could be quite quickly implementable into the Save options of your plugin, like into .csv or some standard equivalent ??

Thanks for your feedback, and have a good One,

Damien

@SkepticMystic
Copy link
Owner

@SkepticMystic SkepticMystic commented Feb 5, 2022

@woodoxherd hey :)
Here is a code snippet you can run in the Obsidian console.
It will create an adjacency matrix in JSON form:

const { resolvedLinks } = app.metadataCache
const filePaths = Object.keys(resolvedLinks)
const adjMatrix = {}
filePaths.forEach(pathI => {
    adjMatrix[pathI] = {}
    filePaths.forEach(pathJ => {
        const degree = resolvedLinks[pathI][pathJ] ?? 0
        adjMatrix[pathI][pathJ] = degree
    })
})

console.log(JSON.stringify(adjMatrix)) 

@SkepticMystic
Copy link
Owner

@SkepticMystic SkepticMystic commented Feb 5, 2022

You could then run that JSON object thru this converter to get it to CSV: https://www.convertcsv.com/json-to-csv.htm

@woodoxherd
Copy link
Author

@woodoxherd woodoxherd commented Feb 8, 2022

Hey @SkepticMystic !

I turned the example above as a new vault in order to try out your snippet, but it seems not to work, as you can see.

Capture d’écran 2022-02-08 à 20 23 50

Maybe that is nothing, maybe i did something wrong ? 🤔
Anyway, i wonder where would be located the JSON object/file into my computer, so that i could process further into CSV convertion ?

@woodoxherd
Copy link
Author

@woodoxherd woodoxherd commented Feb 8, 2022

Here is the vault i used, in case you would need it for some debugging : Adjacency Test.zip

@SkepticMystic
Copy link
Owner

@SkepticMystic SkepticMystic commented Feb 9, 2022

It looks like it ran fine the first time you called it. You can need to run console.log(adjMatrix) to print it to the console. Add that to the bottom of the snippet and run again.

But you'll see that calling it again will give the Duplicate Identifier error. That's because the snippet declares it's variables as constants. So you can either reload Obsidian to wipe those away, or use let in place of const

@woodoxherd
Copy link
Author

@woodoxherd woodoxherd commented Feb 9, 2022

Ok Ross @SkepticMystic thanks, i think i managed my way out of this. \°/

So, for posterity, if ever someone with the same needs than me would land on this page, i used console.log(JSON.stringify(adjMatrix)) into the console (instead of console.log(adjMatrix)), after running the snippet.

In our example, it returns some beautiful raw stuffs :

{"A.md":{"A.md":0,"B.md":1,"C.md":1,"D.md":0,"E.md":0,"F.md":0,"G.md":0,"H.md":0,"I.md":0,"J.md":0,"K.md":0,"L.md":0,"M.md":0,"N.md":0},"B.md":{"A.md":0,"B.md":0,"C.md":1,"D.md":0,"E.md":0,"F.md":0,"G.md":0,"H.md":0,"I.md":0,"J.md":0,"K.md":1,"L.md":0,"M.md":0,"N.md":0},"C.md":{"A.md":0,"B.md":1,"C.md":0,"D.md":1,"E.md":0,"F.md":0,"G.md":1,"H.md":0,"I.md":1,"J.md":0,"K.md":1,"L.md":0,"M.md":0,"N.md":0},"D.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":1,"F.md":0,"G.md":1,"H.md":0,"I.md":0,"J.md":0,"K.md":0,"L.md":0,"M.md":0,"N.md":0},"E.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":1,"G.md":0,"H.md":0,"I.md":0,"J.md":0,"K.md":0,"L.md":0,"M.md":0,"N.md":0},"F.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":0,"G.md":0,"H.md":0,"I.md":0,"J.md":0,"K.md":0,"L.md":1,"M.md":1,"N.md":0},"G.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":1,"F.md":1,"G.md":0,"H.md":0,"I.md":0,"J.md":0,"K.md":0,"L.md":0,"M.md":0,"N.md":0},"H.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":0,"G.md":0,"H.md":0,"I.md":1,"J.md":0,"K.md":0,"L.md":0,"M.md":1,"N.md":1},"I.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":0,"G.md":1,"H.md":0,"I.md":0,"J.md":0,"K.md":0,"L.md":0,"M.md":1,"N.md":1},"J.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":0,"G.md":1,"H.md":0,"I.md":1,"J.md":0,"K.md":0,"L.md":0,"M.md":0,"N.md":1},"K.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":0,"G.md":0,"H.md":1,"I.md":0,"J.md":1,"K.md":0,"L.md":0,"M.md":0,"N.md":0},"L.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":0,"G.md":0,"H.md":1,"I.md":0,"J.md":0,"K.md":0,"L.md":0,"M.md":0,"N.md":1},"M.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":0,"G.md":0,"H.md":0,"I.md":0,"J.md":0,"K.md":0,"L.md":0,"M.md":0,"N.md":1},"N.md":{"A.md":0,"B.md":0,"C.md":0,"D.md":0,"E.md":0,"F.md":0,"G.md":0,"H.md":0,"I.md":0,"J.md":0,"K.md":0,"L.md":0,"M.md":0,"N.md":0}}

Then, just copy this output given by the console, and, after a slight move through Regexr (for instance) in order to eliminate all the ".md", paste everything into the ConvertCSV mentioned above.

Et Voilà : adjmatrixtest.csv ! :)

@woodoxherd woodoxherd closed this Feb 9, 2022
@SkepticMystic
Copy link
Owner

@SkepticMystic SkepticMystic commented Feb 9, 2022

Nice!
I've added that last little snippet of code to my original script, so it's all in one place :)

@woodoxherd
Copy link
Author

@woodoxherd woodoxherd commented Feb 9, 2022

@SkepticMystic
A nice way to close the loop, i must say ! :)

Just as a an opening, a doorway : Would you have at your Knowledge some existing code already done that could process in the other way, i.e., starting from the CSV in order to build the Vault in Obsidian (with a template option, eventually) ??

As i intend to apply some matrix multiplication to the adjMatrix we got, that would be a convenient (and error less) way to vizualise the result back into Obsidian, compared to what i did by hand for our example above.

Please, let me know if ever you have references on this, please, and have a good One ! 🧐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants