Ejemplo n.º 1
0
 def options_clicked(self):
     '''
     Read in json, modify, write it read it in as dict again!
     '''
     with open(self.options_file_path) as json_file:
         test = json.load(json_file)
         txt = prettyjson(test)
     text = easygui.textbox("Contents of file:\t" + self.options_file_path,
                            "Adjust your options", txt)
     if text is not None:  # Cancel pressed (None case)
         dict = (json.loads(text))
         with open(self.options_file_path, 'w') as outfile:
             json.dump(dict, outfile)
def dump_reference_json():
    sys.path.insert(0, join(root, "build/"))
    sys.path.insert(0, join(root, "tests/"))
    from rascal.representations import SphericalInvariants
    from ase.io import read

    np.random.seed(10)
    fns = [
        "diamond_2atom_distorted.json",
        "CaCrP2O7_mvc-11955_symmetrized.json",
        "methane.json",
    ]
    soap_types = ["PowerSpectrum"]
    Nselects = ["all", "all_random", "8_random"]

    sparsification_inputs = []
    for fn, soap_type, Nselect in product(fns, soap_types, Nselects):
        frames = read(join(inputs_path, fn), ":")

        hypers = dict(
            soap_type=soap_type,
            interaction_cutoff=3.5,
            max_radial=2,
            max_angular=2,
            gaussian_sigma_constant=0.4,
            gaussian_sigma_type="Constant",
            cutoff_smooth_width=0.5,
            normalize=False,
            compute_gradients=True,
            expansion_by_species_method="structure wise",
        )

        soap = SphericalInvariants(**hypers)
        managers = soap.transform(frames)
        hyp = deepcopy(hypers)

        # select some features from the possible set
        mapping = soap.get_feature_index_mapping(managers)
        selected_features = {key: [] for key in mapping[0].keys()}
        ids = np.array([key for key in mapping.keys()])
        if Nselect == "all":
            pass
        elif Nselect == "all_random":
            np.random.shuffle(ids)
        elif Nselect == "8_random":
            np.random.shuffle(ids)
            ids = ids[:8]
        else:
            raise NotImplementedError()
        for idx in ids:
            coef_idx = mapping[idx]
            for key in selected_features.keys():
                selected_features[key].append(int(coef_idx[key]))
        # selected_features_global_ids is important for the tests
        selected_features["selected_features_global_ids"] = ids.tolist()
        mapp = dict(coefficient_subselection=selected_features)

        hyp.update(mapp)

        soap_s = SphericalInvariants(**hyp)
        managers_s = soap_s.transform(frames)

        sparsification_inputs.append(
            dict(
                hypers=dict(
                    rep=soap.hypers,
                    rep_sparse=soap_s.hypers,
                    adaptors=json.loads(managers_s.managers.get_parameters()),
                ),
                filename=join(read_inputs_path, fn),
                Nselect=Nselect,
            ))

    fn_out = join(root, dump_path, "sparsification_inputs.json")
    print(fn_out)
    with open(fn_out, "w") as f:
        sparsification_inputs_pretty = prettyjson(sparsification_inputs,
                                                  indent=2,
                                                  maxlinelength=80)
        f.write(sparsification_inputs_pretty)
