Exemple #1
0
def wetbulb(T, p, q=None, Td=None, w=None, cpd=None, Rd=None, Rv=None, Lv=None):
  '''
  Return wet-bulb temperature T_w [K]
  given temperature T [K], pressure p [mb] and exactly one of the following:

    - Specific humidity q [g/kg]
    - Dew-point temperature Td [K]
    - Mass-mixing ratio w [g/kg]

  Specific humidity q can be given as a positional argument while Td and w must
  be named explicitly.
  Uses Bohren & Albrecht p 283 eq 6.70.
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  if cpd is None: cpd = Parameters()['Cpd']
  if Lv is None: Lv = Parameters()['Lv']

  if not q is None:
      return squeeze(_thermodyn.wetbulb_from_q(cpd, Lv, Rd, Rv, T, p, q))
  elif not Td is None:
      return squeeze(_thermodyn.wetbulb_from_tdew(cpd, Lv, Rd, Rv, T, p, Td))
  elif not w is None:
      return squeeze(_thermodyn.wetbulb_from_w(cpd, Lv, Rd, Rv, T, p, w))
  else:
      print "CliMT error: Function thermodyn.wetbulb requires one of q, Td, or \
Exemple #2
0
def wetbulb(T, p, q=None, Td=None, w=None, cpd=None, Rd=None, Rv=None, Lv=None):
    """
  Return wet-bulb temperature T_w [K]
  given temperature T [K], pressure p [mb] and exactly one of the following:

    - Specific humidity q [g/kg]
    - Dew-point temperature Td [K]
    - Mass-mixing ratio w [g/kg]

  Specific humidity q can be given as a positional argument while Td and w must
  be named explicitly.
  Uses Bohren & Albrecht p 283 eq 6.70.
  """
    if Rd is None:
        Rd = Parameters()["Rd"]
    if Rv is None:
        Rv = Parameters()["Rv"]
    if cpd is None:
        cpd = Parameters()["Cpd"]
    if Lv is None:
        Lv = Parameters()["Lv"]

    if not q is None:
        return squeeze(_thermodyn.wetbulb_from_q(cpd, Lv, Rd, Rv, T, p, q))
    elif not Td is None:
        return squeeze(_thermodyn.wetbulb_from_tdew(cpd, Lv, Rd, Rv, T, p, Td))
    elif not w is None:
        return squeeze(_thermodyn.wetbulb_from_w(cpd, Lv, Rd, Rv, T, p, w))
    else:
        print "CliMT error: Function thermodyn.wetbulb requires one of q, Td, or \
Exemple #3
0
 def sanitize_item(cls, key, val):
     key = squeeze(key)
     val = squeeze(val)
     if val:
         func_name = cls.item_lookup.get(key, key)
         if func_name.startswith('sanitize'):
             func_name = 'default'
         val = getattr(cls, func_name, cls.default)(val)
     return key, val
Exemple #4
0
    def __call__(self, videos):
        """Support joint transform
    :param videos: np.ndarray or a list of np.ndarray
    :return:
    """
        videos = utils.unsqueeze(videos)
        h_orig, w_orig = videos[0].shape[-3:-1]

        for attempt in range(10):
            ret = []

            area = h_orig * w_orig
            target_area = random.uniform(0.4, 1.0) * area
            aspect_ratio = random.uniform(3. / 4, 4. / 3)

            w = int(round(math.sqrt(target_area * aspect_ratio)))
            h = int(round(math.sqrt(target_area / aspect_ratio)))

            if random.random() < 0.5:
                w, h = h, w

            if w <= w_orig and h <= h_orig:
                x1 = random.randint(0, w_orig - w)
                y1 = random.randint(0, h_orig - h)

                for tp, video in zip(self.transform_pixel, videos):
                    video = video[..., y1:y1 + h, x1:x1 + w, :]
                    video = imgproc.resize(video, (self.size, self.size),
                                           'bilinear')
                    if tp:
                        video[
                            ...,
                            0] = (video[..., 0] - 128) * (self.size / w) + 128
                        video[
                            ...,
                            1] = (video[..., 1] - 128) * (self.size / h) + 128

                    ret.append(video)

                return utils.squeeze(ret)

        # Fallback
        ret = []
        scales = [
            Scale(self.size, tp, 'bilinear') for tp in self.transform_pixel
        ]
        crop = CenterCrop(self.size)
        for scale, video in zip(scales, videos):
            video = crop(scale(video))
            ret.append(video)

        return utils.squeeze(ret)
def calculate_user_preferences(user_id_list: str=None, start: str=Config.train_start, end:str=Config.train_end, save_path: str='./dev_recommendation_sources/'):
    if user_id_list == 'dev':
        user_id_list = load_pickle(Config.dev_user_list)
    elif user_id_list == 'test':
        user_id_list = load_pickle(Config.test_user_list)
    elif isinstance(user_id_list, str): # 파일 경로를 입력할 경우
        user_id_list = load_pickle(user_id_list)

    print('Load calculating tools...', end='\t')
    try:
        user_time_read = load_user_time_read(Config.user_time_read)
        post_id_encoder = PostIdEncoder(Config.encodings_root)
        tfidf_generator = TFIDFGenerator(Config.tfidf_root)
    except: # When import in Jupyter Notebook
        user_time_read = load_user_time_read("../preprocessed/user_time_read.json")
        post_id_encoder = PostIdEncoder("../encodings/")
        tfidf_generator = TFIDFGenerator("../tfidf")
    print('loaded!')

    post_meta_id = [] # user_id_list의 유저들이 조회한 모든 글
    posts_raw = [] # 유저들의 로그에서 등장한 모든 글을 담을 리스트
    user_preferences_raw = [] # 유저들의 feature 벡터를 담을 리스트

    for user_id in tqdm(user_id_list, desc=f'User Preference Extraction ({start}-{end})'):
        # 설정한 구간에 대한 해당 유저의 로그
        history = filter_read_by_time(user_time_read[user_id], start, end) 
        history = squeeze(list(map(lambda x: x[-1], history)))

        # 유저 로그로부터 TF-IDF 행렬 생성
        user_tfidf = tfidf_generator.generate(post_id_encoder.transform(history), drop_id=False) # 

        # TF-IDF 행렬로부터 유저 feature 벡터를 생성
        preference = sparse.csr_matrix(user_tfidf.iloc[:, 1:].values.sum(axis=0)[:, np.newaxis]) # post_meta_id 컬럼을 제외한 뒤 summation
        user_tfidf = user_tfidf.groupby('post_meta_id').first().reset_index() # faster than drop_duplicates()
        user_tfidf = user_tfidf.loc[~user_tfidf['post_meta_id'].isin(post_meta_id), :]
        if len(user_tfidf) > 0:
            post_meta_id.extend(user_tfidf['post_meta_id'].tolist())
        posts_raw.append(sparse.csr_matrix(user_tfidf.iloc[:, 1:])) # post_meta_id 컬럼을 제외하고 append -> post_meta_id 리스트를 개별적으로 생성하므로 불필요
        user_preferences_raw.append(preference)

    print('Postprocessing...', end='\t')
    posts = sparse.vstack(posts_raw)
    user_preferences = sparse.hstack(user_preferences_raw)
    idf = np.array(np.log(tfidf_generator.DF.values.squeeze()) - np.log((posts != 0).sum(axis=0) + 1e-4)) # 1e-4: to prevent ZeroDivisionError
    recommend_output = (posts.multiply(idf)).dot(user_preferences)
    print('finished!')

    if save_path:
        interval = f'{start}-{end}'
        if interval not in os.listdir(save_path):
            os.mkdir(os.path.join(save_path, interval))
            save_path = os.path.join(save_path, interval)
        sparse.save_npz(os.path.join(save_path, f'recommend_output.npz'), recommend_output)
        sparse.save_npz(os.path.join(save_path, f'user_preferences.npz'), user_preferences)
        np.save(os.path.join(save_path, f'idf.npy'), idf)
        sparse.save_npz(os.path.join(save_path, f'posts.npz'), posts)
        save_as_pickle(os.path.join(save_path, f'post_meta_id.pkl'), post_meta_id)
        print(f'Saved successfully in {save_path}😎')
    else:
        return recommend_output, user_preferences, idf, posts, post_meta_id
Exemple #6
0
 def __getitem__(self, key):
     for obj in [self.Params, self.Grid, self.State]:
         if key in obj:
             if type(obj[key]) is type('string'): return obj[key]
             else: return squeeze(obj[key])
     raise IndexError, '\n\n CliMT.State: %s not in Params, Grid or State' % str(
         key)
Exemple #7
0
def thetae_old(T, p, q):
    """
  Return equivalent potential temperature thetae [K]
  given temperature T [K], pressure p [mb], specific humidity q [g/kg].
  Use Eq. (38) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  """
    return squeeze(_thermodyn.thetae_old(T, p, q))
Exemple #8
0
def CAPE(p, T, q, Virtual=True, MixedLayerDepth=100.0, Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None):
    """
    Returns convective available potential energy [J/kg]
    given pressure p [mb], temperature T [K], spec. humidity q [g/kg].
    Use average parcel properties over layer of depth MixedLayerDepth [mb] above ground.
    If Virtual=True, apply virtual temp correction.
    """
    if Rd is None:
        Rd = Parameters()["Rd"]
    if Rv is None:
        Rv = Parameters()["Rv"]
    if lv0 is None:
        lv0 = Parameters()["Lv"]
    if cpd is None:
        cpd = Parameters()["Cpd"]
    if cpv is None:
        cpv = Parameters()["Cpv"]
    if cl is None:
        cl = Parameters()["Cl"]

    if alltrue(p[0] > p[-1]):
        p = p[::-1]
        T = T[::-1]
        q = q[::-1]

    if Virtual:
        ivirt = 1
    else:
        ivirt = 0

    return squeeze(_thermodyn.cape(Rd, Rv, lv0, cpd, cpv, cl, ivirt, MixedLayerDepth, p, T, q))
Exemple #9
0
def _get_idf(batch: pd.DataFrame, vocab: list, num_docs: int) -> pd.DataFrame:
    """batch 데이터의 IDF 리턴
        - IDF 방식: logarithmic
        - Reference: https://ko.wikipedia.org/wiki/Tf-idf

    Args:
        batch (pd.DataFrame): metadata의 일부
        vocab (list): IDF에 활용할 키워드 리스트
        num_docs (int): metadata(모집단)의 모든 문서, 즉 행의 개수

    Returns:
        pd.DataFrame: (1, vocab 수) 크기의 IDF 행렬
    """
    vocab_size = len(vocab)
    idf = pd.DataFrame(np.zeros((1, vocab_size)), columns=vocab)

    kwd_list = set(squeeze(batch["keyword_list"].tolist()))
    kwd_list_filtered = list(filter(lambda x: x in vocab, kwd_list))

    pbar = tqdm(kwd_list_filtered)
    pbar.set_description("Getting IDF")
    for tag in pbar:
        if tag in vocab:
            idf[tag] = np.log(num_docs) - np.log(
                sum(batch["keyword_list"].apply(lambda x: tag in x)))

    return idf
Exemple #10
0
def theta_old(T,p,q):
  '''
  Return potential temperature theta [K]
  given temperature T [K], pressure p [mb], specific humidity q [g/kg].
  Use Eq. (7) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.theta_old(T,p,q))
