コード例 #1
0
    def __add_floating_block(self, block):
        """
        Add a floating block to be tracked by the miner. This is a block from a chain with a greater or equal cost.
        If the floating block can be added to an existing chain undergoing chain resolution then it will be; otherwise,
        a new chain will be created to undergo chain resolution.
        :param block: The floating block to be added.
        :return: None if the floating block was added to an already tracked chain; otherwise, the newly created
        chain to undergo chain resolution.
        """
        for chain in self.floating_chains:
            cur = chain.blocks[-1]
            if block.is_valid(cur.hash()):

                logging.debug("Add to existing floating chain")
                block.set_previous_hash(cur.hash())
                chain.add(block)

                if chain.is_complete():
                    self.__receive_complete_chain(chain)
                return None

            elif block in chain.blocks:
                return None

        logging.debug("Create new floating chain")
        chain = Chain()
        chain.add(block)
        self.floating_chains.append(chain)
        return chain
コード例 #2
0
ファイル: sync.py プロジェクト: greatbn/kma-blockchain
def sync_overall(save=False, sync_es=False):
    local_chain = sync_local(sync_es)
    NODES = nodes.get_list_node(mongo_conn)
    for peer in NODES:
        endpoint = peer + '/blockchain'
        try:
            r = requests.get(endpoint, timeout=5)
            chain_data = r.json()
            peer_blocks = [Block(block_dict) for block_dict in chain_data]
            peer_chain = Chain(peer_blocks)
            # check valid , if valid and longer, sync local
            if peer_chain.is_valid() and len(peer_chain) > len(local_chain):
                ## insert new block to elasticsearch
                ## find a new block
                ## m is length of local_chains
                ## n la so block moi can them
                m = len(local_chain)
                n = len(peer_chain) - len(local_chain)

                peer_blocks = peer_chain.blocks
                local_blocks = local_chain.blocks
                for i in range(0, n):
                    block = local_blocks[i + m]
                    block_dict = block.dict()
                    es.insert_document(block_dict)
                local_chain = peer_chain
        except Exception as e:
            pass
    if save:
        local_chain.self_save()
    return local_chain
コード例 #3
0
ファイル: node.py プロジェクト: shengbinmeng/blockchain
def sync_overall(peers, save=False):
    best_chain = local_chain
    for peer in peers:
        # try to connect to peer
        peer_chain_url = peer + '/chain'
        try:
            r = requests.get(peer_chain_url, timeout=5)
            peer_chain_dict = r.json()
            peer_blocks = [Block(block_dict) for block_dict in peer_chain_dict]
            peer_chain = Chain(peer_blocks)

            if peer_chain.is_valid() and peer_chain > best_chain:
                best_chain = peer_chain

        except requests.exceptions.ConnectionError:
            print("Peer at %s not running. Continuing to next peer." % peer)
        except requests.exceptions.Timeout:
            print("Timeout when connecting peer at %s." % peer)
        else:
            print(
                "Peer at %s is running. Gathered their blochchain for analysis."
                % peer)
    print("Longest blockchain has %s blocks" % len(best_chain))

    # for now, save the new blockchain over whatever was there
    if save:
        best_chain.save()
    return best_chain
コード例 #4
0
    def run(self):
        # The main ETL processing
        log.info("START")
        t1 = Util.start_timer("total ETL")

        # Get the ETL Chain pipeline config strings
        # Default is to use the section [etl], but may be overidden on cmd line

        config_section = self.options_dict.get('config_section')
        if config_section is None:
            config_section = 'etl'

        chains_str = self.configdict.get(config_section, 'chains')
        if not chains_str:
            raise ValueError('ETL chain entry not defined in section [etl]')

        # Multiple Chains may be specified in the config
        chains_str_arr = chains_str.split(',')
        for chain_str in chains_str_arr:
            # Build single Chain of components and let it run
            chain = Chain(chain_str, self.configdict)
            chain.assemble2()

            # Run the ETL for this Chain
            chain.run()

        Util.end_timer(t1, "total ETL")

        log.info("ALL DONE")
コード例 #5
0
    def __init__(self, **kwargs):
        self.chain = Chain()
        self.walkers = []
        
        self.times = np.zeros(1)
        self.dt = 1e-7
        self.update_time_unit_(**kwargs)

        self.error = 1e-7
        self.update_error_(**kwargs)

        pop = None
        dat = None

        try:
            pop = kwargs['node_population']
        except: KeyError

        try:
            dat = kwargs['node_data']
        except: KeyError

        self.memory_safe = False
        #self.population = []
        if 'memory_safe' in kwargs.keys():
            self.memory_safe = True

        self.add_nodes_(pop, dat, **kwargs)
コード例 #6
0
ファイル: master.py プロジェクト: Jach/ludum_dare_entries
class Master:

  def __init__(self, pos, screen):

    self.images = [\
       pygame.image.load(path.join('data','master_norm.png')).convert_alpha(),\
       pygame.image.load(path.join('data','master_whip1.png')).convert_alpha(),\
       pygame.image.load(path.join('data','master_whip2.png')).convert_alpha(),\
       pygame.image.load(path.join('data','master_whip3.png')).convert_alpha()]
    if pos == 'right':
      for i in range(len(self.images)):
        self.images[i] = pygame.transform.flip(self.images[i], 1, 0)
    self.image = self.images[0]
    self.rect = self.image.get_rect()
    self.width = self.rect.width
    self.height = self.rect.height
    self.state = 0 # for image
    self.chain = Chain(pos, screen)

    if pos == 'left':
      self.rect = self.rect.move(self.width + 30, 500-self.height)
    if pos == 'right':
      self.rect = self.rect.move(800-94-self.width, 500-self.height)


  def whip(self):
    # Change the state of chain
    self.chain.change()

  def update(self):
    self.state += 1
    if self.state == 4: self.state = 0 # turn over
    self.image = self.images[self.state]
    self.whip()
コード例 #7
0
ファイル: kinematics.py プロジェクト: Izzatbek/freecad-symoro
 def __init__(self, table, base_t=None, tool_t=None):
     self.table = table
     if base_t is None:
         self.base_t = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0],
                        [0, 0, 0, 1]]
     else:
         self.base_t = base_t
     if tool_t is None:
         self.tool_t = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0],
                        [0, 0, 0, 1]]
     else:
         self.tool_t = tool_t
     self.joints = get_joints_from_table(self.table)
     # List of actuated joints
     self.ajoints = self.get_ajoints()
     # List of passive joints
     self.pjoints = self.get_pjoints()
     # Actuated joints' variables
     self.q = self.get_q()
     # Passive joints' variables
     self.qp = self.get_qp()
     # Chains
     self.chain = Chain(self.joints)
     # Tuples (root, end) for each loop
     self.loops = get_loops(self.joints)
     # List of loop solvers
     from loop_solver import LoopSolver
     self.loopsolvers = [LoopSolver(self.joints, *l) for l in self.loops]
コード例 #8
0
ファイル: chain_test.py プロジェクト: jkklapp/blocks
class TestChain(unittest.TestCase):
    """Test basic chain functionality."""
    def setUp(self):
        self.chain = Chain()
        self.first = self.chain.first()

    def test_chain_get_first(self):
        first = self.first
        self.assertEqual(0, first.index)
        self.assertEqual('First block', first.data)
        self.assertEqual("0", first.previous_hash)
        self.assertTrue(len(first.previous_hash), 1)
        self.assertNotEqual(first.previous_hash, first.hash)

    def test_chain_get_next(self):
        first = self.first
        second = self.chain.next(first, "Second block")
        self.assertEqual(first.index + 1, second.index)
        self.assertEqual(second.data, "Second block")

    def test_chain_three_block(self):
        first = self.first
        tA = {'from': 'user1', 'to': 'user2', 'item': 'token', 'q': 10}
        tB = {'from': 'user3', 'to': 'user5', 'item': 'token', 'q': 7}
        tC = {'from': 'user0', 'to': 'user15', 'item': 'token', 'q': 3}
        second = self.chain.next(first, tA)
        third = self.chain.next(second, tB)
        fourth = self.chain.next(third, tC)
        # print first, second, third, fourth
        self.assertIsNotNone(fourth)
コード例 #9
0
 def setUp(self):
     super(ChainTest, self).setUp()
     self.executor = MagicMock()
     self.strategy = MagicMock()
     self.chain = Chain(self.executor, self.strategy,
                        test_util.genesis_hash)
     self.chain.strategy = MagicMock()
コード例 #10
0
ファイル: edit.py プロジェクト: smart-fm/simmobility-prod
    def __init__(self, graphics_item, slot, use_this_polyline=None):
        self.graphics_item = graphics_item
        self.graphics_item.setVisible(False)

        road_item = graphics_item.road_item
        graphics_scene = graphics_item.scene()

        self.balls = list()
        if use_this_polyline:
            polyline = use_this_polyline
        else:
            polyline = road_item.polyline
        for i, point in enumerate(polyline):
            ball = Ball(point.x, point.y, Observer(slot, i))
            ball.is_selectable = False
            graphics_scene.addItem(ball)
            self.balls.append(ball)

        self.chains = list()
        for i in range(len(self.balls) - 1):
            ball1 = self.balls[i]
            ball2 = self.balls[i + 1]
            chain = Chain(ball1, ball2)
            chain.is_selectable = False
            graphics_scene.addItem(chain)
            self.chains.append(chain)
コード例 #11
0
def train(disease_group_id):
    """ Use data extracted from request JSON to create structure in a training phase. """

    # Extract training data from JSON
    try:
        data = request.json['points']
    except KeyError:
        return _log_response('Invalid JSON', disease_group_id, 400)

    # Initialise empty structures
    predictor = Chain()
    feed_classes = {}

    # Train predictor (if enough data) and save structures
    if len(data) >= 50:
        try:
            feed_classes = _construct_feed_classes(data)
            X = _convert_training_data_to_matrix(data, feed_classes)
            y = _convert_expert_weighting_to_labels(data)
        except KeyError:
            return _log_response('Invalid JSON', disease_group_id, 400)
        else:
            predictor.train(X, y)
            _save_pickles(disease_group_id, predictor, feed_classes)
            return _log_response('Trained predictor saved', disease_group_id, 200)
    else:
        _save_pickles(disease_group_id, predictor, feed_classes)
        return _log_response('Insufficient training data - empty predictor saved', disease_group_id, 200)
コード例 #12
0
def sync_overall(save=False):
    best_chain = sync_local()
    for peer in PEERS:
        # try to connect to peer
        peer_blockchain_url = peer + 'blockchain.json'
        try:
            r = requests.get(peer_blockchain_url)
            peer_blockchain_dict = r.json()
            peer_blocks = [Block(bdict) for bdict in peer_blockchain_dict]
            peer_chain = Chain(peer_blocks)

            if peer_chain.is_valid() and peer_chain > best_chain:
                best_chain = peer_chain
        except requests.exceptions.ConnectionError:
            print("Peer at %s not running. Continuing to next peer." % peer)
        else:
            print(
                "Peer at %s is running. Gathered their blockchain for analysis."
                % peer)

    print("Longest blockchain is %s blocks" % len(best_chain))

    if save:
        best_chain.self_save()
    return best_chain
