Python settings.BASE_DIR Examples

The following are 10 code examples of settings.BASE_DIR(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module settings , or try the search function .
Example #1
Source File: utils.py    From MobileSF with GNU General Public License v3.0 6 votes vote down vote up
def createUserConfig(MobSF_HOME):
    try:
        CONFIG_PATH = os.path.join(MobSF_HOME,'config.py')
        if isFileExists(CONFIG_PATH) == False:
            SAMPLE_CONF = os.path.join(settings.BASE_DIR,"MobSF/settings.py")
            with io.open(SAMPLE_CONF, mode='r', encoding="utf8", errors="ignore") as f:
                dat=f.readlines()
            CONFIG = list()
            add = False
            for line in dat:
                if "^CONFIG-START^" in line:
                    add = True
                if "^CONFIG-END^" in line:
                    break
                if add:
                    CONFIG.append(line.lstrip())
            CONFIG.pop(0)
            COMFIG_STR = ''.join(CONFIG)
            with io.open(CONFIG_PATH, mode='w', encoding="utf8", errors="ignore") as f:
                f.write(COMFIG_STR)
    except:
        PrintException("[ERROR] Cannot create config file") 
Example #2
Source File: utils.py    From MobileSF with GNU General Public License v3.0 6 votes vote down vote up
def PrintException(msg,web=False):
    try:
        LOGPATH=settings.LOG_DIR
    except:
        LOGPATH = os.path.join(settings.BASE_DIR,"logs/")
    if not os.path.exists(LOGPATH):
        os.makedirs(LOGPATH)
    exc_type, exc_obj, tb = sys.exc_info()
    f = tb.tb_frame
    lineno = tb.tb_lineno
    filename = f.f_code.co_filename
    linecache.checkcache(filename)
    line = linecache.getline(filename, lineno, f.f_globals)
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    dat= '\n['+st+']\n'+msg+' ({0}, LINE {1} "{2}"): {3}'.format(filename, lineno, line.strip(), exc_obj)
    if platform.system()=="Windows":
        print dat
    else:
        if web:
            print Color.BOLD + Color.ORANGE + dat + Color.END
        else:
            print Color.BOLD + Color.RED + dat + Color.END
    with open(LOGPATH + 'MobSF.log','a') as f:
        f.write(dat) 
Example #3
Source File: helpers.py    From kaggle_ndsb2 with Apache License 2.0 6 votes vote down vote up
def enumerate_sax_files(patient_ids=None, filter_slice_type="sax"):
    for sub_dir in ["train", "validate", "test"]:
        for root, _, files in os.walk(settings.BASE_DIR + "\\data_kaggle\\" + sub_dir):
            # print root
            for file_name in files:
                if file_name.endswith(".dcm"):

                    parts = root.split('\\')
                    patient_id = parts[len(parts) - 3]
                    slice_type = parts[len(parts) - 1]
                    if filter_slice_type not in slice_type:
                        continue

                    if patient_ids is not None:
                        if patient_id not in patient_ids:
                            #print "skip " + patient_id
                            continue

                    file_path = root + "\\" + file_name
                    dicom_data = DicomWrapper(root + "\\", file_name)
                    yield dicom_data 
Example #4
Source File: step1_preprocess_luna16.py    From kaggle_ndsb2017 with MIT License 6 votes vote down vote up
def process_lidc_annotations(only_patient=None, agreement_threshold=0):
    # lines.append(",".join())
    file_no = 0
    pos_count = 0
    neg_count = 0
    all_lines = []
    for anno_dir in [d for d in glob.glob("resources/luna16_annotations/*") if os.path.isdir(d)]:
        xml_paths = glob.glob(anno_dir + "/*.xml")
        for xml_path in xml_paths:
            print(file_no, ": ",  xml_path)
            pos, neg, extended = load_lidc_xml(xml_path=xml_path, only_patient=only_patient, agreement_threshold=agreement_threshold)
            if pos is not None:
                pos_count += len(pos)
                neg_count += len(neg)
                print("Pos: ", pos_count, " Neg: ", neg_count)
                file_no += 1
                all_lines += extended
            # if file_no > 10:
            #     break

            # extended_line = [nodule_id, x_center_perc, y_center_perc, z_center_perc, diameter_perc, malignacy, sphericiy, margin, spiculation, texture, calcification, internal_structure, lobulation, subtlety ]
    df_annos = pandas.DataFrame(all_lines, columns=["patient_id", "anno_index", "coord_x", "coord_y", "coord_z", "diameter", "malscore", "sphericiy", "margin", "spiculation", "texture", "calcification", "internal_structure", "lobulation", "subtlety"])
    df_annos.to_csv(settings.BASE_DIR + "lidc_annotations.csv", index=False) 
Example #5
Source File: pedigree_image_utils.py    From seqr with GNU Affero General Public License v3.0 5 votes vote down vote up
def _update_pedigree_image(family, project_guid=None):
    """Uses HaploPainter to (re)generate the pedigree image for the given family.

    Args:
         family (object): seqr Family model.
    """

    individual_records = _get_parsed_individuals(family, project_guid)
    if not individual_records:
        return

    # run HaploPainter to generate the pedigree image
    png_file_path = os.path.join(tempfile.gettempdir(), "pedigree_image_%s.png" % _random_string(10))
    family_id = family.family_id
    with tempfile.NamedTemporaryFile('w', suffix=".fam", delete=True) as fam_file:

        # columns: family, individual id, paternal id, maternal id, sex, affected
        for i in individual_records.values():
            row = [family_id] + [i[key] for key in ['individualId', 'paternalId', 'maternalId', 'sex', 'affected']]
            fam_file.write("\t".join(row))
            fam_file.write("\n")
        fam_file.flush()

        fam_file_path = fam_file.name
        haplopainter_command = "perl " + os.path.join(BASE_DIR, "seqr/management/commands/HaploPainter1.043.pl")
        haplopainter_command += " -b -outformat png -pedfile {fam_file_path} -family {family_id} -outfile {png_file_path}".format(
            fam_file_path=fam_file_path, family_id=family_id, png_file_path=png_file_path)
        os.system(haplopainter_command)

    if not os.path.isfile(png_file_path):
        logger.error("Failed to generated pedigree image for family: %s" % family_id)
        family.pedigree_image = None
        family.save()
        return

    _save_pedigree_image_file(family, png_file_path)

    os.remove(png_file_path) 
Example #6
Source File: utils.py    From MobileSF with GNU General Public License v3.0 5 votes vote down vote up
def getMobSFHome(useHOME):
    try:
        MobSF_HOME = ""
        if useHOME:
            MobSF_HOME = os.path.join(os.path.expanduser('~'),".MobSF")
            #MobSF Home Directory
            if not os.path.exists(MobSF_HOME):
                os.makedirs(MobSF_HOME)
            createUserConfig(MobSF_HOME)
        else:
            MobSF_HOME = settings.BASE_DIR
        #Logs Directory
        LOG_DIR=os.path.join(MobSF_HOME, 'logs/')
        if not os.path.exists(LOG_DIR):
            os.makedirs(LOG_DIR)
        #Certs Directory
        CERT_DIR=os.path.join(LOG_DIR, 'certs/')
        if not os.path.exists(CERT_DIR):
            os.makedirs(CERT_DIR)
        #Download Directory
        DWD_DIR=os.path.join(MobSF_HOME, 'downloads/')
        if not os.path.exists(DWD_DIR):
            os.makedirs(DWD_DIR)
        #Screenshot Directory
        SCREEN_DIR = os.path.join(DWD_DIR, 'screen/')
        if not os.path.exists(SCREEN_DIR):
            os.makedirs(SCREEN_DIR)
        #Upload Directory
        UPLD_DIR=os.path.join(MobSF_HOME, 'uploads/')
        if not os.path.exists(UPLD_DIR):
            os.makedirs(UPLD_DIR)
        return MobSF_HOME
    except:
        PrintException("[ERROR] Creating MobSF Home Directory") 
Example #7
Source File: utils.py    From MobileSF with GNU General Public License v3.0 5 votes vote down vote up
def Migrate(BASE_DIR):
    try:
        manage = os.path.join(BASE_DIR,"manage.py")
        args = ["python", manage, "migrate"]
        subprocess.call(args)
    except:
        PrintException("[ERROR] Cannot Migrate") 
Example #8
Source File: index_handler.py    From k8sMG with GNU General Public License v3.0 5 votes vote down vote up
def on_message(self, message):
        print('get_msg--->',message)
        #第一次进来先读取所有
        task_id = message
        try:
            out_file = '%s/logs/%s.log'%(BASE_DIR,task_id)
            check_exit = os.path.exists(out_file)
            if not check_exit:
                open(out_file,'a').close()
            stdout_file = open(out_file)
            stdout_file.seek(os.path.getsize(out_file))
            if os.path.getsize(out_file) > 10240000:
                self.write_message('file is too large!')
            else:
                with open(out_file,'r') as f:
                    for line in f.readlines():
                        self.write_message(line)
            if task_id in LISTENERS:
                LISTENERS[task_id]['ele'].append(self)
            else:
                LISTENERS[task_id] = {
                    'ele':[self],
                    'stdout_file': stdout_file
                }
            self.task_id = task_id
        except Exception as e:
            self.write_message(str(e)) 
Example #9
Source File: test_controller.py    From personfinder with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    # TODO(nworden): see if there's a way to avoid this. You'd think
    # settings.BASE_DIR would be useful here but I can't figure out how to make
    # it work for prod, local servers, and tests without overriding the value in
    # tests.
    # The Django test client doesn't actually run a whole server, which is
    # really nice because it's much faster, but it does seem to mess with the
    # template loader, I guess because it's not running from where it normally
    # would (in the app directory).
    settings.TEMPLATES[0]['DIRS'] = ['app/resources']
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
    django.setup()
    setup_test_environment()
    self.client = Client() 
Example #10
Source File: step2_train_mass_segmenter.py    From kaggle_ndsb2017 with MIT License 4 votes vote down vote up
def predict_patients(patients_dir, model_path, holdout, patient_predictions, model_type):
    model = get_unet(0.001)
    model.load_weights(model_path)
    for item_name in os.listdir(patients_dir):
        if not os.path.isdir(patients_dir + item_name):
            continue
        patient_id = item_name

        if holdout >= 0:
            patient_fold = helpers.get_patient_fold(patient_id, submission_set_neg=True)
            if patient_fold < 0:
                if holdout != 0:
                    continue
            else:
                patient_fold %= 3
                if patient_fold != holdout:
                    continue

        # if "100953483028192176989979435275" not in patient_id:
        #     continue
        print(patient_id)
        patient_dir = patients_dir + patient_id + "/"
        mass = 0
        img_type = "_i" if model_type == "masses" else "_c"
        slices = glob.glob(patient_dir + "*" + img_type + ".png")
        if model_type == "emphysema":
            slices = slices[int(len(slices) / 2):]
        for img_path in slices:
            src_img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
            src_img = cv2.resize(src_img, dsize=(settings.SEGMENTER_IMG_SIZE, settings.SEGMENTER_IMG_SIZE))
            src_img = prepare_image_for_net(src_img)
            p = model.predict(src_img, batch_size=1)
            p[p < 0.5] = 0
            mass += p.sum()
            p = p[0, :, :, 0] * 255
            # cv2.imwrite(img_path.replace("_i.png", "_mass.png"), p)
            src_img = src_img.reshape((settings.SEGMENTER_IMG_SIZE, settings.SEGMENTER_IMG_SIZE))
            src_img *= 255
            # src_img = cv2.cvtColor(src_img.astype(numpy.uint8), cv2.COLOR_GRAY2BGR)
            # p = cv2.cvtColor(p.astype(numpy.uint8), cv2.COLOR_GRAY2BGRA)
            src_img = cv2.addWeighted(p.astype(numpy.uint8), 0.2, src_img.astype(numpy.uint8), 1 - 0.2, 0)
            cv2.imwrite(img_path.replace(img_type + ".png", "_" + model_type + "o.png"), src_img)

        if mass > 1:
            print(model_type + ": ", mass)
        patient_predictions.append((patient_id, mass))
        df = pandas.DataFrame(patient_predictions, columns=["patient_id", "prediction"])
        df.to_csv(settings.BASE_DIR + model_type + "_predictions.csv", index=False)