Exemple #11
0
def thetae(T, p, wt=None, Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None, p0=None):
    """
  Return equivalent potential temperature thetae [K]
  given temperature T [K], pressure p [mb], and
  total water mass mixing ratio wt [g/kg].
  If wt is not specified, parcel is assumed to be just saturated (no condensate).
  If wt is specified and is less that saturation mixing ratio,
  thetae is computed at the lifting condensation level.
  Uses Eq. (6.119) in Bohren+Albrecht, p.293.
  """
    if Rd is None:
        Rd = Parameters()["Rd"]
    if Rv is None:
        Rv = Parameters()["Rv"]
    if lv0 is None:
        lv0 = Parameters()["Lv"]
    if cpd is None:
        cpd = Parameters()["Cpd"]
    if cpv is None:
        cpv = Parameters()["Cpv"]
    if cl is None:
        cl = Parameters()["Cl"]
    if p0 is None:
        p0 = 1000.0
    if wt is None:
        wt = -1.0 * ones(shape(T))
    return squeeze(_thermodyn.thetae(Rd, Rv, lv0, cpd, cpv, cl, p0, p, T, wt))
Exemple #12
0
def es(T):
  '''
  Return saturation partial pressure of water vapour es [mb]
  at temperature T [K]
  Use Boren and Albrecht "Atmospheric Thermodynamics", p 198
  '''
  return squeeze(_thermodyn.es(T))
Exemple #13
0
def theta_old(T,p,q):
  '''
  Return potential temperature theta [K]
  given temperature T [K], pressure p [mb], specific humidity q [g/kg].
  Use Eq. (7) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.theta_old(T,p,q))
Exemple #14
0
def tdew(p,q):
  '''
  Return dew point temperature tdew  [K]
  at pressure p [mb] and specific humidity q [g/kg]
  Use Eq. (11) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.tdew(p,q))
Exemple #15
0
def pseudoadiab(p, p0, T0, thetae=None, \
                Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None):
    '''
    Returns the temperature [K] at the pressure levels p [mb] along the
    psudoadiabat identified by pressure p0 [mb] and temperature T0 [K].
    If equivalent potential temp thetae is input, then it identifies the
    adiabat overriding p0,T0.
    '''
    if Rd  is None: Rd=Parameters()['Rd']
    if Rv  is None: Rv=Parameters()['Rv']
    if lv0 is None: lv0=Parameters()['Lv']
    if cpd is None: cpd=Parameters()['Cpd']
    if cpv is None: cpv=Parameters()['Cpv']
    if cl  is None: cl=Parameters()['Cl']

    if thetae is not None:
        p0 = zeros(shape(thetae)) + 1.
        T0 = thetae*(p0/1000.)**(Rd/cpd)

    flip=False
    if alltrue(p[0]>p[-1]):
        p=p[::-1]
        flip=True

    T=squeeze(_thermodyn.pseudoadiab(Rd,Rv,lv0,cpd,cpv,cl,p,p0,T0))

    if flip: T = T[::-1]
    return T
Exemple #16
0
def tdew(p,q):
  '''
  Return dew point temperature tdew  [K] 
  at pressure p [mb] and specific humidity q [g/kg]
  Use Eq. (11) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.tdew(p,q))
Exemple #17
0
def recommend(recommend_src: str = Config.recommend_src,
              output_root: str = Config.output_root):
    print('Loading recommendation tools...', end='\t')
    user_time_read = load_user_time_read(Config.user_time_read)
    user_id_list = load_pickle(Config.test_user_list)
    recommend_meta = sparse.load_npz(
        os.path.join(recommend_src, 'recommend_output.npz'))
    post_meta_id = load_pickle(os.path.join(recommend_src, 'post_meta_id.pkl'))
    encoder = PostIdEncoder(Config.encodings_root)
    print('loaded!')

    with open(os.path.join(output_root, 'recommend.txt'), 'w') as file:
        for idx in tqdm(range(len(user_id_list)),
                        desc='Generate Recommendations'):
            user_id = user_id_list[idx]

            seens = filter_read_by_time(user_time_read[user_id],
                                        start=Config.train_start,
                                        end=Config.train_end)
            seens = squeeze(list(map(lambda x: x[-1], seens)))

            recommend_raw = recommend_meta[:, idx]
            recommend_raw = pd.Series(
                recommend_raw.toarray().flatten(),
                index=post_meta_id).sort_values(ascending=False)
            recommend_raw = recommend_raw[
                ~recommend_raw.index.isin(seens)].head(100).index.tolist()
            recommend = list(
                map(lambda x: encoder.inverse_transform(x), recommend_raw))

            file.write(f'{user_id} ')
            file.write(f"{' '.join(recommend)}\n")

    print(f"Saved 'recommend.txt' in {output_root}")
Exemple #18
0
def pseudoadiab(p, p0, T0, thetae=None, \
                Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None):
    '''
    Returns the temperature [K] at the pressure levels p [mb] along the
    psudoadiabat identified by pressure p0 [mb] and temperature T0 [K].
    If equivalent potential temp thetae is input, then it identifies the
    adiabat overriding p0,T0.
    '''
    if Rd  is None: Rd=Parameters()['Rd']
    if Rv  is None: Rv=Parameters()['Rv']
    if lv0 is None: lv0=Parameters()['Lv']
    if cpd is None: cpd=Parameters()['Cpd']
    if cpv is None: cpv=Parameters()['Cpv']
    if cl  is None: cl=Parameters()['Cl']

    if thetae is not None:
        p0 = zeros(shape(thetae)) + 1.
        T0 = thetae*(p0/1000.)**(Rd/cpd)
        
    flip=False
    if alltrue(p[0]>p[-1]):
        p=p[::-1]
        flip=True

    T=squeeze(_thermodyn.pseudoadiab(Rd,Rv,lv0,cpd,cpv,cl,p,p0,T0))

    if flip: T = T[::-1]
    return T
Exemple #19
0
def CINE(p, T, q, Virtual=True, MixedLayerDepth=100.,\
         Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None):
    '''
    Returns convective inhibition energy [J/kg]
    given pressure p [mb], temperature T [K], spec. humidity q [g/kg]
    Use average parcel properties over layer of depth MixedLayerDepth [mb] above ground.
    If Virtual=True, apply virtual temp correction.
    '''
    if Rd  is None: Rd=Parameters()['Rd']
    if Rv  is None: Rv=Parameters()['Rv']
    if lv0 is None: lv0=Parameters()['Lv']
    if cpd is None: cpd=Parameters()['Cpd']
    if cpv is None: cpv=Parameters()['Cpv']
    if cl  is None: cl=Parameters()['Cl']

    if p[0]>p[-1]:
        p=p[::-1]
        T=T[::-1]
        q=q[::-1]

    if Virtual:
        ivirt = 1
    else:
        ivirt = 0
        
    return squeeze(_thermodyn.cine(Rd,Rv,lv0,cpd,cpv,cl,ivirt,MixedLayerDepth,p,T,q))
Exemple #20
0
def CINE(p, T, q, Virtual=True, MixedLayerDepth=100.,\
         Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None):
    '''
    Returns convective inhibition energy [J/kg]
    given pressure p [mb], temperature T [K], spec. humidity q [g/kg]
    Use average parcel properties over layer of depth MixedLayerDepth [mb] above ground.
    If Virtual=True, apply virtual temp correction.
    '''
    if Rd  is None: Rd=Parameters()['Rd']
    if Rv  is None: Rv=Parameters()['Rv']
    if lv0 is None: lv0=Parameters()['Lv']
    if cpd is None: cpd=Parameters()['Cpd']
    if cpv is None: cpv=Parameters()['Cpv']
    if cl  is None: cl=Parameters()['Cl']

    if p[0]>p[-1]:
        p=p[::-1]
        T=T[::-1]
        q=q[::-1]

    if Virtual:
        ivirt = 1
    else:
        ivirt = 0

    return squeeze(_thermodyn.cine(Rd,Rv,lv0,cpd,cpv,cl,ivirt,MixedLayerDepth,p,T,q))
Exemple #21
0
def es(T):
  '''
  Return saturation partial pressure of water vapour es [mb]
  at temperature T [K]
  Use Boren and Albrecht "Atmospheric Thermodynamics", p 198
  '''
  return squeeze(_thermodyn.es(T))
Exemple #22
0
    def squeeze(self, dimension=None):
        """Return a new Variable object with squeezed data.

        Parameters
        ----------
        dimensions : None or str or tuple of str, optional
            Selects a subset of the length one dimensions. If a dimension is
            selected with length greater than one, an error is raised. If
            None, all length one dimensions are squeezed.

        Returns
        -------
        squeezed : Variable
            This array, but with with all or a subset of the dimensions of
            length 1 removed.

        Notes
        -----
        Although this operation returns a view of this variable's data, it is
        not lazy -- the data will be fully loaded.

        See Also
        --------
        numpy.squeeze
        """
        dimensions = dict(zip(self.dimensions, self.shape))
        return utils.squeeze(self, dimensions, dimension)
Exemple #23
0
def pdryadiab(T,theta,q):
  '''
  Return pressure level pdryadiab [mb] at which temperature is T [K]
  on the dry (i.e. unsaturated) adiabat identified by
  potential temperature theta [K] and specific humidity q [g/kg].
  Use Eq. (7) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.pdryadiab(T,theta,q))
Exemple #24
0
def tdryadiab(theta,p,q):
  '''
  Return temperature tdryadiab [K] at level p [mb]
  on the dry (i.e. unsaturated) adiabat identified by
  potential temperature theta [K] and specific humidity q [g/kg].
  Use Eq. (7) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.tdryadiab(theta,p,q))
Exemple #25
0
def tstar(T,p,q):
  '''
  Return saturation point temperature tstar [K]
  (i.e. temperature at lifting condensation level)
  given temperature T [K], pressure p [mb], specific humidity q [g/kg].
  Use Eq. (12) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.tstar(T,p,q))