コード例 #13
0
def run_attacks(algos, pools, expected_blocks, fork_block):
    attack_settings = [
        hash_rent_11, hash_rent_8, mining_pool_ethermine,
        mining_pool_ethermine70, mining_pool_ethermine_and_hashrent,
        mining_pool_ethermine_nanopool
        # protocol incentivized
        # mining_pool_40_incentivized,
        # mining_pool_40_and_hashrent_incentivized
    ]
    total_seconds = expected_blocks * 14
    results = defaultdict(list)

    for chain_rule, algo_name in algos:
        for attack in attack_settings:
            mainnet = Chain('mainnet',
                            pools,
                            parent_chain_blocks=None,
                            chain_score_fn=chain_rule)
            attacker_chain = None
            active_chains = [mainnet]
            lost_at_block = None
            forked = False

            for seconds in range(total_seconds):
                for _chain in active_chains:

                    if _chain.mines_a_block(seconds):
                        _chain.create_block(seconds)
                    if _chain.name == 'mainnet' and _chain.height == fork_block and not forked:
                        print "Chain block time and pools before fork:"
                        print "  - mainnet: block_time:{}, pools:{}".format(
                            mainnet.block_time, mainnet.pools)
                        attacker_chain = mainnet.fork(attack.name,
                                                      attack.pools)
                        mainnet = mainnet.remove_pools(attack.pools)
                        active_chains = []
                        active_chains.append(mainnet)
                        active_chains.append(attacker_chain)
                        forked = True
                        print "Chain block time and pools after fork:"
                        print "  - {}: block_time:{}, pools:{}".format(
                            attacker_chain.name, attacker_chain.block_time,
                            attacker_chain.pools)
                        print "  - mainnet: block_time:{}, pools:{}".format(
                            mainnet.block_time, mainnet.pools)
                    # Check if the mainnet lost. We lost when the attacker is more than
                    # 30 blocks ahead and has a higher score
                    if lost_at_block is None and has_attacker_won(
                            active_chains):
                        lost_at_block = active_chains[0].height
                        print "Lost to attacker at block: {}".format(
                            lost_at_block)

            # Print results after simulation with the chain score over a range of blocks
            print_winner(algo_name, active_chains, expected_blocks, fork_block)
            results[algo_name].append(
                Result(algo_name, active_chains[0], active_chains[1],
                       lost_at_block, attack))
    return results
コード例 #14
0
ファイル: node.py プロジェクト: youht88/bc
 def syncLocalChain(self):
     localChain = Chain([])
     for block in self.database["blockchain"].find({}, {
             "_id": 0
     }).sort([("index", 1)]):
         localBlock = Block(block)
         localChain.addBlock(localBlock)
     return localChain
コード例 #15
0
    def __init__(self, file_lst):

        # Instantiate parse file on instantion
        self.file_lst = file_lst
        self.tempo = None
        self.tpb = None
        self.chain = Chain()
        self._parse()
コード例 #16
0
def main():
    fname = sys.argv[1]
    chain = Chain()
    with open(fname, "r") as f:
        print "Reading " + fname
        chain.record_string(f.read())

    with open("definitions/" + sys.argv[2], "w") as fout:
        print "Writing " + sys.argv[2]
        fout.write(zlib.compress(chain.serialize()))
コード例 #17
0
ファイル: scene.py プロジェクト: smart-fm/simmobility-prod
    def draw_lane_edge(self, lane_edge, tool_box):
        color = QtCore.Qt.blue
        group = tool_box.lane_edge_type(lane_edge.type)
        info = "lane-edge section=%d index=%d type=%s" \
               % (lane_edge.section_id, lane_edge.index, lane_edge.type)
        balls = list()
        for i, point in enumerate(lane_edge.polyline):
            ball = Ball(point.x, point.y, color)
            if i:
                ball.info = info
            else:
                ball.info = info + " polyline-first-point"
            ball.road_item = lane_edge
            ball.lane_edge = lane_edge
            ball.point_index = i
            ball.setZValue(1)
            ball.is_a_line = False
            ball.is_selectable = True
            self.addItem(ball)
            balls.append(ball)
            group.items.append(ball)

        ball0 = balls[0]
        for ball in balls:
            chain = Chain(ball0, ball, color)
            ball0 = ball
            chain.info = info
            chain.road_item = lane_edge
            chain.lane_edge = lane_edge
            chain.setZValue(1)
            chain.is_a_line = True
            chain.is_selectable = True
            self.addItem(chain)
            group.items.append(chain)
コード例 #18
0
ファイル: scene.py プロジェクト: smart-fm/simmobility-prod
    def draw_balls_and_chains(self, lane_edge_or_center_line, info, group,
                              color):
        balls = list()
        for i, point in enumerate(lane_edge_or_center_line.polyline):
            ball = Ball(point.x, point.y, color)
            if i:
                ball.info = info
            else:
                ball.info = info + "polyline-first-point"
            ball.road_item = lane_edge_or_center_line
            ball.lane_edge = lane_edge_or_center_line
            ball.point_index = i
            ball.setZValue(1)
            ball.is_a_line = False
            ball.is_selectable = True
            self.addItem(ball)
            balls.append(ball)
            group.items.append(ball)

        ball0 = balls[0]
        for ball in balls:
            chain = Chain(ball0, ball, color)
            ball0 = ball
            chain.info = info
            chain.road_item = lane_edge_or_center_line
            chain.lane_edge = lane_edge_or_center_line
            chain.setZValue(1)
            chain.is_a_line = True
            chain.is_selectable = True
            self.addItem(chain)
            group.items.append(chain)
コード例 #19
0
def test():
    test_chain = Chain()
    test_num_blocks = 10

    for i in range(1, test_num_blocks + 1):
        token = "".join(
            random.choice(string.ascii_uppercase + string.ascii_lowercase +
                          string.digits) for x in range(16))
        test_chain.next_block(test_chain.tail, token)

    print(test_chain)
コード例 #20
0
 def set_ball_and_chain(self):
     if len(self.polyline) == 1:
         point = self.polyline[0]
         self.ball = Ball(point.x, point.y)
         self.ball.is_selectable = False
         #self.graphics_scene.addItem(self.ball)
         self.chain = Chain(self.ball, None)
         self.chain.is_selectable = False
         self.graphics_scene.addItem(self.chain)
         return
     self.set_fixed_end(self.polyline[-1])
コード例 #21
0
ファイル: sync.py プロジェクト: sensecollective/jbc
def sync_local():
    local_chain = Chain([])
    #We're assuming that the folder and at least initial block exists
    if os.path.exists(CHAINDATA_DIR):
        for filepath in glob.glob(os.path.join(CHAINDATA_DIR, '*.json')):
            with open(filepath, 'r') as block_file:
                try:
                    block_info = json.load(block_file)
                except:
                    print filepath
                local_block = Block(block_info)
                local_chain.add_block(local_block)
    return local_chain
コード例 #22
0
    def __init__(self, tree, interval=60):
        """sets up the async event loop
        Parameters:
        tree - the same merkle tree instance being anchored
        interval - the optional parameter for setting the time interval of the anchor.

        Returns: an object that tracks the current phase, spawns subtasks
        """
        self.tree = tree
        self.loop = Timeloop()
        self.chain = Chain()
        self.interval = interval
        logger.info("The reactor was started at time: {time}",
                    time=datetime.timestamp(datetime.now()))
コード例 #23
0
ファイル: test.py プロジェクト: heronyang/pychain
    def test(self):
        """
        Gerenal test.
        """

        # Creates a new chain
        chain = Chain()

        # Puts some blocks into the chain
        block_1 = Block("apple", "0")
        chain.add(block_1)
        block_1.mine()

        block_2 = Block("banana", block_1.hash)
        chain.add(block_2)
        block_2.mine()

        block_3 = Block("cat", block_2.hash)
        chain.add(block_3)

        # Check if the chain is "not valid" if the last block is not mined
        self.assertFalse(chain.is_valid)
        block_3.mine()

        # Checks if the chain is valid
        self.assertTrue(chain.is_valid)
コード例 #24
0
    def infer(self):
        Flow().defaults()

        model = Model().make_fcn()
        model.load_weights('model_weights.h5')

        chain = Chain().make()

        angle_scale = Scale([-1.0, 1.0], [-0.5, 0.5])
        position_scale = Scale([-3.0, 3.0], [-0.5, 0.5])

        # Reference angles
        theta1_ref = 0.3
        theta2_ref = 0.4
        theta3_ref = 0.35
        ref = np.array([theta1_ref, theta2_ref, theta3_ref])
        print('\nReference angles:')
        print(ref)

        # Forward kinematic positions
        px, py, pz = chain.forward({
            'theta1': theta1_ref,
            'theta2': theta2_ref,
            'theta3': theta3_ref
        })
        print('\nComputed positions:')
        print(np.array([px, py, pz]))

        # Normalize the position
        px = position_scale.forward_scale(px)
        py = position_scale.forward_scale(py)
        pz = position_scale.forward_scale(pz)
        p = np.array([[px, py, pz]])

        # Inference returns normalized angle
        r = model.predict(p)

        # De-normalized inference angles
        theta1_ik = angle_scale.reverse_scale(r[0][0])
        theta2_ik = angle_scale.reverse_scale(r[0][1])
        theta3_ik = angle_scale.reverse_scale(r[0][2])
        inf = np.array([theta1_ik, theta2_ik, theta3_ik])
        print('\nInferred IK angles:')
        print(inf)

        # Relative error
        err = np.abs(ref - inf) / ref
        print('\nAngle error (relative):')
        print(err)
コード例 #25
0
ファイル: loop_solver.py プロジェクト: mc0709/freecad-symoro
    def get_chains(self):
        """Return a list of serial chains constituting the mechanism

        Each serial chain starts with the joint following self.root and
        ends with self.end or a joint with sameas == self.end.
        """
        from chain import Chain
        chain = Chain(self.joints)

        chains = []
        for end_joint in self.end_joints:
            subchain = chain.get_subchain_to(end_joint)
            remove_upto_root(subchain, self.root)
            chains.append(subchain)
        return chains
コード例 #26
0
def get_data(self, index):
    blocks = []
    # We're assuming that the folder and at least initial block exists
    if os.path.exists(CHAINDATA_DIR):
        for filepath in glob.glob(os.path.join(CHAINDATA_DIR, '*.json')):
            with open(filepath, 'r') as block_file:
                try:
                    block_info = json.load(block_file)
                except:
                    print(filepath)
                local_block = Block(block_info)
                blocks.append(local_block)
    blocks.sort(key=lambda block: block.index)
    local_chain = Chain(blocks)
    print(Chain.find_block_by_index(local_chain, index))
