Protyping/mosaiq notebooks

I am trying out some of the mosaiq access features, and was looking at the protyping notebooks. It looks the API has changed somewhat since the notebooks were created. The 2020-06-18-get-treatment-record notebook can be made to import the “identify” function if I adjust the import to pymedphys._trf.manager, instead of pymedphys.labs.managelogfiles.

Would this be the correct way to import from pymedphys._trf, or is _trf supposed to be a private interface that wouldn’t be used in a notebook?

Hi @dg1an3,

Welcome to the PyMedPhys Discourse group :slight_smile: :balloon: :slight_smile:

So that others reading your post know what you’re referring to here is the notebook you’ve referenced:

At the moment I haven’t exposed the identify module as part of the public library API. For the time being its only public API has been through the pymedphys trf orchestrate CLI command. Nevertheless I would be certainly open to exposing it within the library’s API.

If you’re just doing exploratory/prototyping work, feel free to adjust the import within that notebook as you have described as that will find the appropriate module, but this won’t have any guarantee to continue working from one PyMedPhys version to the next. It is as you suspect, a “private” API.

If you would like to begin using it in a way where you would like to have a guarantee that it will continue working from one version of PyMedPhys to the next it would be worth getting what you need exposed within the public API. Let me know if the public API route is the one you’d like to go down and I can point out what needs to happen and guide you in making the pull request needed to expose it.


Hi @SimonBiggs

Thanks for the info. I was just looking at these notebooks for examples of pymedphys doing mosaiq queries - I am less interested in the TRF files. Do you have any examples accessing the Offset or DoseHst tables? it looks like the mosaiq execute statement is just standard sql, right? Maybe I could create an example notebook that queries these other tables.

That would be an amazing contribution. Yes, that execute statement is just standard SQL. Importantly parameters need to be passed through the “parameters” keyword so that the underlying library pymssql is able to sanitize those parameters against SQL injection. That’s only ever really an issue should those parameters come from untrusted user input, but given these examples would be copied by readers of the examples into all sorts of scenarios, best to be secure by default and make sure that all parameter insertion into the SQL string is via that third input parameter :slight_smile:

If at some point down the road we manage to make a database dump of a Mosaiq database, that can be spun up next to the notebook examples by the testing server, and then your examples can be included directly in the docs, and for each doc build your examples would be run as tests to make sure those examples still work… That would be amazing, and that is a place I am very keen to get to at some point in the future. Automated validation of notebook based documentation.

Hi @dg1an3

I’ve already done some work querying the DoseHst tables in pymedphys. What exactly are you looking to get out of those tables? I might already be doing that.

Hi @rembishj

Thanks for the offer. I’ve been making some progress understanding a basic query to get the Tx_DtTm’s for the Dose_Hst associated to a Site, i.e.

cursor.execute('SELECT Tx_DtTm FROM Dose_Hst '
    'WHERE Dose_Hst.SIT_ID = %s'
    'ORDER BY Tx_DtTm', sit_id)

from which I am forming sessions, using the hierarchical clustering algorithm in sklearn (which is only slightly more advanced than how Mosaiq counts sessions, I think).

I am curious about how to calculate the cumulative dose from the Dose_Hst records – am I correct that a reasonable approximation is to sum:

Dose_Hst.Meterset * Dose_Hst.cGrayPerMeterset

for the Dose_Hst records associated to a particular site?

Within the Dose_Hst table is a value called:


This gives you the cumulative dose at the start of that dose history record. To determine what your current cumulative dose is after delivery, you could add the projected dose for that beam on:

Dose_Hst.Dose_Ttl_Act + Dose_Hst.Dose_Addtl_Projected

Or instead, you could use your approach for determining the field dose and add it.

Dose_Hst.Dose_Ttl_Act + (Dose_Hst.Meterset * Dose_Hst.cGrayPerMeterset)

Using this approach, you can view the cumulative dose at any fraction in the treatment history. Does this answer your question properly?

But if you’re working with the up-to-date pymedphys repository, you can find some of this work done in the chart_checks portion of the experimental projects. Here’s an example of tracking the last week of treatments for a specific patient using that.