visidata icon indicating copy to clipboard operation
visidata copied to clipboard

constants, folds, scans

Open MMesch opened this issue 2 years ago • 3 comments

Hi all,

These are more questions but may also become a feature request if there isn't a convenient way to doing so. If I think about common Excel style tables, they often have a strict schema but in addition some fixed values, some aggregations (sometimes called folds), and cumulative columns (sometimes called scans).

  • Is there a way to define a global constant that can be referred to later? Think about a fixed total budget that is then used in a fold or scan to compute different columns. It could also be something simple like the gravitational constant etc.
  • Is there a way to define custom aggregations as folds, taking a starting value and an accumulator function?
  • Another use case is a scan, e.g. a cumulative sum in python can be written as [acc := acc + x if x>0 else x for x in range(20)] and this can be added with the gz= command, but how do you refer to another column instead of range(20) here?

Thanks again for working on this nice tool.

MMesch avatar Sep 29 '21 11:09 MMesch

Is there a way to define a global constant that can be referred to later? Think about a fixed total budget that is then used in a fold or scan to compute different columns. It could also be something simple like the gravitational constant etc.

There is!

I have added this feature to my list of concepts to document, so there is no documentation that I can point to, but we do have a MemorySheet for keeping a recording of constants, and copied cells.

If you have a cell with a value, you can add it to the constants with Alt+m or the longname* memo-cell. Longname open-memos will give you the current list of constants and copied values in VisiData. You can reference them by name in any = Python expression.

open-memos should be attached to Alt+Shift+M, but that is currently not working for me, and I will need to investigate.

  • To execute longnames, press Spacebar, and then type out its name.

anjakefala avatar Oct 04 '21 04:10 anjakefala

Is there a way to define custom aggregations as folds, taking a starting value and an accumulator function?

I am not sure what "folds" means in this context, and perhaps @saulpw will be in a better position to answer this question.

You can add custom aggregators to your .visidatarc, and then they will be a part of your VisiData sessions going forward:

https://www.visidata.org/docs/api/columns#aggregators

I believe the func requirement for vd.aggregator is a Python function that accept an iterator, and returns a scalar.

Does this information help you out?

anjakefala avatar Oct 04 '21 04:10 anjakefala

https://github.com/saulpw/visidata/issues/1399

More requests for referencing a window of rows in a column expression.

anjakefala avatar Jun 20 '22 03:06 anjakefala

#1399 does record that since this issue has been opened, we have added windowing (scanning) as a feature.

Since this issue is over a year old, and I think we have addressed all of the features that we have added to the roadmap, I am going to close this. Please re-open if you have any related questions, or if you want to re-suggest adding something to the roadmap.

Kondo'd.

anjakefala avatar Nov 11 '22 04:11 anjakefala