Exemple #26
0
def tdryadiab(theta,p,q):
  '''
  Return temperature tdryadiab [K] at level p [mb] 
  on the dry (i.e. unsaturated) adiabat identified by 
  potential temperature theta [K] and specific humidity q [g/kg].
  Use Eq. (7) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.tdryadiab(theta,p,q))
Exemple #27
0
def pdryadiab(T,theta,q):
  '''
  Return pressure level pdryadiab [mb] at which temperature is T [K]
  on the dry (i.e. unsaturated) adiabat identified by 
  potential temperature theta [K] and specific humidity q [g/kg].
  Use Eq. (7) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.pdryadiab(T,theta,q))
Exemple #28
0
def tstar(T,p,q):
  '''
  Return saturation point temperature tstar [K]
  (i.e. temperature at lifting condensation level) 
  given temperature T [K], pressure p [mb], specific humidity q [g/kg].
  Use Eq. (12) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.tstar(T,p,q))
Exemple #29
0
def qs(T, p, Rd=None, Rv=None):
  '''
  Return saturation specific humidity qs [g/kg]
  at temperature T [K] and pressure p [mb]
  Use Boren and Albrecht "Atmospheric Thermodynamics", p 186
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  return squeeze(_thermodyn.qs(Rd,Rv,T,p))
Exemple #30
0
def ws(T, p, Rd=None, Rv=None):
  '''
  Return saturation mass mixing ration of water vapour ws [g/kg]
  at temperature T [K] and pressure p [mb]
  Use Boren and Albrecht "Atmospheric Thermodynamics", p 186
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  return squeeze(_thermodyn.ws(Rd,Rv,T,p))
Exemple #31
0
def qs(T, p, Rd=None, Rv=None):
  '''
  Return saturation specific humidity qs [g/kg]
  at temperature T [K] and pressure p [mb]
  Use Boren and Albrecht "Atmospheric Thermodynamics", p 186
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  return squeeze(_thermodyn.qs(Rd,Rv,T,p))
Exemple #32
0
def ws(T, p, Rd=None, Rv=None):
  '''
  Return saturation mass mixing ration of water vapour ws [g/kg]
  at temperature T [K] and pressure p [mb]
  Use Boren and Albrecht "Atmospheric Thermodynamics", p 186
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  return squeeze(_thermodyn.ws(Rd,Rv,T,p))
Exemple #33
0
def esflatau(T,i):
  '''
  Return saturation partial pressure of water vapor es [mb]
  at temperature T [K].
  Use 8th order polynomial fit of Flatau et al (1992),J. App. Met . 31, 1507-1513, Table 4

  i = 1 => return vapour pressure over water (valid -85C < t < 70C)
  i = 2 => return vapour pressure over ice (valid -90C < t < 0C)
  '''
  return squeeze(_thermodyn.esflatau(T,i))
Exemple #34
0
def tmoistadiab(thetaes,p):
  '''
  Return temperature [K] at level p [mb] 
  on the moist (i.e. saturated) pseudo-adiabat identified 
  by saturation equivalent potential temperature thetaes [K].
  Do it by varying t until 
  delthetae := thetaes - thetae(T,p,qs(T,p)) = 0
  Use Eq. (43) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.tmoistadiab(thetaes,p))
Exemple #35
0
def esflatau(T,i):
  '''
  Return saturation partial pressure of water vapor es [mb]
  at temperature T [K].
  Use 8th order polynomial fit of Flatau et al (1992),J. App. Met . 31, 1507-1513, Table 4

  i = 1 => return vapour pressure over water (valid -85C < t < 70C)
  i = 2 => return vapour pressure over ice (valid -90C < t < 0C)    
  '''
  return squeeze(_thermodyn.esflatau(T,i))
Exemple #36
0
def tmoistadiab(thetaes,p):
  '''
  Return temperature [K] at level p [mb]
  on the moist (i.e. saturated) pseudo-adiabat identified
  by saturation equivalent potential temperature thetaes [K].
  Do it by varying t until
  delthetae := thetaes - thetae(T,p,qs(T,p)) = 0
  Use Eq. (43) in Bolton (1980), Mon. Wea. Rev. 108, 1046-1053
  '''
  return squeeze(_thermodyn.tmoistadiab(thetaes,p))
Exemple #37
0
def theta(T, p, q=None, Rd=None, Rv=None, cpd=None, cpv=None, p0=None):
  '''
  Return potential temperature theta [K]
  given temperature T [K], pressure p [mb], specific humidity q [g/kg].
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  if cpd is None: cpd = Parameters()['Cpd']
  if cpv is None: cpv = Parameters()['Cpv']
  if p0  is None: p0  = 1000.
  if q   is None: q   = zeros(shape(T))*0.
  return squeeze(_thermodyn.theta(Rd, Rv, cpd, cpv, p0, p, T, q))
Exemple #38
0
def theta(T, p, q=None, Rd=None, Rv=None, cpd=None, cpv=None, p0=None):
  '''
  Return potential temperature theta [K]
  given temperature T [K], pressure p [mb], specific humidity q [g/kg].
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  if cpd is None: cpd = Parameters()['Cpd']
  if cpv is None: cpv = Parameters()['Cpv']
  if p0  is None: p0  = 1000.
  if q   is None: q   = zeros(shape(T))*0.
  return squeeze(_thermodyn.theta(Rd, Rv, cpd, cpv, p0, p, T, q))
Exemple #39
0
def get_df(batch: pd.DataFrame, vocab: list) -> pd.DataFrame:
    vocab_size = len(vocab)
    df = pd.DataFrame(np.zeros((1, vocab_size)), columns=vocab)
    kwd_list = set(squeeze(batch["keyword_list"].tolist()))
    kwd_list_filtered = list(filter(lambda x: x in vocab, kwd_list))

    pbar = tqdm(kwd_list_filtered)
    pbar.set_description("Getting DF")
    for tag in pbar:
        if tag in vocab:
            df[tag] = sum(batch["keyword_list"].apply(lambda x: tag in x))
    return df
Exemple #40
0
def wsflatau(T,p,i=None,Rd=None,Rv=None):
  '''
  Return saturation mass mixing ratio of water vapor ws [g/kg]
  at temperature T [K] and pressure p [mb]
  Use 8th order polynomial fit of Flatau et al (1992),
  J. App. Met . 31, 1507-1513, Table 4

  i = 1 => return sat. mix. ratio over water (valid -85C < t < 70C)
  i = 2 => return sat. mix. ratio over ice (valid -90C < t < 0C)    
  '''
  if i   is None: i   = 1
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  return squeeze(_thermodyn.wsflatau(T,p,i))
Exemple #41
0
def wsflatau(T,p,i=None,Rd=None,Rv=None):
  '''
  Return saturation mass mixing ratio of water vapor ws [g/kg]
  at temperature T [K] and pressure p [mb]
  Use 8th order polynomial fit of Flatau et al (1992),
  J. App. Met . 31, 1507-1513, Table 4

  i = 1 => return sat. mix. ratio over water (valid -85C < t < 70C)
  i = 2 => return sat. mix. ratio over ice (valid -90C < t < 0C)
  '''
  if i   is None: i   = 1
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  return squeeze(_thermodyn.wsflatau(T,p,i))
Exemple #42
0
def thetaes(T, p,  \
           Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None, p0=None):
  '''
  Return saturation equivalent potential temperature thetae [K]
  given temperature T [K] and pressure p [mb]
  Uses Eq. (6.123) in Bohren+Albrecht, p.293.
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  if lv0 is None: lv0 = Parameters()['Lv']
  if cpd is None: cpd = Parameters()['Cpd']
  if cpv is None: cpv = Parameters()['Cpv']
  if cl  is None: cl  = Parameters()['Cl']
  if p0  is None: p0  = 1000.
  return squeeze(_thermodyn.thetaes(Rd, Rv, lv0, cpd, cpv, cl, p0, p, T))
Exemple #43
0
def thetaes(T, p,  \
           Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None, p0=None):
  '''
  Return saturation equivalent potential temperature thetae [K]
  given temperature T [K] and pressure p [mb]
  Uses Eq. (6.123) in Bohren+Albrecht, p.293.
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  if lv0 is None: lv0 = Parameters()['Lv']
  if cpd is None: cpd = Parameters()['Cpd']
  if cpv is None: cpv = Parameters()['Cpv']
  if cl  is None: cl  = Parameters()['Cl']
  if p0  is None: p0  = 1000.
  return squeeze(_thermodyn.thetaes(Rd, Rv, lv0, cpd, cpv, cl, p0, p, T))
Exemple #44
0
    def refresh(self, Component):
        """
        Update monitor display.
        """
        if not gotMatplotlib or not self.Monitoring: return

        for key in self.Figure.keys():
            Field = Component[key]
            Panel = self.Figure[key]
            grid = Component.Grid

            # If Field is 3D, show zonal average
            if Field.ndim == 3:
                Field = average(Field, axis=grid.grid_layout.index('lon'))
                Field = squeeze(Field)

            # Reset data
            if Field.ndim == 1:
                if min(Field) == max(Field) == 0.: Field = Field + 1.e-7
                MinVal = min(Field) - 0.01 * abs(min(Field))
                MaxVal = max(Field) + 0.01 * abs(max(Field))
                if Panel.orientation == 0:
                    Panel.handle.set_xdata(Field)
                    Panel.axes.set_xlim([MinVal, MaxVal])
                    Panel.axes.get_xaxis().get_major_formatter(
                    ).set_powerlimits((-2, 3))
                else:
                    Panel.handle.set_ydata(Field)
                    Panel.axes.set_ylim([MinVal, MaxVal])
            if Field.ndim == 2:
                if Panel.orientation == 0:
                    Panel.handle.set_data(Field.transpose()[::-1])
                if Panel.orientation == 1:
                    Panel.handle.set_data(Field.transpose())
                # update normalization
                Panel.handle.set_norm(None)

            # Reset title
            day = Component.State.ElapsedTime / 86400.
            try:
                TitleText = Panel.TitleTemplate % day
            except:
                TitleText = Panel.TitleTemplate % (day, min(
                    ravel(Field)), max(ravel(Field)))
            Panel.title.set_text(TitleText)

        # Redraw figure
        self.manager.canvas.draw()
Exemple #45
0
 def __call__(self, videos):
     """Support joint transform
 :param videos: np.ndarray or a list of np.ndarray
 :return:
 """
     if random.random() < 0.5:
         videos = utils.unsqueeze(videos)
         ret = []
         for tp, video in zip(self.transform_pixel, videos):
             video = video[..., ::-1, :]
             if tp:
                 video[..., 0] = 255 - video[..., 0]
             ret.append(video.copy())
         return utils.squeeze(ret)
     else:
         return videos
