<jupyter_start><jupyter_text>Tutorial about the LocData class<jupyter_code>import numpy as np
import pandas as pd

import locan as lc
lc.show_versions(system=False, dependencies=False, verbose=False)<jupyter_output><empty_output><jupyter_text>Sample data A localization has certain properties such as 'position_x'. A list of localizations can be assembled into a dataframe:<jupyter_code>df = pd.DataFrame(
    {
        'position_x': np.arange(0,10),
        'position_y': np.random.random(10),
        'frame': np.arange(0,10),
    })<jupyter_output><empty_output><jupyter_text>Instantiate LocData from a dataframe A LocData object carries localization data together with metadata and aggregated properties for the whole set of localizations.We first instantiate a LocData object from the dataframe:<jupyter_code>locdata = lc.LocData.from_dataframe(dataframe=df)
attributes = [x for x in dir(locdata) if not x.startswith('_')]
attributes<jupyter_output><empty_output><jupyter_text>LocData attributes The class attribute Locdata.count represents the number of all current LocData instantiations.<jupyter_code>print('LocData count: ', lc.LocData.count)<jupyter_output><empty_output><jupyter_text>The localization dataset is provided by the data attribute:<jupyter_code>print(locdata.data.head())<jupyter_output><empty_output><jupyter_text>Aggregated properties are provided by the attribute properties. E.g. the property `position_x` represents the mean of the `position_x` for all localizations. We keep the name, since the aggregated dataset can be treated as just a single locdata event with `position_x`. This is used when dealing with data clusters.<jupyter_code>locdata.properties<jupyter_output><empty_output><jupyter_text>Since spatial coordinates are quite important one can check on *coordinate_labels* and dimension:<jupyter_code>locdata.coordinate_labels
locdata.dimension<jupyter_output><empty_output><jupyter_text>A numpy array of spatial coordinates is returned by:<jupyter_code>locdata.coordinates<jupyter_output><empty_output><jupyter_text>Metadata  For detailed information see the `Tutorial about metadata`. Metadata is provided by the attribute meta and can be printed as<jupyter_code>locdata.print_meta()<jupyter_output><empty_output><jupyter_text>A summary of the most important metadata fields is printed as:<jupyter_code>locdata.print_summary()<jupyter_output><empty_output><jupyter_text>Metadata fields can be printed and changed individually:<jupyter_code>print(locdata.meta.comment)
locdata.meta.comment = 'user comment'
print(locdata.meta.comment)<jupyter_output><empty_output><jupyter_text>LocData.meta.map represents a dictionary structure that can be filled by the user. Both key and value have to be strings, if not a TypeError is thrown.<jupyter_code>print(locdata.meta.map)
locdata.meta.map['user field'] = 'more information'
print(locdata.meta.map)<jupyter_output><empty_output><jupyter_text>Metadata can also be added at Instantiation:<jupyter_code>locdata_2 = lc.LocData.from_dataframe(dataframe=df, meta={'identifier': 'myID_1', 
                                                   'comment': 'my own user comment'})
locdata_2.print_summary()<jupyter_output><empty_output><jupyter_text>Instantiate locdata from selection A LocData object can also be instantiated from a selection of localizations. In this case the LocData object keeps a reference to the original locdata together with a list of indices (or a slice object)). The new dataset is assembled on request of the data attribute. *Typically a selection is derived using a selection method such that using LocData.from_selection() is not often necessary.*<jupyter_code>locdata_2 = lc.LocData.from_selection(locdata, indices=[1,2,3,4])
locdata_3 = lc.LocData.from_selection(locdata, indices=[5,6,7,8])

print('count: ', lc.LocData.count)
print('')
print(locdata_2.data)
locdata_2.print_summary()<jupyter_output><empty_output><jupyter_text>The reference is kept in a private attribute as are the indices.<jupyter_code>print(locdata_2.references)
print(locdata_2.indices)<jupyter_output><empty_output><jupyter_text>The reference is the same for both selections.<jupyter_code>print(locdata_2.references is locdata_3.references)<jupyter_output><empty_output><jupyter_text>Instantiate locdata from collection A LocDat object can further be instantiated from a collection of other LocData objects.<jupyter_code>del(locdata_2, locdata_3)

locdata_1 = lc.LocData.from_selection(locdata, indices=[0,1,2])
locdata_2 = lc.LocData.from_selection(locdata, indices=[3,4,5])
locdata_3 = lc.LocData.from_selection(locdata, indices=[6,7,8])
locdata_c = lc.LocData.from_collection(locdatas=[locdata_1, locdata_2, locdata_3], meta={'identifier': 'my_collection'})

print('count: ', lc.LocData.count, '\n')
print(locdata_c.data, '\n')
print(locdata_c.properties, '\n')
locdata_c.print_summary()<jupyter_output><empty_output><jupyter_text>In this case the reference are also kept in case the original localizations from the collected LocData object are requested.<jupyter_code>print(locdata_c.references)<jupyter_output><empty_output><jupyter_text>In case the collected LocData objects are not needed anymore and should be free for garbage collection the references can be deleted by a dedicated Locdata method<jupyter_code>locdata_c.reduce()
print(locdata_c.references)<jupyter_output><empty_output><jupyter_text>Concatenating LocData objects  Lets have a second dataset with localization data:<jupyter_code>del(locdata_2)

df_2 = pd.DataFrame(
    {
        'position_x': np.arange(0,10),
        'position_y': np.random.random(10),
        'frame': np.arange(0,10),
    })

