INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Grouping function expression help  (Read 511 times)

DangerJP

  • World Citizen
  • ***
  • Posts: 130
Grouping function expression help
« on: October 14, 2024, 10:36:11 am »

Hi,

I would like to use an expression to ouput a list of field values associated with another field.
At first, I thought of using a grouping function, but it seems they can't ouptut a list of values at the moment.

Example:
I'd like to obtain the values for [Genre] associated with a given artist.
I imagine something like this could work:
GroupSummaryQuery(Artist, Genre, 2), where new mode "2" would output the values of [Genre] associated with an artist.
I'm using a pane view as workaround for now (see attached image).

In a perfect world, I'd have a field called [Artist Genre]. For the highlighted artist in the attached image ("Khruangbin"), this would be :
[Artist Genre] = [Psychedelic Rock (123); Neo-Psychedelia (24); Funk (15); Jazz (15); ...], with the values in () being the number of files tagged with each genres.
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2674
Re: Grouping function expression help
« Reply #1 on: October 14, 2024, 02:02:40 pm »

FieldQuery() can do that, but a View using this function will be extremely slow on medium/large collections. You can use ZStats to periodically recalculate the values you need though.

Test with my Video collection, querying for DiCaprio movies:
Code: [Select]
save(ListSort(FieldQuery(Actors, Leonardo DiCaprio, Genre, 0, 1), 0),_stats)/
listmix([L1] /(ListCount(ListGrep([_stats],[L1]))/), 0, ListClean([_stats],1))

Outputs this, sorted by Genre:
Action (4);Adventure (4);Comedy (3);Crime (6);Documentary (1);Drama (18);History (4);Mystery (1);Romance (3);Sci-Fi (2);Thriller (6);Western (3)

Sorting by Count is trickier:
Code: [Select]
save(ListSort(FieldQuery(Actors, Leonardo DiCaprio, Genre, 0, 1), 0),_stats)/
save(listmix(ListCount(ListGrep([_stats],[L1]))=[L1], 0, ListClean([_stats],1)),_counts)/
listmix(regex([L1],/#(\d+)=(.+)#/,-1,0)[R2] /([R1]/),0,listsort([_counts],1))

Output:
Drama (18);Thriller (6);Crime (6);History (4);Adventure (4);Action (4);Western (3);Romance (3);Comedy (3);Sci-Fi (2);Mystery (1);Documentary (1)
Logged

DangerJP

  • World Citizen
  • ***
  • Posts: 130
Re: Grouping function expression help
« Reply #2 on: October 15, 2024, 07:07:20 pm »

Hi zybex,

Thank you so much for your reply!

I can get the first part, i.e., having a list of genres associated with a given artist.

So, I created a new calculated field called [Artist Genre]:
[Artist Genre] = ListSort(FieldQuery(Artist, [Artist], Genre, 0, 1), 0)

For the example in the attached image, this outputs:
[Artist Genre] = Psychedelic Soul; Psychedelic Soul; Singer-Songwriter

Using the ListClean function, I could transform this ouput in:
Psychedelic Soul; Singer-Songwriter
which is exactly what I'm looking for, as it combines all genres of all albums for a given artist in a single list.

However, I can't get the second part, i.e., counting the files associated with each of these genres.
In the same example, this should output:
Psychedelic Soul (23); Singer-Songwriter (11)

I can't use a ListCount on [Artist Genre] since it does not list the 23 genres associated with each files, but only the genres themselves.
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2674
Re: Grouping function expression help
« Reply #3 on: October 15, 2024, 11:49:28 pm »

You just need to use the full expression above including the Save() lines, with Artist instead of Actors. Just paste it on the field definition:

Code: [Select]
save(ListSort(FieldQuery(Artist, [Artist], Genre, 0, 1), 0),_stats)/
save(listmix(ListCount(ListGrep([_stats],[L1]))=[L1], 0, ListClean([_stats],1)),_counts)/
listmix(regex([L1],/#(\d+)=(.+)#/,-1,0)[R2] /([R1]/),0,listsort([_counts],1))
Logged

DangerJP

  • World Citizen
  • ***
  • Posts: 130
Re: Grouping function expression help
« Reply #4 on: October 16, 2024, 06:36:27 pm »

Hi zybex,

I tried your expression and it does not output the number of files, only the number of genre occurences when a match with [Artist] is found.

For my example, it outputs:
Psychedelic Soul (2); Singer-Songwriter (1)
but should outputs:
Psychedelic Soul (23); Singer-Songwriter (11)

Still messing around with expressions, I'll see if I come up with a solution :)

Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2674
Re: Grouping function expression help
« Reply #5 on: October 17, 2024, 02:12:12 am »

You're right, it looks like FieldQuery() only outputs unique values, excluding duplicates. I assumed the function would return one item per file, including duplicates. It worked on my video collection because [Genre] for movies is usually a longer list of items where the order of genres is seldom the same, so "action;comedy" is different from "comedy;action" and both were listed.

@Matt, I assume this is by design as it is usually the desired behavior. Perhaps it makes sense to add a mode for "allow duplicates" ? Or to return the count of each item as DangerJP wants.

Logged

DangerJP

  • World Citizen
  • ***
  • Posts: 130
Re: Grouping function expression help
« Reply #6 on: October 17, 2024, 02:51:07 pm »

You're right, it looks like FieldQuery() only outputs unique values, excluding duplicates. I assumed the function would return one item per file, including duplicates. It worked on my video collection because [Genre] for movies is usually a longer list of items where the order of genres is seldom the same, so "action;comedy" is different from "comedy;action" and both were listed.

@Matt, I assume this is by design as it is usually the desired behavior. Perhaps it makes sense to add a mode for "allow duplicates" ? Or to return the count of each item as DangerJP wants.

🙏
Logged
Pages: [1]   Go Up