quantixed / TrackMateR

Analysis of TrackMate XML outputs in R
https://quantixed.github.io/TrackMateR/
Other
12 stars 1 forks source link

Error in find_td_area #6

Closed insectito closed 1 year ago

insectito commented 1 year ago

Hi @quantixed ,

Thank you so much for the great package. I tried the example and works normally, but when I tried with my own data an error was produced. Do you know how can I fix it?

> library(ggplot2)
> library(TrackMateR)
> xmlPath <- file.choose()
> tmObj <- readTrackMateXML(XMLpath = xmlPath)
Units are:  0.2521142 pixels and 1200 frames 
Collecting spot data...
Matching track data...
Calculating distances...
> 
> # the frame interval is 20 minutes 
> tmObj <- correctTrackMateData(dataList = tmObj, tscalar = 20, tunit = "min")
Correcting timescale. 
> # generate a report
> reportDataset(tmObj)

Error in find_td_area(r = radius, xy = c(x0, y0), a = c(0, calibration[3,  : 
  min(x1, x2, y1, y2) >= 0 is not TRUE

Thank you

quantixed commented 1 year ago

Hi @insectito, thanks for reporting the problem. The scaling of your data might not be compatible with the default (track density looks for tracks within a 2 unit radius).

The error message is saying that the track it's analyzing is outside the image. Since I don't think this is possible in TrackMate, I think there's a bug in the rescaling step.

insectito commented 1 year ago

Hi @quantixed ,

Thank you very much for your response.

I tried again with your suggestions but I had the same message errors. I then tried again but with a smaller XML file created from a smaller region of the original movie. And this one does work fine (even without changing the calibration or the search radius). Is there a maximum XML file size ?

On a different note, my time lapse has 46 frames (20 minutes interval) so I am a little bit confused by the x-axis in these 2 plots 2daea265-55fe-4687-ad12-3f7f46d7b68f and after unit calibration with tmObj <- correctTrackMateData(dataList = tmObj, xyunit = "um", tscalar = 20, tunit = "min") I get this

b8a782ca-8af0-47d8-9051-0400407288a5 Finally, I also get this error when I try to load a different XML file:

> tmObj <- readTrackMateXML(XMLpath = xmlPath)
Units are:  0.2521142 pixels and 1200 frames 
Error in file(con, "w") : all connections are in use

I also attach the file that I used for these plots.

Thank you very much for your help,
D06_TR_2022 11 23-1-1 - Copy.zip

quantixed commented 1 year ago

Thanks for the info. Using your file, and after installing the latest version of TrackMateR, I can load in the XML file and plot it, i.e. I am not getting an error in find_td_area(). So, I'm wondering if you are not running the latest version or maybe if there is a subtle difference e.g. between mac/PC.

On the scaling, the scalar is what the unit should be multiplied by to correct it rather than what it should be (if that makes sense). So to rescale your files you need to do: tmObj <- correctTrackMateData(dataList = tmObj, xyscalar = 1, xyunit = "um", tscalar = 1/60, tunit = "min") Without scaling, it says Units are: 0.2521142 pixels and 1200 frames which should be 0.2521142 um (I guess) and 1200 seconds. To go from seconds to minutes correctly the tscalar should be 1/60. To leave xy unchanged, you need a scalar of 1.

Again, after scaling I can run it without an error. I do get a warning which is because the jump distance plot fails, but it is just a warning and you still get the report.

insectito commented 1 year ago

Hi @quantixed,

Thank you, now the script works fine with a small file (3.5 MB), but when I try with a larger file (27 MB) I get the same error. I am using the package version 0.3.2.

> reportDataset(tmObj)
Error in find_td_area(r = radius, xy = c(x0, y0), a = c(0, calibration[3,  : 
  min(x1, x2, y1, y2) >= 0 is not TRUE

I attach a sample file. Thank you very much.

C02_TR_2022 11 23.zip

quantixed commented 1 year ago

Just a quick comment to say that I can reproduce that error and I'll take a look at what's causing it. Thanks for raising the issue.

quantixed commented 1 year ago

Hi @insectito, I think I have fixed the problem now. The file you sent has tracks that are outside the image. So, now when the file gets loaded in, this is checked and dealt with. The default settings for jump distance and track density don't do well for your data, but that's a secondary problem. You can at least look at your file now. If you update, by running devtools::install_github("quantixed/TrackMateR") and then try again, it should work!

insectito commented 1 year ago

Hi @quantixed,

Thank you very much for fixing the error, now the command reportDataset(tmObj)works. How come there are tracks outside of the image? Is it possible to change the defaults for reportDataset(tmObj) such as the radius for the track density?

I am currently trying to compare two data sets using compareDatasets()but I get a new error:

Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff, nDcentral = nDcntr) : 
  singular gradient matrix at initial parameter estimates
There was an error in fitting jump distance with 2 populations. Try again using different parameters for `init` and/or `nPop`.
Units are:  0.2521142 pixels and 1200 frames 
Error in file(con, "w") : all connections are in use

Here is a link to download the data that causes this error: https://drive.google.com/file/d/1JQ2RQmH5_kGt7jAstOR7Y1kz9Ra9W6uc/view?usp=share_link

Thank you very much.

quantixed commented 1 year ago

How come there are tracks outside of the image?

Not sure but my guess is that the centre of the spot that TrackMate is making is computed to be outside the image.

Is it possible to change the defaults for reportDataset(tmObj) such as the radius for the track density?

There is not an easy way to do this currently. It is possible to make all of the individual plots that reportDataset() makes and adjust the parameters by hand, but there isn't a way to tweak it. I agree this would be handy so I will try to add it soon.

Thanks for sending your data. It runs OK for me and I get an output. The error message is caused by two things:

  1. "Error in nlsModel" is a problem with fitting 2 populations to the jump distances. This is actually just a warning. The fitting failed so the plot didn't get made, but you will still get an output.
  2. "Error in file(con, "w")" is caused by reading in a big file without parallel processing. This will cause a crash. I do not see this on my system (mac). Are you using Windows?
insectito commented 1 year ago

Hi @quantixed,

Thank you for your help. The system that I am using is:

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

when I run closeAllConnections() then compareDatasets() I can compare up to 3 files apparently.

Do you have sample data sets to test the dataset comparison?

Thank you

quantixed commented 1 year ago

Hi @insectito

Thanks for the feedback.

I have just pushed a new version where it is possible to alter the parameters directly in compareDatasets() or in reportDataset(). The docs have been updated to show what can be changed.

With this new version, I can take the data that you sent and get an output and comparison by doing compareDatasets(radius = 100), the default value is too small for your data.

Note that your calibration files should read

value,unit
1,um
20,min

The jump distance fitting still fails on your data, but this error is now dealt with so that you still get outputs even if the fitting fails. The default is to try to find two populations. Normally, switching to one population fixes things (i.e. compareDatasets(radius = 100, nPop = 1)`), but for some reason this fails too. With some experimenting you might be able to find the right parameters for your data.

So I think this version means you should be able to analyse your data finally.

I wasn't able to get closeAllConnections() working within the package without breaking stuff. I'm keen to fix this error (although I don't see it on my system) though, so if that has solved your problem and you can compare many files, let me know!

Do you have sample data sets to test the dataset comparison?

Some sample datasets for the comparison is a good idea. I will try to set something up soon.