Exemple #46
0
def qsflatau(T, p, i=None, Rd=None, Rv=None):
    """
  Return saturation specific  humidity qs [g/kg]
  at temperature T [K] and pressure p [mb]
  Use 8th order polynomial fit of Flatau et al (1992),
  J. App. Met . 31, 1507-1513, Table 4

  i = 1 => return sat. mix. ratio over water (valid -85C < t < 70C)
  i = 2 => return sat. mix. ratio over ice (valid -90C < t < 0C)
  """
    if i is None:
        i = 1
    if Rd is None:
        Rd = Parameters()["Rd"]
    if Rv is None:
        Rv = Parameters()["Rv"]
    return squeeze(_thermodyn.qsflatau(Rd, Rv, T, p, i))
Exemple #47
0
def z(p, T, p0=None, R=None, g=None):
    '''
    Returns height z given temperature T [K] and pressure p.
    If gas constant R is not specified, then dry air value is used.
    If grav. accn. g is not specified, then earth value is used.
    If surface pressure p0 is not specified, then p0=p[-1].
    '''
    flip=False
    if alltrue(p[0]>p[-1]):
        p=p[::-1]
        flip=True
    if R is None: R=Parameters()['Rd']
    if g is None: g=Parameters()['g']
    if p0 is None: p0=p[-1]
    z=squeeze(_thermodyn.z(R,g,p0,p,T))
    if flip: z=z[::-1]
    return z
Exemple #48
0
def z(p, T, p0=None, R=None, g=None):
    '''
    Returns height z given temperature T [K] and pressure p.
    If gas constant R is not specified, then dry air value is used.
    If grav. accn. g is not specified, then earth value is used.
    If surface pressure p0 is not specified, then p0=p[-1].
    '''
    flip=False
    if alltrue(p[0]>p[-1]):
        p=p[::-1]
        flip=True
    if R is None: R=Parameters()['Rd']
    if g is None: g=Parameters()['g']
    if p0 is None: p0=p[-1]
    z=squeeze(_thermodyn.z(R,g,p0,p,T))
    if flip: z=z[::-1]
    return z
Exemple #49
0
 def numbers(cls, number):
     number = number.lstrip(';., ')
     if '+' in number:
         number = '+' + number.split('+', 1)[1]
     elif not number.startswith(tuple('+(0123456789'.split())):
         number = number.lstrip((string.ascii_letters + ' :/'))
     if number:
         if not number.startswith('('):
             # Remove (0) things including the 0 inside strings
             number = re.sub(r'\(\s*0\s*\)', ' ', number)
         number = re.sub(r'[()/.\\-]', ' ', number)
         number = squeeze(number)
         # Avoid single leading zeros
         if number[0] == '0' and number[1] == ' ':
             number = '0' + number[2:]
         number = number.replace("'", '')
     return number
Exemple #50
0
    def refresh(self, Component):
        """
        Update monitor display.
        """
        if not gotMatplotlib or not self.Monitoring:
            return

        for key in self.Figure.keys():
            Field = Component[key]
            Panel = self.Figure[key]

            # If Field is 3D, show zonal average
            if ndim(Field) == 3:
                Field = average(Field, axis=2)
                Field = squeeze(Field)

            # Reset data
            if ndim(Field) == 1:
                if min(Field) == max(Field) == 0.0:
                    Field = Field + 1.0e-7
                MinVal = min(Field) - 0.01 * abs(min(Field))
                MaxVal = max(Field) + 0.01 * abs(max(Field))
                if Panel.orientation == 0:
                    Panel.handle.set_xdata(Field)
                    Panel.axes.set_xlim([MinVal, MaxVal])
                else:
                    Panel.handle.set_ydata(Field)
                    Panel.axes.set_ylim([MinVal, MaxVal])
            if ndim(Field) == 2:
                if Panel.orientation == 0:
                    Panel.handle.set_data(Field[::-1])
                if Panel.orientation == 1:
                    Panel.handle.set_data(Field)
                # update normalization
                Panel.handle.set_norm(None)

            # Reset title
            day = Component.State.ElapsedTime / 86400.0
            try:
                TitleText = Panel.TitleTemplate % day
            except:
                TitleText = Panel.TitleTemplate % (day, min(ravel(Field)), max(ravel(Field)))
            Panel.title.set_text(TitleText)

        # Redraw figure
        self.manager.canvas.draw()
Exemple #51
0
def thetae(T, p, wt=None, \
           Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None, p0=None):
  '''
  Return equivalent potential temperature thetae [K]
  given temperature T [K], pressure p [mb], and
  total water mass mixing ratio wt [g/kg].
  If wt is not specified, parcel is assumed to be just saturated (no condensate).
  If wt is specified and is less that saturation mixing ratio,
  thetae is computed at the lifting condensation level.
  Uses Eq. (6.119) in Bohren+Albrecht, p.293.
  '''
  if Rd  is None: Rd  = Parameters()['Rd']
  if Rv  is None: Rv  = Parameters()['Rv']
  if lv0 is None: lv0 = Parameters()['Lv']
  if cpd is None: cpd = Parameters()['Cpd']
  if cpv is None: cpv = Parameters()['Cpv']
  if cl  is None: cl  = Parameters()['Cl']
  if p0  is None: p0  = 1000.
  if wt  is None: wt  = -1.*ones(shape(T))
  return squeeze(_thermodyn.thetae(Rd, Rv, lv0, cpd, cpv, cl, p0, p, T, wt))
Exemple #52
0
def thetaes(T, p, Rd=None, Rv=None, lv0=None, cpd=None, cpv=None, cl=None, p0=None):
    """
  Return saturation equivalent potential temperature thetae [K]
  given temperature T [K] and pressure p [mb]
  Uses Eq. (6.123) in Bohren+Albrecht, p.293.
  """
    if Rd is None:
        Rd = Parameters()["Rd"]
    if Rv is None:
        Rv = Parameters()["Rv"]
    if lv0 is None:
        lv0 = Parameters()["Lv"]
    if cpd is None:
        cpd = Parameters()["Cpd"]
    if cpv is None:
        cpv = Parameters()["Cpv"]
    if cl is None:
        cl = Parameters()["Cl"]
    if p0 is None:
        p0 = 1000.0
    return squeeze(_thermodyn.thetaes(Rd, Rv, lv0, cpd, cpv, cl, p0, p, T))
Exemple #53
0
    def __getitem__(self, idx):
        vid_name, target = self.dataset[idx]
        length = len(
            glob.glob(
                os.path.join(self.root, rgb_folder_name, vid_name,
                             '*.' + rgb_pattern.split('.')[1]))) - 1

        if self.train:
            samples = np.random.randint(0, length - self.n_frames + 1,
                                        self.n_samples)  # randint: exclusive
        else:
            if length > self.n_samples:
                samples = np.round(
                    np.linspace(0, length - self.n_frames,
                                self.n_samples)).astype(
                                    int)  # linspace: inclusive
            else:
                samples = np.arange(0, length - self.n_frames +
                                    1)  # arange: exclusive

        # load raw data
        inputs = []
        for loader in self.loaders:
            vid = []
            for s in samples:
                clip = []
                for t in range(self.n_frames):
                    frame_id = s + t + 1
                    image = loader(self.root, vid_name, frame_id)
                    clip.append(image)
                vid.append(clip)
            inputs.append(np.array(vid))

        # transform
        if self.transforms is not None:
            for i, transform in enumerate(self.transforms):
                if self.transforms[i] is not None:
                    inputs[i] = transform(inputs[i])

        return utils.squeeze(inputs), target
