def todo_file(writeback=True): try: with open(UPLOAD_TODO_FILE, 'rt') as json_file: todo = load(json_file) except (IOError, OSError, ValueError): todo = {} yield todo if writeback: try: with open(UPLOAD_TODO_FILE, 'wt') as json_file: save(todo, json_file) except (OSError, IOError) as save_err: print("Error saving {}:".format(UPLOAD_TODO_FILE), save_err)
def save_json(self, frame, keep): json = JsonCreator(self.boxes) if keep: goal = "goal_OK" else: goal = "goal_KO" json.save(frame, goal, self.conf) del self.boxes[:]
def save_pretrained(self, path_to_save: str): """ Save current configuration :param str path_to_save: String that represents path to the directory where this will be saved. """ with Path(path_to_save, 'OptConfig.json').open('w+t', encoding='UTF-8') as fp: save(self.to_kwargs(), fp)
def setJson(request): if request.method == 'POST': # путь уже содержит папку, нужно брать только название файла, тогда папка не будет дублироваться json = Json.objects.create( name = str(str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+" - "+request.POST.get("name"))) ) json.json.save(request.POST.get("json_name"), ContentFile(request.POST.get("json").encode('utf-8'))) json.results.save(request.POST.get("results_name"), ContentFile(res.encode('utf-8'))) json.save() return HttpResponse(status=200) else: return HttpResponse(status=404)
def save_pretrained(self, path_to_save: str, enforce_path: bool = False): """ Save current configuration :param str path_to_save: String that represents path to the directory where this will be saved. :param bool enforce_path: True if path_to_save specifies the target file. Otherwise, it will generate 'TrainConfig.json' under the specified path. """ path_to_save = Path(path_to_save) if enforce_path else Path( path_to_save, 'TrainConfig.json') with path_to_save.open('w+t', encoding='UTF-8') as fp: save(self.to_kwargs(), fp)
def train(self, tr_x, tr_y, va_x=None, va_y=None, te_x=None): # データのセット validation = va_x is not None lgb_train = optuna_lgb.Dataset( tr_x, tr_y, categorical_feature=self.categorical_features, free_raw_data=False, ) if validation: lgb_eval = optuna_lgb.Dataset( va_x, va_y, reference=lgb_train, categorical_feature=self.categorical_features, free_raw_data=False, ) # ハイパーパラメータの設定 params = dict(self.params) num_round = params.pop("num_boost_round") best_params, tuning_history = dict(), list() # 学習 if validation: early_stopping_rounds = params.pop("early_stopping_rounds") self.model = optuna_lgb.train( params, lgb_train, num_round, valid_sets=[lgb_train, lgb_eval], verbose_eval=1000, early_stopping_rounds=early_stopping_rounds, best_params=best_params, tuning_history=tuning_history, ) else: self.model = optuna_lgb.train( params, lgb_train, num_round, valid_sets=[lgb_train], verbose_eval=1000, best_params=best_params, tuning_history=tuning_history, ) print(f"Best Params: {best_params}") with open( f"{self.optuna_path}/{self.run_fold_name}_best_params.json", "w" ) as f: json.save(best_params, f, indent=4, separators=(",", ": "))
def get_article(): """ Get article from local data store. If not found forward request to the real wikipedia and rectord the result. """ global DATA key = json.dumps(request.args) ret = DATA.get(key) if ret: return ret if "record" in sys.argv: with open(DATAFILE, "w") as fp: ret = urllib.urlopen("http://wikipedia.org/w/index.php?" + urlencode(request.args)).read() DATA[key] = ret json.save(fp, DATA) return ret return None
def index(request): name = '%s %s' % (request.user.first_name, request.user.last_name) organization = request.session['org'] org = models.Organization.objects.get(name=organization) if request.method == 'POST' and request.session['permission'] == 'user': if request.POST['func'] == 'deleteTag': try: tag = models.Tag.objects.get(mac=request.POST['modify_mac']) tag.name = '' tag.level = 0 tag.owner = '' tag.json = None tag.save() except models.TagOrg.DoesNotExist: messages.add_message(request, messages.INFO, '請選擇正確的裝置') except models.Tag.DoesNotExist: messages.add_message(request, messages.INFO, '請選擇正確的裝置') if request.POST['func'] == 'modifyDevice': ifile = '' if request.POST.get('jsonfile', '') == '': if 'jsonfile' in request.FILES: upload = request.FILES['jsonfile'] if upload.name.upper().endswith('.JSON'): try: json = models.Json.objects.get(archive='%s/%s' % (org, upload), organization=org) messages.add_message(request, messages.INFO, '%s 已經存在' % (upload.name)) except models.Json.DoesNotExist: json = models.Json(archive=upload, organization=org) json.save() ifile = upload.name else: messages.add_message(request, messages.INFO, '%s 檔案類型錯誤' % (upload)) else: ifile = request.POST['jsonfile'] try: tag = models.Tag.objects.get(mac=request.POST['modify_mac']) tag_orgs = models.TagOrg.objects.get(tag=tag, organization=org) tag.name = request.POST['dev_name'] tag.level = request.POST['level'] tag.owner = request.POST['keeper'] if ifile == '': tag.json = None else: json = models.Json.objects.get(archive='%s/%s' % (org, ifile), organization=org) tag.json = json tag.save() except models.TagOrg.DoesNotExist: messages.add_message(request, messages.INFO, '請選擇正確的裝置') except models.Tag.DoesNotExist: messages.add_message(request, messages.INFO, '請選擇正確的裝置') if request.POST['func'] == 'addFile': for upload in request.FILES.getlist('file'): err_files = [] if upload.name.upper().endswith('.JSON'): try: json = models.Json.objects.get(archive='%s/%s' % (org, upload), organization=org) err_files.append(upload.name) except models.Json.DoesNotExist: json = models.Json(archive=upload, organization=org) json.save() elif upload.name.upper().endswith('.BMP') or upload.name.upper( ).endswith('.JPG') or upload.name.upper().endswith( '.JPEG') or upload.name.upper().endswith('.PNG'): width = 1200 height = 825 # upload image file try: img = models.Image.objects.get(archive='%s/%s' % (org, upload), organization=org) err_files.append(upload.name) except models.Image.DoesNotExist: im = Image.open(upload) if im.size[0] != width or im.size[1] != height: x = width / im.size[0] y = height / im.size[1] if x > y: nim = im.resize((width, int(im.size[1] * x))) else: nim = im.resize((int(im.size[0] * y), height)) im = nim.crop((0, 0, width, height)) im.save('%s%s/%s' % (settings.MEDIA_ROOT, org, upload)) img = models.Image(archive='%s/%s' % (org, upload), organization=org) img.save() """ # add ImageBin try: img = models.Image.objects.get(archive='%s/%s' % (org, upload), organization=org) img_bin, created = models.ImageBin.objects.get_or_create( image = img ) except models.ImageBin.DoesNotExist: messages.add_message(request, messages.INFO, 'Add ImageBin fail') """ # add Bin try: bin = models.Bin.objects.get(archive='%s/%s.bin' % (org, upload), organization=org) err_files.append('%s.bin' % (upload)) except models.Bin.DoesNotExist: bi = Image.open(upload) pix = bi.load() raw = bytearray(width * height) for h in range(height): for w in range(width): r, g, b = pix[w, h] if r > 0x80 and g < 0x80 and b < 0x80: raw[w + h * width] = 0x80 else: black = 0.3 * r + 0.587 * g + 0.114 * b if black < 0x90: raw[w + h * width] = 0 else: raw[w + h * width] = 0xFF output = open( '%s%s/%s.bin' % (settings.MEDIA_ROOT, org, upload), "wb") output.write(raw) output.close() bin = models.Bin(archive='%s/%s.bin' % (org, upload), organization=org) bin.save() img = models.Image.objects.get(archive='%s/%s' % (org, upload), organization=org) img_bin, created = models.ImageBin.objects.get_or_create( image=img, bin=bin) """ # map Image and Bin try: img = models.Image.objects.get(archive='%s/%s' % (org, upload), organization=org) bin = models.Bin.objects.get(archive='%s/%s.bin' % (org, upload), organization=org) img_bin = models.ImageBin.objects.get(image=img) img_bin.bin = bin img_bin.save() except models.ImageBin.DoesNotExist: messages.add_message(request, messages.INFO, 'Map Image and Bin fail') """ elif upload.name.endswith('.bin'): width = 1200 height = 825 raw = bytearray(upload.read()) # upload bin file try: bin = models.Bin.objects.get(archive='%s/%s' % (org, upload), organization=org) err_files.append(upload.name) except models.Bin.DoesNotExist: if len(raw) == width * height: bin = models.Bin(archive=upload, organization=org) bin.save() """ # add ImageBin try: bin = models.Bin.objects.get(archive='%s/%s' % (org, upload), organization=org) img_bin, created = models.ImageBin.objects.get_or_create( bin = bin ) except models.ImageBin.DoesNotExist: messages.add_message(request, messages.INFO, 'Add ImageBin fail') """ # add Image try: img = models.Image.objects.get(archive='%s/%s.png' % (org, upload), organization=org) err_files.append('%s.png' % (upload)) except models.Image.DoesNotExist: raw = numpy.array(raw) newImage = Image.new('RGB', (width, height), 'White') for h in range(height): for w in range(width): if (raw[w + width * h] == 0): newImage.putpixel( (w, h), ImageColor.getcolor("black", "RGB")) if (raw[w + width * h] == 128): newImage.putpixel( (w, h), ImageColor.getcolor("red", "RGB")) newImage.save('%s%s/%s.png' % (settings.MEDIA_ROOT, org, upload)) img = models.Image(archive='%s/%s.png' % (org, upload), organization=org) img.save() bin = models.Bin.objects.get(archive='%s/%s' % (org, upload), organization=org) img_bin, created = models.ImageBin.objects.get_or_create( bin=bin, image=img) """ # map Image and Bin try: bin = models.Bin.objects.get(archive='%s/%s' % (org, upload), organization=org) img = models.Image.objects.get(archive='%s/%s.png' % (org, upload), organization=org) img_bin = models.ImageBin.objects.get(bin=bin) img_bin.image = img img_bin.save() except models.ImageBin.DoesNotExist: messages.add_message(request, messages.INFO, 'Map Image and Bin fail') """ else: messages.add_message(request, messages.INFO, '%s 檔案類型錯誤' % (upload)) for err in err_files: messages.add_message(request, messages.INFO, '%s 已經存在' % (err)) if request.POST['func'] == 'deleteFile': if request.POST['file'].upper().endswith('.JSON'): try: json = models.Json.objects.get(archive='%s/%s' % (org, request.POST['file']), organization=org) json.delete() messages.add_message(request, messages.SUCCESS, '刪除檔案成功') except: messages.add_message(request, messages.INFO, '刪除檔案失敗') elif request.POST['file'].upper( ).endswith('.BMP') or request.POST['file'].upper( ).endswith('.JPG') or request.POST['file'].upper().endswith( '.JPEG') or request.POST['file'].upper().endswith('.PNG'): try: img = models.Image.objects.get(archive='%s/%s' % (org, request.POST['file']), organization=org) img_bin = models.ImageBin.objects.get(image=img) img_bin.bin.delete() img.delete() messages.add_message(request, messages.SUCCESS, '刪除檔案成功') except models.ImageBin.DoesNotExist: img = models.Image.objects.get(archive='%s/%s' % (org, request.POST['file']), organization=org) img.delete() messages.add_message(request, messages.SUCCESS, '刪除檔案成功') except: messages.add_message(request, messages.INFO, '刪除檔案失敗') elif request.POST['file'].upper().endswith('.BIN'): try: bin = models.Bin.objects.get(archive='%s/%s' % (org, request.POST['file']), organization=org) img_bin = models.ImageBin.objects.get(bin=bin) img_bin.image.delete() bin.delete() messages.add_message(request, messages.SUCCESS, '刪除檔案成功') except models.ImageBin.DoesNotExist: bin = models.Bin.objects.get(archive='%s/%s' % (org, request.POST['file']), organization=org) bin.delete() messages.add_message(request, messages.SUCCESS, '刪除檔案成功') except: messages.add_message(request, messages.INFO, '刪除檔案失敗') if request.POST['func'] == 'modifyFile': if request.POST['file'].upper().endswith('.JSON'): try: json = models.Json.objects.get(archive='%s/%s' % (org, request.POST['file']), organization=org) json.archive.name = '%s/%s.json' % ( org, request.POST['file_name']) json.save() os.chdir(settings.MEDIA_ROOT) os.rename(request.POST['file'], '%s/%s.json' % (org, request.POST['file_name'])) messages.add_message(request, messages.SUCCESS, '修改檔案成功') except: messages.add_message(request, messages.INFO, '修改檔案失敗') elif request.POST['file'].upper( ).endswith('.BMP') or request.POST['file'].upper( ).endswith('.JPG') or request.POST['file'].upper().endswith( '.JPEG') or request.POST['file'].upper().endswith('.PNG'): try: img = models.Image.objects.get(archive='%s/%s' % (org, request.POST['file']), organization=org) img.archive.name = '%s/%s%s' % ( org, request.POST['file_name'], os.path.splitext(request.POST['file'])[1]) img.save() os.chdir(settings.MEDIA_ROOT) os.rename( request.POST['file'], '%s/%s%s' % (org, request.POST['file_name'], os.path.splitext(request.POST['file'])[1])) messages.add_message(request, messages.SUCCESS, '修改檔案成功') """ img_bin = models.ImageBin.objects.get(image=img) img_bin.image.archive.name = '%s/%s%s' % (org, request.POST['name'], img.extension) img_bin.bin.archive.name = '%s/%s%s.bin' % (org, request.POST['name'], img.extension) img_bin.save() os.chdir(settings.MEDIA_ROOT) os.rename(request.POST['file'], '%s/%s%s' % (org, request.POST['name'], img.extension)) os.rename('%s.bin' % (request.POST['file']), '%s/%s%s.bin' % (org, request.POST['name'], img.extension)) """ except: messages.add_message(request, messages.INFO, '修改檔案失敗') elif request.POST['file'].upper().endswith('.BIN'): try: bin = models.Bin.objects.get(archive='%s/%s' % (org, request.POST['file']), organization=org) bin.archive.name = '%s/%s.bin' % ( org, request.POST['file_name']) bin.save() os.chdir(settings.MEDIA_ROOT) os.rename(request.POST['file'], '%s/%s.bin' % (org, request.POST['file_name'])) messages.add_message(request, messages.SUCCESS, '刪除檔案成功') except: messages.add_message(request, messages.INFO, '刪除檔案失敗') return redirect('/') tag_orgs = models.TagOrg.objects.filter(organization=org) jsons = models.Json.objects.filter(organization=org) imgs = models.Image.objects.filter(organization=org) bins = models.Bin.objects.filter(organization=org) files = chain(jsons, imgs, bins) grids = [] for bin in bins: img_bin = models.ImageBin.objects.get(bin=bin) grids.append({"name": bin.basename, "image": img_bin.image.archive}) messages.get_messages(request) return render(request, 'device.html', locals())
def save_param(paramdict, paramfile): """saves parameter dict to JSON""" with open(paramfile, 'rb') as f: json.save(paramdict, f)
from django.core.management.base import BaseCommand, CommandError from django.conf import settings from django.core.management import call_command import json class Command(BaseCommand): args = '' help = 'loads the initial data into database' def handle(self, *args, **options): call_command('loaddata', 'Rainfall-England.json', verbosity=0) result = {'message': "Successfully Loading initial data"} return json.dumps(result) call_command('loaddata', 'Rainfall-England.json', verbosity=0) result = {'message': "Successfully Loading initial data"} json.save(result)
def saveToFile(self, fn): f = open(fn, 'w') json.save(f, self._data) f.close()
def save_state(j): fp = 'work/%s/work.json' % j['id'] with open(fp,'w') as f: return json.save(j, f)
#!/usr/bin/env python2.7
geojson_geotransform[filename] = geo_dict return geojson_geotransform def create_geoparameters_json(self): geoparameters = self.create_geoparameters() json_geoparameters = "%s/geoparameters.json" % self.file_output with open(json_geoparameters, 'w') as json_file: json.dump(my_details, json_file) def create_TFRecord(self): pass create_tf.create_geoparameters_json() create_tf = SEN12MS_geotiff( dir_input = "/home/aybarpc01/Github/Geohormiguitas/data/lc_93/", file_output = "/home/aybarpc01/Github/Geohormiguitas/data/") json.dumps(geoparameters, ensure_ascii=False) json.save(geoparameters,"/home/aybarpc01/Github/Geohormiguitas/data/lc_93/geoparameters.json") dir_input = "/home/aybarpc01/Github/Geohormiguitas/data/lc_93/*.tif" tif_list = glob.glob(path,recursive=True) dasda = SEN12MS_toolkit() parameters = SEN12MS_geotiff.create_geoparameters(dir_input = tiff_path) SEN12MS_geotiff() @classmethod def tf_to_geotiffs(self, tf, geotransform, dir_output = '.'): pass