예제 #1
0
def update_data_item_from_data_element(data_item: DataItem.DataItem, data_element, data_file_path=None):
    data_item.ensure_data_source()
    version = data_element["version"] if "version" in data_element else 1
    if version == 1:
        update_data_item_from_data_element_1(data_item, data_element, data_file_path)
    else:
        raise NotImplementedError("Data element version {:d} not supported.".format(version))
예제 #2
0
def update_data_item_from_data_element(
        data_item: DataItem.DataItem,
        data_element: DataElementType,
        data_file_path: typing.Optional[pathlib.Path] = None) -> None:
    data_item.ensure_data_source()
    version = data_element["version"] if "version" in data_element else 1
    if version == 1:
        update_data_item_from_data_element_1(data_item, data_element,
                                             data_file_path)
    else:
        raise NotImplementedError(
            "Data element version {:d} not supported.".format(version))
예제 #3
0
 def __get_storage_for_item(
         self, item: DataItem.DataItem) -> DataItemStorageAdapter:
     if not item.uuid in self.__data_item_storage:
         storage_handler = self.make_storage_handler(item)
         self.register_data_item(item, item.uuid, storage_handler,
                                 item.write_to_dict())
     return self.__data_item_storage.get(item.uuid)
예제 #4
0
 def append_data_item(self, data_item: DataItem.DataItem) -> None:
     assert not self.get_item_by_uuid("data_items", data_item.uuid)
     self.append_item("data_items", data_item)
     data_item.write_data_if_not_delayed()  # initially write to disk
예제 #5
0
def update_data_item_from_data_element_1(data_item: DataItem.DataItem, data_element: typing.Dict, data_file_path=None):
    assert data_item
    with data_item.data_item_changes(), data_item.data_source_changes():
        # file path
        # master data
        if data_file_path is not None:
            data_item.source_file_path = data_file_path
        data_and_metadata = convert_data_element_to_data_and_metadata(data_element)
        data = data_and_metadata.data
        dimensional_calibrations = data_and_metadata.dimensional_calibrations
        intensity_calibration = data_and_metadata.intensity_calibration
        is_sequence = data_and_metadata.is_sequence
        collection_dimension_count = data_and_metadata.collection_dimension_count
        datum_dimension_count = data_and_metadata.datum_dimension_count
        data_shape_data_dtype = data_and_metadata.data_shape_and_dtype
        is_same_shape = data_item.data_shape == data_shape_data_dtype[0] and data_item.data_dtype == data_shape_data_dtype[1] and data_item.is_sequence == is_sequence and data_item.collection_dimension_count == collection_dimension_count and data_item.datum_dimension_count == datum_dimension_count
        if is_same_shape:
            with data_item.data_ref() as data_ref:
                sub_area = data_element.get("sub_area")
                if sub_area is not None:
                    top = sub_area[0][0]
                    bottom = sub_area[0][0] + sub_area[1][0]
                    left = sub_area[0][1]
                    right = sub_area[0][1] + sub_area[1][1]
                    data_ref.master_data[top:bottom, left:right] = data[top:bottom, left:right]
                else:
                    data_ref.master_data[:] = data[:]
                data_ref.data_updated()  # trigger change notifications
            if dimensional_calibrations is not None:
                for dimension, dimensional_calibration in enumerate(dimensional_calibrations):
                    data_item.set_dimensional_calibration(dimension, dimensional_calibration)
            if intensity_calibration:
                data_item.set_intensity_calibration(intensity_calibration)
            data_item.metadata = data_and_metadata.metadata
        else:
            data_item.set_xdata(data_and_metadata)
        # title
        if "title" in data_element:
            data_item.title = data_element["title"]
        # description
        # dates are _local_ time and must use this specific ISO 8601 format. 2013-11-17T08:43:21.389391
        # time zones are offsets (east of UTC) in the following format "+HHMM" or "-HHMM"
        # daylight savings times are time offset (east of UTC) in format "+MM" or "-MM"
        # timezone is for conversion and is the Olson timezone string.
        # datetime.datetime.strptime(datetime.datetime.isoformat(datetime.datetime.now()), "%Y-%m-%dT%H:%M:%S.%f" )
        # datetime_modified, datetime_modified_tz, datetime_modified_dst, datetime_modified_tzname is the time at which this image was modified.
        # datetime_original, datetime_original_tz, datetime_original_dst, datetime_original_tzname is the time at which this image was created.
        utc_datetime = data_and_metadata.timestamp
        data_item.created = utc_datetime
        if "time_zone" in data_and_metadata.metadata.get("description", dict()):
            timezone_dict = copy.deepcopy(data_and_metadata.metadata["description"]["time_zone"])
            timezone = timezone_dict.get("timezone")
            if timezone is not None:
                data_item.timezone = timezone
            timezone_offset = timezone_dict.get("tz")
            if timezone_offset is not None:
                data_item.timezone_offset = timezone_offset