Exemple #54
0
def getOligos(rID, seq, vectorID, userID):
	
	content = "\nSequencing Primers for " + vectorID + "\n\nVector length: " + `len(seq.strip())` + " nt\n"
	
	protocolPropID = propHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["protocol"], prop_Category_Name_ID_Map["Classifiers"])
	seqPropID = propHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["sequence"], prop_Category_Name_ID_Map["DNA Sequence"])
	
	cdnaPropID = propHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["cdna insert"], prop_Category_Name_ID_Map["DNA Sequence Features"])
	
	projectPropID = propHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["packet id"], prop_Category_Name_ID_Map["General Properties"])
	
	namePropID = propHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["name"], prop_Category_Name_ID_Map["General Properties"])
	
	statusPropID = propHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["status"], prop_Category_Name_ID_Map["General Properties"])
	
	cdnaStart = rHandler.findReagentFeatureStart(rID, cdnaPropID)
	cdnaEnd = rHandler.findReagentFeatureEnd(rID, cdnaPropID)
	
	if cdnaStart > 0:
		content += "\ncDNA: " + `cdnaStart` + "-" + `cdnaEnd` + "\n"
	
	# find all Oligos whose protocol is 'sequencing'
	cursor.execute("SELECT r.reagentID, r.groupID, s.sequence FROM Sequences_tbl s, ReagentPropList_tbl p1, ReagentPropList_tbl p2, Reagents_tbl r WHERE p1.propertyID=" + `protocolPropID` + " AND p1.propertyValue='sequencing' AND p1.reagentID=r.reagentID AND r.reagentTypeID='3' AND p1.reagentID=p2.reagentID AND p2.propertyID=" + `seqPropID` + " AND p2.propertyValue=s.seqID AND p1.status='ACTIVE' AND p2.status='ACTIVE' AND r.status='ACTIVE' AND s.status='ACTIVE'")
	results = cursor.fetchall()
	
	seq = utils.squeeze(seq).lower()
	
	uPackets = getCurrentUserProjects(userID)
	
	#print "Content-type:text/html"
	#print
	#print reagentID
	uPackets.sort()
	#print `uPackets`
	
	features = rHandler.findAllReagentFeatures(Vector(rID), rID)
	overlapping_features = []
	
	tmp_features = {}	# linear list of oligos, regular array
	
	for result in results:
		overlapping_features = []
		
		if result:
			
			#print "Content-type:text/html"
			#print
			#print len(result)
			
			oligoID = int(result[0])
			groupID = int(result[1])
			
			oligo_lims_id = "O" + `groupID`
			#print oligo_lims_id
			
			# April 26, 2010, Karen's request: Ignore Oligos whose status is 'Failed' or 'Do Not Use'
			oligoStatus = rHandler.findSimplePropertyValue(oligoID, statusPropID)

			if oligoStatus:
				#print oligoStatus
				if oligoStatus.lower() == 'failed' or oligoStatus.lower() == 'do not use':
					continue
			
			oligoPacket = int(rHandler.findSimplePropertyValue(oligoID, projectPropID))
			#print oligoPacket
			
			if oligoPacket:
				if oligoPacket not in uPackets:
					#print oligoPacket
					continue
			
			oligoName = rHandler.findSimplePropertyValue(oligoID, namePropID)
			
			#if oligoName:
				#oligoNamesDict[oligoID] = oligoName
			#else:
				#oligoNamesDict[oligoID] = ""
			
			#print `features`
			
			if len(result) == 3:
				oligoSeq = utils.squeeze(result[2].strip()).lower()
				#print oligoSeq
				
				tm = dnaHandler.calculateTm(oligoSeq)
				
				if seq.find(oligoSeq) >= 0:
					fStart = seq.find(oligoSeq) + 1
					fEnd = fStart + len(oligoSeq) - 1
					
					direction = "forward"
					
					for f_tmp in features:
						tmp_f_Start = f_tmp.getFeatureStartPos()
						tmp_f_End = f_tmp.getFeatureEndPos()
						
						# 4 cases:
						# Feature contained within ORF (tmp_fStart >= fStart and tmp_fEnd <= fEnd)
						# Feature starts inside ORF (tmp_fStart >= fStart and tmp_fStart <= fEnd)
						# Feature ends inside ORF (tmp_fEnd >= fStart and tmp_fEnd <= fEnd)
						# ORF contained within feature (tmp_fStart <= fStart and tmp_fEnd >= fEnd)
						if (tmp_f_Start >= fStart and tmp_f_End <= fEnd) or (tmp_f_End >= fStart and tmp_f_End <= fEnd) or (tmp_f_Start >= fStart and tmp_f_Start <= fEnd) or (tmp_f_Start <= fStart and tmp_f_End >= fEnd):
							overlapping_features.append(f_tmp)
					
				else:
					revSeq = dnaHandler.reverse_complement(oligoSeq)
					direction = "reverse"
					
					if seq.find(revSeq) < 0:
						continue
					
					fStart = seq.find(revSeq) + 1
					fEnd = fStart + len(revSeq)
					
					# Note: start pos > end pos, as we're talking about reverse Oligo sequence
					revStart = fEnd - 1
					revEnd = fStart
				
					for f_tmp in features:
						tmp_f_Start = f_tmp.getFeatureStartPos()
						tmp_f_End = f_tmp.getFeatureEndPos()
						
						# 4 cases: (remember, revStart > revEnd, e.g. O1100 for V2559 - start 2583, end 2559)
						# Feature contained within ORF (tmp_f_Start <= revStart and tmp_f_End >= revEnd)
						# Feature starts inside ORF (tmp_f_Start >= revStart and tmp_f_Start <= revEnd)
						# Feature ends inside ORF (tmp_f_End >= revStart and tmp_f_End <= revEnd)
						# ORF contained within feature (tmp_f_Start >= revStart and tmp_f_End <= revEnd)
						if (tmp_f_Start >= revStart and tmp_f_End <= revEnd) or (tmp_f_End >= revStart and tmp_f_End <= revEnd) or (tmp_f_Start >= revStart and tmp_f_Start <= revEnd) or (tmp_f_Start <= revStart and tmp_f_End >= revEnd):
							overlapping_features.append(f_tmp)
					
					fStart = revEnd
					fEnd = revStart

				oFeature = SequenceFeature(oligoName, oligo_lims_id, fStart, fEnd, direction, oligoSeq, tm)
				tmp_features[oFeature] = overlapping_features
	
	tmp_oligos = {}
	
	for tmpFeature in tmp_features.keys():
		
		oligoStart = tmpFeature.getFeatureStartPos()
		
		if tmp_oligos.has_key(oligoStart):
			tmp_o_list = tmp_oligos[oligoStart]
		else:
			tmp_o_list = []
			tmp_oligos[oligoStart] = tmpFeature
		
		tmp_o_list.append(tmpFeature)
		tmp_oligos[oligoStart] = tmp_o_list
		
	for oligoStart in sorted(tmp_oligos.keys()):
		oFeatures = tmp_oligos[oligoStart]
		
		for tmpFeature in oFeatures:
			
			overlapping_features = tmp_features[tmpFeature]
			
			oligoName = tmpFeature.getFeatureType()
			oligo_lims_id = tmpFeature.getFeatureName()
			oligoSeq = tmpFeature.getFeatureDescrType()
			tm = tmpFeature.getFeatureDescrName()
			
			fStart = tmpFeature.getFeatureStartPos()
			fEnd = tmpFeature.getFeatureEndPos()
			
			direction = tmpFeature.getFeatureDirection()
			
			if oligoName:
				content += '\n--------------------------------------------------\n' + oligo_lims_id + " " + oligoName + ": " + oligoSeq + ", " + direction + "\n\n"
			
			else:
				content += '\n--------------------------------------------------\n' + oligo_lims_id + ": " + oligoSeq + ", " + direction + "\n\n"
				
			content += "Start position = " + `fStart` + "\n"
			content += "Stop position = " + `fEnd` + "\n"
			
			content += "\nTm: " + tm + "ºC\n"
	
			if len(overlapping_features) > 0:
				content += "\nOverlapping features:\n"
			
				for tmp_f in overlapping_features:
					
					tmp_fStart = tmp_f.getFeatureStartPos()
					tmp_fEnd = tmp_f.getFeatureEndPos()
			
					if tmp_f.getFeatureType().lower() == 'cdna insert':
						content += "cDNA"
					#elif tmp_f.getFeatureType().lower() == 'intron':
						#content += tmp_f.getFeatureName().title() + " intron"
					elif tmp_f.getFeatureType().lower() == 'promoter':
						content += tmp_f.getFeatureName().title() + " promoter"
					elif tmp_f.getFeatureType().lower() == "5' cloning site" or tmp_f.getFeatureType().lower() == "3' cloning site":
						content += tmp_f.getFeatureName().title() + " cloning site"
					elif tmp_f.getFeatureType().lower() == 'restriction site':
						content += tmp_f.getFeatureName().title() + " restriction site"
					elif tmp_f.getFeatureType().lower() == 'intron':
						content += tmp_f.getFeatureName().title() + " intron"
					elif tmp_f.getFeatureType().lower() == 'polya':
						content += tmp_f.getFeatureName().title() + " polyA"
					elif tmp_f.getFeatureType().lower() == 'origin':
						content += tmp_f.getFeatureName().title() + " ori."
					elif tmp_f.getFeatureType().lower() == 'tag type':
						content += tmp_f.getFeatureName().title() + " tag"
					elif tmp_f.getFeatureType().lower() == 'transcription terminator':
						content += tmp_f.getFeatureName().title() + " tscn. ter."
					else:
						content += tmp_f.getFeatureName().title()
						
					content += ": " + `tmp_fStart` + "-" + `tmp_fEnd` + '\n'

	content +="\n--------------------------------------------------\n\nNOTE: Only Oligos in OpenFreezer whose protocol is 'sequencing' and whose sequence is an exact match to the vector are reported.\n"
	return content
