def entity_date_to_timestamp(entity_date_time): """Convert Synapse object date/time string (from modifiedOn or createdOn properties) to a timestamp.""" date_and_time = entity_date_time.split(".")[0] date_time_obj = datetime.datetime.strptime(date_and_time, "%Y-%m-%dT%H:%M:%S") return to_unix_epoch_time(date_time_obj)
def to_synapse_annotations(annotations): """Transforms a simple flat dictionary to a Synapse-style Annotation object.""" if is_synapse_annotations(annotations): return annotations synapseAnnos = {} for key in Annotations.system_properties: if hasattr(annotations, key): synapseAnnos[key] = getattr(annotations, key) for key, value in annotations.items(): if key in ['id', 'etag', 'blobAnnotations', 'creationDate', 'uri']: synapseAnnos[key] = value elif key in ['stringAnnotations', 'longAnnotations', 'doubleAnnotations', 'dateAnnotations']\ and isinstance(value, collections.Mapping): synapseAnnos.setdefault(key, {}).update({k: to_list(v) for k, v in value.items()}) else: elements = to_list(value) if all((isinstance(elem, str) for elem in elements)): synapseAnnos.setdefault('stringAnnotations', {})[key] = elements elif all((isinstance(elem, bool) for elem in elements)): synapseAnnos.setdefault('stringAnnotations', {})[key] = [str(element).lower() for element in elements] elif all((isinstance(elem, int) for elem in elements)): synapseAnnos.setdefault('longAnnotations', {})[key] = elements elif all((isinstance(elem, float) for elem in elements)): synapseAnnos.setdefault('doubleAnnotations', {})[key] = elements elif all((is_date(elem) for elem in elements)): synapseAnnos.setdefault('dateAnnotations', {})[key] = [to_unix_epoch_time(elem) for elem in elements] # TODO: support blob annotations # elif all((isinstance(elem, ???) for elem in elements)): # synapseAnnos.setdefault('blobAnnotations', {})[key] = [???(elem) for elem in elements] else: synapseAnnos.setdefault('stringAnnotations', {})[key] = [str(elem) for elem in elements] return synapseAnnos
def _convert_to_annotations_list(annotations): nested_annos = {} for key, value in annotations.items(): elements = to_list(value) element_cls = _annotation_value_list_element_type(elements) if issubclass(element_cls, str): nested_annos[key] = {'type': 'STRING', 'value': elements} elif issubclass(element_cls, bool): nested_annos[key] = { 'type': 'BOOLEAN', 'value': ['true' if e else 'false' for e in elements] } elif issubclass(element_cls, int): nested_annos[key] = { 'type': 'LONG', 'value': [str(e) for e in elements] } elif issubclass(element_cls, float): nested_annos[key] = { 'type': 'DOUBLE', 'value': [str(e) for e in elements] } elif issubclass(element_cls, (datetime.date, datetime.datetime)): nested_annos[key] = { 'type': 'TIMESTAMP_MS', 'value': [str(to_unix_epoch_time(e)) for e in elements] } else: nested_annos[key] = { 'type': 'STRING', 'value': [str(e) for e in elements] } return nested_annos
def to_submission_status_annotations(annotations, is_private=True): """ Converts a normal dictionary to the format used to annotate submission statuses, which is different from the format used to annotate entities. :param annotations: A normal Python dictionary whose values are strings, floats, ints or doubles :param is_private: Set privacy on all annotations at once. These can be set individually using :py:func:`set_privacy`. Example:: from synapseclient.annotations import to_submission_status_annotations, from_submission_status_annotations from datetime import datetime as Datetime ## create a submission and get its status submission = syn.submit(evaluation, 'syn11111111') submission_status = syn.getSubmissionStatus(submission) ## add annotations submission_status.annotations = {'foo':'bar', 'shoe_size':12, 'IQ':12, 'timestamp':Datetime.now()} ## convert annotations submission_status.annotations = to_submission_status_annotations(submission_status.annotations) submission_status = syn.store(submission_status) Synapse categorizes these annotations by: stringAnnos, doubleAnnos, longAnnos. """ if is_submission_status_annotations(annotations): return annotations synapseAnnos = {} for key, value in annotations.items(): if key in [ 'objectId', 'scopeId', 'stringAnnos', 'longAnnos', 'doubleAnnos' ]: synapseAnnos[key] = value elif isinstance(value, bool): synapseAnnos.setdefault('stringAnnos', []) \ .append({'key': key, 'value': str(value).lower(), 'isPrivate': is_private}) elif isinstance(value, int): synapseAnnos.setdefault('longAnnos', []) \ .append({'key': key, 'value': value, 'isPrivate': is_private}) elif isinstance(value, float): synapseAnnos.setdefault('doubleAnnos', []) \ .append({'key': key, 'value': value, 'isPrivate': is_private}) elif isinstance(value, str): synapseAnnos.setdefault('stringAnnos', []) \ .append({'key': key, 'value': value, 'isPrivate': is_private}) elif is_date(value): synapseAnnos.setdefault('longAnnos', []) \ .append({'key': key, 'value': to_unix_epoch_time(value), 'isPrivate': is_private}) else: synapseAnnos.setdefault('stringAnnos', []) \ .append({'key': key, 'value': str(value), 'isPrivate': is_private}) return synapseAnnos
def _process(self, deleteSamplesDf, modifiedOn): col = ("genieSampleId" if self._fileType == "sampleRetraction" else "geniePatientId") deleteSamplesDf.rename(columns={0: col}, inplace=True) samples = [ process_functions.checkGenieId(sample, self.center) for sample in deleteSamplesDf[col] ] deleteSamplesDf[col] = samples modifiedOn = to_unix_epoch_time( datetime.datetime.strptime(modifiedOn, "%Y-%m-%dT%H:%M:%S")) deleteSamplesDf["retractionDate"] = modifiedOn deleteSamplesDf["center"] = self.center return deleteSamplesDf
def string_to_unix_epoch_time_milliseconds(string_time): """ Takes dates in this format: 2018-10-25T20:16:07.959Z and turns it into unix epoch time in milliseconds Args: string_time: string in this format: 2018-10-25T20:16:07.959Z Returns: unix epoch time in milliseconds """ datetime_obj = datetime.datetime.strptime( string_time.split(".")[0], "%Y-%m-%dT%H:%M:%S") return to_unix_epoch_time(datetime_obj)