Ejemplo n.º 3
0
def test():
    txt = """{
            "grid": {"port": "COM5"},
            "policy": {
                "movingaverage": 5,
                "hysteresis": 5,
                "fan1": {
                    "name": "fan1", "signal": "cpu", "mode": "auto", "speed": 100,
                    "curve": [[0, 75], [65, 75], [75, 100]]
                },
                "fan2": {
                    "name": "fan2", "signal": "gpu", "mode": "auto", "speed": 100,
                    "curve": [[0, 75], [65, 75], [75, 100]]
                }
            },
            "signals": {
                "cpu": {
                    "fn": "max",
                    "sensors": [
                        "/intelcpu/0, CPU Core #1",
                        "/intelcpu/0, CPU Core #2",
                        "/intelcpu/0, CPU Core #3",
                        "/intelcpu/0, CPU Core #4"
                    ]
                },
                "gpu": {
                    "fn": "max", "sensors": ["/nvidiagpu/0"]
                },
                "sys": {
                    "fn": "max",
                    "sensors": ["/intelcpu/0", "/nvidiagpu/0"]
                }
            },
            "app": {
                "startwithwindows": true, "startminimized": false, "closetotray": true
            }
        }"""
    obj = json.loads(txt)
    txt = prettyjson(obj, indent=2, maxlinelength=70)
    print("demo = " + txt)
    print()
    print()

    txt = """{
    }"""
    obj = json.loads(txt)
    txt = prettyjson(obj, maxlinelength=80)
    print("empty = " + txt)
    print()
    print()

    txt = """{
        "empty_1d": [],
        "empty_2d": [[],[]],
        "empty_dict_1d": {},
        "empty_dict_2d": {"a":{},"b":{}},
        "halfempty_2d": [[223, "hello"], []],
        "string_value": "this is a string",
        "int_value": 5,
        "float_value": 223.45e3,
        "float_array_1d": [0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532, 0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532, 0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532],
        "floats_2d_1": [
            [0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14],
            [0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14],
            [0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14]
        ],
        "floats_2d_2": [
            [0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532, 0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532, 0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532],
            [0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532, 0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532, 0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532],
            [0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532, 0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532, 0.5543, 2.33576, 0.32333455, 1.2345663, -123.445677, 5.2234e14, 84492033, -1229340.2234, 12234.5532]
        ],
        "floats_dict_1d": { "val1":  0.5543, "val2": 2.33576, "val3":  0.32333455, "val4": 1.2345663, "val5": -123.445677, "val6": 5.2234e14 },
        "floats_dict_2d": {
            "params1": { "val1": 0.5543, "val2": 2.33576, "val3":  0.32333455, "val4": 1.2345663, "val5": -123.445677, "val6": 5.2234e14 },
            "params2": { "val1": 0.5543, "val2": 2.33576, "val3":  0.32333455, "val4": 1.2345663, "val5": -123.445677, "val6": 5.2234e14 },
            "params3": { "val1": 0.5543, "val2": 2.33576, "val3":  0.32333455, "val4": 1.2345663, "val5": -123.445677, "val6": 5.2234e14 }
        },
        "strings_1d_1": [
            "Lorem ipsum dolor sit amet",
            "Lorem ipsum dolor sit amet",
            "Lorem ipsum dolor sit amet"
        ],
        "strings_1d_2": [
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
        ],
        "strings_1d_3": [
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        ],
        "strings_1d_4": [
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        ]
    }"""
    obj = json.loads(txt)
    txt = prettyjson(obj, maxlinelength=80)
    print("test = " + txt)
Ejemplo n.º 4
0
 def getjson(self):
     return prettyjson(self.settings, maxlinelength=45)