locdata_2 = lc.LocData.from_dataframe(dataframe=df_2)

print('First locdata:')
print(locdata.data.head())
print('')
print('Second locdata:')
print(locdata_2.data.head())<jupyter_output><empty_output><jupyter_text>In order to combine two sets of localization data from two LocData objects into a single LocData object use the class method *LocData.concat*:<jupyter_code>locdata_new = lc.LocData.concat([locdata, locdata_2])
print('Number of localizations in locdata_new: ', len(locdata_new))
locdata_new.data.head()<jupyter_output><empty_output><jupyter_text>Modifying data in place In case localization data has been modified in place, i.e. the dataset attribute is changed, all properties and hulls must be recomputed. This is best done by re-instantiating the LocData object using `LocData.from_dataframe()`; but it can also be done using the `LocData.reset()` function.<jupyter_code>del(df, locdata)

df = pd.DataFrame(
    {
        'position_x': np.arange(0,10),
        'position_y': np.random.random(10),
        'frame': np.arange(0,10),
    })

locdata = lc.LocData.from_dataframe(dataframe=df)

print(locdata.data.head())
locdata.centroid<jupyter_output><empty_output><jupyter_text>Now if localization data is changed in place (which you should not do unless you have a good reason), properties and bounding box are not automatically adjusted.<jupyter_code>locdata.dataframe = pd.DataFrame(
    {
        'position_x': np.arange(0,8),
        'position_y': np.random.random(8),
        'frame': np.arange(0,8),
    })

print(locdata.data.head())
locdata.centroid  # so this returns incorrect values here<jupyter_output><empty_output><jupyter_text>Update them by re-instantiating a new LocData object:<jupyter_code>locdata_new = lc.LocData.from_dataframe(dataframe=locdata.data)
locdata_new.centroid
locdata_new.meta<jupyter_output><empty_output><jupyter_text>Alternatively you can use `reset()`. In this case, however, metadata is not updated and will provide wrong information.<jupyter_code>locdata.reset()
locdata.centroid
locdata.meta<jupyter_output><empty_output><jupyter_text>Copy LocData Shallow and deep copies can be made from LocData instances. In either case the class variable count and the metadata is not just copied but adjusted accordingly.<jupyter_code>print('count: ', lc.LocData.count)
print('')
print(locdata_2.meta)
from copy import copy, deepcopy

print('count before: ', lc.LocData.count)
locdata_copy = copy(locdata_2)
locdata_deepcopy = deepcopy(locdata_2)
print('count after: ', lc.LocData.count)
print(locdata_copy.meta)
print(locdata_deepcopy.meta)<jupyter_output><empty_output><jupyter_text>Adding a property Any property that is created for a set of localizations (and represented as a python dictionary) can be added to the Locdata object. As an example, we compute the maximum distance between any two localizations and add that `max_distance` as new property to `locdata`.<jupyter_code>max_distance = lc.max_distance(locdata)
max_distance
locdata.properties.update(max_distance)
locdata.properties<jupyter_output><empty_output><jupyter_text>Adding a property to each localization in LocData.data In case you have processed your data and come up with a new property for each localization in the LocData object, this property can be added to data. As an example, we compute the nearest neighbor distance for each localization and add `nn_distance` as new property.<jupyter_code>locdata.data
nn = lc.NearestNeighborDistances().compute(locdata)
nn.results<jupyter_output><empty_output><jupyter_text>To add `nn_distance` as new property to each localization in LocData object, use the `pandas.assign` function on the `locdata.dataframe`.<jupyter_code>locdata.dataframe = locdata.dataframe.assign(nn_distance=nn.results['nn_distance'])
locdata.data<jupyter_output><empty_output><jupyter_text>Adding nn_distance as new property to each localization in LocData object with dataframe=None In case the LocData object was created with LocData.from_selection() the LocData.dataframe attribute is None and LocData.data is generated from the referenced locdata and the index list. In this case LocData.dataframe can still be filled with additional data that is merged upon returning LocData.data.<jupyter_code>locdata_selection = lc.LocData.from_selection(locdata, indices=[1, 3, 4, 5])
locdata_selection.data
locdata_selection.dataframe
nn_selection = lc.NearestNeighborDistances().compute(locdata_selection)
nn_selection.results<jupyter_output><empty_output><jupyter_text>Make sure the indices in nn.results match those in dat_selection.data:<jupyter_code>locdata_selection.data.index
nn_selection.results.index = locdata_selection.data.index
nn_selection.results<jupyter_output><empty_output><jupyter_text>Then assign the corresponding result to dataframe:<jupyter_code>locdata_selection.dataframe = locdata_selection.dataframe.assign(nn_distance= nn_selection.results['nn_distance'])
locdata_selection.dataframe<jupyter_output><empty_output><jupyter_text>Calling `data` will return the complete dataset.<jupyter_code>locdata_selection.data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09116279069767441, 'fraction_numerical': 0.007255813953488372, 'mean_word_length': 6.9992559523809526, 'pattern_counts': {'":': 0, '<': 133, '<?xml version=': 0, '>': 133, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '33', 'dir': 'jupyter-structured-clean-dedup', 'id': '506533', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3395, 'n_tokens_neox': 3334, 'n_words': 901, 'path': 'docs/tutorials/notebooks/LocData.ipynb', 'repo_name': 'super-resolution/Locan', 'size': '10750'}