コード例 #27
0
    def get_chains(self):
        """Return a list of serial chains constituting the mechanism

        Each serial chain starts with the joint following self.root and
        ends with self.end or a joint with sameas == self.end.
        """
        from chain import Chain
        chain = Chain(self.joints)

        chains = []
        for end_joint in self.end_joints:
            subchain = chain.get_subchain_to(end_joint)
            remove_upto_root(subchain, self.root)
            chains.append(subchain)
        return chains
コード例 #28
0
ファイル: sync.py プロジェクト: greatbn/kma-blockchain
def sync_local(sync_es=False):
    local_chain = Chain([])
    if os.path.exists(CHAINDATA_DIR):
        for filepath in sorted(glob.glob(os.path.join(CHAINDATA_DIR,
                                                      '*.json'))):
            with open(filepath, 'r') as block_file:
                try:
                    block_info = json.load(block_file)
                except:
                    raise Exception("block error")
                if sync_es:
                    es.insert_document(block_info)
                local_block = Block(block_info)
                local_chain.add_block(local_block)
    return local_chain
コード例 #29
0
 def __init__(self):
     self.chain = Chain()
     self.C = Canvas()
     self.e = Entry(self.C)
     self.l = Label(self.C, text="File name:")
     self.addbutton = Button(self.C, text="Add file", command=self.addfile)
     self.genbutton = Button(self.C, text="Generate", command=self.generate)
     self.T = Text(self.C)
     self.C.pack()
     self.l.pack(side=LEFT)
     self.e.pack(side=LEFT)
     self.addbutton.pack(side=LEFT)
     self.genbutton.pack()
     self.T.pack()
     mainloop()
コード例 #30
0
ファイル: chain_db.py プロジェクト: tirth/BlockStuff
    def sync(self, chain: Chain):
        new_blocks = chain.get_new()

        if len(new_blocks) < 1:
            print('no new blocks')
            return

        if not chain.verify():
            print('invalid chain')
            return

        print(f'syncing chain, {len(new_blocks)} new blocks')
        self.add_many(new_blocks)

        chain.synced()
コード例 #31
0
class MKParser:

    def __init__(self, file_lst):

        # Instantiate parse file on instantion
        self.file_lst = file_lst
        self.tempo = None
        self.tpb = None
        self.chain = Chain()
        self._parse()

    # Parse midi file:
    #    - Group in notes if they occur simultaneously
    #    - Look for tempo messages
    #    - Sequence them into the MK Dictionary
    def _parse(self):
        for file in self.file_lst:
            mid = mido.MidiFile(file)
            self.tpb = mid.ticks_per_beat
            old_lst = []
            temp_lst = []
            for track in mid.tracks:
                for message in track:
                    #print (message)
                    if message.type == "set_tempo":
                        self.tempo = message.tempo
                    elif message.type == "note_on":
                        if message.time == 0:
                            temp_lst.append(message.note)
                        else:
                            temp_lst.append(message.note)
                            self._sequence(old_lst,temp_lst,message.time)
                            old_lst = temp_lst
                            temp_lst = []

    # Add note permutations to MK Chain
    def _sequence(self, old_lst,temp_lst, duration):
        for i in old_lst:
            for j in temp_lst:
                self.chain.add(i,j,self._time(duration))

    # Get time using tempo and time from midi file
    def _time(self,ticks):
        time_ms = ((ticks*self.tempo)/self.tpb)/1000
        return int(time_ms - (time_ms % 30) + 30)

    def return_chain(self):
        return self.chain
コード例 #32
0
ファイル: kinematics.py プロジェクト: Izzatbek/freecad-symoro
def get_looproot(joints, joint):
    """Return the loop root for loop ending at joint

    Argument 2, joint, must be the end joint with non-None sameas attribute.
    The loop root is the last common joints of the two subchains ending at,
    respectively joint and joint.sameas.
    """
    chain = Chain(joints)
    subchain1 = chain.get_subchain_to(joint)
    subchain2 = chain.get_subchain_to(joint.sameas)

    #returns the first common antecedant for both loops
    for jnt in subchain2[::-1]:
        if (jnt in subchain1):
            return jnt
    return None
コード例 #33
0
ファイル: kinematics.py プロジェクト: Izzatbek/freecad-symoro
 def __init__(self, table, base_t=None, tool_t=None):
     self.table = table
     if base_t is None:
         self.base_t = [
             [1, 0, 0, 0],
             [0, 1, 0, 0],
             [0, 0, 1, 0],
             [0, 0, 0, 1]]
     else:
         self.base_t = base_t
     if tool_t is None:
         self.tool_t = [
             [1, 0, 0, 0],
             [0, 1, 0, 0],
             [0, 0, 1, 0],
             [0, 0, 0, 1]]
     else:
         self.tool_t = tool_t
     self.joints = get_joints_from_table(self.table)
     # List of actuated joints
     self.ajoints = self.get_ajoints()
     # List of passive joints
     self.pjoints = self.get_pjoints()
     # Actuated joints' variables
     self.q = self.get_q()
     # Passive joints' variables
     self.qp = self.get_qp()
     # Chains
     self.chain = Chain(self.joints)
     # Tuples (root, end) for each loop
     self.loops = get_loops(self.joints)
     # List of loop solvers
     from loop_solver import LoopSolver
     self.loopsolvers = [LoopSolver(self.joints, *l) for l in self.loops]
コード例 #34
0
ファイル: rule.py プロジェクト: wil/pyroman
	def generate(self):
		"""
		Generate iptables-rules for this firewall rule.
		"""
		inface = ""
		outface = ""
		# look up interfaces
		if self.client:
			inface = self.client.iface
		if self.server:
			outface = self.server.iface

		if inface == outface \
			and self.client and not self.client.islocalhost() \
			and self.server and not self.server.islocalhost():
			return

		# skip forwarding chains when not forwarding
		if not Firewall.forwarding:
			if not ((self.client and self.client.islocalhost()) or \
			        (self.server and self.server.islocalhost())):
				return

		chain = Chain.get_chain(inface, outface, \
			self.client, self.server, self.loginfo)

		vrules4, vrules6 = [""], [""]
		if self.service:
			vrules4 = self.service.get_filter("d", 4)
			vrules6 = self.service.get_filter("d", 6)
		for vr in vrules4:
			chain.append4("%s -j %s" % (vr, self.target), self.loginfo)
		for vr in vrules6:
			chain.append6("%s -j %s" % (vr, self.target), self.loginfo)
コード例 #35
0
 def wrapper(self, *args, **kwargs):
     self.elapsed, id, cash, chain = time.time(), True if len(
         args) == 4 else False, self.get_cash(), Chain(
             args[0]) if not len(args) == 4 else None
     option = chain.get_option(
         args[1], args[2],
         args[3]) if not id else ClientMethods.fetch_by_id(
             self.client, args[1])
     self.direction = 'debit' if args[
         4 if not id else 2] == 'buy' else 'credit'
     self.legs = [{
         'option': option['url'],
         'side': args[4 if not id else 2],
         'position_effect': args[5 if not id else 3],
         'ratio_quantity': kwargs.get('ratio_quantity', 1)
     }]
     self.price = option[
         'high_fill_rate_buy_price' if args[4 if not id else 2] ==
         'buy' else 'high_fill_rate_sell_price'] if not kwargs.get(
             'price') else kwargs['price']
     self.quantity = kwargs.get(
         'quantity', 1) if not kwargs.get('max_quantity') else (
             math.floor(cash / self.price) if math.floor(
                 cash / self.price) <= kwargs['max_quantity'] else 1)
     return func(self, *args, **kwargs)
コード例 #36
0
 def test_load_2(self):
     '''Test chaining of two files.'''
     tmpfile = testfname.replace('test_tree', 'test_tree_2_tmp')
     shutil.copyfile(testfname, tmpfile)
     chain = Chain(testfname.replace('.root', '*.root'), 'test_tree')
     self.assertEqual(len(chain), 200)
     os.remove(tmpfile)
コード例 #37
0
ファイル: user.py プロジェクト: Vladikasik/Bean-coin
    def find_blocks(self):

        curr_chain = Chain.get_current()

        for i in curr_chain:
            if i[user_from] == self.address or i[user_to] == self.address:
                self.blocks.append(i)
コード例 #38
0
ファイル: baseseq.py プロジェクト: anton386/baseseq
    def run(self):

        # Phase 1 - Detection of BarCode
        self.bc = BarCode(self.bam)
        sys.stderr.write("[%s] Starting BarCode Analysis \n" % (self.get_time(),))
        
        self.bc.simple_approach()
        sys.stderr.write("[%s] Analyzed BarCodes \n" % (self.get_time(),))
        
        self.bc.write_barcodes(self.barcodes)
        sys.stderr.write("[%s] Wrote BarCodes\n" % (self.get_time(),))

        # Phase 2 - Rewrite BAM
        sys.stderr.write("[%s] Starting Sort and Rewrite BAM\n" % (self.get_time(),))
        
        self.bc.load_barcodes(self.barcodes)
        sys.stderr.write("[%s] Loaded BarCodes\n" % (self.get_time(),))

        self.bc.bam.reset()
        self.bc.sort_and_rewrite_bam(self.rewritten_bam)
        pysam.sort("-n", self.rewritten_bam, self.rewritten_sorted_bam.replace(".bam", ""))
        sys.stderr.write("[%s] Sort and Rewrite BAM\n" % (self.get_time(),))
        
        # Phase 3 - Build Consensus
        self.consensus = Consensus(self.rewritten_sorted_bam, self.ref)
        sys.stderr.write("[%s] Starting Consensus Building\n" % (self.get_time(),))
        
        self.consensus.build()
        sys.stderr.write("[%s] Built and Calculated Consensus\n" % (self.get_time(),))
        
        self.consensus.infer_consensus(self.consensus_reference)
        sys.stderr.write("[%s] Inferred Consensus\n" % (self.get_time(),))

        # Phase 4 - Call Variants and Haplotypes
        self.consensus.output_consensus_genomes(self.consensus_genomes)
        sys.stderr.write("[%s] Output Consensus Genomes\n" % (self.get_time(),))

        self.consensus.output_haplotype_distribution(self.haplotype_distribution)
        sys.stderr.write("[%s] Output Haplotype Distribution\n" % (self.get_time(),))
        
        self.ovcf = VCF(self.vcf, crossmap=self.crossmap)
        self.ovcf.get_variants(self.ref.sequence,
                               self.consensus.consensus_genomes)
        self.ovcf.output_vcf(self.ref.sequence)
        sys.stderr.write("[%s] Output VCF\n" % (self.get_time(),))

        # Phase 5 - Summary Statistics and Chain Files
        f_out = open(self.out, "w")
        self.consensus.output_consensus_coverage(f_out)
        self.ovcf.output_variants_distribution(f_out)
        self.bc.output_reads_in_barcode_distribution(f_out)
        f_out.close()
        sys.stderr.write("[%s] Output Summary Statistics\n" % (self.get_time(),))

        self.ochain = Chain(self.chain)
        self.ochain.output_chain(self.ref,
                                 self.consensus.inferred_consensus,
                                 self.consensus.inferred_structure)
        sys.stderr.write("[%s] Output Chain File\n" % (self.get_time(),))