async def change_edge(document_controller: DocumentController.DocumentController, model_data_item: DataItem.DataItem, eels_data_item: DataItem.DataItem, edge: "ElementalMappingEdge") -> None:
    """Change the eels data item and associated items to display new edge.

    The library will be changed in the following way:
        - the pick region will be renamed
        - the pick data item will connect fit/signal regions to new edge data structure
        - the background subtraction computation will use edge intervals from new edge
        - the pick, background, subtracted, and eels line plot data items will be renamed
        - the eels line plot will connect fit/signal regions to new edge data structure
        - the edge reference will reference the new edge
    """
    document_model = document_controller.document_model

    computation = None  # type: Symbolic.Computation
    for computation_ in document_model.computations:
        if computation_.source == eels_data_item and computation_.processing_id == "eels.background_subtraction11":
            computation = computation_
            break

    edge_ref_data_structure = None
    old_edge_data_structure = None
    for data_structure in document_model.data_structures:
        if data_structure.source == eels_data_item and data_structure.structure_type == "elemental_mapping_edge_ref":
            edge_ref_data_structure = data_structure
            old_edge_data_structure = data_structure.get_referenced_object("edge")
            break

    if not computation or not edge_ref_data_structure or not old_edge_data_structure:
        return

    pick_region = edge_ref_data_structure.get_referenced_object("pick_region")

    if not eels_data_item or not pick_region:
        return

    pick_region.label = "{} {}".format(_("Pick"), str(edge.electron_shell))

    for connection in copy.copy(document_model.connections):
        if connection.parent == eels_data_item and connection.source_property in ("fit_interval", "signal_interval"):
            source_property = connection.source_property
            target_property = connection.target_property
            target = connection._target
            document_model.remove_connection(connection)
            new_connection = Connection.PropertyConnection(edge.data_structure, source_property, target, target_property, parent=eels_data_item)
            document_model.append_connection(new_connection)

    for computation_variable in computation.variables:
        if computation_variable.name in ("fit_interval", "signal_interval"):
            computation_variable.specifier = document_model.get_object_specifier(edge.data_structure)

    eels_data_item.title = "{} EELS Data of {}".format(pick_region.label, model_data_item.title)

    for connection in copy.copy(document_model.connections):
        if connection.parent == eels_data_item and connection.source_property in ("fit_interval", "signal_interval"):
            source_property = connection.source_property
            target_property = connection.target_property
            target = connection._target
            document_model.remove_connection(connection)
            new_connection = Connection.PropertyConnection(edge.data_structure, source_property, target, target_property, parent=eels_data_item)
            document_model.append_connection(new_connection)

    edge_ref_data_structure.remove_referenced_object("edge")
    edge_ref_data_structure.set_referenced_object("edge", edge.data_structure)

    # the eels item will need the initial computation results to display properly (view to intervals)
    await document_model.compute_immediate(document_controller.event_loop, computation)
    eels_display_item = document_model.get_display_item_for_data_item(eels_data_item)
    eels_display_item.view_to_intervals(eels_data_item.xdata, [edge.fit_interval, edge.signal_interval])