Exemple #55
0
def drawMap():
	
	dbConn = DatabaseConn()
	db = dbConn.databaseConnect()
	
	cursor = db.cursor()
	hostname = dbConn.getHostname()
	root_path = dbConn.getRootDir()
	
	form = cgi.FieldStorage(keep_blank_values="True")
	
	#print "Content-type:text/html"
	#print
	#print `form`
	
	if form.has_key("rID"):
		rID = form.getvalue("rID")
	else:	# script executed from command line
		# Worst-case scenarios
		#rID = 260	# V260
		#rID = 230	# V260
		#rID = 80	# V80 - ok
		#rID = 69661	# V2412 - not bad at all, just one of the oligos overlaps exactly with promoter (1 nt difference)
		#rID = 23999 	# V59541
		rID = 97918
		#print rID

	if form.has_key("user_id_hidden"):
		userID = form.getvalue("user_id_hidden")
	else:	# command-line execution
		userID = 1	# debug

	reagentID = rHandler.convertDatabaseToReagentID(rID)
	
	uPackets = getCurrentUserProjects(userID)
	
	#print "Content-type:text/html"
	#print
	#print reagentID
	uPackets.sort()
	#print `uPackets`

	namePropID = pHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["name"], prop_Category_Name_ID_Map["General Properties"])
	statusPropID = pHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["status"], prop_Category_Name_ID_Map["General Properties"])
	projectPropID = pHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["packet id"], prop_Category_Name_ID_Map["General Properties"])
	
	rName = rHandler.findSimplePropertyValue(rID, namePropID)
	rTypeID = rHandler.findReagentTypeID(rID)
	
	try:
		os.remove(root_path + "Reagent/vector_maps/" + reagentID + "_Oligo_map.pdf")
	except OSError:
		pass
	
	#print root_path
	
	c = Canvas(root_path + "Reagent/vector_maps/" + reagentID + "_Oligo_map.pdf")
	
	c.setPageSize((1500,1500))
	c.setStrokeColorRGB(0,0,0)
	c.saveState()
	
	# Draw circle
	origin = 0
	origin_x = 750
	origin_y = 750
	radius = 200
	
	c.circle(origin_x, origin_y, radius)
	c.restoreState()
	
	# Divide circle into 100-unit sectors
	rSeqID = rHandler.findDNASequenceKey(rID)
	rSeq = sHandler.findSequenceByID(rSeqID)
	seqLen = len(rSeq)
	
	#print "Content-type:text/html"
	#print
	#print `rID`
	
	unit_angle_measure = float(360) / float(seqLen)
	#print unit_angle_measure
	
	# Mark 1 on the circle - KEEP, Karen said!
	c.setLineWidth(1)
	c.setStrokeColor(black)
	c.setFillColor(black)
	c.saveState()

	path = c.beginPath()
	
	# Draw a triangle pointing down above the circle
	#path.moveTo(origin_x, origin_y+radius+7)
	#path.lineTo(origin_x-5, origin_y+radius+14)
	#path.lineTo(origin_x+5, origin_y+radius+14)
	#path.lineTo(origin_x, origin_y+radius+7)
	
	# Draw a triangle pointing up inside the circle
	#path.moveTo(origin_x, origin_y+radius-10)
	#path.lineTo(origin_x-5, origin_y+radius-14)
	#path.lineTo(origin_x+5, origin_y+radius-14)
	#path.lineTo(origin_x, origin_y+radius-10)
	
	# Draw a triangle pointing right inside the circle
	path.moveTo(origin_x, origin_y+radius-14)
	path.lineTo(origin_x, origin_y+radius-28)
	path.lineTo(origin_x+8, origin_y+radius-21)
	path.lineTo(origin_x, origin_y+radius-14)
	c.drawPath(path, True, True)
	
	# label 1
	t = c.beginText()
	t.setStrokeColor(black)
	t.setFillColor(black)
	t.setFont("Helvetica-Bold", 16)
	#t.setTextOrigin(origin_x-5, origin_y+radius+19)	# above circle
	t.setTextOrigin(origin_x-3, origin_y+radius-50)	# above circle
	t.textOut("1")
	c.drawText(t)
	
	c.restoreState()

	# Calculate feature segment sizes
	sequenceFeatures = rHandler.findReagentSequenceFeatures(rID)
	#print `sequenceFeatures`

	# Draw legend
	ox_legend = 1280
	oy_legend = 1465
	
	prev_legend = oy_legend
	
	# draw frame
	c.setStrokeColor(darkgray)
	c.setFillColor(white)
	c.saveState()
	
	featureNames = rtPropHandler.findReagentTypeAttributeNamesByCategory(rTypeID, prop_Category_Name_ID_Map["DNA Sequence Features"])
	featureNames.sort()

	if len(featureNames) > 15:
		origin_spacer = -19
		coeff = 12
	else:
		origin_spacer = 65
		coeff = 15
	
	origin_legend = prev_legend+origin_spacer-len(featureNames)*coeff
	
	x = (990 - origin_legend) / len(featureNames)
	#print x
	
	if x < 12:
		x = 12
	
	# WHEN WANT TO ADJUST LEGEND GREY BOX HEIGHT: UPDATE THE VALUE ADDED TO legend_height AND THE VALUE SUBTRACTED FROM origin_legend
	legend_height = len(featureNames)*x + 55
	
	c.rect(ox_legend-25, origin_legend-35, 215, legend_height, 1, 1)	# good for specific rtype attributes
	
	c.restoreState()
	
	protocolPropID = pHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["protocol"], prop_Category_Name_ID_Map["Classifiers"])
	seqPropID = pHandler.findReagentPropertyInCategoryID(prop_Name_ID_Map["sequence"], prop_Category_Name_ID_Map["DNA Sequence"])
	
	sequenceFeatures = rHandler.findAllReagentFeatures(Vector(rID), rID)
	
	# Print reagent ID and size at the centre of the circle and print name at the top of the page
	c.setFont("Helvetica-Bold", 32)
	c.setFillColor(black)
	c.saveState()
	
	c.drawCentredString(origin_x, origin_y+35, reagentID)
	c.drawCentredString(origin_x, origin_y+10, "nt 1 - " + `seqLen`)
	
	if rName:
		c.setFillColor(blue)	
		c.drawCentredString(origin_x, 1415, rName)
	
	c.restoreState()
	
	# find all Oligos whose protocol is 'sequencing'
	# April 26, 2010: Do not include status in this query, as it might just not be recorded for the oligo at all (early entries)
	cursor.execute("SELECT r.reagentID, r.groupID, s.sequence FROM Sequences_tbl s, ReagentPropList_tbl p1, ReagentPropList_tbl p2, Reagents_tbl r WHERE p1.propertyID=" + `protocolPropID` + " AND p1.propertyValue='sequencing' AND p1.reagentID=r.reagentID AND r.reagentTypeID='3' AND p1.reagentID=p2.reagentID AND p2.propertyID=" + `seqPropID` + " AND p2.propertyValue=s.seqID AND p1.status='ACTIVE' AND p2.status='ACTIVE' AND r.status='ACTIVE' AND s.status='ACTIVE'")
	
	#print "SELECT r.reagentID, r.groupID, s.sequence FROM Sequences_tbl s, ReagentPropList_tbl p1, ReagentPropList_tbl p2, Reagents_tbl r WHERE p1.propertyID=" + `protocolPropID` + " AND p1.propertyValue='sequencing' AND p1.reagentID=r.reagentID AND r.reagentTypeID='3' AND p1.reagentID=p2.reagentID AND p2.propertyID=" + `seqPropID` + " AND p2.propertyValue=s.seqID AND p1.status='ACTIVE' AND p2.status='ACTIVE' AND r.status='ACTIVE' AND s.status='ACTIVE'"
	
	results = cursor.fetchall()
	
	rSeq = utils.squeeze(rSeq).lower()
	#print rSeq
	
	oligoFeatures = []
	
	oligoIDsDict = {}	# O123 => 123
	oligoNamesDict = {}	# 123 => "O123 Name"
	oligoProjectDict = {}	# 123 => 7
	
	for result in results:
		
		if result:
			oligoID = int(result[0])
			groupID = int(result[1])
			
			oligo_lims_id = "O" + `groupID`
			oligoIDsDict[oligo_lims_id] = oligoID
			#print oligo_lims_id
			
			oligoName = rHandler.findSimplePropertyValue(oligoID, namePropID)
			
			# April 26, 2010, Karen's request: Ignore Oligos whose status is 'Failed' or 'Do Not Use'
			oligoStatus = rHandler.findSimplePropertyValue(oligoID, statusPropID)

			if oligoStatus:
				#print oligoStatus
				if oligoStatus.lower() == 'failed' or oligoStatus.lower() == 'do not use':
					continue
			
			if oligoName:
				oligoNamesDict[oligoID] = oligoName
			else:
				oligoNamesDict[oligoID] = ""
			
			#cursor.execute("SELECT propertyValue FROM ReagentPropList_tbl WHERE reagentID=" + `oligoID` + " AND propertyID=" + `namePropID` + " AND status='ACTIVE'")
			#result2 = cursor.fetchone()
			
			#if result2:
				#oligoName = result2[0]
				#oligoNamesDict[oligoID] = oligoName
			#else:
				#oligoNamesDict[oligoID] = ""
			
			# PROJECT - MUST CAST TO INT
			oligoPacket = int(rHandler.findSimplePropertyValue(oligoID, projectPropID))
			#print oligoPacket
			
			if oligoPacket:
				if oligoPacket not in uPackets:
					#print oligoPacket
					continue
			
			#cursor.execute("SELECT propertyValue FROM ReagentPropList_tbl WHERE reagentID=" + `oligoID` + " AND propertyID=" + `projectPropID` + " AND status='ACTIVE'")
			#result3 = cursor.fetchone()
			
			#if result3:
				#oligoPacket = int(result3[0])
				##print oligoPacket
				
				#if oligoPacket not in uPackets:
					##print oligoPacket
					#continue
				
				#oligoProjectDict[oligoID] = oligoPacket
			#else:
				#oligoProjectDict[oligoID] = 0
			
			if len(result) == 3:
				oligoSeq = result[2].strip().lower()
				#print oligoSeq
				
				tm = sHandler.calculateTm(oligoSeq)
				
				# Changes made April 26, 2010: Karen caught that the sense/antisense check does not work for T3 and T7 vectors and said to check oligo sequence both ways
				#if rSeq.find(oligoSeq) < 0:
					#continue
					
				if rSeq.find(oligoSeq) >= 0:
					fStart = rSeq.find(oligoSeq) + 1
					fEnd = fStart + len(oligoSeq) - 1
					
					#if oHandler.isSenseOligo(oligoID):
					oFeature = SequenceFeature("sequencing primer", oligo_lims_id, fStart, fEnd, 'forward', oligoSeq, tm)
					#else:
						#oFeature = SequenceFeature("sequencing primer", oligo_lims_id, fStart, fEnd, 'reverse', oligoSeq, tm)
					
				else:
					revSeq = sHandler.reverse_complement(oligoSeq)
					
					if rSeq.find(revSeq) < 0:
						continue
					
					fStart = rSeq.find(revSeq) + 1
					fEnd = fStart + len(revSeq)
					
					# Note: start pos > end pos, as we're talking about reverse Oligo sequence
					revStart = fEnd - 1
					revEnd = fStart
				
					# Still, record sense/antisense for displaying on the map ('forward' and 'reverse' here are just static values in Feature class, it doesn't reflect sequence orientation in our case - just use as a temporary placeholder)
					#if oHandler.isSenseOligo(oligoID):
						#oligoFeatures.append(oFeature)
						#oFeature = SequenceFeature("sequencing primer", oligo_lims_id, revStart, revEnd, 'forward', oligoSeq, tm)
					#else:
					oFeature = SequenceFeature("sequencing primer", oligo_lims_id, revStart, revEnd, 'reverse', oligoSeq, tm)
				
				oligoFeatures.append(oFeature)
	
				sequenceFeatures.append(oFeature)

	# SORT features by size, so that short features are not hidden behind the long ones
	fSizes = []
	sortedFeatures = []
	
	fSortedPos = {}
	
	fStartPos = []
	fEndPos = []
	
	for feature in sequenceFeatures:
		fSize = int(feature.getFeatureSize())
		
		if fSize > 0:
			fSizes.append(fSize)
		
		#if fSize > 150:
		f_start_tmp = int(feature.getFeatureStartPos())
		fStartPos.append(f_start_tmp)
		
		fSortedPos[f_start_tmp] = feature
	
		f_end_tmp = int(feature.getFeatureEndPos())
		fEndPos.append(f_end_tmp)
		
	
	fEndPos.sort()
	fStartPos.sort()
	
	#print `fStartPos`
	#print `fEndPos`
	
	fSizes.sort(reverse=True)
	#print `fSizes`
	
	for fs in fSizes:
		for feature in sequenceFeatures:
			fSize = feature.getFeatureSize()

			# added existence check July 17/08 - different features may have same sizes so end up with duplicate features in list (e.g. cloning sizes appeared twice on the map)
			if fs == fSize and feature not in sortedFeatures:
				sortedFeatures.append(feature)
	
	# Order: 5' site, 3' site, 5' linker, 3' linker, then the rest
	sites_color = featureNameColorMap["5' cloning site"]		# same for 3' site
	
	# 5' site
	c.setStrokeColor(sites_color)
	c.setFillColor(sites_color)
	c.saveState()
	c.rect(ox_legend-15, prev_legend-20, 25,8, 1, 1)
	c.restoreState
	
	t = c.beginText()
	t.setStrokeColor(sites_color)
	t.setFillColor(sites_color)
	t.setFont("Helvetica-Bold", 10)
	t.setTextOrigin(ox_legend+12, prev_legend-20)
	t.textOut(" - " + "5' CLONING SITE")
	c.drawText(t)
	c.restoreState
	
	prev_legend = prev_legend-15
	
	# 3' site
	c.setStrokeColor(sites_color)
	c.setFillColor(sites_color)
	c.saveState()
	c.rect(ox_legend-15, prev_legend-20, 25,8, 1, 1)
	c.restoreState
	
	t = c.beginText()
	t.setStrokeColor(sites_color)
	t.setFillColor(sites_color)
	t.setFont("Helvetica-Bold", 10)
	t.setTextOrigin(ox_legend+12, prev_legend-20)
	t.textOut(" - " + "3' CLONING SITE")
	c.drawText(t)
	c.restoreState

	prev_legend = prev_legend-15
	
	# Show legend for linkers
	linkers_color = featureNameColorMap["5' linker"]
	
	# 5' linker
	c.setStrokeColor(linkers_color)
	c.setFillColor(linkers_color)
	c.saveState()
	c.rect(ox_legend-15, prev_legend-20, 25,8, 1, 1)
	c.restoreState
	
	t = c.beginText()
	t.setStrokeColor(linkers_color)
	t.setFillColor(linkers_color)
	t.setFont("Helvetica-Bold", 10)
	t.setTextOrigin(ox_legend+12, prev_legend-20)
	t.textOut(" - " + "5' LINKER")
	c.drawText(t)
	c.restoreState

	prev_legend = prev_legend-15
	
	# 3' linker
	c.setStrokeColor(linkers_color)
	c.setFillColor(linkers_color)
	c.saveState()
	c.rect(ox_legend-15, prev_legend-20, 25,8, 1, 1)
	c.restoreState
	
	t = c.beginText()
	t.setStrokeColor(linkers_color)
	t.setFillColor(linkers_color)
	t.setFont("Helvetica-Bold", 10)
	t.setTextOrigin(ox_legend+12, prev_legend-20)
	t.textOut(" - " + "3' LINKER")
	c.drawText(t)
	c.restoreState

	prev_legend = prev_legend-15
	
	# Output the rest of the features in alphabetical order
	featureNames = featureNameColorMap.keys()
	featureNames.sort()
	
	#print `featureNames`
	
	for featureName in featureNames:
		#print featureName
		if featureNameColorMap.has_key(featureName):
			color = featureNameColorMap[featureName]
			
			if featureName != "5' cloning site" and featureName != "3' cloning site" and featureName != "5' linker" and featureName != "3' linker" and color != None:
				#print featureName
				c.setStrokeColor(color)
				c.setFillColor(color)
				c.saveState()
				c.rect(ox_legend-15, prev_legend-20, 25,8, 1, 1)
				c.restoreState
				
				t = c.beginText()
				t.setStrokeColor(color)
				t.setFillColor(color)
				t.setFont("Helvetica-Bold", 10)
				t.setTextOrigin(ox_legend+12, prev_legend-20)
				t.textOut(" - " + featureName.upper())
				c.drawText(t)
				
				c.restoreState
				
				prev_legend = prev_legend-15

	# Sequencing Primer
	oligoColor = "#7cfc00"
	
	c.setStrokeColor(oligoColor)
	c.setFillColor(oligoColor)
	c.saveState()
	c.rect(ox_legend-15, prev_legend-20, 25,8, 1, 1)
	c.restoreState
	
	t = c.beginText()
	t.setStrokeColor(oligoColor)
	t.setFillColor(oligoColor)
	t.setFont("Helvetica-Bold", 10)
	t.setTextOrigin(ox_legend+12, prev_legend-20)
	t.textOut(" - " + "SEQUENCING PRIMER")
	c.drawText(t)
	c.restoreState

	prev_legend = prev_legend-15
	
	ox_labels = 40
	oy_labels = 40
	#prev_legend = oy_labels
	
	for feature in sortedFeatures:
		
		fType = feature.getFeatureType()
		fValue = feature.getFeatureName()
		fSize = feature.getFeatureSize()
		
		#print fType
		#print fValue
		#print fSize
		
		# color
		if featureNameColorMap.has_key(fType):
			fColor = featureNameColorMap[fType]
			textColor = fColor
			
		elif fType == 'sequencing primer':
			fColor = "#7cfc00"
			textColor = "black"

			o_id = oligoIDsDict[fValue]
			oligoName = oligoNamesDict[o_id]
			
			if len(oligoName) > 0:
				fValue += " " + oligoName
		
		# property name
		if fType == 'cdna insert':
			fValue = "cDNA Insert"
			
		elif fType == 'promoter':
			fValue = fValue + " " + fType
		
		fStart = feature.getFeatureStartPos()
		fEnd = feature.getFeatureEndPos()
		fDir = feature.getFeatureDirection()
		
		# value
		#if fType == 'sequencing primer':
		
			#oligoDir = feature.getFeatureDirection()
		
			#if oligoDir == 'forward':
				#oligoType = 'Sense'
			#else:
				#oligoType = 'Antisense'
			
			#fTxt = fValue + " (" + `fStart` + "-" + `fEnd` + "), " + oligoDir
		#else:
		
		fTxt = fValue + " (" + `fStart` + "-" + `fEnd` + ")"
			
		if fSize > 0:
			f_start = fStart * unit_angle_measure
		
			#print "Start " + `fStart`
			#print "End " + `fEnd`
		
			startAngle = 90 - f_start
			#print "Start angle " + `startAngle`
			
			f_end = fEnd * unit_angle_measure
			endAngle = 90 - f_end
			#print "End angle " + `endAngle`
			
			extAngle = -1*(f_end - f_start)
			#print "Ext angle " + `extAngle`

			x1 = origin_x - radius
			y1 = origin_y - radius
			
			x2 = origin_x + radius
			y2 = origin_y + radius
			
			p = c.beginPath()
			
			c.setLineWidth(10)
			c.setLineJoin(1)
			
			c.setStrokeColor(fColor)
			c.saveState()
			
			p.arc(x1, y1, x2, y2, startAngle, extAngle)
			c.drawPath(p)
			c.restoreState()
			
			# common to all
			startAngle_rad = (startAngle * math.pi) / 180.0
			endAngle_rad = (endAngle * math.pi) / 180.0
			
			c.setStrokeColor(fColor)
			c.setFillColor(fColor)
			c.setFont("Helvetica-Bold", 9)
			c.saveState()
			
			arc_x_start = origin_x + (radius+5)*math.cos(startAngle_rad)
			arc_y_start = origin_y + (radius+5)*math.sin(startAngle_rad)
			
			arc_x_end = origin_x+(radius+5)*math.cos(endAngle_rad)
			arc_y_end = origin_y+(radius+5)*math.sin(endAngle_rad)
			
			# draw label
			#c.setStrokeColorRGB(0,0,1)
			#c.setFillColorRGB(0,0,1)
			c.setStrokeColor(fColor)
			c.setFillColor(fColor)
			c.setFont("Helvetica-Bold", 12)
			c.saveState()
			
			# draw line
			delta = 45
			
			if fStart < seqLen/2:
				
				if arc_y_start > origin_y:
					
					# THIS WORKS!!!!
					c.setStrokeColor(fColor)
					c.setFillColor(fColor)
					c.setLineWidth(1)
					c.saveState()
					
					fInd = fStartPos.index(fStart)
					fY = len(fStartPos) - fInd

					c.line(arc_x_start, arc_y_start, arc_x_start+math.fabs(math.sin(delta))*(fInd*10), arc_y_start+math.fabs(math.cos(delta))*(fY*10))

					#c.setStrokeColorRGB(0,0,1)
					#c.setFillColorRGB(0,0,1)

					c.setStrokeColor(textColor)
					c.setFillColor(textColor)
		
					c.setFont("Helvetica-Bold", 8)
					c.saveState()

					#c.drawString(arc_x_start+math.fabs(math.sin(delta))*(fInd*10)+2, arc_y_start+math.fabs(math.cos(delta))*(fY*10)-math.cos(delta)*0.9, fValue + " (" + `fStart` + "-" + `fEnd` + ")")
					
					c.drawString(arc_x_start+math.fabs(math.sin(delta))*(fInd*10)+2, arc_y_start+math.fabs(math.cos(delta))*(fY*10)-math.cos(delta)*0.9, fTxt)

					c.restoreState()
					
				else:
					fInd = fStartPos.index(fStart)
					fY = len(fStartPos) - fInd

					c.setStrokeColor(fColor)
					c.setFillColor(fColor)
					c.setLineWidth(1)
					c.saveState()
					
					c.line(arc_x_start, arc_y_start, arc_x_start+math.fabs(math.sin(delta))*(fY*10), arc_y_start-math.fabs(math.cos(delta))*(fInd*10))
				
					c.restoreState()
					
					#c.setStrokeColorRGB(0,0,1)
					#c.setFillColorRGB(0,0,1)
					
					c.setStrokeColor(textColor)
					c.setFillColor(textColor)
		
					c.setFont("Helvetica-Bold", 8)
					c.saveState()
				
					#c.drawString(arc_x_start+math.fabs(math.sin(delta))*(fY*10)+2, arc_y_start-math.fabs(math.cos(delta))*(fInd*10)-math.cos(delta)*13, fValue + " (" + `fStart` + "-" + `fEnd` + ")")
					
					c.drawString(arc_x_start+math.fabs(math.sin(delta))*(fY*10)+2, arc_y_start-math.fabs(math.cos(delta))*(fInd*10)-math.cos(delta)*13, fTxt)
					
					c.restoreState()
			else:
				if arc_y_start > origin_y:
					
					fInd = fStartPos.index(fStart)
					fY = len(fStartPos) - fInd

					c.setStrokeColor(fColor)
					c.setFillColor(fColor)
					c.setLineWidth(1)
					c.saveState()
					
					c.line(arc_x_start, arc_y_start, arc_x_start-math.fabs(math.sin(delta)*(fY*10)), arc_y_start+math.fabs(math.cos(delta)*(fInd*10)))
					
					c.restoreState()
					
					# draw label
					#c.setStrokeColorRGB(0,0,1)
					#c.setFillColorRGB(0,0,1)
					
					c.setStrokeColor(textColor)
					c.setFillColor(textColor)
		
					c.setFont("Helvetica-Bold", 8)
					c.saveState()
					
					#c.drawRightString(arc_x_start-math.fabs(math.sin(delta)*(fY*10))+2, arc_y_start+math.fabs(math.cos(delta)*(fInd*10)), fValue + " (" + `fStart` + "-" + `fEnd` + ")")
					
					c.drawRightString(arc_x_start-math.fabs(math.sin(delta)*(fY*10))+2, arc_y_start+math.fabs(math.cos(delta)*(fInd*10)), fTxt)
					
					c.restoreState()
				else:
					
					fInd = fStartPos.index(fStart)
					fY = len(fStartPos) - fInd

					c.setStrokeColor(fColor)
					c.setFillColor(fColor)
					c.setLineWidth(1)
					c.saveState()
					
					c.line(arc_x_start, arc_y_start, arc_x_start-math.fabs(math.sin(delta)*(fInd*10)), arc_y_start-math.fabs(math.cos(delta)*(fY*10)))
				
					c.restoreState()
					
					# draw label
					#c.setStrokeColorRGB(0,0,1)
					#c.setFillColorRGB(0,0,1)
					
					c.setStrokeColor(textColor)
					c.setFillColor(textColor)

					c.setFont("Helvetica-Bold", 8)
					c.saveState()
					
					#c.drawRightString(arc_x_start-math.fabs(math.sin(delta)*(fInd*10))-2, arc_y_start-math.fabs(math.cos(delta)*(fY*10))-math.cos(delta)*12, fValue + " (" + `fStart` + "-" + `fEnd` + ")")
					
					c.drawRightString(arc_x_start-math.fabs(math.sin(delta)*(fInd*10))-2, arc_y_start-math.fabs(math.cos(delta)*(fY*10))-math.cos(delta)*12, fTxt)
					
					c.restoreState()

	# print Oligo info
	#prev_legend = prev_legend-10
	prev_legend = 1365
	
	t = c.beginText()
	t.setStrokeColor(black)
	t.setFillColor(black)
	t.setFont("Helvetica-Bold", 10)
	t.setTextOrigin(ox_labels, prev_legend-15)
	t.textOut("Sequencing primers for " + reagentID + ":")
	c.drawText(t)
	c.restoreState

	prev_legend = prev_legend-18
	
	fColor = "#7cfc00"

	# sort oligos
	tmp_oligos = {}
	
	#print `oligoFeatures`
	
	for oFeature in oligoFeatures:
		oligoID = oFeature.getFeatureName()
		#print oligoID
		oligoStart = oFeature.getFeatureStartPos()
		#print oligoStart
		
		if tmp_oligos.has_key(oligoStart):
			tmp_o_list = tmp_oligos[oligoStart]
		else:
			tmp_o_list = []
			tmp_oligos[oligoStart] = oFeature
		
		tmp_o_list.append(oFeature)
		tmp_oligos[oligoStart] = tmp_o_list
		
		oligoSeq = oFeature.getFeatureDescrType()
		#print oligoSeq
	
	#print `tmp_oligos`

	for oligoStart in sorted(tmp_oligos.keys()):
		oFeatures = tmp_oligos[oligoStart]
		
		for oFeature in oFeatures:
			oligoID = oFeature.getFeatureName()
			#oligoSeq = oFeature.getFeatureDescrType()
			oligoTm = oFeature.getFeatureDescrName()
			oligoEnd = oFeature.getFeatureEndPos()
			oligoDir = oFeature.getFeatureDirection()
			
			o_rID = oligoIDsDict[oligoID]
			oligoName = oligoNamesDict[o_rID]
			
			#if oligoDir == 'forward':
				#oligoType = 'Sense'
			#else:
				#oligoType = 'Antisense'
			
			c.setStrokeColor(fColor)
			c.setFillColor(fColor)
			c.saveState()
			c.rect(ox_labels+5, prev_legend-15, 15,6, 1, 1)
			c.restoreState
		
			t = c.beginText()
			t.setStrokeColor(black)
			t.setFillColor(black)
			t.setFont("Helvetica-Bold", 10)
			t.setTextOrigin(ox_labels+25, prev_legend-15)
			
			if len(oligoName) > 0:
				#t.textOut(oligoID + ": " + oligoName + " (" + `oligoStart` + "-" + `oligoEnd` + ")" ", " + oligoType)
				t.textOut(oligoID + ": " + oligoName + " (" + `oligoStart` + "-" + `oligoEnd` + ")" ", " + oligoDir)
			else:
				#t.textOut(oligoID + ": (" + `oligoStart` + "-" + `oligoEnd` + ")" ", " + oligoType)
				t.textOut(oligoID + ": (" + `oligoStart` + "-" + `oligoEnd` + ")" ", " + oligoDir)
			
			#print oligoID + ": (" + `oligoStart` + "-" + `oligoEnd` + ")" ", " + oligoType
			
			c.drawText(t)
			c.restoreState
			prev_legend = prev_legend-15
		
			c.restoreState()
	
	c.showPage()
	c.save()
