def genRandomTree(): tree_size = 1000 - TreeItem.objects.count() loaded = 0 if tree_size > 0: loaded = loadMultiSprings(tree_size) leaves = Structure.objects.all() # только те элементы, которые еще не в дереве filtered = [leaf.item_id for leaf in leaves] elements = TreeItem.objects.all() inserted = len(elements) - len(leaves) if inserted >0: # создаем корневой элемент if len(leaves) == 0: root = Structure() root.item_id = elements[0] root.item_parent = elements[0] root.save() pointer = root else: pointer = leaves[0] options = { 0: reset, 1: sibling, 2: child } for s in elements[1:]: if s not in filtered: pointer = options[randint(0,2)](pointer, s) return tree_size, loaded, inserted
def parse_ct(self, ct_filepath, transcript_id, experiment_config): structure = None n_structs = 0 with open(ct_filepath) as ct_file: for line in ct_file: # if it's an energy line, we're looking at a brand new structure # the .ct format is a bit annoying because it's not tab delimited. # instead it's delimited by variable numbers of spaces. # calling split() with no parameter makes it split on any length # of whitespace - i.e. so that each element is 1 word # from_pos = bits[0] bits = line.strip().split() if len(bits) != 6: # brand new structure # save existing structure to DB if structure != None: db_session.add(structure) # Parse the energy out using regex search = re.search("ENERGY = (-[0-9\.]+)", line) if search == None: # No energy data - for some reason this happens for some structures. # If this happens, just ignore the entire ct file by returning return energy = search.group(1) # Create the new structure object, we'll commit it later... structure = Structure( structure_prediction_run_id=experiment_config["structure_prediction_run_id"], transcript_id=transcript_id, energy=energy, ) # insert the experiment into the DB. can now access ID db_session.commit() n_structs += 1 else: to_pos = bits[4] structure.add_value(to_pos) db_session.add(structure) db_session.commit() # insert remaining data into DB print("[" + str(n_structs) + "] structures added")
def parse_ct(self, ct_filepath, transcript_id, experiment_config): structure = None n_structs = 0 with open(ct_filepath) as ct_file: for line in ct_file: # if it's an energy line, we're looking at a brand new structure # the .ct format is a bit annoying because it's not tab delimited. # instead it's delimited by variable numbers of spaces. # calling split() with no parameter makes it split on any length # of whitespace - i.e. so that each element is 1 word # from_pos = bits[0] bits = line.strip().split() if len(bits) != 6: # brand new structure # save existing structure to DB if structure != None: db_session.add(structure) # Parse the energy out using regex search = re.search('ENERGY = (-[0-9\.]+)', line) if search == None: # No energy data - for some reason this happens for some structures. # If this happens, just ignore the entire ct file by returning return energy = search.group(1) # Create the new structure object, we'll commit it later... structure = Structure( structure_prediction_run_id=experiment_config["structure_prediction_run_id"], transcript_id=transcript_id, energy=energy ) # insert the experiment into the DB. can now access ID db_session.commit() n_structs += 1 else: to_pos = bits[4] structure.add_value(to_pos) db_session.add(structure) db_session.commit() # insert remaining data into DB print ("["+str(n_structs)+"] structures added")
class StructureTests(unittest.TestCase): """Tests for Structure""" def setUp(self): self.structure=Structure('001') def test_unicode(self): self.assertEqual(str(self.structure), 'Structure 001') def test_add_node(self): self.node1=Node2d('001',0.0,0.0) self.structure.addNode(self.node1) self.assertEqual(self.structure.nodeCount, 1)
def structures(request, view_pk, depth, structure_type_pk): if request.method == 'GET': return render(request, 'structures.json', {"structures": Structure.objects.filter(slice_location = depth, structure_type__pk = structure_type_pk, view__pk = view_pk)}) else: new_structure = json.loads(request.body) s = Structure(slice_location = depth, view = View.objects.get(pk = view_pk), structure_type = StructureType.objects.get(pk = structure_type_pk), json = new_structure["features"][0]['geometry']['coordinates']) s.save() return HttpResponse(status=204)
def init_db(): db.create_all() if not Structure.query.get(2004): asset_safety = Structure( id=2004, name='Asset Safety', system_id=None, corporation_id=None, ) db.session.add(asset_safety) db.session.commit()
def setUp(self): self.stiffness = 10.0 self.original_length = 0.9 self.spacing = 1.0 self.mass = 1.0 #set up the structure self.structure = Structure("001") for i in range(10): self.structure.addNode( MassedNode2d("%s" % str(i + 1), (self.spacing * i), 0.0, self.mass)) for i in range(9): self.structure.addElement( Spring2d("%s" % str(i + 1), self.structure.n[i], self.structure.n[i + 1], self.stiffness, self.original_length)) self.structure.n[0].cx = True self.structure.n[0].cy = True self.structure.n[9].cx = True self.structure.n[9].cy = True
def get_location_multi(character, location_id_list, raise_on_missing=True): station_id_list = [] structure_id_list = [] system_id_list = [] for location_id in location_id_list: if 60000000 <= location_id < 64000000: # station station_id_list.append(location_id) elif 30000000 < location_id < 32000000: # system system_id_list.append(location_id) elif location_id > 50000000: # structure structure_id_list.append(location_id) elif location_id == 2004: structure_id_list.append(location_id) elif raise_on_missing: raise BadLocationError( 'location_id {} does not correspond to station' ', system, or structure'.format(location_id)) location_dict = {} location_dict.update(Station.get_multi(station_id_list)) location_dict.update(System.get_multi(system_id_list)) location_dict.update(Structure.get_multi(character, structure_id_list)) return location_dict
def setUp(self): self.stiffness = 10.0 self.original_length = 0.9 self.spacing = 1.0 self.mass = 1.0 # set up the structure self.structure = Structure("001") for i in range(10): self.structure.addNode(MassedNode2d("%s" % str(i + 1), (self.spacing * i), 0.0, self.mass)) for i in range(9): self.structure.addElement( Spring2d( "%s" % str(i + 1), self.structure.n[i], self.structure.n[i + 1], self.stiffness, self.original_length, ) ) self.structure.n[0].cx = True self.structure.n[0].cy = True self.structure.n[9].cx = True self.structure.n[9].cy = True
def setUp(self): self.structure=Structure('001')
def child(current, add): new_ = Structure() new_.item_id = add new_.item_parent = current.item_id new_.save() return new_
def sibling(current, add): new_ = Structure() new_.item_id = add new_.item_parent = current.item_parent new_.save() return new_
def reset(current, add): new_ = Structure() new_.item_id = add new_.item_parent = add new_.save() return new_
class ParticleSpringSystemAlgorithmTests(unittest.TestCase): """Tests for Particle Spring System algorithm""" def setUp(self): self.stiffness = 10.0 self.original_length = 0.9 self.spacing = 1.0 self.mass = 1.0 # set up the structure self.structure = Structure("001") for i in range(10): self.structure.addNode(MassedNode2d("%s" % str(i + 1), (self.spacing * i), 0.0, self.mass)) for i in range(9): self.structure.addElement( Spring2d( "%s" % str(i + 1), self.structure.n[i], self.structure.n[i + 1], self.stiffness, self.original_length, ) ) self.structure.n[0].cx = True self.structure.n[0].cy = True self.structure.n[9].cx = True self.structure.n[9].cy = True def test_nodeCount(self): self.assertEqual(self.structure.nodeCount, 10) def test_springCount(self): self.assertEqual(self.structure.elementCount, 9) def test_pssResetForce(self): pssResetForce(self.structure) for node in self.structure.n: self.assertEqual(node.rx, 0.0) self.assertEqual(node.ry, 0.0) def test_pssCalculateElementForces_horizontal(self): pssCalculateElementForces(self.structure) for element in self.structure.e: self.assertEqual(element.nx, (self.spacing / self.original_length) * self.stiffness) self.assertEqual(element.ny, 0.0) def test_pssCalculateElementForces_vertical(self): self.displacement = self.spacing * 2.0 self.structure.n[1].y = self.displacement self.assertEqual(self.structure.n[1].x, self.spacing) self.assertEqual(self.structure.n[1].y, self.displacement) self.assertEqual(self.structure.e[0].dx, self.spacing) self.assertEqual(self.structure.e[0].dy, self.displacement) length = self.structure.e[0].length self.assertEqual(length, math.sqrt((self.displacement ** 2) + (self.spacing ** 2))) pssCalculateElementForces(self.structure) force = math.sqrt(self.structure.e[0].nx ** 2 + self.structure.e[0].ny ** 2) self.assertEqual(force, (length / self.original_length) * self.stiffness) def test_pssCalculateAccelerations(self): xforce = 1.0 yforce = 2.0 for node in self.structure.n: node.rx = xforce node.ry = yforce pssCalculateAccelerations(self.structure) for node in self.structure.n: ax = xforce / self.mass ay = yforce / self.mass if node.cx: ax = 0.0 if node.cy: ay = 0.0 self.assertEqual(node.ax, ax) self.assertEqual(node.ay, ay) def test_particleSpringSystemAlgorithm(self): self.assertEqual(start_pss(self.structure, 1.0, 0.0001, 1000000), True)
class ParticleSpringSystemAlgorithmTests(unittest.TestCase): """Tests for Particle Spring System algorithm""" def setUp(self): self.stiffness = 10.0 self.original_length = 0.9 self.spacing = 1.0 self.mass = 1.0 #set up the structure self.structure = Structure("001") for i in range(10): self.structure.addNode( MassedNode2d("%s" % str(i + 1), (self.spacing * i), 0.0, self.mass)) for i in range(9): self.structure.addElement( Spring2d("%s" % str(i + 1), self.structure.n[i], self.structure.n[i + 1], self.stiffness, self.original_length)) self.structure.n[0].cx = True self.structure.n[0].cy = True self.structure.n[9].cx = True self.structure.n[9].cy = True def test_nodeCount(self): self.assertEqual(self.structure.nodeCount, 10) def test_springCount(self): self.assertEqual(self.structure.elementCount, 9) def test_pssResetForce(self): pssResetForce(self.structure) for node in self.structure.n: self.assertEqual(node.rx, 0.0) self.assertEqual(node.ry, 0.0) def test_pssCalculateElementForces_horizontal(self): pssCalculateElementForces(self.structure) for element in self.structure.e: self.assertEqual(element.nx, (self.spacing / self.original_length) * self.stiffness) self.assertEqual(element.ny, 0.0) def test_pssCalculateElementForces_vertical(self): self.displacement = self.spacing * 2.0 self.structure.n[1].y = self.displacement self.assertEqual(self.structure.n[1].x, self.spacing) self.assertEqual(self.structure.n[1].y, self.displacement) self.assertEqual(self.structure.e[0].dx, self.spacing) self.assertEqual(self.structure.e[0].dy, self.displacement) length = self.structure.e[0].length self.assertEqual(length, math.sqrt((self.displacement**2) + (self.spacing**2))) pssCalculateElementForces(self.structure) force = math.sqrt(self.structure.e[0].nx**2 + self.structure.e[0].ny**2) self.assertEqual(force, (length / self.original_length) * self.stiffness) def test_pssCalculateAccelerations(self): xforce = 1.0 yforce = 2.0 for node in self.structure.n: node.rx = xforce node.ry = yforce pssCalculateAccelerations(self.structure) for node in self.structure.n: ax = xforce / self.mass ay = yforce / self.mass if node.cx: ax = 0.0 if node.cy: ay = 0.0 self.assertEqual(node.ax, ax) self.assertEqual(node.ay, ay) def test_particleSpringSystemAlgorithm(self): self.assertEqual(start_pss(self.structure, 1.0, 0.0001, 1000000), True)