Ejemplo n.º 5
0
def main():
	parser = argparse.ArgumentParser(
		usage='%(prog)s [command][options]',
		description="Configure Gadget Device using SysFS and ConfigFS",
		formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=999))

	# parser.add_argument("-L", "--conf", nargs='?', help="include (shell pattern match)", default='')
	# parser.add_argument("paths", metavar='Path', type=str, nargs=argparse.REMAINDER, help="pathname", default=[])

	# parser.add_argument("-D", "--disable", help="Disable the current Gadget", action='store_true')
	# parser.add_argument("path", metavar='path', nargs='?', type=str, help="pathname", default=None)
	# parser.add_argument("-A", "--attr", help="Dump Scheme Attr", action='store_true')
	# parser.add_argument("-2", "--scheme2json", help="Scheme Attr to JSON", action='store_true')
	# parser.add_argument("-S", "--scheme", help="Scheme Attr to JSON", action='store_true')

	group = parser.add_mutually_exclusive_group(required=False)

	group.add_argument("--export", help="Export JSON to STDOUT", action='store_true')
	group.add_argument("--add", type=str, help="Add Gadget device definitions from JSON file", default=None)
	group.add_argument("--remove", type=str, help="Remove Named Gadget device definition\n \n", default=None)
	# group.add_argument("-R", "--remove", nargs='?', type=str, help='Remove Gadget device definition', default=None)

	group.add_argument("--enable", type=str, help="enable specified Gadget Device", default=None)
	group.add_argument("--disable", help="disable currently enabled Gadget Device", action='store_true')
	group.add_argument("--query-gadget", help="display currently enabled Gadget Device", action='store_true')
	group.add_argument("--query-gadgets", help="display current Gadget Devices\n \n", action='store_true')
	group.add_argument("--query-gadget-functions", help="display current Gadgets functions\n \n", action='store_true')

	parser.add_argument("--soft-disconnect", help="Disconnect or detach the UDC", action='store_true')
	parser.add_argument("--soft-connect", help="Connect or attach the UDC", action='store_true')
	#group.add_argument("--query-soft-connect", help="display current UDC Soft-Connect status", action='store_true')
	group.add_argument("--query-udc", help="display current UDC function and state", action='store_true')
	group.add_argument("--query-udc-state", help="display current UDC state", action='store_true')
	group.add_argument("--query-udc-function", help="display current UDC function\n \n", action='store_true')

	# group.add_argument("path", metavar='path', nargs='?', type=str, help="Gadget Device Scheme", default=None)

	parser.add_argument("--idVendor", type=str, help="Optional device idVendor attribute")
	parser.add_argument("--idProduct", type=str, help="Optional device idProduct attribute")

	parser.add_argument("--manufacturer", type=str, help="Optional device manufacturer string")
	parser.add_argument("--product", type=str, help="Optional device product string")
	parser.add_argument("--serialnumber", type=str, help="Optional device serialnumber string")

	parser.add_argument("--dev_addr", type=str, help="Optional ecm dev_addr attribute")
	parser.add_argument("--host_addr", type=str, help="Optional ecm host_addr attribute")

	parser.add_argument("--name", nargs='?', type=str, help='device name override', default=None)
	# parser.add_argument("-I", "--id", nargs='?', type=int, help='enable ID', default=1)
	parser.add_argument("--test", action='store_true')

	args = parser.parse_args()

	# print("args: %s" % (args), file=sys.stderr)
	# print("", file=sys.stderr)

	if args.test:
		sys_config_path = "sys/kernel/config/usb_gadget"
	else:
		sys_config_path = "/sys/kernel/config/usb_gadget"

	m = ManageGadget(sys_config_path)
	if args.query_gadget:
		print("Currently configured: %s" % (m.query_gadget_verbose()), file=sys.stderr)
		exit(0)
	if args.query_gadgets:
		print("Currently defined gadgets: %s" % (m.query_gadgets()), file=sys.stderr)
		exit(0)
	# if args.query_soft_connect:
	# 	print("Soft-Connect: %s" % (m.query_soft_connect()))
	# 	exit(0)
	if args.query_udc:
		print("UDC State: %s" % (m.query_udc_state()), file=sys.stderr)
		print("UDC Function: %s" % (m.query_udc_function()), file=sys.stderr)
		exit(0)

	if args.query_udc_state:
		print("UDC State: %s" % (m.query_udc_state()), file=sys.stderr)
		exit(0)
	if args.query_udc_function:
		print("UDC Function: %s" % (m.query_udc_function()), file=sys.stderr)
		exit(0)
	if args.query_gadget_functions:
		print("UDC Function: %s" % (m.query_gadget_functions()), file=sys.stderr)
		exit(0)

	m = ManageGadget(sys_config_path, test=args.test)
	#if not m.checkfs(verbose=False):
	#	if not args.test:
	#		exit(1)
	#	os.makedirs("sys/kernel/config/usb_gadget")

	# m.find_udcs(verbose=True)
	# m.check_current(verbose=False)

	if args.disable:
		m.disable_current()
		exit(0)

	if args.enable is not None:
		m.enable_current(args.enable)
		exit(0)

	if args.soft_disconnect:
		m.soft_connect(False)
		exit(0)

	if args.soft_connect:
		m.soft_connect(True)
		exit(0)

	if args.export:
		e = ExportGadget(sys_config_path)
		devices = e.export_devices()
		#j = json.dumps(devices, indent=4)
		j = prettyjson(devices, indent=4, maxlinelength=100)
		print("# Gadget Device Definition File")
		print("# %s" % (date.today()))
		print(re.sub(r'\\\\', r'\\', re.sub(r'"(#.*)":.*,', r'\1', j)))
		print("# %s" % ("vim: syntax=off"))

		exit(0)

	if args.remove is not None:
		r = RemoveGadget(sys_config_path, m)
		r.remove_device(args.remove)
		exit(0)

	if args.add is not None:
		m.add_device_file(args.add, new_device_name=args.name, args=args)

	exit(1)