Exemple #56
0
    def __init__(self, Component, FieldKey, Subplot):

        # get Field value
        Field = Component.State[FieldKey]
        Dims = KnownFields[FieldKey][2]

        # Figure out axes
        AxisKey = []
        if Dims == "2D":
            for i in range(2):
                if shape(Field)[i] > 1:
                    AxisKey.append(["lat", "lon"][i])
        if Dims == "3D":
            for i in range(3):
                if shape(Field)[i] > 1:
                    AxisKey.append(["lev", "lat", "lon"][i])

        # If Field is 3D, show zonal average
        Field = squeeze(Field)
        if ndim(Field) == 3:
            Field = average(Field, axis=2)
            Field = squeeze(Field)
            AxisKey.pop(2)

        # Axes names and values
        AxisName = []
        AxisVal = []
        for key in AxisKey:
            AxisName.append(Component.Grid.long_name[key])
            AxisVal.append(Component.Grid[key])

        # Get handles to figure and properties
        if len(AxisName) == 1:
            if min(Field) == max(Field) == 0.0:
                Field = Field + 1.0e-7
            MinVal = min(Field) - 0.01 * abs(min(Field))
            MaxVal = max(Field) + 0.01 * abs(max(Field))
            if AxisKey[0] == "lev":
                pylab.ylabel(AxisName[0])
                self.orientation = 0
                self.handle = pylab.plot(Field, AxisVal[0], "bo-").pop(0)
                self.axes = pylab.gca()
                pylab.xlim(MinVal, MaxVal)
                pylab.ylim(AxisVal[0][-1], AxisVal[0][0])
            else:
                if Subplot in ["111", "212", "223", "224"]:
                    pylab.xlabel(AxisName[0])
                self.orientation = 1
                self.handle = pylab.plot(AxisVal[0], Field, "bo-").pop(0)
                self.axes = pylab.gca()
                pylab.ylim([MinVal, MaxVal])
                pylab.xlim([AxisVal[0][0], AxisVal[0][-1]])

        elif len(AxisName) == 2:
            xmin = AxisVal[1][0]
            xmax = AxisVal[1][-1]
            ymin = AxisVal[0][0]
            ymax = AxisVal[0][-1]
            if AxisKey[0] == "lev":
                self.orientation = 0
                self.handle = pylab.imshow(Field[::-1], extent=(xmin, xmax, ymin, ymax), interpolation="bilinear")
                pylab.setp(pylab.gca(), "xlim", [xmin, xmax], "ylim", [ymax, ymin])
            else:
                self.orientation = 1
                self.handle = pylab.imshow(Field, extent=(xmin, xmax, ymin, ymax), interpolation="bilinear")
                pylab.setp(pylab.gca(), "xlim", [xmin, xmax], "ylim", [ymin, ymax])
            # write x label only if subplot does not have other subplot underneath
            if Subplot in ["111", "212", "223", "224"]:
                pylab.xlabel(AxisName[1])
            pylab.ylabel(AxisName[0])
            self.handle.set_norm(None)

        # Title
        self.TitleTemplate = "%s  [%s]" % (FieldKey, KnownFields[FieldKey][1]) + " %6.2f days"
        if len(AxisName) == 2:
            self.TitleTemplate = self.TitleTemplate + "\n min=%g max=%g"
        day = Component.State.ElapsedTime / 86400.0
        try:
            TitleText = self.TitleTemplate % day
        except:
            TitleText = self.TitleTemplate % (day, min(ravel(Field)), max(ravel(Field)))
        self.title = pylab.title(TitleText)
        self.title.set_fontsize(12)
