Generating filter from complex tiddler set

I want to generate a simple filter that returns an average value. Example, I want to know the average age of all males (which are tiddlers tagged with person and male). I have macros to determine a single age. Fundamentally, the end filter that I am currently generating is correct.

Eg.

=62 =87 =5 +[average[]]

However, since I wrongly used a macro to return this string, it has embedded span tags and double quotes to my filter string, which causes the filter, when used, to return a wrong value.

The macro I wrote is somewhat complex. It has to filter out just male individuals, ensure that individual has a birth and death event (separate tiddlers), and use those to determine the age. Wrongly, I used various list and reveal widgets (this causes the span tags to appear in my result string) to determine if I can call my macro getAgeInYears.

I’m sure this is a common problem. Can someone point me to how this should be done? Obviously, I’m not looking for a solution to my exact problem. I suspect there is general way to code this type of problem.

Should I be appending these strings to a variable to generate my filter string? If so, how?

Thanks,

Craig

The first thing that comes to my mind is that you can use the wikify widget to eliminate the html tags that were generated, since a text output “Return the plain text of the rendered output (ie HTML tags are omitted)”. Then, in the wikify widget, you could use your filter.

But you shouldn’t need to do that so in the first place. Here’s an example on how you can use filters:

<$let
isMale="[get[gender]match[male]]"
isDead="[get[death]]"
getAge="[get[birth]format:date[YYYY]negate[]] [get[death]format:date[YYYY]else<now YYYY>]+[sum[]]"
AverageMaleAge={{{ [all[tiddlers]filter<isMale>]:map[subfilter<getAge>] +[average[]round[]] }}}
AverageMaleAgeDeath={{{ [all[tiddlers]filter<isMale>filter<isDead>]:map[subfilter<getAge>] +[average[]round[]] }}}
>

The average male age (alive and dead) is <<AverageMaleAge>>, and the average male died at <<AverageMaleAgeDeath>>

</$let>

In this example I get the date of death and date of birth from the same tiddler but you could adapt this to look for the data in another tiddler. If you provide a bit more context on that I can try to help you - if you want.

https://demos.tiddlyhost.com/#How%20to%20apply%20conditional%20filters

2 Likes

@telumire Thank you. I thought the use of the $wikify widget would the easiest to implement since the macros were already written. As you describe, that worked as expected.

I will attempt, in the coming days after Christmas, to see if I can implement the filters as you described.

I have plans to dramatically add more complex filters. Such as, showing the average and median age of death of all ancestors of a selected individual.

Thank you… Happy / Merry Christmas and Happy New Year to you and all the subscribers of this group. This is a great community. And, be safe out there. This winter storm that is crossing North America has arrived here in southern Ontario today. I might just stay warm and keep coding :slight_smile:

1 Like

I too am interested in complex solutions so here is a few ideas to consider.

It is well worth looking at the kin operator for this kind of operation “walk a tree” because the operator itself can find all members in a hierarchy to any depth (or height for that matter) then you can use this list of titles as input to one or more mathematical operations including with the map operator.

You can also save the result as a variable, (sometimes you will need to wikify it) then use this in future operations, so you do not need to “walk the tree again”, even to extract from the full list, a sublist eg; isDead.

  • One other tip is use english to reduce the complexity eg; also have the filter isDeadMale="[get[gender]match[male]get[death]]"

I think you recommended this to me before when I started this TW genealogical solution. At the time I couldn’t get to do what I wanted. But, that was likely my TW knowledge at the time. I have since written a few macros to do the job, including a blood relationship calculator—which given two nodes returns a string that describes the blood relationship (if any). Oddly, I wrote a blood relationship calculator back 1995 (VB6). In 2010 I converted that method to Php so it could be used on a Drupal site, then last year I converted that to a JavaSript macro for my TW solution. Gee, I did that the GEDCOM import method too.

I’ll have time next week. I’ll go back and review the kin operator. Thanks.