Esempio n. 1
0
def add_link_to_dashboard(source, target, target_path):
    if not dashboard.dash_found:
        return

    logger.debug('Creating database entry linking {} to {}.'.format(
            source, target))
    if DRYRUN:
        return

    target_record = dashboard.get_scan(target)
    if target_record:
        # Already in database, no work to do.
        return

    db_source = dashboard.get_scan(source)
    if not db_source:
        logger.error("Source scan {} not found in dashboard database. "
                "Can't create link {}".format(source, target))
        return

    try:
        dashboard.add_scan(target, source_id=db_source.id)
    except Exception as e:
        logger.error("Failed to add link {} to dashboard database. "
                "Reason: {}. Removing link from file system to re-attempt "
                "later.".format(target, str(e)))
        if not target_path:
            # No link was made
            return
        try:
            os.remove(target_path)
        except:
            logger.error("Failed to clean up link {}".format(target_path))
Esempio n. 2
0
def process_scan(ident, file_stem, tags, tag):
    if not db_ignore:
        logger.info("Adding scan {} to dashboard".format(file_stem))
        try:
            dashboard.get_scan(file_stem, create=True)
        except datman.scanid.ParseException as e:
            logger.error("Failed adding scan {} to dashboard with "
                         "error: {}".format(file_stem, e))

    try:
        blacklist_entry = datman.utils.read_blacklist(scan=file_stem,
                                                      config=cfg)
    except datman.scanid.ParseException:
        logger.error("{} is not a datman ID. Skipping.".format(file_stem))
        return

    if blacklist_entry:
        logger.warn("Skipping export of {} due to blacklist entry '{}'".format(
            file_stem, blacklist_entry))
        return

    try:
        export_formats = tags.get(tag)['formats']
    except KeyError:
        logger.error("Export settings for tag: {} not found for "
                     "study: {}".format(tag, cfg.study_name))
        return

    export_formats = series_is_processed(ident, file_stem, export_formats)
    if not export_formats:
        logger.warn("Scan: {} has been processed. Skipping".format(file_stem))
        return

    return export_formats
Esempio n. 3
0
def update_dashboard(scan_names):
    for file_stem in scan_names:
        logger.info("Adding scan {} to dashboard".format(file_stem))
        try:
            dashboard.get_scan(file_stem, create=True)
        except Exception as e:
            logger.error("Failed adding scan {} to dashboard with "
                         "error: {}".format(file_stem, e))
Esempio n. 4
0
def _fetch_blacklist(
    scan=None,
    subject=None,
    bids_ses=None,
    study=None,
    config=None,
    use_bids=False,
):
    """
    Helper function for 'read_blacklist()'. Gets the blacklist contents from
    the dashboard's database
    """
    if not (scan or subject or study or config):
        raise MetadataException(
            "Can't retrieve dashboard blacklist info "
            "without either 1) a scan name 2) a subject "
            "ID 3) a study ID or 4) a datman config "
            "object"
        )

    if scan:
        if use_bids:
            db_scan = dashboard.get_bids_scan(scan)
        else:
            db_scan = dashboard.get_scan(scan)
        if db_scan and db_scan.blacklisted():
            try:
                return db_scan.get_comment()
            except Exception:
                return db_scan.get_comment()
        return

    if subject:
        if use_bids or bids_ses:
            if not bids_ses:
                bids_ses = "01"
            db_subject = dashboard.get_bids_subject(subject, bids_ses, study)
        else:
            db_subject = dashboard.get_subject(subject)
        blacklist = db_subject.get_blacklist_entries()
    else:
        if config:
            study = config.study_name
        db_study = dashboard.get_project(study)
        blacklist = db_study.get_blacklisted_scans()

    entries = {}
    for entry in blacklist:
        if use_bids:
            if not entry.scan.bids_name:
                # Ignore scans without a bids name if bids was requested
                continue
            scan_name = entry.scan.bids_name
        else:
            scan_name = str(entry.scan) + "_" + entry.scan.description

        entries[scan_name] = entry.comment

    return entries
Esempio n. 5
0
def get_scan_db_record(scan_name):
    try:
        scan = dashboard.get_scan(scan_name)
    except Exception as e:
        logger.error("Failed to retrieve dashboard record for {}. "
                     "Reason - {}".format(scan_name, e))
        return None
    return scan
