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

[Question] Is it possible to attach a unique label to a vertex #6

Open
ducky427 opened this issue Oct 13, 2015 · 5 comments
Open

[Question] Is it possible to attach a unique label to a vertex #6

ducky427 opened this issue Oct 13, 2015 · 5 comments

Comments

@ducky427
Copy link

Hi,
Thanks for this library. It looks really interesting and powerful.

I am looking at the spreadsheet example and trying to understand the vertex collection functions. By the looks of it, (f/signal-map v) returns a map of type {<id> value ...}, where <id> is the vertex number in the graph.

Is it possible to change that <id> into something user specified, so that the end user can reason about which value is associated with which node without trying to look it up in the graph.

I hope this makes sense.

cheers

@ducky427
Copy link
Author

I can work around this issue by keeping a map of {<vertex-id> <my-id>...}. But it'll be nice to have that support as part of the graph.

@postspectacular
Copy link
Member

Hiya, thanks for your interest & a good question. Custom vertex IDs are supported, but not exactly as what you're after...

  1. when you create a new compute-graph, you can specify an ::id-gen-fn option. This is a single-arg function (taking nil or the previous ID) and produces the next vertex ID. By default this is simply a counter, but you could e.g. use this to generate UUIDs as vertex IDs:
(require '[thi.ng.fabric.core :as f])

(defn random-uuid (fn [_] (java.util.UUID/randomUUID)))

(def g (f/compute-graph {::f/id-gen-fn random-uuid}))

This however doesn't directly do what you asked for and I guess this is something which could be refactored (but would have to be supplied at construction time and couldn't be changed afterwards).

  1. The other solution is to just add a custom a user field to each vertex:
(def VAT (f/add-vertex! g 1.2 {:cell-id :vat-rate}))

Then to obtain that (e.g. inside a collection function, whilst processing the signal map):

(->> sig-id (f/vertex-for-id g) f/state :cell-id)

Does this help?

@ducky427
Copy link
Author

Thanks a lot for your response.

In the local context, for example inside a collection function, I think approach 2) would definitely work to get a mapping from vertex-id to :cell-id as you know the former.

In the longer term, I think I think vspec could probably be checked if it has a key called :id (or something) and only use ::next-id if it is not present.

This would also really help the end user in introspecting the values of the vertices in a large enough graph.

Thanks again.

@postspectacular
Copy link
Member

That's a great idea and I will refactor this asap... would also be faster to acess during processing, but would too need to include a check that the user specified ID is unique during construction. cheers!

@ducky427
Copy link
Author

Thanks a lot!

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

No branches or pull requests

2 participants