コード例 #39
0
ファイル: loop_solver.py プロジェクト: galou/drawbot
    def __init__(self, joints, pjoints, mjoints):
        self.joints = joints
        self.pjoints = pjoints
        self.mjoints = mjoints
        from numpy import inf
        self.ub_prismatic = inf

        # Chains
        self.chain = Chain(self.joints)
        self.loops = get_loops(self.joints)

        #self.end_joints = self.get_end_joints()
        self.chains = self.get_chains()
コード例 #40
0
ファイル: sync.py プロジェクト: zhilinwww/jbc
def sync_overall(save=False):
  best_chain = sync_local()
  for peer in PEERS:
    #try to connect to peer
    peer_blockchain_url = peer + 'blockchain.json'
    try:
      r = requests.get(peer_blockchain_url)
      peer_blockchain_dict = r.json()
      peer_blocks = [Block(bdict) for bdict in peer_blockchain_dict]
      peer_chain = Chain(peer_blocks)

      print peer_chain.is_valid()
      if peer_chain.is_valid() and len(peer_chain) > len(best_chain):
        best_chain = peer_chain

    except requests.exceptions.ConnectionError:
      print "Peer at %s not running. Continuing to next peer." % peer
    else:
      print "Peer at %s is running. Gathered their blochchain for analysis." % peer
  print "Longest blockchain is %s blocks" % len(best_chain)
  #for now, save the new blockchain over whatever was there
  if save:
    best_chain.self_save()
  return best_chain
コード例 #41
0
ファイル: baseseq.py プロジェクト: anton386/baseseq
    def assemble_consensus_genomes(self):
        
        # build consensus
        self.consensus = Consensus(self.rewritten_sorted_bam, self.ref)
        sys.stderr.write("[%s] Starting Consensus Building\n" % (self.get_time(),))
        
        self.consensus.build(debug=self.debug)
        sys.stderr.write("[%s] Built and Calculated Consensus\n" % (self.get_time(),))
        
        self.consensus.infer_consensus(self.consensus_reference)
        sys.stderr.write("[%s] Inferred Consensus\n" % (self.get_time(),))

        self.consensus.output_consensus_genomes(self.consensus_genomes)
        sys.stderr.write("[%s] Output Consensus Genomes\n" % (self.get_time(),))

        self.consensus.output_haplotype_distribution(self.haplotype_distribution)
        sys.stderr.write("[%s] Output Haplotype Distribution\n" % (self.get_time(),))

        self.quark = Quark(self.ref.sequence)
        self.quark.distance_matrix(sorted(self.consensus.freq_distribution.items(),
                                          key=lambda q: q[1],
                                          reverse=True))
        self.quark.graph_it()
        self.quark.rank_it(self.rank)
        sys.stderr.write("[%s] Output Quark Analysis\n" % (self.get_time(),))
        
        self.ovcf = VCF(self.vcf, crossmap=self.crossmap)
        self.ovcf.get_variants(self.ref.sequence,
                               self.consensus.consensus_genomes)
        self.ovcf.output_vcf(self.ref.sequence)
        sys.stderr.write("[%s] Output VCF\n" % (self.get_time(),))

        self.summary_statistics()
        sys.stderr.write("[%s] Output Summary Statistics\n" % (self.get_time(),))

        self.ochain = Chain(self.chain)
        self.ochain.output_chain(self.ref,
                                 self.consensus.inferred_consensus,
                                 self.consensus.inferred_structure)
        sys.stderr.write("[%s] Output Chain File\n" % (self.get_time(),))
コード例 #42
0
ファイル: stack.py プロジェクト: MichelleJaeg/quantipy
    def get_chain(self, name=None, data_keys=None, filters=None, x=None, y=None,
                  views=None, post_process=True, orient_on=None, select=None):
        """
        Construct a "chain" shaped subset of Links and their Views from the Stack.
        
        A chain is a one-to-one or one-to-many relation with an orientation that
        defines from which axis (x or y) it is build. 

        Parameters
        ----------
        name : str, optional
            If not provided the name of the chain is generated automatically.
        data_keys, filters, x, y, views : str or list of str
            Views will be added reflecting the order in ``views`` parameter. If
            both ``x`` and ``y`` have multiple items, you must specify the
            ``orient_on`` parameter.
        post_process : bool, default True
            If file meta is found, views inside the chain will get their Values-
            axes codes checked against the category lists and missing codes will
            be added.
        orient_on : {'x', 'y'}, optional
             Must be specified if both ``x`` and ``y`` are lists of multiple
             items.
        select : tbc.
            :TODO: document this!

        Returns
        -------
        chain : Chain object instance
        """

        #Make sure all the given keys are in lists
        data_keys = self._force_key_as_list(data_keys)
        # filters = self._force_key_as_list(filters)
        views = self._force_key_as_list(views)

        if orient_on:
            if x is None:
                x = self.describe()['x'].drop_duplicates().values.tolist()
            if y is None:            
                y = self.describe()['y'].drop_duplicates().values.tolist()
            if views is None:
                views = self._Stack__view_keys
                views = [v for v in views if '|default|' not in v]
            return self.__get_chains(name=name, data_keys=data_keys,
                                     filters=filters, x=x, y=y, views=views,
                                     post_process=post_process,
                                     orientation=orient_on, select=select)
        else:
            chain = Chain(name)
            found_views = []
            missed_views = []

            #Make sure all the given keys are in lists
            x = self._force_key_as_list(x)
            y = self._force_key_as_list(y)

            if data_keys is None:
                # Apply lazy data_keys if none given
                data_keys = self.keys()

            the_filter = "no_filter" if filters is None else filters

            if self.__has_list(data_keys):
                for key in data_keys:

                    # Use describe method to get x keys if not supplied.
                    if x is None:
                        x_keys = self.describe()['x'].drop_duplicates().values.tolist()
                    else:
                        x_keys = x

                    # Use describe method to get y keys if not supplied.
                    if y is None:
                        y_keys = self.describe()['y'].drop_duplicates().values.tolist()
                    else:
                        y_keys = y

                     # Use describe method to get view keys if not supplied.
                    if views is None:
                        v_keys = self.describe()['view'].drop_duplicates().values.tolist()
                        v_keys = [v_key for v_key in v_keys if '|default|'
                                  not in v_key]
                    else:
                        v_keys = views

                    chain._validate_x_y_combination(x_keys, y_keys, orient_on)
                    chain._derive_attributes(key,the_filter,x_keys,y_keys,views)

                    # Apply lazy name if none given
                    if name is None:
                        chain._lazy_name()

                    for x_key in x_keys:
                        for y_key in y_keys:

                            if views is None:
                                chain[key][the_filter][x_key][y_key] = self[key][the_filter][x_key][y_key]
                            else:
                                for view in views:
                                    try:
                                        chain[key][the_filter][x_key][y_key][view] = self[key][the_filter][x_key][y_key][view]

                                        if view not in found_views:
                                            found_views.append(view)
                                    except KeyError:
                                        if view not in missed_views:
                                            missed_views.append(view)
            else:
                raise ValueError('One or more of your data_keys ({data_keys}) is not in the stack ({stack_keys})'.format(data_keys=data_keys, stack_keys=self.keys()))
            if found_views:
                chain.views = [view for view in chain.views
                               if view in found_views]

            for view in missed_views:
                if view in found_views:
                    missed_views.remove(view)

        if post_process:
            chain._post_process_shapes(self[chain.data_key].meta)

        if select is not None:
            for view in chain[key][the_filter][x_key][y_key]:
                df = chain[key][the_filter][x_key][y_key][view].dataframe
                levels = df.index.levels
                selection = {}
                for var in select:
                    level = functions.find_variable_level(levels, var)
                    if level is not None:
                        selection[var] = level

                #Don't do anything if the selection doesnt produce a result
                if selection:
                    # selection = {var: functions.find_variable_level(levels, var) for var in select}
                    list_of_dfs = [df.xs(var, level=selection[var]) for var in selection.keys()]
                    new_df = pd.concat(list_of_dfs)
                    # Reconstruct the index
                    new_df.index= pd.MultiIndex.from_product([levels[0],selection.keys()], names=df.index.names)
                    chain[key][the_filter][x_key][y_key][view].dataframe = new_df

        return chain