Esempio n. 6
0
def _update_scan_checklist(entries):
    """
    Helper function for 'update_blacklist()'. Updates the dashboard's database.
    """
    try:
        user = dashboard.get_default_user()
    except:
        raise MetadataException(
            "Can't update dashboard QC information without "
            "a default dashboard user defined. Please add "
            "'DEFAULT_DASH_USER' to your config file.")

    for scan_name in entries:
        scan = dashboard.get_scan(scan_name)
        if not scan:
            raise MetadataException("{} does not exist in the dashboard "
                                    "database".format(scan_name))
        scan.add_checklist_entry(user.id,
                                 comment=entries[scan_name],
                                 sign_off=False)
Esempio n. 7
0
def _fetch_blacklist(scan=None, subject=None, study=None, config=None):
    """
    Helper function for 'read_blacklist()'. Gets the blacklist contents from
    the dashboard's database
    """
    if not (scan or subject or study or config):
        raise MetadataException(
            "Can't retrieve dashboard blacklist info "
            "without either 1) a scan name 2) a subject ID 3) a study ID or "
            "4) a datman config object")

    if scan:
        db_scan = dashboard.get_scan(scan)
        if db_scan and db_scan.blacklisted():
            try:
                return db_scan.get_comment().encode('utf-8')
            except:
                return db_scan.get_comment()
        return

    if subject:
        db_subject = dashboard.get_subject(subject)
        blacklist = db_subject.get_blacklist_entries()
    else:
        if config:
            study = config.study_name
        db_study = dashboard.get_project(study)
        blacklist = db_study.get_blacklisted_scans()

    entries = {}
    for entry in blacklist:
        scan_name = str(entry.scan) + "_" + entry.scan.description
        try:
            scan_name = scan_name.encode('utf-8')
            comment = entry.comment.encode('utf-8')
        except:
            comment = entry.comment
        entries[scan_name] = comment

    return entries
Esempio n. 8
0
def process_subject(subject, cfg, be, bids_dir, rewrite):
    """
    Convert subject in DATMAN folder to BIDS-style
    """

    ident = scanid.parse(subject)
    subscan = scan.Scan(subject, cfg)
    bids_sub = ident.get_bids_name()
    bids_ses = ident.timepoint
    exp_path = make_bids_template(bids_dir, "sub-" + bids_sub,
                                  "ses-" + bids_ses)

    dm_to_bids = []

    if dashboard.dash_found:
        db_subject = dashboard.get_subject(subject)
        db_subject.add_bids(bids_sub, bids_ses)

    # Construct initial BIDS transformation info
    scan_list = list(sort_by_series(subscan.niftis))
    for i, series in enumerate(scan_list):

        # Construct bids name
        logger.info("Processing {}".format(series))
        bids_dict = get_tag_bids_spec(cfg, series.tag, series.site)
        if not bids_dict:
            continue
        bids_dict.update({"sub": bids_sub, "ses": bids_ses})

        # Deal with reference scans
        if bids_dict.get('is_ref', False):
            target_dict = get_tag_bids_spec(cfg, scan_list[i + 1].tag,
                                            series.site)
            bids_dict.update({'task': target_dict['task']})

        bids_prefix = be.construct_bids_name(bids_dict)
        class_path = os.path.join(exp_path, bids_dict["class"])

        # Make dm2bids transformation file, update source if applicable
        bidsfiles = BIDSFile(bids_sub, bids_ses, series, class_path,
                             bids_prefix, bids_dict).update_source(cfg, be)

        if bidsfiles is None:
            logger.error("Cannot find derivative of {}".format(series))
            logger.warning("Skipping!")
            continue

        if isinstance(bidsfiles, list):
            dm_to_bids.extend(bidsfiles)
        else:
            dm_to_bids.append(bidsfiles)

    # Apply prioritization calls
    dm_to_bids = prioritize_scans(dm_to_bids)

    # Prepare fieldmap information (requires knowledge about all scans)
    dm_to_bids = prepare_fieldmaps(dm_to_bids)

    # Transfer files over
    for k in dm_to_bids:
        if os.path.exists(k.dest_nii) and not rewrite:
            logger.info("Output file {} already exists!".format(k.dest_nii))
            continue
        k.transfer_files()
        if dashboard.dash_found:
            db_series = dashboard.get_scan(k.series.path)
            db_series.add_bids(str(k))

    return