Exemple #57
0
 def __getitem__(self, key):
     for obj in [self.Params, self.Grid, self.State]:
         if key in obj:
             if type(obj[key]) is type('string'): return obj[key]
             else: return squeeze(obj[key])
     raise IndexError,'\n\n CliMT.State: %s not in Params, Grid or State' % str(key)
Exemple #58
0
    def __init__(self, Component, FieldKey, Subplot):
        
        # get Field value
        Field = Component.State[FieldKey]
        Dims  = KnownFields[FieldKey][2]

        # Figure out axes
        AxisKey = []
        if Dims == '2D':
            for i in range(2):
                if shape(Field)[i] > 1: AxisKey.append(['lat','lon'][i])
        if Dims == '3D':
            for i in range(3):
                if shape(Field)[i] > 1: AxisKey.append(['lev','lat','lon'][i])

        # If Field is 3D, show zonal average
        Field = squeeze(Field)
        if rank(Field) == 3:
            Field = average(Field,axis=2)
            Field = squeeze(Field)
            AxisKey.pop(2)

        # Axes names and values
        AxisName = []
        AxisVal  = []
        for key in AxisKey:
            AxisName.append(Component.Grid.long_name[key])
            AxisVal.append(Component.Grid[key])
        
        # Get handles to figure and properties
        if len(AxisName) == 1:
            if min(Field) == max(Field) == 0.: Field=Field+1.e-7
            MinVal = min(Field) - 0.01*abs(min(Field))
            MaxVal = max(Field) + 0.01*abs(max(Field))
            if AxisKey[0] == 'lev':
                pylab.ylabel(AxisName[0])
                self.orientation = 0
                self.handle = pylab.plot(Field, AxisVal[0], 'bo-').pop(0)
                self.axes = pylab.gca()
                pylab.xlim(MinVal, MaxVal)
                pylab.ylim(AxisVal[0][-1], AxisVal[0][0] )
            else:
                if Subplot in ['111','212','223','224']: pylab.xlabel(AxisName[0])
                self.orientation = 1
                self.handle = pylab.plot(AxisVal[0], Field, 'bo-').pop(0)
                self.axes = pylab.gca()
                pylab.ylim([MinVal, MaxVal])
                pylab.xlim([ AxisVal[0][0], AxisVal[0][-1] ])

        elif len(AxisName) == 2:
            xmin = AxisVal[1][0]
            xmax = AxisVal[1][-1]
            ymin = AxisVal[0][0]
            ymax = AxisVal[0][-1]
            if AxisKey[0] == 'lev':
               self.orientation = 0
               self.handle = pylab.imshow(\
                    Field[::-1],\
                    extent=(xmin, xmax, ymin, ymax),\
                    interpolation='bilinear')
               pylab.setp(pylab.gca(), 'xlim',[xmin,xmax], 'ylim',[ymax,ymin])
            else:
               self.orientation = 1
               self.handle = pylab.imshow( \
                    Field, \
                    extent=(xmin, xmax, ymin, ymax),\
                    interpolation='bilinear')
               pylab.setp(pylab.gca(), 'xlim',[xmin,xmax], 'ylim',[ymin,ymax])
            # write x label only if subplot does not have other subplot underneath
            if Subplot in ['111','212','223','224']: pylab.xlabel(AxisName[1])
            pylab.ylabel(AxisName[0])
            self.handle.set_norm(None)

        # Title
        self.TitleTemplate = '%s  [%s]' % (FieldKey,KnownFields[FieldKey][1]) + ' %6.2f days' 
        if len(AxisName) == 2: self.TitleTemplate = self.TitleTemplate + '\n min=%g max=%g' 
        day = Component.State.ElapsedTime/86400.
        try:
            TitleText = self.TitleTemplate % day
        except:
            TitleText = self.TitleTemplate % (day, min(ravel(Field)), max(ravel(Field)))
        self.title = pylab.title(TitleText)
        self.title.set_fontsize(12)