コード例 #43
0
ファイル: baseseq.py プロジェクト: anton386/baseseq
class BaseSeq(Helper):

    def __init__(self, bam, barcodes=None, out=None, ref=None,
                 rewritten_bam=None,
                 consensus_reference=None,
                 consensus_genomes=None,
                 haplotype_distribution=None,
                 vcf=None,
                 chain=None,
                 crossmap=None,
                 export=None,
                 rank=None,
                 debug=None):
        
        self.bam = bam
        self.barcodes = barcodes
        self.out = out
        self.ref = Reference(ref)

        self.rewritten_bam = rewritten_bam
        self.rewritten_sorted_bam = rewritten_bam.replace(".bam", ".sorted.bam") if rewritten_bam else None
        self.consensus_reference = consensus_reference
        self.consensus_genomes = consensus_genomes
        self.haplotype_distribution = haplotype_distribution
        self.vcf = vcf
        self.chain = chain
        self.crossmap = crossmap
        self.export = export
        self.rank = rank
        self.debug = int(debug)
    
    def get_barcodes(self):
        # simple approach - align, take soft-clipped, and use the arbitrary 20 bases
        # intermediate approach - use the seed and extend approach
        out = open(self.out, "w")

        self.bc = BarCode(self.bam)
        self.bc.simple_approach()

        for k, v in sorted(self.bc.barcode_to_read.items()):
            q = sorted(v)
            out.write("%s\t%s\n" % (k, ",".join(q)))

        out.close()

    def error_correction_barcodes(self):

        # start analysis
        self.bc = BarCode(self.bam)
        sys.stdout.write("[%s] Starting Error Correction Analysis\n" % (self.get_time(),))

        # load barcodes
        self.bc.load_barcodes(self.barcodes)
        sys.stdout.write("[%s] Loaded BarCodes\n" % (self.get_time(),))

        # cluster barcodes
        self.bc.cluster_barcodes()
        sys.stdout.write("[%s] Clustered BarCodes\n" % (self.get_time(),))

    
    def filter_barcodes(self, barcode, export="fastq"):
        list_of_ids = []
        with open(self.barcodes, "r") as f:
            for line in f:
                data = line.strip("\r\n").split("\t")
                if barcode == data[0]:
                    list_of_ids = data[1].split(",")
                    break

        self.bc = BarCode(self.bam)
        self.bc.filter_and_export(list_of_ids, self.out, export=export)


    def sort_and_rewrite_bam(self):
        
        self.bc = BarCode(self.bam)
        sys.stderr.write("[%s] Starting Sort and Rewrite BAM\n" % (self.get_time(),))
        
        self.bc.load_barcodes(self.barcodes)
        sys.stderr.write("[%s] Loaded BarCodes\n" % (self.get_time(),))

        self.bc.sort_and_rewrite_bam(self.rewritten_bam)
        pysam.sort("-n", self.rewritten_bam, self.rewritten_sorted_bam.replace(".bam", ""))
        sys.stderr.write("[%s] Sort and Rewrite BAM\n" % (self.get_time(),))

    def split_bam_by_barcode(self):

        self.bc = BarCode(self.bam)
        sys.stderr.write("[%s] Starting procedure to split BAM by barcode\n" % (self.get_time(),))

        self.bc.split_bam_into_barcodes(self.ref, self.out, self.export)
        sys.stderr.write("[%s] Finished splitting BAM by barcode id\n" % (self.get_time(),))
        

    def assemble_consensus_genomes(self):
        
        # build consensus
        self.consensus = Consensus(self.rewritten_sorted_bam, self.ref)
        sys.stderr.write("[%s] Starting Consensus Building\n" % (self.get_time(),))
        
        self.consensus.build(debug=self.debug)
        sys.stderr.write("[%s] Built and Calculated Consensus\n" % (self.get_time(),))
        
        self.consensus.infer_consensus(self.consensus_reference)
        sys.stderr.write("[%s] Inferred Consensus\n" % (self.get_time(),))

        self.consensus.output_consensus_genomes(self.consensus_genomes)
        sys.stderr.write("[%s] Output Consensus Genomes\n" % (self.get_time(),))

        self.consensus.output_haplotype_distribution(self.haplotype_distribution)
        sys.stderr.write("[%s] Output Haplotype Distribution\n" % (self.get_time(),))

        self.quark = Quark(self.ref.sequence)
        self.quark.distance_matrix(sorted(self.consensus.freq_distribution.items(),
                                          key=lambda q: q[1],
                                          reverse=True))
        self.quark.graph_it()
        self.quark.rank_it(self.rank)
        sys.stderr.write("[%s] Output Quark Analysis\n" % (self.get_time(),))
        
        self.ovcf = VCF(self.vcf, crossmap=self.crossmap)
        self.ovcf.get_variants(self.ref.sequence,
                               self.consensus.consensus_genomes)
        self.ovcf.output_vcf(self.ref.sequence)
        sys.stderr.write("[%s] Output VCF\n" % (self.get_time(),))

        self.summary_statistics()
        sys.stderr.write("[%s] Output Summary Statistics\n" % (self.get_time(),))

        self.ochain = Chain(self.chain)
        self.ochain.output_chain(self.ref,
                                 self.consensus.inferred_consensus,
                                 self.consensus.inferred_structure)
        sys.stderr.write("[%s] Output Chain File\n" % (self.get_time(),))
    
    
    def summary_statistics(self):
        # coverage per genome
        # variants per genome
        # estimate PCR and sequencing errors
        # barcode distribution
        f_out = open(self.out, "w")

        self.bc = BarCode(self.bam)  #TEMP
        self.bc.load_barcodes(self.barcodes)  #TEMP
        
        self.consensus.output_consensus_coverage(f_out)
        self.ovcf.output_variants_distribution(f_out)
        self.bc.output_reads_in_barcode_distribution(f_out)
        
        f_out.close()

    def run(self):

        # Phase 1 - Detection of BarCode
        self.bc = BarCode(self.bam)
        sys.stderr.write("[%s] Starting BarCode Analysis \n" % (self.get_time(),))
        
        self.bc.simple_approach()
        sys.stderr.write("[%s] Analyzed BarCodes \n" % (self.get_time(),))
        
        self.bc.write_barcodes(self.barcodes)
        sys.stderr.write("[%s] Wrote BarCodes\n" % (self.get_time(),))

        # Phase 2 - Rewrite BAM
        sys.stderr.write("[%s] Starting Sort and Rewrite BAM\n" % (self.get_time(),))
        
        self.bc.load_barcodes(self.barcodes)
        sys.stderr.write("[%s] Loaded BarCodes\n" % (self.get_time(),))

        self.bc.bam.reset()
        self.bc.sort_and_rewrite_bam(self.rewritten_bam)
        pysam.sort("-n", self.rewritten_bam, self.rewritten_sorted_bam.replace(".bam", ""))
        sys.stderr.write("[%s] Sort and Rewrite BAM\n" % (self.get_time(),))
        
        # Phase 3 - Build Consensus
        self.consensus = Consensus(self.rewritten_sorted_bam, self.ref)
        sys.stderr.write("[%s] Starting Consensus Building\n" % (self.get_time(),))
        
        self.consensus.build()
        sys.stderr.write("[%s] Built and Calculated Consensus\n" % (self.get_time(),))
        
        self.consensus.infer_consensus(self.consensus_reference)
        sys.stderr.write("[%s] Inferred Consensus\n" % (self.get_time(),))

        # Phase 4 - Call Variants and Haplotypes
        self.consensus.output_consensus_genomes(self.consensus_genomes)
        sys.stderr.write("[%s] Output Consensus Genomes\n" % (self.get_time(),))

        self.consensus.output_haplotype_distribution(self.haplotype_distribution)
        sys.stderr.write("[%s] Output Haplotype Distribution\n" % (self.get_time(),))
        
        self.ovcf = VCF(self.vcf, crossmap=self.crossmap)
        self.ovcf.get_variants(self.ref.sequence,
                               self.consensus.consensus_genomes)
        self.ovcf.output_vcf(self.ref.sequence)
        sys.stderr.write("[%s] Output VCF\n" % (self.get_time(),))

        # Phase 5 - Summary Statistics and Chain Files
        f_out = open(self.out, "w")
        self.consensus.output_consensus_coverage(f_out)
        self.ovcf.output_variants_distribution(f_out)
        self.bc.output_reads_in_barcode_distribution(f_out)
        f_out.close()
        sys.stderr.write("[%s] Output Summary Statistics\n" % (self.get_time(),))

        self.ochain = Chain(self.chain)
        self.ochain.output_chain(self.ref,
                                 self.consensus.inferred_consensus,
                                 self.consensus.inferred_structure)
        sys.stderr.write("[%s] Output Chain File\n" % (self.get_time(),))
        
    
    def assemble_genomes(self):
        pass

    
    def assemble_genomes_from_fastq(self):
        pass
コード例 #44
0
def run_scft(D):
    abn_melt=Scft_Obj("ABn_melt")
    cell=[D]
    #cell=[1.70]
    print "D=",D
    box=SimBox(1,cell)
    Nx=128
    grid=Grid(box,box.shape,[Nx])
    XN=100.0 
    AB=Chem(2,grid,XN)
    N_intpo=1
    Ns=500
    N_blk=10
    ABn_multiblk=Chain(N_blk,Ns,"ABn",N_intpo,grid)   
# prepare the initial lamellar field
    field_param=[0,2] # on X axis, one period     
    Fields_Init(ABn_multiblk,AB,grid,"lamellar",field_param)
    #pl.plot(grid.x,AB.W[0,:,0,0],'ro')
    for i in range(300):      
        pesudo_spectrum_mde(ABn_multiblk,AB,grid)
        density(ABn_multiblk,AB,grid,abn_melt)
        SimpleMixing_AB(AB,ABn_multiblk,grid)  
    
    free_energy_abmelt(ABn_multiblk,AB,grid,abn_melt)
    #io=Scft_IO(["Dump_Field","Dump_Result"],chem=AB)
    print "FE:",abn_melt.f_tot, abn_melt.f_FH
    pl.plot(grid.x,AB.R[0,:,0,0],'ro')
    pl.plot(grid.x,AB.R[1,:,0,0],'b^')
    pl.show()
    #crank_nickson_mde(ABn_multiblk,AB,grid)
    #t=np.arange(Ns+1)
    #pl.plot(grid.x,AB.R[0,:,0,0]  ,'bo',label="RA")
    #pl.plot(grid.x,AB.R[1,:,0,0]  ,'r^',label="RB")
    #pl.plot(t[0:Ns+1],np.log(ABn_multiblk.qf[0:Ns+1,0,0,0])  ,'ro',label="qf")
    #pl.plot(t[0:501],ABn_multiblk.qf[0:501,120,0,0],'go',label="qf")
    #pl.plot(t[0:Ns+1],np.log(ABn_multiblk.qf1[0:Ns+1,0,0,0]),'b^',label="qf1")
    #data=ABn_multiblk.qf[0:Ns,:,0,0]-ABn_multiblk.qf1[0:Ns,:,0,0]
    #print "shape",np.shape(data)
    #data=np.reshape(data,(Ns,len(grid.x)))  
    #pl.imshow(data)
    #pl.plot(grid.x,data[100,:],'r^',label="qf")
    #pl.plot(grid.x,data[300,:],'b^',label="qf")
    #pl.show()
    return abn_melt.f_tot



  

    pl.plot(grid.x,AB.R[0,:,0,0])
    t=np.arange(501)
    t1=np.arange(500*N_intpo+1)
    t1=t1/N_intpo
    #pl.plot(t[0:501],np.log(ABn_multiblk.qf[0:501,0,0,0]),'bo',label="qf")
    #pl.show()
    #pl.plot(t[0:501],np.log(ABn_multiblk.qf[0:501,0,0,0]),'bo',label="qf")
    crank_nickson_mde(ABn_multiblk,AB,grid)
    ABn_multiblk.qf[:,:,:,:]=ABn_multiblk.qf1[:,:,:,:] 
    ABn_multiblk.qb[:,:,:,:]=ABn_multiblk.qb1[:,:,:,:] 
    #density(ABn_multiblk,AB,grid,abn_melt)
    pl.plot(grid.x,AB.R[0,:,0,0],'ro')
    pl.plot(grid.x,AB.R[1,:,0,0],'b^')
    #pl.show()
    #pl.plot(t,np.log(ABn_multiblk.qloop[:,0,0,0]))
    #pl.show()
    #crank_nickson_mde(ABn_multiblk,AB,grid)
    #print "q_loop",ABn_multiblk.qloop[:,0,0,0]
    #pl.plot(t,ABn_multiblk.qloop[:,0,0,0]-ABn_multiblk.qf[:,0,0,0])
    #pl.plot(t1[0:1000],ABn_multiblk.qloop[0:1000,0,0,0],label="qloop")
    #pl.plot(t[0:501],ABn_multiblk.qf[0:501,0,0,0],'bo',label="qf")
    #pl.plot(t[0:501],np.log(ABn_multiblk.qf1[0:501,0,0,0]),'r^',label="qf1")
    #pl.plot(grid.x,AB.R[0,:,0,0],'ro',label="RA")
    #pl.plot(grid.x,AB.R[1,:,0,0],'bo',label="RB")
    pl.showbar()
    pl.show()
    print "RA",AB.R[0,:,0,0]
