Tech Blog

A script to use an Alma set as a basis for analysis

Although it is possible to create a set in Alma from an analysis, for now it is not possible to do the opposite.
In Analytics you would need to create an “is equal to / is in” filter with lots of data and doing it manually is very difficult, so I developed a Python script that retrieves all the data from a set of Alma directly via API or reads it from a field of a CSV file (which could be obtained from an exported set or even from another source); the data obtained will be used to construct the filter and insert it into an analysis.

How to use the script

As an example, I have a set of titles in Alma and I want to use all MMS IDs from it as a basis for an analysis report containing creation and modification date, and total number of loans.

In Analytics, create a new Title Analysis (o use an existing one):

Add a “is prompted” filter on the column you want to populate with data from the set (if you want you can add more filters, but only one can be a “is prompted” filter type)

Go to the “Advanced” tab, select all and copy the text into the “Analysis XML” box:

Past all xml in a text editor and save it in the same directory of the script (report_example.xml):

In Alma, copy the set id from “Set Details”:

Open a terminal/CMD in the script directory and invoke this command:

python report_example.xml -s 2073710990003298 -o report_output.xml

The script retrieves the analysis in report_example.xml, collecting all members of the set and writes the modified analysis in report_output.xml.
Now open the report_output.xml file:

As you can see, the script added the set members as a “IN” filter of  “Bibliographic Details”.”MMS ID” column, that was previously filtered with a “Is prompted” filter.

Now you can re-open the analysis in Analytics, or if you prefer create a new empty one, then go to the “Advanced” tab and delete all content in the “Analysis XML” box:

Copy all the content of report_output.xml file and past it into the box, then click on the “Apply XML” button:

Now you can see the analysis changed:

And here you get the results:

SET id as source of data (via Configuration and Administration API)

The example above uses the “retrieve set members” API method to collect data: a Configuration and Administration API key is needed and has to be copied in the INI section of the script:

If you add the -d argument when you invoke the script, the data are collected from the description tag instead of the id tag (look here about the xml data format returned by the retrieve members set API  :

CSV file as source of data

Instead of set id, you can use a CSV file as source of data, getting it for example from an excel/calc export of a set:

Save it as CSV file (ex., result.csv) with headings in the same directory of the script, and remember the column from where to collect the data (ex., “MMS ID”), then invoke this command:

python report_example.xml -f results.csv -c "MMS ID" -o report_output.xml

and then proceed to copy the new analysis in Analytics as above.

This method is recommended for large data sets, because it is faster and doesn’t need multiple API calls.

I have tested the script using a large set with 42000 members, and it worked fine, even if Analytics was obviously slow to give the results: let me know if you have success or failures with larger sets.


5 Replies to “A script to use an Alma set as a basis for analysis”

  1. Thank you for this post. It works great. Would you mind to show me how to save MMS ID (16 digits) in a CSV file? a MMS ID consists of 16 digits. A CSV file does not save numbers more than 15 digits.

  2. Additional comment: correction: A CSV file saves numbers which have more than 15 digits. However, a CSV does NOT save numbers which have more than 15 digits correctly.

  3. When you export a spreadsheet into a csv file in Calc choose the option “Save the cell content as shown”, in Excel set the mmsid column format as text, it will keep all digits in the CSV file (check it using a text editor).
    If you want to reimport the csv file in spreadsheet remember to set the MMS ID column as text in the import wizard to avoid the scientific notation numbers displaying.

  4. I saved MMS ID as text in Excel and save the file as csv format. But, when I reopen the csv file, the MMS_ID were converted into the scientific notation numbers displaying and changed the last two digits into 70. Therefore, the second option that you suggested in this instruction did not work. Also, I could not fine “Calc” option in Excel. The Clac option belongs to the LibreOffice spreadsheet?

Leave a Reply