コード例 #45
0
ファイル: hierview.py プロジェクト: gokceneraslan/ProDy
    def update(self, **kwargs):
        """Update (or build) hierarchical view of atoms.  This method is called 
        at instantiation, but can be used to rebuild the hierarchical view when
        attributes of atoms change."""

        atoms = self._atoms
        if isinstance(atoms, AtomGroup):
            ag = atoms
            selstr = False
            _indices = arange(atoms._n_atoms)
        else:
            ag = atoms.getAtomGroup()
            _indices = atoms._getIndices()
            selstr = atoms.getSelstr()

        set_indices = False
        if atoms == ag:
            set_indices = True
        
        _dict = dict()
        _residues = list()
        _segments = list()
        _chains = list()

        # also set the attributes, so that when no segments or chains 
        # are available, num/iter methods won't raise exceptions
        self._dict = _dict
        self._residues = _residues
        self._segments = _segments
        self._chains = _chains 
        
        acsi = atoms.getACSIndex()
        n_atoms = len(ag)

        # identify segments
        if set_indices:
            segindex = -1
            segindices = zeros(n_atoms, int)
        sgnms = ag._getSegnames()
        if sgnms is None:
            _segments = None
        else:
            if selstr:
                sgnms = sgnms[_indices]
            s = sgnms[0]
            if len(unique(sgnms)) == 1:
                if  s != '':
                    segment = Segment(ag, _indices, acsi=acsi, unique=True, 
                                      selstr=selstr)
                    _dict[s] = segment
                    _segments.append(segment)
                    LOGGER.info('Hierarchical view contains segments.')
                else: 
                    _segments = None
            else:
                ps = None       # previous segment name 
                for i, s in enumerate(sgnms):
                    if s == ps or s in _dict:
                        continue
                    ps = s
                    idx = _indices[i:][sgnms[i:] == s]
                    segment = Segment(ag, idx, acsi=acsi, unique=True, 
                                       selstr=selstr)
                    if set_indices:
                        segindex += 1
                        segindices[idx] = segindex
                    _dict[s] = segment
                    _segments.append(segment)
                LOGGER.info('Hierarchical view contains segments.')

        if set_indices:
            ag._data['segindices'] = segindices
            chindex = -1
            chindices = zeros(n_atoms, int)

        # identify chains
        chids = ag._getChids()
        if chids is None:
            _chains = None
        else:
            if selstr:
                chids = chids[_indices]
            if _segments is None:
                if len(unique(chids)) == 1:
                    chain = Chain(ag, _indices, acsi=acsi, unique=True,
                                  selstr=selstr)
                    _dict[(None, chids[0] or None)] = chain
                    _chains.append(chain)
                else:
                    pc = None
                    for i, c in enumerate(chids):
                        if c == pc or (None, c) in _dict:
                            continue
                        pc = c
                        idx = _indices[i:][chids[i:] == c]
                        chain = Chain(ag, idx, acsi=acsi, unique=True,
                                      selstr=selstr)
                        if set_indices:
                            chindex += 1
                            chindices[idx] = chindex
                        _dict[(None, c)] = chain
                        _chains.append(chain)
            else:
                pc = chids[0]
                ps = sgnms[0]
                _i = 0
                for i, c in enumerate(chids):
                    s = sgnms[i]
                    if c == pc and s == ps:
                        continue
                    s_c = (ps, pc or None)
                    chain = _dict.get(s_c)
                    if chain is None:
                        segment = _dict[ps]
                        idx = _indices[_i:i]
                        chain = Chain(ag, idx, acsi=acsi, segment=segment, 
                                       unique=True, selstr=selstr)
                        if set_indices:
                            chindex += 1
                            chindices[idx] = chindex
                        _dict[s_c] = chain
                        _chains.append(chain)
                    else:
                        idx = _indices[_i:i]
                        chindices[idx] = chain._indices[0]
                        chain._indices = concatenate((chain._indices, idx))
                    pc = c
                    ps = s
                    _i = i
                s_c = (ps, pc or None)
                chain = _dict.get(s_c)
                idx = _indices[_i:]
                if chain is None:
                    segment = _dict[ps]
                    if set_indices:
                        chindex += 1
                        chindices[idx] = chindex
                    chain = Chain(ag, idx, acsi=acsi, segment=segment, 
                                   unique=True, selstr=selstr)
                    _dict[s_c] = chain
                    _chains.append(chain)
                else:
                    if set_indices:
                        chindices[idx] = chain._indices[0]
                    chain._indices = concatenate((chain._indices, idx)) 

        if set_indices:
            ag._data['chindices'] = chindices
        if kwargs.get('chain') == True:
            return
    
        if set_indices:
            resindex = -1
            resindices = zeros(n_atoms, int)
    
        rnums = ag._getResnums()
        if rnums is None:
            raise ValueError('resnums are not set')
        if selstr:
            rnums = rnums[_indices]
        nones = None
        if _segments is None:
            if nones is None:
                nones = [None] * len(rnums)
            sgnms = nones
        if _chains is None:
            if nones is None:
                nones = [None] * len(rnums)
            chids = nones
        icods = ag._getIcodes()
        if icods is None:
            if nones is None:
                nones = [None] * len(rnums)
            icods = nones
        elif selstr:
                icods = icods[_indices]

        pr = rnums[0]
        pi = icods[0] or None          
        pc = chids[0]
        ps = sgnms[0]
        _j = 0
        _append = _residues.append
        _get = _dict.get
        _set = _dict.__setitem__
        for j, r in enumerate(rnums):
            i = icods[j] or None
            c = chids[j]
            s = sgnms[j]
            if r != pr or i != pi or c != pc or s != ps:
                s_c_r_i = (ps, pc, pr, pi)
                res = _get(s_c_r_i)
                if res is None:
                    chain = _get((ps, pc))
                    idx = _indices[_j:j]
                    res = Residue(ag, idx, acsi=acsi, chain=chain, 
                                   unique=True, selstr=selstr)
                    if set_indices:
                        resindex += 1
                        resindices[idx] = resindex
                    _append(res)
                    _set(s_c_r_i, res)
                else:
                    res._indices = concatenate((res._indices, _indices[_j:j]))
                ps = s
                pc = c
                pr = r
                pi = i
                _j = j 
        s_c_r_i = (ps, pc, pr, pi)
        res = _get(s_c_r_i)
        idx = _indices[_j:]
        if res is None:
            chain = _get((ps, pc))
            res = Residue(ag, idx, acsi=acsi, chain=chain, unique=True, 
                           selstr=selstr)
            if set_indices:
                resindex += 1
                resindices[idx] = resindex
            _append(res)
            _set(s_c_r_i, res)
        else:
            if set_indices:
                resindices[idx] = res._indices[0]
            res._indices = concatenate((res._indices, idx))
        
        if set_indices:
            ag._data['resindices'] = resindices
コード例 #46
0
ファイル: loop_solver.py プロジェクト: galou/drawbot
class LoopSolver():
    def __init__(self, joints, pjoints, mjoints):
        self.joints = joints
        self.pjoints = pjoints
        self.mjoints = mjoints
        from numpy import inf
        self.ub_prismatic = inf

        # Chains
        self.chain = Chain(self.joints)
        self.loops = get_loops(self.joints)

        #self.end_joints = self.get_end_joints()
        self.chains = self.get_chains()

    def get_qm(self, index=None):
        """Returns the list of joint variables of moving joints"""
        if (index is None):
            return [jnt.q for jnt in self.mjoints]
        else:
            return self.mjoints[index].q

    def get_qp(self, index=None):
        """Returns the list of joint variables of passive joints"""
        if (index is None):
            return [jnt.q for jnt in self.pjoints]
        else:
            return self.pjoints[index].q

    def set_qp(self, q):
        """Sets the joint variables of passive joints"""
        for i, jnt in enumerate(self.pjoints):
            jnt.q = q[i]

    def set_qm(self, q):
        """Sets the joint variables of moving joints"""
        for i, jnt in enumerate(self.mjoints):
            jnt.q = q[i]

    def get_end_joints(self):
        """Return the list of self.end and all joints with sameas == end."""
        ends = []
        for start, end in self.loops:
            l = ([end] +
                [jnt for jnt in self.joints if jnt.sameas is end])
            ends += l
        return ends

    def get_chains(self):
        """Return a list of serial chains constituting the mechanism

        Each serial chain starts with the joint following self.root and
        ends with self.end or a joint with sameas == self.end.
        """
        chains = []
        for root, end in self.loops:
            subchain = self.chain.get_subchain_to(end)
            remove_upto_root(subchain, root)
            chains.append(subchain)
        return chains

    def J(self, x):
        """Returns the squared difference of transformation matrices
        calculated from the common root for 2 chains of a loop as a cost function
        Is calculated for each loop at the same time."""
        from numpy import identity
        from numpy import dot, sum

        J = 0
        matrices = []
        for chain in self.chains:
            T = identity(4)
            for jnt in chain:
                T = dot(T, jnt.T)
            matrices.append(T)

        # There are 2 chains for each loop starting as transformation matrix from the common joint
        for i in range(0, len(matrices), 2):
            J += sum((matrices[i] - matrices[i+1])**2)
        return J

    def least_J(self, x):
        from numpy import identity
        from numpy import dot

        J = 0
        matrices = []
        for chain in self.chains:
            T = identity(4)
            for jnt in chain:
                T = dot(T, jnt.T)
            matrices.append(T)

	    from numpy import zeros
	    dy = zeros(12 * (len(matrices) // 2))
        for i in range(0, len(matrices), 2):
            dy[(i // 2) * 12: ((i // 2) + 1) * 12] = ((matrices[i][:3, :] - matrices[i+1][:3, :])**2).flatten()
        return dy

    def cost(self, x):
        self.set_qp(x)
        return self.J(x)

    def set_ub(self, ub_prismatic):
        self.ub_prismatic = ub_prismatic

    def solve(self):
        if len(self.pjoints) == 0:
            return
        from numpy import array
        from scipy.optimize import minimize
        qs = self.get_qp()
        x0 = array(qs)
        #bounds = self.set_bounds(self.pjoints)
        obj = minimize(self.cost, x0, tol=1e-7, method='L-BFGS-B')#, bounds=bounds)
        x = obj.x
        self.set_qp(x)
        return obj.fun

    def cost_all(self, x):
        self.set_qm(x)
        return self.J(x)

    def set_bounds(self, jnts):
        from math import pi
        bounds = []
        for i, jnt in enumerate(jnts):
            b = (-pi, pi) if jnt.isrevolute() else (0, self.ub_prismatic)
            bounds.append(b)
        return bounds

    def find_close(self):
        """Finds a solution by including active joints into optimization problem"""
        from numpy import array
        from scipy.optimize import minimize
        qs = self.get_qm()
        x0 = array(qs)
        #bounds = self.set_bounds(self.mjoints)
        obj = minimize(self.cost_all, x0, tol=1e-7, method = 'L-BFGS-B')
        x = obj.x
        self.set_qm(x)
        return obj.fun

    def find_random(self, max_cost):
        """Finds a solution by including active joints and initializing all the joints randomly"""
        from numpy import array
        from scipy.optimize import minimize
        qs = self.get_qm()
        x0 = array(qs)
        bounds = self.set_bounds(self.mjoints)
        from random import uniform
        for j in range(10):
            for i, jnt in enumerate(self.mjoints):
                x0[i] = uniform(bounds[i][0], bounds[i][1])
            obj = minimize(self.cost_all, x0, tol=1e-7, method = 'L-BFGS-B')
            if obj.fun < max_cost:
                break
        x = obj.x
        self.set_qm(x)
        return obj.fun
コード例 #47
0
ファイル: jacobians.py プロジェクト: mc0709/freecad-symoro
def serialKinematicJacobian(joints):
    """Return the kinematic Jacobian of a serial chain

    Passive joints are considered to be as if they were actuated. The end
    joint is the last joint in the list and the base joint is its farest
    antecedent.
    """
    from chain import Chain
    from chain import get_subchain_to

    chain = Chain(get_subchain_to(joint=joints[-1], joints=joints))

    # We need the position of last joint frame in the base joints frame
    from serialmechanism import end_transform
    Tend = end_transform(chain.joints)
    Pend = Tend[0:3, 3]

    from numpy.matlib import zeros, identity, cross
	# jacobian only includes moving joints
    jacobian = zeros((6, len(chain.get_mjoints())))
    T = identity(4)
    k = 0
    for jnt in chain.joints:
        T *= jnt.T
		# Take P and a vectors
        P = T[0:3, 3]
        a = T[0:3, 2]
        if jnt.isrevolute():
			# revolute: [ahat*(Pend-P), a]
            DP = Pend - P
            a_hat = zeros((3,3))
            a_hat[0, 1] = -a[2]
            a_hat[0, 2] = a[1]
            a_hat[1, 0] = a[2]
            a_hat[1, 2] = -a[0]
            a_hat[2, 0] = -a[1]
            a_hat[2, 1] = a[0]
            jacobian_trans = a_hat * DP
            #jacobian_trans = cross(P, DP, axis=0)
            jacobian_rot = a
        if jnt.isprismatic():
			# prismatic: [a, 0]
            jacobian_trans = a
            jacobian_rot = zeros((3, 1))
		# skip fixed joints
        if not(jnt.isfixed()):
            jacobian[:3, k] = jacobian_trans
            jacobian[3:, k] = jacobian_rot
            k += 1
    # If the last joint is fixed, apply the Jacobian transposition
    if (joints[-1].isfixed()):
        P = jnt.T[0:3, 2]
        jac_transposition = identity(6)
        jac_transposition[3, 1] = -P[2]
        jac_transposition[3, 2] = P[1]
        jac_transposition[4, 0] = P[2]
        jac_transposition[4, 2] = -P[0]
        jac_transposition[5, 0] = -P[1]
        jac_transposition[5, 1] = P[0]

        jacobian = jac_transposition * jacobian
    return jacobian
コード例 #48
0
 def testNone(self):
     chain1 = Chain(self.space, 20, pm.Vec2d(250, 0))
     self.assertFalse(None, chain1)
     self.assertFalse(None, chain1.get_end())
     self.assertFalse(None, chain1.get())
コード例 #49
0
ファイル: kinematics.py プロジェクト: Izzatbek/freecad-symoro
class Kinematics():
    def __init__(self, table, base_t=None, tool_t=None):
        self.table = table
        if base_t is None:
            self.base_t = [
                [1, 0, 0, 0],
                [0, 1, 0, 0],
                [0, 0, 1, 0],
                [0, 0, 0, 1]]
        else:
            self.base_t = base_t
        if tool_t is None:
            self.tool_t = [
                [1, 0, 0, 0],
                [0, 1, 0, 0],
                [0, 0, 1, 0],
                [0, 0, 0, 1]]
        else:
            self.tool_t = tool_t
        self.joints = get_joints_from_table(self.table)
        # List of actuated joints
        self.ajoints = self.get_ajoints()
        # List of passive joints
        self.pjoints = self.get_pjoints()
        # Actuated joints' variables
        self.q = self.get_q()
        # Passive joints' variables
        self.qp = self.get_qp()
        # Chains
        self.chain = Chain(self.joints)
        # Tuples (root, end) for each loop
        self.loops = get_loops(self.joints)
        # List of loop solvers
        from loop_solver import LoopSolver
        self.loopsolvers = [LoopSolver(self.joints, *l) for l in self.loops]

    # TODO: ajoints as property
    def get_ajoints(self):
        """Return the list of active joints, always in the same order"""
        ajoints = []
        for jnt in self.joints:
            if (jnt.isactuated()):
                ajoints.append(jnt)
        return ajoints

    # TODO: pjoints as property
    def get_pjoints(self):
        """Return the list of passive joints, always in the same order"""
        pjoints = []
        for jnt in self.joints:
            if (jnt.ispassive()):
                pjoints.append(jnt)
        return pjoints

    # TODO: cjoints as property
    def get_cjoints(self):
        """Return the list of cut joints, always in the same order"""
        cjoints = []
        for jnt in self.joints:
            if not(jnt.sameas is None):
                cjoints.append(jnt)

    # TODO: define q as property
    def get_q(self, index=None):
        """Returns the list of joint variables of active joints"""
        if (index is None):
            return [jnt.q for jnt in self.ajoints]
        else:
            return self.ajoints[index].q

    # TODO: define qp as property
    def get_qp(self, index=None):
        """Returns the list of joint variables of passive joints"""
        if (index is None):
            return [jnt.q for jnt in self.pjoints]
        else:
            return self.pjoints[index].q

    def set_q(self, q, index=None):
        if (index is None):
            index = range(len(self.ajoints))
        try:
            self.ajoints[index].q = q
        except (AttributeError, TypeError):
            for i in index:
                self.ajoints[i].q = q[i]

    def set_qp(self, q, index=None):
        if (index is None):
            index = range(len(self.pjoints))
        try:
            self.pjoints[index].q = q
        except (AttributeError, TypeError):
            for i in index:
                self.pjoints[i].q = q[i]

    def get_joint_transform(self, joint):
        """Return the transform from base to joint

        Return a tuple (m, Pjminus1), where m is the transformation
        matrix from base (not base joint) to the joint, and Pjminus1 is the
        position of previous joint.
        """
        # Get the list of joints from base to joint

        # The transform from the base to the base joint is given by
        # self.base_t.
        l = self.base_t[0] + self.base_t[1] + self.base_t[2] + self.base_t[3]
        from FreeCAD import Base
        m = Base.Matrix(*l)
        Pj = Base.Vector(0, 0, 0)
        for jnt in self.chain.get_subchain_to(joint):
            # Pjminus1 is Pj from the step before
            Pjminus1 = Pj
            m *= Ttomatrix(jnt.T)
            Pj = Base.Vector(m.A14, m.A24, m.A34)
        return m, Pjminus1

    def solve_loops(self):
        for ls in self.loopsolvers:
            ls.solve()
コード例 #50
0
ファイル: test.py プロジェクト: zhilinwww/jbc
block_one_dir_invalid = {"nonce": "1225518", "index": "1", "hash": "00000c575050241e0a4df1acd7e6fb90cc1f599e2cc2908ec8225e10915006cc", "timestamp": "1508895386", "prev_hash": "0000010101193736271818923939229219283747438293874548392789878987", "data": "I block #1"}
block_one_invalid_obj = Block(block_one_dir_invalid)
assert not block_one_invalid_obj.is_valid()
#nonce doesn't give it the correct hash
block_one_dir_invalid = {"nonce": "1000000", "index": "1", "hash": "00000c575050241e0a4df1acd7e6fb90cc1f599e2cc2908ec8225e10915006cc", "timestamp": "1508895386", "prev_hash": "000002f9c703dc80340c08462a0d6acdac9d0e10eb4190f6e57af6bb0850d03c", "data": "I block #1"}
block_one_invalid_obj = Block(block_one_dir_invalid)
assert not block_one_invalid_obj.is_valid()


#####################################
#
#  Bringing Chains into play
#
#####################################

blockchain = Chain([block_zero, block_one, block_two])
assert blockchain.is_valid()
assert len(blockchain) == 3

empty_chain = Chain([])
assert len(empty_chain) == 0
empty_chain.add_block(block_zero)
assert len(empty_chain) == 1
empty_chain = Chain([])
assert len(empty_chain) == 0

another_blockchain = Chain([another_block_zero, another_block_one, another_block_two])
assert another_blockchain.is_valid()
assert len(another_blockchain) == 3

assert blockchain == another_blockchain
コード例 #51
0
ファイル: chains.py プロジェクト: echelon/molecular-sdg
def identify_chains(mol, rings=None):
	"""
	Identify all chains in the molecule.
	"""
	
	def build_connect_mat(mol):
		"""Build a connection matrix we can use in Floyd's algorithm"""
		inf = float('Infinity') # 'Not connected' is repr by infinity
		newMat = [[inf for x in range(mol.size)] for y in range(mol.size)]
		for i in range(mol.size):
			for j in range(mol.size):
				val = mol.connectMat[i][j]
				val = inf if not val else 1
				newMat[i][j] = val
		return newMat

	def get_core_vertices(coreFlags):
		"""Get a list of core vertices from core chain flags"""
		core = []
		for i in range(len(coreFlags)):
			if coreFlags[i]:
				core.append(i)
		return core

	def get_noncore_vertices(coreFlags):
		"""Get a list of non-core vertices from core chain flags."""
		noncore = []
		for i in range(len(coreFlags)):
			if not coreFlags[i]:
				noncore.append(i)
		return noncore

	def disconnect_vertices(matrix, removeVerts = []):
		"""Disconnect the vertices specified from the adj matrix"""
		inf = float('Infinity')
		length = len(matrix)
		for vert in removeVerts:
			for i in range(length):
				matrix[i][vert] = inf
				matrix[vert][i] = inf

	def find_maxweight_verts(shortestPaths):
		"""
		Find the maximum weight in a ShortestPaths object. We want to 
		find the longest chains, not the shortest ones. 
		"""
		inf = float('Infinity')
		DNE = [inf, -inf, None]
		length = shortestPaths.size()
		maxWeight = -1
		maxPath = (-1, -1)
		for i in range(length):
			for j in range(length):
				weight = shortestPaths.getWeight(i, j)
				if weight in DNE:
					continue
				if weight > maxWeight:
					maxWeight = weight
					maxPath = (i, j)

		return maxPath

	# Ring atoms cannot be considered in chain perception. 
	ringAtoms = []
	if rings:
		ratoms = []
		for r in rings:
			ratoms += r
		ringAtoms = list(set(ratoms))

	# Find, identify the "core chain" atoms
	coreFlags = _identify_core_chain_atoms(mol, ringAtoms)

	# Copy the molecule's connectivity matrix, then specify that all
	# non-"core chain" atoms are to be removed in the first pass.
	connectMat = build_connect_mat(mol)
	removeAtoms = get_noncore_vertices(coreFlags)

	# Don't reuse capping substituents.
	unusableCaps = []

	# Get all of the chains, starting with the longest. 
	# Assign arbitrary capping substituents. 
	chains = []
	while True:
		# Disconnect the specified atoms from the connection matrix
		# This ensures no two chains consist of the same atoms. 
		disconnect_vertices(connectMat, removeAtoms)

		# (Re)calculate the shortest paths, and extract the longest chain
		s = ShortestPaths(connectMat)
		verts = find_maxweight_verts(s)
		chain = s.findPath(verts[0], verts[1], include=True)

		# FIXME: I assume this is the only condition that breaks the loop...
		if type(chain) != list:
			break

		def reserve_end_cap(chain, pos):
			"""
			Choose an end cap atom arbitrarily; the cap cannot be part
			of the chain, nor can it be previously selected.
			"""
			atom = chain[pos]
			diff = frozenset(mol.alphaAtoms[atom]) - frozenset(chain)
			diff -= frozenset(unusableCaps)
			if not diff:
				cap = chain.pop(pos)
			else:
				cap = list(diff)[0]
			unusableCaps.append(cap)
			unusableCaps.extend(list(chain))
			return cap

		end1 = reserve_end_cap(chain, 0)
		end2 = reserve_end_cap(chain, -1)

		# Save the chain, end caps, then set up the removal of all
		# atoms that were in it. 
		removeAtoms = chain[:]
		removeAtoms.extend([end1, end2])

		chain = Chain(chain)
		chain.caps = (end1, end2) # FIXME: Not a good interface!

		chains.append(chain)

	return chains
コード例 #52
0
ファイル: test_chain.py プロジェクト: Izzatbek/freecad-symoro
    'r': 0,
    }

jnt1 = Joint(antc=None, **dummy_jnt_parameters)
jnt2 = Joint(antc=jnt1, **dummy_jnt_parameters)
jnt3 = Joint(antc=jnt2, **dummy_jnt_parameters)
jnt4 = Joint(antc=jnt2, **dummy_jnt_parameters)
jnt5 = Joint(antc=jnt4, **dummy_jnt_parameters)
joints = [jnt1, jnt2, jnt3]
#print(jnt1)
#print(jnt2)
#print(jnt3)
#print(jnt4)
#print(jnt5)

chain = Chain(joints)
#print([chain.get_subchain_from(jnt) for jnt in get_children(joints, jnt1)])
assert(chain.base == jnt1)
assert(chain.tools == [jnt3])
assert(get_children(joints, jnt1) == [jnt2])
assert(get_children(joints, jnt2) == [jnt3])
assert(chain.get_subchain_from(jnt3) == [jnt3])
assert(chain.get_subchain_from(jnt2) == [jnt2, jnt3])
assert(chain.get_subchain_from(jnt1) == [jnt1, jnt2, jnt3])
assert(chain.get_subchain_to(jnt1) == [jnt1])
assert(chain.get_subchain_to(jnt2) == [jnt1, jnt2])
assert(chain.get_subchain_to(jnt3) == [jnt1, jnt2, jnt3])

joints = [jnt1, jnt2, jnt3, jnt4]
chain = Chain(joints)
assert(chain.base == jnt1)
コード例 #53
0
ファイル: hierview.py プロジェクト: crosvera/ProDy
    def update(self, **kwargs):
        """Rebuild hierarchical view of atoms.  This method is called at 
        instantiation, but can be used to rebuild the hierarchical view 
        when attributes of atoms change."""
        
        array = np.array
        atoms = self._atoms
        if isinstance(atoms, AtomGroup):
            ag = atoms
            _indices = np.arange(ag._n_atoms)
            selstr = False
        else:
            ag = atoms.getAtomGroup()
            _indices = atoms._indices
            selstr = atoms.getSelstr()
        
        acsi = self._atoms.getACSIndex()
        
        n_atoms = len(ag)
        self._dict = _dict = dict()
        self._chains = _chains = list()
        self._residues = _residues = list()
        self._segments = _segments = list()

        segindex = -1
        segindices = np.zeros(n_atoms, int)
        chindex = -1
        chindices = np.zeros(n_atoms, int)
        resindex = -1
        resindices = np.zeros(n_atoms, int)
        
        sgnms = ag._getSegnames()
        if sgnms is None:
            _segments = None
        else:
            if selstr:
                sgnms = sgnms[_indices]
            unique = np.unique(sgnms)
            s = sgnms[0]
            if len(unique) == 1:
                if  s != '':
                    segment = Segment(ag, _indices, acsi=acsi, unique=True, 
                                      selstr=selstr)
                    _dict[s] = segment
                    _segments.append(segment)
                    LOGGER.info('Hierarchical view contains segments.')
                else: 
                    _segments = None
            else:
                ps = None
                for i, s in enumerate(sgnms):
                    if s == ps or s in _dict:
                        continue
                    ps = s
                    segindex += 1
                    idx = _indices[i:][sgnms[i:] == s]
                    segment = Segment(ag, idx, acsi=acsi, unique=True, 
                                       selstr=selstr)
                    segindices[idx] = segindex
                    _dict[s] = segment
                    _segments.append(segment)
                LOGGER.info('Hierarchical view contains segments.')

        chids = ag._getChids()
        if chids is None:
            _chains = None
        else:
            if selstr:
                chids = chids[_indices]
            if _segments is None:
                if len(np.unique(chids)) == 1:
                    chain = Chain(ag, _indices, acsi=acsi, unique=True)
                    _dict[(None, chids[0] or None)] = chain
                    _chains.append(chain)
                else:
                    pc = None
                    for i, c in enumerate(chids):
                        if c == pc or (None, c) in _dict:
                            continue
                        pc = c
                        chindex += 1
                        idx = _indices[i:][chids[i:] == c]
                        chain = Chain(ag, idx, acsi=acsi, unique=True)
                        chindices[idx] = chindex
                        _dict[(None, c)] = chain
                        _chains.append(chain)
            else:
                pc = chids[0]
                ps = sgnms[0]
                _i = 0
                for i, c in enumerate(chids):
                    s = sgnms[i]
                    if c == pc and s == ps:
                        continue
                    s_c = (ps, pc or None)
                    chain = _dict.get(s_c)
                    if chain is None:
                        segment = _dict[ps]
                        chindex += 1
                        idx = _indices[_i:i]
                        chain = Chain(ag, idx, acsi=acsi, segment=segment, 
                                       unique=True)
                        chindices[idx] = chindex
                        _dict[s_c] = chain
                        segment._dict[pc] = len(segment._list)
                        segment._list.append(chain)
                        _chains.append(chain)
                    else:
                        idx = _indices[_i:i]
                        chindices[idx] = chain._indices[0]
                        chain._indices = np.concatenate((chain._indices, idx))
                    pc = c
                    ps = s
                    _i = i
                s_c = (ps, pc or None)
                chain = _dict.get(s_c)
                idx = _indices[_i:]
                if chain is None:
                    segment = _dict[ps]
                    chindex += 1
                    chindices[idx] = chindex
                    chain = Chain(ag, idx, acsi=acsi, segment=segment, 
                                   unique=True)
                    _dict[s_c] = chain
                    segment._dict[pc] = len(segment._list)
                    segment._list.append(chain)
                    _chains.append(chain)
                else:
                    chindices[idx] = chain._indices[0]
                    chain._indices = np.concatenate((chain._indices, idx)) 
        
        if kwargs.get('chain') == True:
            return
    
        rnums = ag._getResnums()
        if rnums is None:
            raise ValueError('resnums are not set')
        if selstr:
            rnums = rnums[_indices]
        nones = None
        if _segments is None:
            if nones is None:
                nones = [None] * len(rnums)
            sgnms = nones
        if _chains is None:
            if nones is None:
                nones = [None] * len(rnums)
            chids = nones
        icods = ag._getIcodes()
        if icods is None:
            if nones is None:
                nones = [None] * len(rnums)
            icods = nones
        elif selstr:
                icods = icods[_indices]

        pr = rnums[0]
        pi = icods[0] or None          
        pc = chids[0]
        ps = sgnms[0] or None
        _j = 0
        for j, r in enumerate(rnums):
            i = icods[j] or None
            c = chids[j] or None
            s = sgnms[j]
            if r != pr or i != pi or c != pc or s != ps:
                s_c_r_i = (ps, pc, pr, pi)
                res = _dict.get(s_c_r_i)
                if res is None:
                    chain = _dict.get((ps, pc))
                    resindex += 1
                    idx = _indices[_j:j]
                    res = Residue(ag, idx, acsi=acsi, chain=chain, 
                                   unique=True, selstr=selstr)
                    resindices[idx] = resindex
                    if chain is not None:
                        chain._dict[(pr, pi)] = len(chain._list)
                        chain._list.append(res)
                    _residues.append(res)
                    _dict[s_c_r_i] = res
                else:
                    res._indices = np.concatenate((res._indices, 
                                                   _indices[_j:j]))
                ps = s
                pc = c
                pr = r
                pi = i
                _j = j 
        s_c_r_i = (ps, pc, pr, pi)
        res = _dict.get(s_c_r_i)
        idx = _indices[_j:]
        if res is None:
            chain = _dict.get((ps, pc))
            resindex += 1
            res = Residue(ag, idx, acsi=acsi, chain=chain, unique=True, 
                           selstr=selstr)
            resindices[idx] = resindex
            if chain is not None:
                chain._dict[(pr, pi)] = len(chain._list)
                chain._list.append(res)
            _residues.append(res)
            _dict[s_c_r_i] = res
        else:
            resindices[idx] = res._indices[0]
            res._indices = np.concatenate((res._indices, idx))
        
        ag._data['segindices'] = segindices
        ag._data['chindices'] = chindices
        ag._data['resindices'] = resindices